blob: ae41eeb225de4cdf7f7364a1cb38071d14c47465 [file] [log] [blame]
Yumin Xiafa2bce72017-04-09 16:20:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev08d18742018-03-15 16:31:28 -04002/*
Alexander Afanasyev60514ec2020-06-03 14:18:53 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Yumin Xiafa2bce72017-04-09 16:20:25 -07004 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Yumin Xiafa2bce72017-04-09 16:20:25 -070020#include "validator/certificate-fetcher-ndns-appcert.hpp"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050021
Yumin Xiafa2bce72017-04-09 16:20:25 -070022#include "ndns-label.hpp"
Yumin Xiafa2bce72017-04-09 16:20:25 -070023#include "daemon/name-server.hpp"
24#include "daemon/rrset-factory.hpp"
25#include "mgmt/management-tool.hpp"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050026#include "util/cert-helper.hpp"
27#include "validator/validator.hpp"
Yumin Xiafa2bce72017-04-09 16:20:25 -070028
Davide Pesaventobdd88c12020-11-26 00:35:08 -050029#include "boost-test.hpp"
Yumin Xiafa2bce72017-04-09 16:20:25 -070030#include "unit/database-test-data.hpp"
31
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040032#include <ndn-cxx/security/validation-policy-simple-hierarchy.hpp>
Davide Pesaventobdd88c12020-11-26 00:35:08 -050033#include <ndn-cxx/util/dummy-client-face.hpp>
Yumin Xiafa2bce72017-04-09 16:20:25 -070034
35namespace ndn {
36namespace ndns {
37namespace tests {
38
Yumin Xiafa2bce72017-04-09 16:20:25 -070039BOOST_AUTO_TEST_SUITE(AppCertFetcher)
40
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040041static unique_ptr<security::Validator>
Davide Pesavento2a3bb842019-03-22 17:39:29 -040042makeValidatorAppCert(Face& face)
Yumin Xiafa2bce72017-04-09 16:20:25 -070043{
Davide Pesaventobdd88c12020-11-26 00:35:08 -050044 return make_unique<security::Validator>(make_unique<security::ValidationPolicySimpleHierarchy>(),
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040045 make_unique<CertificateFetcherAppCert>(face));
Yumin Xiafa2bce72017-04-09 16:20:25 -070046}
47
48class AppCertFetcherFixture : public DbTestData
49{
50public:
51 AppCertFetcherFixture()
Davide Pesavento2a3bb842019-03-22 17:39:29 -040052 : m_validatorFace(m_io, m_keyChain, {true, true})
53 , m_validator(makeValidatorAppCert(m_validatorFace))
Yumin Xiafa2bce72017-04-09 16:20:25 -070054 {
55 // build the data and certificate for this test
56 buildAppCertAndData();
57
Davide Pesavento2a3bb842019-03-22 17:39:29 -040058 auto serverValidator = NdnsValidatorBuilder::create(m_validatorFace, 10, 0,
Davide Pesaventobdd88c12020-11-26 00:35:08 -050059 UNIT_TESTS_TMPDIR "/validator.conf");
Davide Pesavento2a3bb842019-03-22 17:39:29 -040060 // initialize all servers
61 auto addServer = [this, &serverValidator] (const Name& zoneName) {
62 m_serverFaces.push_back(make_unique<util::DummyClientFace>(m_io, m_keyChain,
63 util::DummyClientFace::Options{true, true}));
64 m_serverFaces.back()->linkTo(m_validatorFace);
65
Yumin Xiafa2bce72017-04-09 16:20:25 -070066 // validator is used only for check update signature
67 // no updates tested here, so validator will not be used
68 // passing m_validator is only for construct server
69 Name certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
Davide Pesavento2a3bb842019-03-22 17:39:29 -040070 Name(zoneName).append("NDNS"));
71 auto server = make_shared<NameServer>(zoneName, certName, *m_serverFaces.back(),
72 m_session, m_keyChain, *serverValidator);
73 m_servers.push_back(std::move(server));
Yumin Xiafa2bce72017-04-09 16:20:25 -070074 };
75 addServer(m_testName);
76 addServer(m_netName);
77 addServer(m_ndnsimName);
78 advanceClocks(time::milliseconds(10), 1);
79 }
80
Yumin Xiafa2bce72017-04-09 16:20:25 -070081private:
82 void
83 buildAppCertAndData()
84 {
85 // create NDNS-stored certificate and the signed data
Davide Pesaventobdd88c12020-11-26 00:35:08 -050086 Identity ndnsimIdentity = m_keyChain.createIdentity(m_ndnsimName);
Yumin Xiafa2bce72017-04-09 16:20:25 -070087 Key randomKey = m_keyChain.createKey(ndnsimIdentity);
88 Certificate ndnsStoredAppCert = randomKey.getDefaultCertificate();
89 RrsetFactory rf(TEST_DATABASE.string(), m_ndnsimName, m_keyChain,
90 CertHelper::getIdentity(m_keyChain, Name(m_ndnsimName).append(label::NDNS_ITERATIVE_QUERY))
91 .getDefaultKey()
92 .getDefaultCertificate()
93 .getName());
94 rf.onlyCheckZone();
95 Rrset appCertRrset = rf.generateCertRrset(randomKey.getName().getSubName(-2),
96 VERSION_USE_UNIX_TIMESTAMP, DEFAULT_RR_TTL,
97 ndnsStoredAppCert);
98 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
99 tool.addRrset(appCertRrset);
100
101 m_appCertSignedData = Data(Name(m_ndnsimName).append("randomData"));
102 m_keyChain.sign(m_appCertSignedData, signingByCertificate(ndnsStoredAppCert));
103
104 // load this certificate as the trust anchor
105 m_validator->loadAnchor("", std::move(ndnsStoredAppCert));
106 }
107
108public:
Davide Pesavento2a3bb842019-03-22 17:39:29 -0400109 util::DummyClientFace m_validatorFace;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400110 unique_ptr<security::Validator> m_validator;
Davide Pesavento2a3bb842019-03-22 17:39:29 -0400111 std::vector<unique_ptr<util::DummyClientFace>> m_serverFaces;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700112 std::vector<shared_ptr<ndns::NameServer>> m_servers;
113 Data m_appCertSignedData;
114};
115
Yumin Xiafa2bce72017-04-09 16:20:25 -0700116BOOST_FIXTURE_TEST_CASE(Basic, AppCertFetcherFixture)
117{
118 bool hasValidated = false;
119 m_validator->validate(m_appCertSignedData,
120 [&] (const Data& data) {
121 hasValidated = true;
122 BOOST_CHECK(true);
123 },
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400124 [&] (const Data& data, const security::ValidationError& str) {
Yumin Xiafa2bce72017-04-09 16:20:25 -0700125 hasValidated = true;
126 BOOST_CHECK(false);
127 });
128 advanceClocks(time::milliseconds(10), 1000);
129 BOOST_CHECK_EQUAL(hasValidated, true);
130}
131
132BOOST_AUTO_TEST_SUITE_END()
133
134} // namespace tests
135} // namespace ndns
136} // namespace ndn