blob: d62edf27d61c20355639afe2fbf52a88c6517b5e [file] [log] [blame]
Alexander Afanasyevba2cf392017-01-13 19:05:23 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * 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{
124 VALIDATE_SUCCESS(this->data, "Should get accepted, as interests bring cert");
125 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
126
127 for (const auto& sentInterest : this->face.sentInterests) {
128 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
129 }
130}
131
132BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureData, T, Failures, CertificateFetcherDirectFetchFixture<T>)
133{
134 VALIDATE_FAILURE(this->data, "Should fail, as non-direct interests don't bring data");
135 BOOST_CHECK_GT(this->face.sentInterests.size(), 2);
136
137 for (const auto& sentInterest : this->face.sentInterests) {
138 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
139 }
140}
141
142BOOST_FIXTURE_TEST_CASE(ValidateSuccessInterest, CertificateFetcherDirectFetchFixture<Cert>)
143{
144 VALIDATE_SUCCESS(this->interest, "Should get accepted, normal and/or direct interests bring certs");
145 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
146
147 // 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)) {
156 BOOST_CHECK(sentInterest.template getTag<lp::NextHopFaceIdTag>() == nullptr);
157 }
158}
159
160BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailureInterest, T, Failures, CertificateFetcherDirectFetchFixture<T>)
161{
162 VALIDATE_FAILURE(this->interest, "Should fail, as all interests either NACKed or timeout");
163 BOOST_CHECK_GT(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_AUTO_TEST_SUITE_END() // TestCertificateFetcherDirectFetch
179BOOST_AUTO_TEST_SUITE_END() // V2
180BOOST_AUTO_TEST_SUITE_END() // Security
181
182} // namespace tests
183} // namespace v2
184} // namespace security
185} // namespace ndn