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 | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 22 | #include "security/v2/certificate-fetcher-direct-fetch.hpp" |
| 23 | #include "security/v2/validation-policy-simple-hierarchy.hpp" |
| 24 | #include "lp/nack.hpp" |
| 25 | #include "lp/tags.hpp" |
| 26 | |
| 27 | #include "boost-test.hpp" |
| 28 | #include "validator-fixture.hpp" |
| 29 | |
| 30 | #include <boost/range/adaptor/strided.hpp> |
| 31 | #include <boost/range/adaptor/sliced.hpp> |
| 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"); |
| 143 | BOOST_CHECK_GT(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 144 | |
Zhiyi Zhang | a1302f6 | 2017-10-31 10:22:35 -0700 | [diff] [blame] | 145 | // odd interests |
| 146 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 147 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 148 | } |
| 149 | |
| 150 | // even interests |
| 151 | for (const auto& sentInterest : this->face.sentInterests | |
| 152 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 153 | boost::adaptors::strided(2)) { |
Alexander Afanasyev | ba2cf39 | 2017-01-13 19:05:23 -0800 | [diff] [blame] | 154 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>) |
| 159 | { |
| 160 | VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs"); |
| 161 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 162 | |
| 163 | // odd interests |
| 164 | for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) { |
| 165 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr); |
| 166 | } |
| 167 | |
| 168 | // even interests |
| 169 | for (const auto& sentInterest : this->face.sentInterests | |
| 170 | boost::adaptors::sliced(1, this->face.sentInterests.size()) | |
| 171 | boost::adaptors::strided(2)) { |
| 172 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>) |
| 177 | { |
| 178 | VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout"); |
| 179 | BOOST_CHECK_GT(this->face.sentInterests.size(), 4); |
| 180 | |
| 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)) { |
| 190 | BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch |
| 195 | BOOST_AUTO_TEST_SUITE_END() // V2 |
| 196 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 197 | |
| 198 | } // namespace tests |
| 199 | } // namespace v2 |
| 200 | } // namespace security |
| 201 | } // namespace ndn |