blob: 2f27878ab4764db45ec9dcd1ea55e61d9f01a9c2 [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 Afanasyevba2cf392017-01-13 19:05:23 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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
22#include "security/v2/certificate-fetcher-direct-fetch.hpp"
23#include "security/v2/validation-policy-simple-hierarchy.hpp"
24#include "lp/nack.hpp"
25#include "lp/tags.hpp"
26
27#include "boost-test.hpp"
28#include "validator-fixture.hpp"
29
30#include <boost/range/adaptor/strided.hpp>
31#include <boost/range/adaptor/sliced.hpp>
32
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");
143 BOOST_CHECK_GT(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
158BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
159{
160 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
161 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
162
163 // odd interests
164 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
165 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
166 }
167
168 // even interests
169 for (const auto& sentInterest : this->face.sentInterests |
170 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
171 boost::adaptors::strided(2)) {
172 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
173 }
174}
175
176BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
177{
178 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
179 BOOST_CHECK_GT(this->face.sentInterests.size(), 4);
180
181 // odd interests
182 for (const auto& sentInterest : this->face.sentInterests | boost::adaptors::strided(2)) {
183 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() != nullptr);
184 }
185
186 // even interests
187 for (const auto& sentInterest : this->face.sentInterests |
188 boost::adaptors::sliced(1, this->face.sentInterests.size()) |
189 boost::adaptors::strided(2)) {
190 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
191 }
192}
193
194BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
195BOOST_AUTO_TEST_SUITE_END() // V2
196BOOST_AUTO_TEST_SUITE_END() // Security
197
198} // namespace tests
199} // namespace v2
200} // namespace security
201} // namespace ndn