blob: 3f236aa92ccbac9c7455b769a6a7c2e67287478e [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/*
3 * Copyright (c) 2014-2018, 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
20#include "validator/validator.hpp"
21#include "validator/certificate-fetcher-ndns-appcert.hpp"
22#include "ndns-label.hpp"
23#include "util/cert-helper.hpp"
24#include "daemon/name-server.hpp"
25#include "daemon/rrset-factory.hpp"
26#include "mgmt/management-tool.hpp"
27
28#include "test-common.hpp"
29#include "dummy-forwarder.hpp"
30#include "unit/database-test-data.hpp"
31
32#include <ndn-cxx/util/io.hpp>
33#include <ndn-cxx/security/v2/validation-policy-simple-hierarchy.hpp>
34
35namespace ndn {
36namespace ndns {
37namespace tests {
38
Yumin Xiafa2bce72017-04-09 16:20:25 -070039BOOST_AUTO_TEST_SUITE(AppCertFetcher)
40
41unique_ptr<security::v2::Validator>
42CreateValidatorAppCert(Face& face)
43{
44 return make_unique<security::v2::Validator>(make_unique<::ndn::security::v2::ValidationPolicySimpleHierarchy>(),
45 make_unique<CertificateFetcherAppCert>(face));
46}
47
48class AppCertFetcherFixture : public DbTestData
49{
50public:
51 AppCertFetcherFixture()
52 : m_forwarder(m_io, m_keyChain)
53 , m_face(m_forwarder.addFace())
54 , m_validator(CreateValidatorAppCert(m_face))
55 {
56 // build the data and certificate for this test
57 buildAppCertAndData();
58
59 auto validatorOnlyForConstructServer = NdnsValidatorBuilder::create(m_face, 10, 0, TEST_CONFIG_PATH "/" "validator.conf");
60 // initlize all servers
61 auto addServer = [&] (const Name& zoneName) {
62 Face& face = m_forwarder.addFace();
63 // validator is used only for check update signature
64 // no updates tested here, so validator will not be used
65 // passing m_validator is only for construct server
66 Name certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
67 Name(zoneName).append("NDNS"));
68 auto server = make_shared<NameServer>(zoneName, certName, face,
69 m_session, m_keyChain, *validatorOnlyForConstructServer);
70 m_servers.push_back(server);
71 };
72 addServer(m_testName);
73 addServer(m_netName);
74 addServer(m_ndnsimName);
75 advanceClocks(time::milliseconds(10), 1);
76 }
77
78 ~AppCertFetcherFixture()
79 {
80 m_face.getIoService().stop();
81 m_face.shutdown();
82 }
83
84private:
85 void
86 buildAppCertAndData()
87 {
88 // create NDNS-stored certificate and the signed data
89 Identity ndnsimIdentity = addIdentity(m_ndnsimName);
90 Key randomKey = m_keyChain.createKey(ndnsimIdentity);
91 Certificate ndnsStoredAppCert = randomKey.getDefaultCertificate();
92 RrsetFactory rf(TEST_DATABASE.string(), m_ndnsimName, m_keyChain,
93 CertHelper::getIdentity(m_keyChain, Name(m_ndnsimName).append(label::NDNS_ITERATIVE_QUERY))
94 .getDefaultKey()
95 .getDefaultCertificate()
96 .getName());
97 rf.onlyCheckZone();
98 Rrset appCertRrset = rf.generateCertRrset(randomKey.getName().getSubName(-2),
99 VERSION_USE_UNIX_TIMESTAMP, DEFAULT_RR_TTL,
100 ndnsStoredAppCert);
101 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
102 tool.addRrset(appCertRrset);
103
104 m_appCertSignedData = Data(Name(m_ndnsimName).append("randomData"));
105 m_keyChain.sign(m_appCertSignedData, signingByCertificate(ndnsStoredAppCert));
106
107 // load this certificate as the trust anchor
108 m_validator->loadAnchor("", std::move(ndnsStoredAppCert));
109 }
110
111public:
112 DummyForwarder m_forwarder;
113 ndn::Face& m_face;
114 unique_ptr<security::v2::Validator> m_validator;
115 std::vector<shared_ptr<ndns::NameServer>> m_servers;
116 Data m_appCertSignedData;
117};
118
119
120BOOST_FIXTURE_TEST_CASE(Basic, AppCertFetcherFixture)
121{
122 bool hasValidated = false;
123 m_validator->validate(m_appCertSignedData,
124 [&] (const Data& data) {
125 hasValidated = true;
126 BOOST_CHECK(true);
127 },
128 [&] (const Data& data, const security::v2::ValidationError& str) {
129 hasValidated = true;
130 BOOST_CHECK(false);
131 });
132 advanceClocks(time::milliseconds(10), 1000);
133 BOOST_CHECK_EQUAL(hasValidated, true);
134}
135
136BOOST_AUTO_TEST_SUITE_END()
137
138} // namespace tests
139} // namespace ndns
140} // namespace ndn