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 | /* |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/v2/certificate-fetcher-direct-fetch.hpp" |
| 23 | #include "ndn-cxx/security/v2/validation-policy-simple-hierarchy.hpp" |
| 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" |
| 28 | #include "tests/unit/security/v2/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 { |
| 35 | namespace v2 { |
| 36 | namespace tests { |
| 37 | |
| 38 | using namespace ndn::tests; |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Security) |
| 41 | BOOST_AUTO_TEST_SUITE(V2) |
| 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: |
| 61 | CertificateFetcherDirectFetchFixture() |
| 62 | : data("/Security/V2/ValidatorFixture/Sub1/Sub3/Data") |
| 63 | , interest("/Security/V2/ValidatorFixture/Sub1/Sub3/Interest") |
| 64 | , interestNoTag("/Security/V2/ValidatorFixture/Sub1/Sub3/Interest2") |
| 65 | { |
| 66 | Identity subSubIdentity = addSubCertificate("/Security/V2/ValidatorFixture/Sub1/Sub3", subIdentity); |
| 67 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 68 | |
| 69 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 70 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 71 | m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity)); |
| 72 | |
| 73 | data.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 74 | interest.setTag(make_shared<lp::IncomingFaceIdTag>(123)); |
| 75 | |
| 76 | processInterest = [this] (const Interest& interest) { |
| 77 | auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>(); |
| 78 | if (nextHopFaceIdTag == nullptr) { |
| 79 | makeResponse(interest); // respond only to the "infrastructure" interest |
| 80 | } |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | makeResponse(const Interest& interest); |
| 86 | |
| 87 | public: |
| 88 | Data data; |
| 89 | Interest interest; |
| 90 | Interest interestNoTag; |
| 91 | }; |
| 92 | |
| 93 | template<> |
| 94 | void |
| 95 | CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest) |
| 96 | { |
| 97 | auto cert = cache.find(interest); |
| 98 | if (cert == nullptr) { |
| 99 | return; |
| 100 | } |
| 101 | face.receive(*cert); |
| 102 | } |
| 103 | |
| 104 | template<> |
| 105 | void |
| 106 | CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest) |
| 107 | { |
| 108 | // do nothing |
| 109 | } |
| 110 | |
| 111 | template<> |
| 112 | void |
| 113 | CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest) |
| 114 | { |
| 115 | lp::Nack nack(interest); |
| 116 | nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 117 | face.receive(nack); |
| 118 | } |
| 119 | |
| 120 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 121 | |
| 122 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>) |
| 123 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 124 | VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs"); |
| 125 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 126 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 127 | // odd interests |
| 128 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 129 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 130 | } |
| 131 | |
| 132 | // even interests |
| 133 | for (const auto& sentInterest : this->face.sentInterests | |
| 134 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 135 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 136 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 141 | { |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 142 | 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] | 143 | // Direct fetcher sends two interests each time - to network and face |
| 144 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
| 145 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 146 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 147 | // odd interests |
| 148 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 149 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 150 | } |
| 151 | |
| 152 | // even interests |
| 153 | for (const auto& sentInterest : this->face.sentInterests | |
| 154 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 155 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 156 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 161 | { |
| 162 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 163 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 164 | |
| 165 | // odd interests |
| 166 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 167 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 168 | } |
| 169 | |
| 170 | // even interests |
| 171 | for (const auto& sentInterest : this->face.sentInterests | |
| 172 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 173 | boost::adaptors::strided(2)) { |
| 174 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 179 | { |
| 180 | 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] | 181 | // Direct fetcher sends two interests each time - to network and face |
| 182 | // 3 retries on nack or timeout (2 * (1 + 3) = 4) |
| 183 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 184 | |
| 185 | // odd interests |
| 186 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 187 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 188 | } |
| 189 | |
| 190 | // even interests |
| 191 | for (const auto& sentInterest : this->face.sentInterests | |
| 192 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 193 | boost::adaptors::strided(2)) { |
| 194 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
| 199 | BOOST_AUTO_TEST_SUITE_END() // V2 |
| 200 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 201 | |
| 202 | } // namespace tests |
| 203 | } // namespace v2 |
| 204 | } // namespace security |
| 205 | } // namespace ndn |