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 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 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" |
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" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp" |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -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 | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | using namespace ndn::security; |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(Security) |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork) |
| 36 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 37 | struct Cert {}; |
| 38 | struct Timeout {}; |
| 39 | struct Nack {}; |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 40 | |
| 41 | template<class Response> |
| 42 | class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy, |
| 43 | CertificateFetcherFromNetwork> |
| 44 | { |
| 45 | public: |
| 46 | CertificateFetcherFromNetworkFixture() |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 47 | : data("/Security/ValidatorFixture/Sub1/Sub3/Data") |
| 48 | , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest") |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 50 | Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 51 | cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate()); |
| 52 | |
| 53 | m_keyChain.sign(data, signingByIdentity(subSubIdentity)); |
| 54 | m_keyChain.sign(interest, signingByIdentity(subSubIdentity)); |
| 55 | |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 56 | processInterest = [this] (const Interest& i) { makeResponse(i); }; |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void |
| 60 | makeResponse(const Interest& interest); |
| 61 | |
| 62 | public: |
| 63 | Data data; |
| 64 | Interest interest; |
| 65 | }; |
| 66 | |
| 67 | template<> |
| 68 | void |
| 69 | CertificateFetcherFromNetworkFixture<Cert>::makeResponse(const Interest& interest) |
| 70 | { |
| 71 | auto cert = cache.find(interest); |
| 72 | if (cert == nullptr) { |
| 73 | return; |
| 74 | } |
| 75 | face.receive(*cert); |
| 76 | } |
| 77 | |
| 78 | template<> |
| 79 | void |
| 80 | CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest) |
| 81 | { |
| 82 | // do nothing |
| 83 | } |
| 84 | |
| 85 | template<> |
| 86 | void |
| 87 | CertificateFetcherFromNetworkFixture<Nack>::makeResponse(const Interest& interest) |
| 88 | { |
| 89 | lp::Nack nack(interest); |
| 90 | nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE)); |
| 91 | face.receive(nack); |
| 92 | } |
| 93 | |
| 94 | using Failures = boost::mpl::vector<Timeout, Nack>; |
| 95 | |
| 96 | BOOST_FIXTURE_TEST_CASE(ValidateSuccess, CertificateFetcherFromNetworkFixture<Cert>) |
| 97 | { |
| 98 | VALIDATE_SUCCESS(this->data, "Should get accepted, as normal interests bring cert"); |
| 99 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 100 | this->face.sentInterests.clear(); |
| 101 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 102 | this->advanceClocks(1_h, 2); // expire validator caches |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 103 | |
| 104 | VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs"); |
| 105 | BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); |
| 106 | } |
| 107 | |
| 108 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>) |
| 109 | { |
| 110 | VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 111 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 112 | // first interest + 3 retries |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 113 | BOOST_TEST(this->face.sentInterests.size() == 4); |
Ashlesh Gawande | 3e39a4d | 2018-08-30 16:49:13 -0500 | [diff] [blame] | 114 | |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 115 | this->face.sentInterests.clear(); |
| 116 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 117 | this->advanceClocks(1_h, 2); // expire validator caches |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 118 | |
| 119 | VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data"); |
Davide Pesavento | 2acce25 | 2022-09-08 22:03:03 -0400 | [diff] [blame] | 120 | BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT); |
| 121 | BOOST_TEST(this->face.sentInterests.size() == 4); |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork |
Alexander Afanasyev | 7bc10fa | 2017-01-13 16:56:26 -0800 | [diff] [blame] | 125 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 126 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 127 | } // namespace ndn::tests |