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