blob: eb77031b250c44aedfe252141d6fafee64008b87 [file] [log] [blame]
Alexander Afanasyevba2cf392017-01-13 19:05:23 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhanga1302f62017-10-31 10:22:35 -07002/*
Davide Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Alexander Afanasyevba2cf392017-01-13 19:05:23 -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-direct-fetch.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/tags.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080026
Davide Pesavento2acce252022-09-08 22:03:03 -040027#include "tests/test-common.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040028#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080029
Davide Pesavento49e1e872023-11-11 00:45:23 -050030#include <boost/mp11/list.hpp>
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080031#include <boost/range/adaptor/sliced.hpp>
Davide Pesavento7e780642018-11-24 15:51:34 -050032#include <boost/range/adaptor/strided.hpp>
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080033
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034namespace ndn::tests {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080035
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040036using namespace ndn::security;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080037
38BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080039BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch)
40
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040041struct Cert {};
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040042struct Nack {};
Davide Pesavento0c526032024-01-31 21:14:01 -050043struct Timeout {};
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080044
45template<class Response>
46class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
47 CertificateFetcherDirectFetch>
48{
49public:
Alexander Afanasyev1660d002019-03-18 10:45:39 -040050 enum class ResponseType {
51 INFRASTRUCTURE,
52 DIRECT,
53 BOTH
54 };
55
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080056 CertificateFetcherDirectFetchFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040057 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
58 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
59 , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2")
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080060 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040061 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080062 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
63
64 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
65 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
66 m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity));
67
68 data.setTag(make_shared<lp::IncomingFaceIdTag>(123));
69 interest.setTag(make_shared<lp::IncomingFaceIdTag>(123));
70
71 processInterest = [this] (const Interest& interest) {
72 auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>();
73 if (nextHopFaceIdTag == nullptr) {
Alexander Afanasyev1660d002019-03-18 10:45:39 -040074 if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) {
75 makeResponse(interest);
76 }
77 }
78 else {
79 if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) {
80 makeResponse(interest);
81 }
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080082 }
83 };
84 }
85
86 void
Davide Pesavento0c526032024-01-31 21:14:01 -050087 makeResponse(const Interest& interest)
88 {
89 if constexpr (std::is_same_v<Response, Cert>) {
90 auto cert = cache.find(interest);
91 if (cert == nullptr) {
92 return;
93 }
94 face.receive(*cert);
95 }
96 else if constexpr (std::is_same_v<Response, Nack>) {
97 face.receive(makeNack(interest, lp::NackReason::NO_ROUTE));
98 }
99 else {
100 // do nothing
101 }
102 }
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800103
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400104 void
105 setResponseType(ResponseType type)
106 {
107 responseType = type;
108 }
109
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800110public:
111 Data data;
112 Interest interest;
113 Interest interestNoTag;
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400114 ResponseType responseType = ResponseType::INFRASTRUCTURE;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800115};
116
Davide Pesavento0c526032024-01-31 21:14:01 -0500117using FailureModes = boost::mp11::mp_list<Nack, Timeout>;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800118
119BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
120{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700121 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
122 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800123
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700124 // odd interests
125 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
126 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
127 }
128
129 // even interests
130 for (const auto& sentInterest : this->face.sentInterests |
131 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
132 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800133 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
134 }
135}
136
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400137BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>)
138{
139 setResponseType(ResponseType::DIRECT);
140 static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true);
141
142 VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs");
143 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
144
145 for (const auto& sentInterest : this->face.sentInterests) {
146 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
147 }
148}
149
Davide Pesavento0c526032024-01-31 21:14:01 -0500150BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, FailureModes,
151 CertificateFetcherDirectFetchFixture<T>)
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800152{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700153 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400154 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500155 // Direct fetcher sends two interests each time - to network and face
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400156 // 3 retries on nack or timeout (2 * (1 + 3) = 8)
Davide Pesavento2acce252022-09-08 22:03:03 -0400157 BOOST_TEST(this->face.sentInterests.size() == 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800158
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700159 // odd interests
160 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
161 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
162 }
163
164 // even interests
165 for (const auto& sentInterest : this->face.sentInterests |
166 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
167 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800168 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
169 }
170}
171
Davide Pesavento0c526032024-01-31 21:14:01 -0500172BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, FailureModes,
173 CertificateFetcherDirectFetchFixture<T>)
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400174{
175 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
176 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
177
178 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400179 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400180 // Direct fetcher sends two interests each time - to network and face
181 // 3 retries on nack or timeout (1 + 3 = 4)
Davide Pesavento2acce252022-09-08 22:03:03 -0400182 BOOST_TEST(this->face.sentInterests.size() == 4);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400183
184 for (const auto& sentInterest : this->face.sentInterests) {
185 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
186 }
187}
188
Davide Pesavento0c526032024-01-31 21:14:01 -0500189BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, FailureModes,
190 CertificateFetcherDirectFetchFixture<T>)
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400191{
192 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
193 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
194
195 this->data.template removeTag<lp::IncomingFaceIdTag>();
196 this->interest.template removeTag<lp::IncomingFaceIdTag>();
197
198 VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected");
Davide Pesavento2acce252022-09-08 22:03:03 -0400199 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
200 BOOST_TEST(this->face.sentInterests.size() == 0);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400201}
202
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800203BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
204{
205 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
206 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
207
208 // odd interests
209 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
210 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
211 }
212
213 // even interests
214 for (const auto& sentInterest : this->face.sentInterests |
215 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
216 boost::adaptors::strided(2)) {
217 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
218 }
219}
220
Davide Pesavento0c526032024-01-31 21:14:01 -0500221BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, FailureModes,
222 CertificateFetcherDirectFetchFixture<T>)
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800223{
224 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400225 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500226 // Direct fetcher sends two interests each time - to network and face
227 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
Davide Pesavento2acce252022-09-08 22:03:03 -0400228 BOOST_TEST(this->face.sentInterests.size() == 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800229
230 // odd interests
231 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
232 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
233 }
234
235 // even interests
236 for (const auto& sentInterest : this->face.sentInterests |
237 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
238 boost::adaptors::strided(2)) {
239 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
240 }
241}
242
243BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800244BOOST_AUTO_TEST_SUITE_END() // Security
245
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400246} // namespace ndn::tests