blob: 7757f5b81e291e7087a03b926565b463d33b729b [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"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/nack.hpp"
25#include "ndn-cxx/lp/tags.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080027
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040029#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080030
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
34namespace ndn {
35namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040036inline namespace v2 {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080037namespace tests {
38
39using namespace ndn::tests;
40
41BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080042BOOST_AUTO_TEST_SUITE(TestCertificateFetcherDirectFetch)
43
44class Cert
45{
46};
47
48class Timeout
49{
50};
51
52class Nack
53{
54};
55
56template<class Response>
57class CertificateFetcherDirectFetchFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
58 CertificateFetcherDirectFetch>
59{
60public:
Alexander Afanasyev1660d002019-03-18 10:45:39 -040061 enum class ResponseType {
62 INFRASTRUCTURE,
63 DIRECT,
64 BOTH
65 };
66
67public:
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080068 CertificateFetcherDirectFetchFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040069 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
70 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
71 , interestNoTag("/Security/ValidatorFixture/Sub1/Sub3/Interest2")
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080072 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040073 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080074 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
75
76 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
Eric Newberryb74bbda2020-06-18 19:33:58 -070077 interest.setCanBePrefix(false);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080078 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
Eric Newberryb74bbda2020-06-18 19:33:58 -070079 interestNoTag.setCanBePrefix(false);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080080 m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity));
81
82 data.setTag(make_shared<lp::IncomingFaceIdTag>(123));
83 interest.setTag(make_shared<lp::IncomingFaceIdTag>(123));
84
85 processInterest = [this] (const Interest& interest) {
86 auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>();
87 if (nextHopFaceIdTag == nullptr) {
Alexander Afanasyev1660d002019-03-18 10:45:39 -040088 if (responseType == ResponseType::INFRASTRUCTURE || responseType == ResponseType::BOTH) {
89 makeResponse(interest);
90 }
91 }
92 else {
93 if (responseType == ResponseType::DIRECT || responseType == ResponseType::BOTH) {
94 makeResponse(interest);
95 }
Alexander Afanasyevba2cf392017-01-13 19:05:23 -080096 }
97 };
98 }
99
100 void
101 makeResponse(const Interest& interest);
102
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400103 void
104 setResponseType(ResponseType type)
105 {
106 responseType = type;
107 }
108
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800109public:
110 Data data;
111 Interest interest;
112 Interest interestNoTag;
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400113 ResponseType responseType = ResponseType::INFRASTRUCTURE;
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800114};
115
116template<>
117void
118CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest)
119{
120 auto cert = cache.find(interest);
121 if (cert == nullptr) {
122 return;
123 }
124 face.receive(*cert);
125}
126
127template<>
128void
129CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest)
130{
131 // do nothing
132}
133
134template<>
135void
136CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest)
137{
138 lp::Nack nack(interest);
139 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
140 face.receive(nack);
141}
142
143using Failures = boost::mpl::vector<Timeout, Nack>;
144
145BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
146{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700147 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
148 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800149
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700150 // odd interests
151 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
152 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
153 }
154
155 // even interests
156 for (const auto& sentInterest : this->face.sentInterests |
157 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
158 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800159 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
160 }
161}
162
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400163BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>)
164{
165 setResponseType(ResponseType::DIRECT);
166 static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true);
167
168 VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs");
169 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
170
171 for (const auto& sentInterest : this->face.sentInterests) {
172 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
173 }
174}
175
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800176BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
177{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700178 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500179 // Direct fetcher sends two interests each time - to network and face
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400180 // 3 retries on nack or timeout (2 * (1 + 3) = 8)
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500181 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800182
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700183 // odd interests
184 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
185 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
186 }
187
188 // even interests
189 for (const auto& sentInterest : this->face.sentInterests |
190 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
191 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800192 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
193 }
194}
195
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400196BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
197{
198 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
199 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
200
201 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
202 // Direct fetcher sends two interests each time - to network and face
203 // 3 retries on nack or timeout (1 + 3 = 4)
204 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
205
206 for (const auto& sentInterest : this->face.sentInterests) {
207 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
208 }
209}
210
211BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataNoTagDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
212{
213 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
214 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
215
216 this->data.template removeTag<lp::IncomingFaceIdTag>();
217 this->interest.template removeTag<lp::IncomingFaceIdTag>();
218
219 VALIDATE_FAILURE(this->data, "Should fail, as no interests are expected");
220 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0);
221 BOOST_CHECK(this->lastError.getCode() != ValidationError::Code::IMPLEMENTATION_ERROR);
222}
223
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800224BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
225{
226 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
227 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
228
229 // odd interests
230 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
231 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
232 }
233
234 // even interests
235 for (const auto& sentInterest : this->face.sentInterests |
236 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
237 boost::adaptors::strided(2)) {
238 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
239 }
240}
241
242BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
243{
244 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500245 // Direct fetcher sends two interests each time - to network and face
246 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
247 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800248
249 // odd interests
250 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
251 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
252 }
253
254 // even interests
255 for (const auto& sentInterest : this->face.sentInterests |
256 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
257 boost::adaptors::strided(2)) {
258 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
259 }
260}
261
262BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800263BOOST_AUTO_TEST_SUITE_END() // Security
264
265} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400266} // inline namespace v2
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800267} // namespace security
268} // namespace ndn