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