blob: e2d543c099d84cf32b648e4d57f7e2ae2a787932 [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/*
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -05003 * Copyright (c) 2013-2018 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/v2/certificate-fetcher-direct-fetch.hpp"
23#include "ndn-cxx/security/v2/validation-policy-simple-hierarchy.hpp"
24#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"
28#include "tests/unit/security/v2/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 {
35namespace v2 {
36namespace tests {
37
38using namespace ndn::tests;
39
40BOOST_AUTO_TEST_SUITE(Security)
41BOOST_AUTO_TEST_SUITE(V2)
42BOOST_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:
61 CertificateFetcherDirectFetchFixture()
62 : data("/Security/V2/ValidatorFixture/Sub1/Sub3/Data")
63 , interest("/Security/V2/ValidatorFixture/Sub1/Sub3/Interest")
64 , interestNoTag("/Security/V2/ValidatorFixture/Sub1/Sub3/Interest2")
65 {
66 Identity subSubIdentity = addSubCertificate("/Security/V2/ValidatorFixture/Sub1/Sub3", subIdentity);
67 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
68
69 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
70 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
71 m_keyChain.sign(interestNoTag, signingByIdentity(subSubIdentity));
72
73 data.setTag(make_shared<lp::IncomingFaceIdTag>(123));
74 interest.setTag(make_shared<lp::IncomingFaceIdTag>(123));
75
76 processInterest = [this] (const Interest& interest) {
77 auto nextHopFaceIdTag = interest.template getTag<lp::NextHopFaceIdTag>();
78 if (nextHopFaceIdTag == nullptr) {
79 makeResponse(interest); // respond only to the "infrastructure" interest
80 }
81 };
82 }
83
84 void
85 makeResponse(const Interest& interest);
86
87public:
88 Data data;
89 Interest interest;
90 Interest interestNoTag;
91};
92
93template<>
94void
95CertificateFetcherDirectFetchFixture<Cert>::makeResponse(const Interest& interest)
96{
97 auto cert = cache.find(interest);
98 if (cert == nullptr) {
99 return;
100 }
101 face.receive(*cert);
102}
103
104template<>
105void
106CertificateFetcherDirectFetchFixture<Timeout>::makeResponse(const Interest& interest)
107{
108 // do nothing
109}
110
111template<>
112void
113CertificateFetcherDirectFetchFixture<Nack>::makeResponse(const Interest& interest)
114{
115 lp::Nack nack(interest);
116 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
117 face.receive(nack);
118}
119
120using Failures = boost::mpl::vector<Timeout, Nack>;
121
122BOOST_FIXTURE_TEST_CASE(ValidateSuccessData, CertificateFetcherDirectFetchFixture<Cert>)
123{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700124 VALIDATE_SUCCESS(this->data, "Should get accepted, normal and/or direct interests bring certs");
125 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800126
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700127 // odd interests
128 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
129 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
130 }
131
132 // even interests
133 for (const auto& sentInterest : this->face.sentInterests |
134 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
135 boost::adaptors::strided(2)) {
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800136 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
137 }
138}
139
140BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
141{
Zhiyi Zhanga1302f62017-10-31 10:22:35 -0700142 VALIDATE_FAILURE(this->data, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500143 // Direct fetcher sends two interests each time - to network and face
144 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
145 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
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
160BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
161{
162 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
163 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
164
165 // odd interests
166 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
167 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
168 }
169
170 // even interests
171 for (const auto& sentInterest : this->face.sentInterests |
172 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
173 boost::adaptors::strided(2)) {
174 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
175 }
176}
177
178BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
179{
180 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500181 // Direct fetcher sends two interests each time - to network and face
182 // 3 retries on nack or timeout (2 * (1 + 3) = 4)
183 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 8);
Alexander Afanasyevba2cf392017-01-13 19:05:23 -0800184
185 // odd interests
186 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
187 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
188 }
189
190 // even interests
191 for (const auto& sentInterest : this->face.sentInterests |
192 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
193 boost::adaptors::strided(2)) {
194 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
195 }
196}
197
198BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
199BOOST_AUTO_TEST_SUITE_END() // V2
200BOOST_AUTO_TEST_SUITE_END() // Security
201
202} // namespace tests
203} // namespace v2
204} // namespace security
205} // namespace ndn