blob: 56677b7f8a85125a073e9fd2dda5882f1b216638 [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"
23#include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/lp/nack.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
Alexander Afanasyev09236c22020-06-03 13:42:38 -040027#include "tests/unit/security/validator-fixture.hpp"
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080028
29namespace ndn {
30namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040031inline namespace v2 {
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080032namespace tests {
33
34using namespace ndn::tests;
35
36BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080037BOOST_AUTO_TEST_SUITE(TestCertificateFetcherFromNetwork)
38
39class Cert
40{
41};
42
43class Timeout
44{
45};
46
47class Nack
48{
49};
50
51template<class Response>
52class CertificateFetcherFromNetworkFixture : public HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy,
53 CertificateFetcherFromNetwork>
54{
55public:
56 CertificateFetcherFromNetworkFixture()
Alexander Afanasyev09236c22020-06-03 13:42:38 -040057 : data("/Security/ValidatorFixture/Sub1/Sub3/Data")
58 , interest("/Security/ValidatorFixture/Sub1/Sub3/Interest")
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080059 {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040060 Identity subSubIdentity = addSubCertificate("/Security/ValidatorFixture/Sub1/Sub3", subIdentity);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -080061 cache.insert(subSubIdentity.getDefaultKey().getDefaultCertificate());
62
63 m_keyChain.sign(data, signingByIdentity(subSubIdentity));
64 m_keyChain.sign(interest, signingByIdentity(subSubIdentity));
65
66 processInterest = bind(&CertificateFetcherFromNetworkFixture<Response>::makeResponse, this, _1);
67 }
68
69 void
70 makeResponse(const Interest& interest);
71
72public:
73 Data data;
74 Interest interest;
75};
76
77template<>
78void
79CertificateFetcherFromNetworkFixture<Cert>::makeResponse(const Interest& interest)
80{
81 auto cert = cache.find(interest);
82 if (cert == nullptr) {
83 return;
84 }
85 face.receive(*cert);
86}
87
88template<>
89void
90CertificateFetcherFromNetworkFixture<Timeout>::makeResponse(const Interest& interest)
91{
92 // do nothing
93}
94
95template<>
96void
97CertificateFetcherFromNetworkFixture<Nack>::makeResponse(const Interest& interest)
98{
99 lp::Nack nack(interest);
100 nack.setHeader(lp::NackHeader().setReason(lp::NackReason::NO_ROUTE));
101 face.receive(nack);
102}
103
104using Failures = boost::mpl::vector<Timeout, Nack>;
105
106BOOST_FIXTURE_TEST_CASE(ValidateSuccess, CertificateFetcherFromNetworkFixture<Cert>)
107{
108 VALIDATE_SUCCESS(this->data, "Should get accepted, as normal interests bring cert");
109 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
110 this->face.sentInterests.clear();
111
Davide Pesavento0f830802018-01-16 23:58:58 -0500112 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800113
114 VALIDATE_SUCCESS(this->interest, "Should get accepted, as interests bring certs");
115 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 2);
116}
117
118BOOST_FIXTURE_TEST_CASE_TEMPLATE(ValidateFailure, T, Failures, CertificateFetcherFromNetworkFixture<T>)
119{
120 VALIDATE_FAILURE(this->data, "Should fail, as interests don't bring data");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500121 // first interest + 3 retries
122 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
123
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800124 this->face.sentInterests.clear();
125
Davide Pesavento0f830802018-01-16 23:58:58 -0500126 this->advanceClocks(1_h, 2); // expire validator caches
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800127
128 VALIDATE_FAILURE(this->interest, "Should fail, as interests don't bring data");
Ashlesh Gawande3e39a4d2018-08-30 16:49:13 -0500129 BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 4);
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800130}
131
132BOOST_AUTO_TEST_SUITE_END() // TestCertificateFetcherFromNetwork
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800133BOOST_AUTO_TEST_SUITE_END() // Security
134
135} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400136} // inline namespace v2
Alexander Afanasyev7bc10fa2017-01-13 16:56:26 -0800137} // namespace security
138} // namespace ndn