blob: 0caa12ff9dd3593bda66ee0334177ea26d1f5355 [file] [log] [blame]
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -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-from-network.hpp"
23#include "security/v2/validation-policy-simple-hierarchy.hpp"
24#include "lp/nack.hpp"
25
26#include "boost-test.hpp"
27#include "validator-fixture.hpp"
28
29namespace ndn {
30namespace security {
31namespace v2 {
32namespace tests {
33
34using namespace ndn::tests;
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(V2)
38BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork)
39
40class Cert
41{
42};
43
44class Timeout
45{
46};
47
48class Nack
49{
50};
51
52template<class Response>
53class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
54 CertificateFetcherFromNetwork>
55{
56public:
57 CertificateFetcherFromNetworkFixture()
58 : data("/Security/V2/ValidatorFixture/Sub1/Sub3/Data")
59 , interest("/Security/V2/ValidatorFixture/Sub1/Sub3/Interest")
60 {
61 Identity subSubIdentity = addSubCertificate("/Security/V2/ValidatorFixture/Sub1/Sub3", subIdentity);
62 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
63
64 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
65 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
66
67 processInterest = bind(&CertificateFetcherFromNetworkFixture<Response>::makeResponse, this, _1);
68 }
69
70 void
71 makeResponse(const Interest& interest);
72
73public:
74 Data data;
75 Interest interest;
76};
77
78template<>
79void
80CertificateFetcherFromNetworkFixture<Cert>::makeResponse(const Interest& interest)
81{
82 auto cert = cache.find(interest);
83 if (cert == nullptr) {
84 return;
85 }
86 face.receive(*cert);
87}
88
89template<>
90void
91CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest)
92{
93 // do nothing
94}
95
96template<>
97void
98CertificateFetcherFromNetworkFixture<Nack>::makeResponse(const Interest& interest)
99{
100 lp::Nack nack(interest);
101 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
102 face.receive(nack);
103}
104
105using Failures = boost::mpl::vector<Timeout, Nack>;
106
107BOOST_FIXTURE_TEST_CASE(ValidateSuccess, CertificateFetcherFromNetworkFixture<Cert>)
108{
109 VALIDATE_SUCCESS(this->data, "Should get accepted, as normal interests bring cert");
110 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
111 this->face.sentInterests.clear();
112
113 this->advanceClocks(time::hours(1), 2); // expire validator caches
114
115 VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs");
116 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
117}
118
119BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>)
120{
121 VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data");
122 BOOST_CHECK_GT(this->face.sentInterests.size(), 2);
123 this->face.sentInterests.clear();
124
125 this->advanceClocks(time::hours(1), 2); // expire validator caches
126
127 VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data");
128 BOOST_CHECK_GT(this->face.sentInterests.size(), 2);
129}
130
131BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork
132BOOST_AUTO_TEST_SUITE_END() // V2
133BOOST_AUTO_TEST_SUITE_END() // Security
134
135} // namespace tests
136} // namespace v2
137} // namespace security
138} // namespace ndn