blob: d2e05658cb1dc6e9e44dcfdbc1abf0d8c6a9668d [file] [log] [blame]
Manika Mittal95c80702017-01-27 09:54:41 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Manika Mittal95c80702017-01-27 09:54:41 -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
22#include "security/v2/certificate-bundle-fetcher.hpp"
23#include "security/v2/validation-policy-simple-hierarchy.hpp"
24#include "util/regex/regex-pattern-list-matcher.hpp"
25#include "lp/nack.hpp"
26
27#include "boost-test.hpp"
28#include "validator-fixture.hpp"
29
30namespace ndn {
31namespace security {
32namespace v2 {
33namespace tests {
34
35using namespace ndn::tests;
36
37BOOST_AUTO_TEST_SUITE(Security)
38BOOST_AUTO_TEST_SUITE(V2)
39BOOST_AUTO_TEST_SUITE(TestCertificateBundleFetcher)
40
41class CertificateBundleFetcherWrapper : public CertificateBundleFetcher
42{
43public:
44 CertificateBundleFetcherWrapper(Face& face)
45 : CertificateBundleFetcher(make_unique<CertificateFetcherFromNetwork>(face), face)
46 {
47 }
48};
49
50class Bundle
51{
52};
53
54class Cert
55{
56};
57
58class Timeout
59{
60};
61
62class Nack
63{
64};
65
66template<class Response>
67class CertificateBundleFetcherFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
68 CertificateBundleFetcherWrapper>
69{
70public:
71 CertificateBundleFetcherFixture()
72 : data("/Security/V2/ValidatorFixture/Sub1/Sub3/Data")
73 {
74 subSubIdentity = addSubCertificate("/Security/V2/ValidatorFixture/Sub1/Sub3", subIdentity);
75 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
76
77 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
78 bundleRegexMatcher = make_shared<RegexPatternListMatcher>("<>*<_BUNDLE><>*", nullptr);
79 processInterest = [this] (const Interest& interest) {
80 // check if the interest is for Bundle or individual certificates
81 if (bundleRegexMatcher->match(interest.getName(), 0, interest.getName().size())) {
82 makeResponse(interest);
83 }
84 else {
85 auto cert = cache.find(interest);
86 if (cert == nullptr) {
87 return;
88 }
89 face.receive(*cert);
90 }
91 };
92 }
93
94 void
95 makeResponse(const Interest& interest);
96
97public:
98 Data data;
99 Identity subSubIdentity;
100 shared_ptr<RegexPatternListMatcher> bundleRegexMatcher;
101};
102
103template<>
104void
105CertificateBundleFetcherFixture<Bundle>::makeResponse(const Interest& interest)
106{
107 Block certList = Block(tlv::Content);
108 Name bundleName(interest.getName());
109
110 if (!bundleName.get(-1).isSegment() || bundleName.get(-1).toSegment() == 0) {
111 Block subSubCert = subSubIdentity.getDefaultKey().getDefaultCertificate().wireEncode();
112 certList.push_back(subSubCert);
113
114 if (!bundleName.get(-1).isSegment()) {
115 bundleName
116 .appendVersion()
117 .appendSegment(0);
118 }
119 }
120 else {
121 Block subCert = subIdentity.getDefaultKey().getDefaultCertificate().wireEncode();
122 Block anchor = identity.getDefaultKey().getDefaultCertificate().wireEncode();
123 certList.push_back(subCert);
124 certList.push_back(anchor);
125 }
126
127 shared_ptr<Data> certBundle = make_shared<Data>();
128 certBundle->setName(bundleName);
Davide Pesavento0f830802018-01-16 23:58:58 -0500129 certBundle->setFreshnessPeriod(100_s);
Manika Mittal95c80702017-01-27 09:54:41 -0800130 certBundle->setContent(certList);
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000131 certBundle->setFinalBlock(name::Component::fromSegment(1));
Manika Mittal95c80702017-01-27 09:54:41 -0800132
133 m_keyChain.sign(*certBundle, signingWithSha256());
134
135 face.receive(*certBundle);
136}
137
138template<>
139void
140CertificateBundleFetcherFixture<Timeout>::makeResponse(const Interest& interest)
141{
Davide Pesavento0f830802018-01-16 23:58:58 -0500142 this->advanceClocks(200_s);
Manika Mittal95c80702017-01-27 09:54:41 -0800143}
144
145template<>
146void
147CertificateBundleFetcherFixture<Nack>::makeResponse(const Interest& interest)
148{
149 lp::Nack nack(interest);
150 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
151 face.receive(nack);
152}
153
154BOOST_FIXTURE_TEST_CASE(ValidateSuccessWithBundle, CertificateBundleFetcherFixture<Bundle>)
155{
156 VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the bundle segments");
157 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2); // produced bundle has 2 segments
158
159 for (const auto& sentInterest : this->face.sentInterests) {
160 BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size()));
161 }
162}
163
164using SuccessWithoutBundle = boost::mpl::vector<Nack, Timeout>;
165
166BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateSuccessWithoutBundle, T, SuccessWithoutBundle, CertificateBundleFetcherFixture<T>)
167{
168 VALIDATE_SUCCESS(this->data, "Should get accepted, as interest brings the certs");
169 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4); // since interest for Bundle fails, each cert is retrieved
170
171 bool toggle = true;
172 for (const auto& sentInterest : this->face.sentInterests) {
173 if (toggle) {
174 // every alternate interest is going to be that of a bundle
175 BOOST_CHECK(this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size()));
176 }
177 else {
178 BOOST_CHECK(!this->bundleRegexMatcher->match(sentInterest.getName(), 0, sentInterest.getName().size()));
179 }
180 toggle = !toggle;
181 }
182}
183
184BOOST_AUTO_TEST_SUITE_END() // TestCertificateBundleFetcher
185BOOST_AUTO_TEST_SUITE_END() // V2
186BOOST_AUTO_TEST_SUITE_END() // Security
187
188} // namespace tests
189} // namespace v2
190} // namespace security
191} // namespace ndn