blob: 25e0be53f23bd6b99d1f26e13946ed3aa0730d2b [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/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 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"
23#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/nack.hpp"
25#include "ndn-cxx/lp/tags.hpp"
Alexander Afanasyevba2cf392017-01-13 19:05:23 -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 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));
Eric Newberryb74bbda2020-06-18 19:33:58 -070076 interest.setCanBePrefix(false);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080077 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
Eric Newberryb74bbda2020-06-18 19:33:58 -070078 interestNoTag.setCanBePrefix(false);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080079 m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity));
80
81 data.setTag(make_shared<lp::IncomingFaceIdTag>(123));
82 interest.setTag(make_shared<lp::IncomingFaceIdTag>(123));
83
84 processInterest = [this] (const Interest& interest) {
85 auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>();
86 if (nextHopFaceIdTag == nullptr) {
Alexander Afanasyev1660d002019-03-18 10:45:39 -040087 if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) {
88 makeResponse(interest);
89 }
90 }
91 else {
92 if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) {
93 makeResponse(interest);
94 }
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080095 }
96 };
97 }
98
99 void
100 makeResponse(const Interest& interest);
101
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400102 void
103 setResponseType(ResponseType type)
104 {
105 responseType = type;
106 }
107
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800108public:
109 Data data;
110 Interest interest;
111 Interest interestNoTag;
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400112 ResponseType responseType = ResponseType::INFRASTRUCTURE;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800113};
114
115template<>
116void
117CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest)
118{
119 auto cert = cache.find(interest);
120 if (cert == nullptr) {
121 return;
122 }
123 face.receive(*cert);
124}
125
126template<>
127void
128CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest)
129{
130 // do nothing
131}
132
133template<>
134void
135CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest)
136{
137 lp::Nack nack(interest);
138 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
139 face.receive(nack);
140}
141
142using Failures = boost::mpl::vector<Timeout, Nack>;
143
144BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
145{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700146 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
147 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800148
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700149 // odd interests
150 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
151 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
152 }
153
154 // even interests
155 for (const auto& sentInterest : this->face.sentInterests |
156 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
157 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800158 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
159 }
160}
161
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400162BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>)
163{
164 setResponseType(ResponseType::DIRECT);
165 static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true);
166
167 VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs");
168 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
169
170 for (const auto& sentInterest : this->face.sentInterests) {
171 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
172 }
173}
174
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800175BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
176{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700177 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500178 // Direct fetcher sends two interests each time - to network and face
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400179 // 3 retries on nack or timeout (2 * (1 + 3) = 8)
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500180 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800181
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700182 // odd interests
183 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
184 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
185 }
186
187 // even interests
188 for (const auto& sentInterest : this->face.sentInterests |
189 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
190 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800191 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
192 }
193}
194
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400195BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
196{
197 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
198 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
199
200 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
201 // Direct fetcher sends two interests each time - to network and face
202 // 3 retries on nack or timeout (1 + 3 = 4)
203 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
204
205 for (const auto& sentInterest : this->face.sentInterests) {
206 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
207 }
208}
209
210BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
211{
212 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
213 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
214
215 this->data.template removeTag<lp::IncomingFaceIdTag>();
216 this->interest.template removeTag<lp::IncomingFaceIdTag>();
217
218 VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected");
219 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0);
220 BOOST_CHECK(this->lastError.getCode() != ValidationError::Code::IMPLEMENTATION_ERROR);
221}
222
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800223BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
224{
225 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
226 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
227
228 // odd interests
229 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
230 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
231 }
232
233 // even interests
234 for (const auto& sentInterest : this->face.sentInterests |
235 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
236 boost::adaptors::strided(2)) {
237 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
238 }
239}
240
241BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
242{
243 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500244 // Direct fetcher sends two interests each time - to network and face
245 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
246 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800247
248 // odd interests
249 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
250 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
251 }
252
253 // even interests
254 for (const auto& sentInterest : this->face.sentInterests |
255 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
256 boost::adaptors::strided(2)) {
257 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
258 }
259}
260
261BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800262BOOST_AUTO_TEST_SUITE_END() // Security
263
264} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400265} // inline namespace v2
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800266} // namespace security
267} // namespace ndn