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