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 | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 35 | inline namespace v2 { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 36 | namespace tests { |
| 37 | |
| 38 | using namespace ndn::tests; |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch) |
| 42 | |
| 43 | class Cert |
| 44 | { |
| 45 | }; |
| 46 | |
| 47 | class Timeout |
| 48 | { |
| 49 | }; |
| 50 | |
| 51 | class Nack |
| 52 | { |
| 53 | }; |
| 54 | |
| 55 | template<class Response> |
| 56 | class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 57 | CertificateFetcherDirectFetch> |
| 58 | { |
| 59 | public: |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 60 | enum class ResponseType { |
| 61 | INFRASTRUCTURE, |
| 62 | DIRECT, |
| 63 | BOTH |
| 64 | }; |
| 65 | |
| 66 | public: |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 67 | CertificateFetcherDirectFetchFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 68 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
| 69 | , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest") |
| 70 | , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2") |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 71 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 72 | Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 73 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 74 | |
| 75 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 76 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 77 | m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity)); |
| 78 | |
| 79 | data.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 80 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 81 | |
| 82 | processInterest = [this] (const Interest& interest) { |
| 83 | auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>(); |
| 84 | if (nextHopFaceIdTag == nullptr) { |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 85 | if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) { |
| 86 | makeResponse(interest); |
| 87 | } |
| 88 | } |
| 89 | else { |
| 90 | if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) { |
| 91 | makeResponse(interest); |
| 92 | } |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 93 | } |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | makeResponse(const Interest& interest); |
| 99 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 100 | void |
| 101 | setResponseType(ResponseType type) |
| 102 | { |
| 103 | responseType = type; |
| 104 | } |
| 105 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 106 | public: |
| 107 | Data data; |
| 108 | Interest interest; |
| 109 | Interest interestNoTag; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 110 | ResponseType responseType = ResponseType::INFRASTRUCTURE; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | template<> |
| 114 | void |
| 115 | CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest) |
| 116 | { |
| 117 | auto cert = cache.find(interest); |
| 118 | if (cert == nullptr) { |
| 119 | return; |
| 120 | } |
| 121 | face.receive(*cert); |
| 122 | } |
| 123 | |
| 124 | template<> |
| 125 | void |
| 126 | CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest) |
| 127 | { |
| 128 | // do nothing |
| 129 | } |
| 130 | |
| 131 | template<> |
| 132 | void |
| 133 | CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest) |
| 134 | { |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 135 | face.receive(makeNack(interest, lp::NackReason::NO_ROUTE)); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 139 | |
| 140 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>) |
| 141 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 142 | VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs"); |
| 143 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 144 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 145 | // odd interests |
| 146 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 147 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 148 | } |
| 149 | |
| 150 | // even interests |
| 151 | for (const auto& sentInterest : this->face.sentInterests | |
| 152 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 153 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 154 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 155 | } |
| 156 | } |
| 157 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 158 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>) |
| 159 | { |
| 160 | setResponseType(ResponseType::DIRECT); |
| 161 | static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true); |
| 162 | |
| 163 | VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs"); |
| 164 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 165 | |
| 166 | for (const auto& sentInterest : this->face.sentInterests) { |
| 167 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 168 | } |
| 169 | } |
| 170 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 171 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 172 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 173 | 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] | 174 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 175 | // Direct fetcher sends two interests each time - to network and face |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 176 | // 3 retries on nack or timeout (2 * (1 + 3) = 8) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 177 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 178 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 179 | // odd interests |
| 180 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 181 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 182 | } |
| 183 | |
| 184 | // even interests |
| 185 | for (const auto& sentInterest : this->face.sentInterests | |
| 186 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 187 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 188 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 189 | } |
| 190 | } |
| 191 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 192 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 193 | { |
| 194 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 195 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 196 | |
| 197 | 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] | 198 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 199 | // Direct fetcher sends two interests each time - to network and face |
| 200 | // 3 retries on nack or timeout (1 + 3 = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 201 | BOOST_TEST(this->face.sentInterests.size() == 4); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 202 | |
| 203 | for (const auto& sentInterest : this->face.sentInterests) { |
| 204 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 209 | { |
| 210 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 211 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 212 | |
| 213 | this->data.template removeTag<lp::IncomingFaceIdTag>(); |
| 214 | this->interest.template removeTag<lp::IncomingFaceIdTag>(); |
| 215 | |
| 216 | VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 217 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
| 218 | BOOST_TEST(this->face.sentInterests.size() == 0); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 221 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 222 | { |
| 223 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 224 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 225 | |
| 226 | // odd interests |
| 227 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 228 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 229 | } |
| 230 | |
| 231 | // even interests |
| 232 | for (const auto& sentInterest : this->face.sentInterests | |
| 233 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 234 | boost::adaptors::strided(2)) { |
| 235 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 240 | { |
| 241 | 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] | 242 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 243 | // Direct fetcher sends two interests each time - to network and face |
| 244 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 245 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 246 | |
| 247 | // odd interests |
| 248 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 249 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 250 | } |
| 251 | |
| 252 | // even interests |
| 253 | for (const auto& sentInterest : this->face.sentInterests | |
| 254 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 255 | boost::adaptors::strided(2)) { |
| 256 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 261 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 262 | |
| 263 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 264 | } // inline namespace v2 |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 265 | } // namespace security |
| 266 | } // namespace ndn |