blob: ee66ad5f06d079d174084578b59d0e27e666ca20 [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 Pesavento49e1e872023-11-11 00:45:23 -050030#include <boost/mp11/list.hpp>
31
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032namespace ndn::tests {
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080033
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034using namespace ndn::security;
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080035
36BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080037BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork)
38
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040039struct Cert {};
40struct Timeout {};
41struct Nack {};
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080042
43template<class Response>
44class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
45 CertificateFetcherFromNetwork>
46{
47public:
48 CertificateFetcherFromNetworkFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040049 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
50 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080051 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040052 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080053 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
54
55 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
56 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
57
Davide Pesavento2e481fc2021-07-02 18:20:03 -040058 processInterest = [this] (const Interest& i) { makeResponse(i); };
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080059 }
60
61 void
62 makeResponse(const Interest& interest);
63
64public:
65 Data data;
66 Interest interest;
67};
68
69template<>
70void
71CertificateFetcherFromNetworkFixture<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
80template<>
81void
82CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest)
83{
84 // do nothing
85}
86
87template<>
88void
89CertificateFetcherFromNetworkFixture<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 Pesavento49e1e872023-11-11 00:45:23 -050096using Failures = boost::mp11::mp_list<Timeout, Nack>;
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080097
98BOOST_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 Pesavento0f830802018-01-16 23:58:58 -0500104 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800105
106 VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs");
107 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
108}
109
110BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>)
111{
112 VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data");
Davide Pesavento2acce252022-09-08 22:03:03 -0400113 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500114 // first interest + 3 retries
Davide Pesavento2acce252022-09-08 22:03:03 -0400115 BOOST_TEST(this->face.sentInterests.size() == 4);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500116
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800117 this->face.sentInterests.clear();
118
Davide Pesavento0f830802018-01-16 23:58:58 -0500119 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800120
121 VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data");
Davide Pesavento2acce252022-09-08 22:03:03 -0400122 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
123 BOOST_TEST(this->face.sentInterests.size() == 4);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800124}
125
126BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800127BOOST_AUTO_TEST_SUITE_END() // Security
128
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400129} // namespace ndn::tests