blob: 7ffc2766db3fb94a1b30259bd5283d59f42a2abb [file] [log] [blame]
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -08004 *
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 Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/certificate-fetcher-from-network.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/nack.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040028#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032using namespace ndn::security;
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080033
34BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080035BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork)
36
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040037struct Cert {};
38struct Timeout {};
39struct Nack {};
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080040
41template<class Response>
42class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
43 CertificateFetcherFromNetwork>
44{
45public:
46 CertificateFetcherFromNetworkFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040047 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
48 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080049 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040050 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080051 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
52
53 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
54 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
55
Davide Pesavento2e481fc2021-07-02 18:20:03 -040056 processInterest = [this] (const Interest& i) { makeResponse(i); };
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080057 }
58
59 void
60 makeResponse(const Interest& interest);
61
62public:
63 Data data;
64 Interest interest;
65};
66
67template<>
68void
69CertificateFetcherFromNetworkFixture<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
78template<>
79void
80CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest)
81{
82 // do nothing
83}
84
85template<>
86void
87CertificateFetcherFromNetworkFixture<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
94using Failures = boost::mpl::vector<Timeout, Nack>;
95
96BOOST_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 Pesavento0f830802018-01-16 23:58:58 -0500102 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800103
104 VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs");
105 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
106}
107
108BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>)
109{
110 VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data");
Davide Pesavento2acce252022-09-08 22:03:03 -0400111 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500112 // first interest + 3 retries
Davide Pesavento2acce252022-09-08 22:03:03 -0400113 BOOST_TEST(this->face.sentInterests.size() == 4);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500114
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800115 this->face.sentInterests.clear();
116
Davide Pesavento0f830802018-01-16 23:58:58 -0500117 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800118
119 VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data");
Davide Pesavento2acce252022-09-08 22:03:03 -0400120 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
121 BOOST_TEST(this->face.sentInterests.size() == 4);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800122}
123
124BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800125BOOST_AUTO_TEST_SUITE_END() // Security
126
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400127} // namespace ndn::tests