blob: 7c9ba9007daac3edcd8046bd45beef39f3610d5b [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 Pesaventoaee2ada2022-02-18 14:43:02 -05003 * Copyright (c) 2013-2022 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
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080030#include <boost/range/adaptor/sliced.hpp>
Davide Pesavento7e780642018-11-24 15:51:34 -050031#include <boost/range/adaptor/strided.hpp>
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080032
33namespace ndn {
34namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040035inline namespace v2 {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080036namespace tests {
37
38using namespace ndn::tests;
39
40BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080041BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch)
42
43class Cert
44{
45};
46
47class Timeout
48{
49};
50
51class Nack
52{
53};
54
55template<class Response>
56class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
57 CertificateFetcherDirectFetch>
58{
59public:
Alexander Afanasyev1660d002019-03-18 10:45:39 -040060 enum class ResponseType {
61 INFRASTRUCTURE,
62 DIRECT,
63 BOTH
64 };
65
66public:
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080067 CertificateFetcherDirectFetchFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040068 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
69 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
70 , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2")
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080071 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040072 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080073 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
74
75 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
76 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
77 m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity));
78
79 data.setTag(make_shared<lp::IncomingFaceIdTag>(123));
80 interest.setTag(make_shared<lp::IncomingFaceIdTag>(123));
81
82 processInterest = [this] (const Interest& interest) {
83 auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>();
84 if (nextHopFaceIdTag == nullptr) {
Alexander Afanasyev1660d002019-03-18 10:45:39 -040085 if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) {
86 makeResponse(interest);
87 }
88 }
89 else {
90 if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) {
91 makeResponse(interest);
92 }
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080093 }
94 };
95 }
96
97 void
98 makeResponse(const Interest& interest);
99
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400100 void
101 setResponseType(ResponseType type)
102 {
103 responseType = type;
104 }
105
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800106public:
107 Data data;
108 Interest interest;
109 Interest interestNoTag;
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400110 ResponseType responseType = ResponseType::INFRASTRUCTURE;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800111};
112
113template<>
114void
115CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest)
116{
117 auto cert = cache.find(interest);
118 if (cert == nullptr) {
119 return;
120 }
121 face.receive(*cert);
122}
123
124template<>
125void
126CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest)
127{
128 // do nothing
129}
130
131template<>
132void
133CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest)
134{
Davide Pesavento2acce252022-09-08 22:03:03 -0400135 face.receive(makeNack(interest, lp::NackReason::NO_ROUTE));
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800136}
137
138using Failures = boost::mpl::vector<Timeout, Nack>;
139
140BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
141{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700142 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
143 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800144
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700145 // odd interests
146 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
147 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
148 }
149
150 // even interests
151 for (const auto& sentInterest : this->face.sentInterests |
152 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
153 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800154 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
155 }
156}
157
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400158BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>)
159{
160 setResponseType(ResponseType::DIRECT);
161 static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true);
162
163 VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs");
164 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
165
166 for (const auto& sentInterest : this->face.sentInterests) {
167 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
168 }
169}
170
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800171BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
172{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700173 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400174 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500175 // Direct fetcher sends two interests each time - to network and face
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400176 // 3 retries on nack or timeout (2 * (1 + 3) = 8)
Davide Pesavento2acce252022-09-08 22:03:03 -0400177 BOOST_TEST(this->face.sentInterests.size() == 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800178
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700179 // odd interests
180 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
181 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
182 }
183
184 // even interests
185 for (const auto& sentInterest : this->face.sentInterests |
186 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
187 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800188 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
189 }
190}
191
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400192BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
193{
194 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
195 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
196
197 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400198 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400199 // Direct fetcher sends two interests each time - to network and face
200 // 3 retries on nack or timeout (1 + 3 = 4)
Davide Pesavento2acce252022-09-08 22:03:03 -0400201 BOOST_TEST(this->face.sentInterests.size() == 4);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400202
203 for (const auto& sentInterest : this->face.sentInterests) {
204 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
205 }
206}
207
208BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
209{
210 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
211 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
212
213 this->data.template removeTag<lp::IncomingFaceIdTag>();
214 this->interest.template removeTag<lp::IncomingFaceIdTag>();
215
216 VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected");
Davide Pesavento2acce252022-09-08 22:03:03 -0400217 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
218 BOOST_TEST(this->face.sentInterests.size() == 0);
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400219}
220
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800221BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
222{
223 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
224 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
225
226 // odd interests
227 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
228 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
229 }
230
231 // even interests
232 for (const auto& sentInterest : this->face.sentInterests |
233 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
234 boost::adaptors::strided(2)) {
235 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
236 }
237}
238
239BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
240{
241 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
Davide Pesavento2acce252022-09-08 22:03:03 -0400242 BOOST_TEST(this->lastError.getCode() == ValidationError::CANNOT_RETRIEVE_CERT);
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500243 // Direct fetcher sends two interests each time - to network and face
244 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
Davide Pesavento2acce252022-09-08 22:03:03 -0400245 BOOST_TEST(this->face.sentInterests.size() == 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800246
247 // odd interests
248 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
249 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
250 }
251
252 // even interests
253 for (const auto& sentInterest : this->face.sentInterests |
254 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
255 boost::adaptors::strided(2)) {
256 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
257 }
258}
259
260BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800261BOOST_AUTO_TEST_SUITE_END() // Security
262
263} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400264} // inline namespace v2
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800265} // namespace security
266} // namespace ndn