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 | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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 {}; |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 42 | struct Nack {}; |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 43 | struct Timeout {}; |
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 | |
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 |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 87 | makeResponse(const Interest& interest) |
| 88 | { |
| 89 | if constexpr (std::is_same_v<Response, Cert>) { |
| 90 | auto cert = cache.find(interest); |
| 91 | if (cert == nullptr) { |
| 92 | return; |
| 93 | } |
| 94 | face.receive(*cert); |
| 95 | } |
| 96 | else if constexpr (std::is_same_v<Response, Nack>) { |
| 97 | face.receive(makeNack(interest, lp::NackReason::NO_ROUTE)); |
| 98 | } |
| 99 | else { |
| 100 | // do nothing |
| 101 | } |
| 102 | } |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 104 | void |
| 105 | setResponseType(ResponseType type) |
| 106 | { |
| 107 | responseType = type; |
| 108 | } |
| 109 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 110 | public: |
| 111 | Data data; |
| 112 | Interest interest; |
| 113 | Interest interestNoTag; |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 114 | ResponseType responseType = ResponseType::INFRASTRUCTURE; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 117 | using FailureModes = boost::mp11::mp_list<Nack, Timeout>; |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 118 | |
| 119 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>) |
| 120 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 121 | VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs"); |
| 122 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 123 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 124 | // odd interests |
| 125 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 126 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 127 | } |
| 128 | |
| 129 | // even interests |
| 130 | for (const auto& sentInterest : this->face.sentInterests | |
| 131 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 132 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 133 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 134 | } |
| 135 | } |
| 136 | |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 137 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>) |
| 138 | { |
| 139 | setResponseType(ResponseType::DIRECT); |
| 140 | static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true); |
| 141 | |
| 142 | VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs"); |
| 143 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 144 | |
| 145 | for (const auto& sentInterest : this->face.sentInterests) { |
| 146 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 147 | } |
| 148 | } |
| 149 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 150 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, FailureModes, |
| 151 | CertificateFetcherDirectFetchFixture<T>) |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 152 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 153 | 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] | 154 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 155 | // Direct fetcher sends two interests each time - to network and face |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 156 | // 3 retries on nack or timeout (2 * (1 + 3) = 8) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 157 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 158 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 159 | // odd interests |
| 160 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 161 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 162 | } |
| 163 | |
| 164 | // even interests |
| 165 | for (const auto& sentInterest : this->face.sentInterests | |
| 166 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 167 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 168 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 169 | } |
| 170 | } |
| 171 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 172 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, FailureModes, |
| 173 | CertificateFetcherDirectFetchFixture<T>) |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 174 | { |
| 175 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 176 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 177 | |
| 178 | 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] | 179 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 180 | // Direct fetcher sends two interests each time - to network and face |
| 181 | // 3 retries on nack or timeout (1 + 3 = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 182 | BOOST_TEST(this->face.sentInterests.size() == 4); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 183 | |
| 184 | for (const auto& sentInterest : this->face.sentInterests) { |
| 185 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 186 | } |
| 187 | } |
| 188 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 189 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, FailureModes, |
| 190 | CertificateFetcherDirectFetchFixture<T>) |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 191 | { |
| 192 | this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT); |
| 193 | static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true); |
| 194 | |
| 195 | this->data.template removeTag<lp::IncomingFaceIdTag>(); |
| 196 | this->interest.template removeTag<lp::IncomingFaceIdTag>(); |
| 197 | |
| 198 | VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 199 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
| 200 | BOOST_TEST(this->face.sentInterests.size() == 0); |
Alexander Afanasyev | 1660d00 | 2019-03-18 10:45:39 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 203 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 204 | { |
| 205 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 206 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 207 | |
| 208 | // odd interests |
| 209 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 210 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 211 | } |
| 212 | |
| 213 | // even interests |
| 214 | for (const auto& sentInterest : this->face.sentInterests | |
| 215 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 216 | boost::adaptors::strided(2)) { |
| 217 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 218 | } |
| 219 | } |
| 220 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 221 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, FailureModes, |
| 222 | CertificateFetcherDirectFetchFixture<T>) |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 223 | { |
| 224 | 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] | 225 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 226 | // Direct fetcher sends two interests each time - to network and face |
| 227 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 228 | BOOST_TEST(this->face.sentInterests.size() == 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 229 | |
| 230 | // odd interests |
| 231 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 232 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 233 | } |
| 234 | |
| 235 | // even interests |
| 236 | for (const auto& sentInterest : this->face.sentInterests | |
| 237 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 238 | boost::adaptors::strided(2)) { |
| 239 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 244 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 245 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 246 | } // namespace ndn::tests |