Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/certificate-fetcher-direct-fetch.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/lp/tags.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp" |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 28 | #include "tests/unit/security/validator-fixture.hpp" |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 30 | #include <boost/range/adaptor/sliced.hpp> |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 31 | #include <boost/range/adaptor/strided.hpp> |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 33 | namespace ndn::tests { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 35 | using namespace ndn::security; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch) |
| 39 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 40 | struct Cert {}; |
| 41 | struct Timeout {}; |
| 42 | struct Nack {}; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 43 | |
| 44 | template<class Response> |
| 45 | class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 46 | CertificateFetcherDirectFetch> |
| 47 | { |
| 48 | public: |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 49 | enum class ResponseType { |
| 50 | INFRASTRUCTURE, |
| 51 | DIRECT, |
| 52 | BOTH |
| 53 | }; |
| 54 | |
| 55 | public: |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 56 | CertificateFetcherDirectFetchFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 57 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
| 58 | , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest") |
| 59 | , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2") |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 61 | Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 62 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 63 | |
| 64 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 65 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 66 | m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity)); |
| 67 | |
| 68 | data.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 69 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 70 | |
| 71 | processInterest = [this] (const Interest& interest) { |
| 72 | auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>(); |
| 73 | if (nextHopFaceIdTag == nullptr) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 74 | if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) { |
| 75 | makeResponse(interest); |
| 76 | } |
| 77 | } |
| 78 | else { |
| 79 | if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) { |
| 80 | makeResponse(interest); |
| 81 | } |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | makeResponse(const Interest& interest); |
| 88 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 89 | void |
| 90 | setResponseType(ResponseType type) |
| 91 | { |
| 92 | responseType = type; |
| 93 | } |
| 94 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 95 | public: |
| 96 | Data data; |
| 97 | Interest interest; |
| 98 | Interest interestNoTag; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 99 | ResponseType responseType = ResponseType::INFRASTRUCTURE; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | template<> |
| 103 | void |
| 104 | CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest) |
| 105 | { |
| 106 | auto cert = cache.find(interest); |
| 107 | if (cert == nullptr) { |
| 108 | return; |
| 109 | } |
| 110 | face.receive(*cert); |
| 111 | } |
| 112 | |
| 113 | template<> |
| 114 | void |
| 115 | CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest) |
| 116 | { |
| 117 | // do nothing |
| 118 | } |
| 119 | |
| 120 | template<> |
| 121 | void |
| 122 | CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest) |
| 123 | { |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 124 | face.receive(makeNack(interest, lp::NackReason::NO_ROUTE)); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>) |
| 130 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 131 | VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs"); |
| 132 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 133 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 134 | // odd interests |
| 135 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 136 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 137 | } |
| 138 | |
| 139 | // even interests |
| 140 | for (const auto& sentInterest : this->face.sentInterests | |
| 141 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 142 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 143 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 144 | } |
| 145 | } |
| 146 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 147 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>) |
| 148 | { |
| 149 | setResponseType(ResponseType::DIRECT); |
| 150 | static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true); |
| 151 | |
| 152 | VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs"); |
| 153 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 154 | |
| 155 | for (const auto& sentInterest : this->face.sentInterests) { |
| 156 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 157 | } |
| 158 | } |
| 159 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 160 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 161 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 162 | VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 163 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 164 | // Direct fetcher sends two interests each time - to network and face |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 165 | // 3 retries on nack or timeout (2 * (1 + 3) = 8) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 166 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 167 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 168 | // odd interests |
| 169 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 170 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 171 | } |
| 172 | |
| 173 | // even interests |
| 174 | for (const auto& sentInterest : this->face.sentInterests | |
| 175 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 176 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 177 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 178 | } |
| 179 | } |
| 180 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 181 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 182 | { |
| 183 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 184 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 185 | |
| 186 | VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 187 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 188 | // Direct fetcher sends two interests each time - to network and face |
| 189 | // 3 retries on nack or timeout (1 + 3 = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 190 | BOOST_TEST(this->face.sentInterests.size() == 4); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 191 | |
| 192 | for (const auto& sentInterest : this->face.sentInterests) { |
| 193 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 198 | { |
| 199 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 200 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 201 | |
| 202 | this->data.template removeTag<lp::IncomingFaceIdTag>(); |
| 203 | this->interest.template removeTag<lp::IncomingFaceIdTag>(); |
| 204 | |
| 205 | VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 206 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
| 207 | BOOST_TEST(this->face.sentInterests.size() == 0); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 210 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 211 | { |
| 212 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 213 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 214 | |
| 215 | // odd interests |
| 216 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 217 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 218 | } |
| 219 | |
| 220 | // even interests |
| 221 | for (const auto& sentInterest : this->face.sentInterests | |
| 222 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 223 | boost::adaptors::strided(2)) { |
| 224 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 229 | { |
| 230 | VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 231 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 232 | // Direct fetcher sends two interests each time - to network and face |
| 233 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 234 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 235 | |
| 236 | // odd interests |
| 237 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 238 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 239 | } |
| 240 | |
| 241 | // even interests |
| 242 | for (const auto& sentInterest : this->face.sentInterests | |
| 243 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 244 | boost::adaptors::strided(2)) { |
| 245 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 250 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 251 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 252 | } // namespace ndn::tests |