blob: 9f8c24f98b708f4541742e35e69ebdd99a865eea [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));
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{
135 lp::Nack nack(interest);
136 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
137 face.receive(nack);
138}
139
140using Failures = boost::mpl::vector<Timeout, Nack>;
141
142BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
143{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700144 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
145 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800146
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700147 // odd interests
148 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
149 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
150 }
151
152 // even interests
153 for (const auto& sentInterest : this->face.sentInterests |
154 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
155 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800156 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
157 }
158}
159
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400160BOOST_FIXTURE_TEST_CASE(ValidateSuccessDataDirectOnly, CertificateFetcherDirectFetchFixture<Cert>)
161{
162 setResponseType(ResponseType::DIRECT);
163 static_cast<CertificateFetcherDirectFetch&>(validator.getFetcher()).setSendDirectInterestOnly(true);
164
165 VALIDATE_SUCCESS(this->data, "Should get accepted, direct interests bring certs");
166 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
167
168 for (const auto& sentInterest : this->face.sentInterests) {
169 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
170 }
171}
172
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800173BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
174{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700175 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500176 // Direct fetcher sends two interests each time - to network and face
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400177 // 3 retries on nack or timeout (2 * (1 + 3) = 8)
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500178 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800179
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700180 // odd interests
181 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
182 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
183 }
184
185 // even interests
186 for (const auto& sentInterest : this->face.sentInterests |
187 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
188 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800189 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
190 }
191}
192
Alexander Afanasyev1660d002019-03-18 10:45:39 -0400193BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureDataDirectOnly, T, Failures, CertificateFetcherDirectFetchFixture<T>)
194{
195 this->setResponseType(CertificateFetcherDirectFetchFixture<T>::ResponseType::DIRECT);
196 static_cast<CertificateFetcherDirectFetch&>(this->validator.getFetcher()).setSendDirectInterestOnly(true);
197
198 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
199 // Direct fetcher sends two interests each time - to network and face
200 // 3 retries on nack or timeout (1 + 3 = 4)
201 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
202
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");
217 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0);
218 BOOST_CHECK(this->lastError.getCode() != ValidationError::Code::IMPLEMENTATION_ERROR);
219}
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");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500242 // Direct fetcher sends two interests each time - to network and face
243 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
244 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800245
246 // odd interests
247 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
248 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
249 }
250
251 // even interests
252 for (const auto& sentInterest : this->face.sentInterests |
253 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
254 boost::adaptors::strided(2)) {
255 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
256 }
257}
258
259BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800260BOOST_AUTO_TEST_SUITE_END() // Security
261
262} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400263} // inline namespace v2
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800264} // namespace security
265} // namespace ndn