blob: caf941a43d2aded60c191dbb35fc9ac676faaddd [file] [log] [blame]
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -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
Alexander Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/certificate-fetcher-from-network.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/nack.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040028#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080029
30namespace ndn {
31namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040032inline namespace v2 {
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080033namespace tests {
34
35using namespace ndn::tests;
36
37BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080038BOOST_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()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040058 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
59 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080060 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040061 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080062 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
63
64 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
Eric Newberryb74bbda2020-06-18 19:33:58 -070065 interest.setCanBePrefix(false);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080066 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
67
68 processInterest = bind(&CertificateFetcherFromNetworkFixture<Response>::makeResponse, this, _1);
69 }
70
71 void
72 makeResponse(const Interest& interest);
73
74public:
75 Data data;
76 Interest interest;
77};
78
79template<>
80void
81CertificateFetcherFromNetworkFixture<Cert>::makeResponse(const Interest& interest)
82{
83 auto cert = cache.find(interest);
84 if (cert == nullptr) {
85 return;
86 }
87 face.receive(*cert);
88}
89
90template<>
91void
92CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest)
93{
94 // do nothing
95}
96
97template<>
98void
99CertificateFetcherFromNetworkFixture<Nack>::makeResponse(const Interest& interest)
100{
101 lp::Nack nack(interest);
102 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
103 face.receive(nack);
104}
105
106using Failures = boost::mpl::vector<Timeout, Nack>;
107
108BOOST_FIXTURE_TEST_CASE(ValidateSuccess, CertificateFetcherFromNetworkFixture<Cert>)
109{
110 VALIDATE_SUCCESS(this->data, "Should get accepted, as normal interests bring cert");
111 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
112 this->face.sentInterests.clear();
113
Davide Pesavento0f830802018-01-16 23:58:58 -0500114 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800115
116 VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs");
117 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
118}
119
120BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>)
121{
122 VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500123 // first interest + 3 retries
124 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
125
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800126 this->face.sentInterests.clear();
127
Davide Pesavento0f830802018-01-16 23:58:58 -0500128 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800129
130 VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500131 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800132}
133
134BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800135BOOST_AUTO_TEST_SUITE_END() // Security
136
137} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400138} // inline namespace v2
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800139} // namespace security
140} // namespace ndn