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