Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [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 | 7bc10fa | 2017-01-13 16:56:26 -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-from-network.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" |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 27 | #include "tests/unit/security/validator-fixture.hpp" |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 31 | inline namespace v2 { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
| 34 | using namespace ndn::tests; |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork) |
| 38 | |
| 39 | class Cert |
| 40 | { |
| 41 | }; |
| 42 | |
| 43 | class Timeout |
| 44 | { |
| 45 | }; |
| 46 | |
| 47 | class Nack |
| 48 | { |
| 49 | }; |
| 50 | |
| 51 | template<class Response> |
| 52 | class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 53 | CertificateFetcherFromNetwork> |
| 54 | { |
| 55 | public: |
| 56 | CertificateFetcherFromNetworkFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 57 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
| 58 | , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest") |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 59 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 60 | Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 61 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 62 | |
| 63 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
Eric Newberry | b74bbda | 2020-06-18 19:33:58 -0700 | [diff] [blame] | 64 | interest.setCanBePrefix(false); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 65 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 66 | |
| 67 | processInterest = bind(&CertificateFetcherFromNetworkFixture<Response>::makeResponse, this, _1); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | makeResponse(const Interest& interest); |
| 72 | |
| 73 | public: |
| 74 | Data data; |
| 75 | Interest interest; |
| 76 | }; |
| 77 | |
| 78 | template<> |
| 79 | void |
| 80 | CertificateFetcherFromNetworkFixture<Cert>::makeResponse(const Interest& interest) |
| 81 | { |
| 82 | auto cert = cache.find(interest); |
| 83 | if (cert == nullptr) { |
| 84 | return; |
| 85 | } |
| 86 | face.receive(*cert); |
| 87 | } |
| 88 | |
| 89 | template<> |
| 90 | void |
| 91 | CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest) |
| 92 | { |
| 93 | // do nothing |
| 94 | } |
| 95 | |
| 96 | template<> |
| 97 | void |
| 98 | CertificateFetcherFromNetworkFixture<Nack>::makeResponse(const Interest& interest) |
| 99 | { |
| 100 | lp::Nack nack(interest); |
| 101 | nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 102 | face.receive(nack); |
| 103 | } |
| 104 | |
| 105 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 106 | |
| 107 | BOOST_FIXTURE_TEST_CASE(ValidateSuccess, CertificateFetcherFromNetworkFixture<Cert>) |
| 108 | { |
| 109 | VALIDATE_SUCCESS(this->data, "Should get accepted, as normal interests bring cert"); |
| 110 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 111 | this->face.sentInterests.clear(); |
| 112 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 113 | this->advanceClocks(1_h, 2); // expire validator caches |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 114 | |
| 115 | VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs"); |
| 116 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 117 | } |
| 118 | |
| 119 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>) |
| 120 | { |
| 121 | VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data"); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 122 | // first interest + 3 retries |
| 123 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
| 124 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 125 | this->face.sentInterests.clear(); |
| 126 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 127 | this->advanceClocks(1_h, 2); // expire validator caches |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 128 | |
| 129 | VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data"); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 134 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 135 | |
| 136 | } // namespace tests |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 137 | } // inline namespace v2 |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 138 | } // namespace security |
| 139 | } // namespace ndn |