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