Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 4 | * |
| 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 Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 20 | #include "validator/certificate-fetcher-ndns-appcert.hpp" |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 21 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 22 | #include "ndns-label.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 23 | #include "daemon/name-server.hpp" |
| 24 | #include "daemon/rrset-factory.hpp" |
| 25 | #include "mgmt/management-tool.hpp" |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 26 | #include "util/cert-helper.hpp" |
| 27 | #include "validator/validator.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 29 | #include "boost-test.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 30 | #include "unit/database-test-data.hpp" |
| 31 | |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/security/validation-policy-simple-hierarchy.hpp> |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 33 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace ndns { |
| 37 | namespace tests { |
| 38 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(AppCertFetcher) |
| 40 | |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 41 | static unique_ptr<security::Validator> |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 42 | makeValidatorAppCert(Face& face) |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 43 | { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 44 | return make_unique<security::Validator>(make_unique<security::ValidationPolicySimpleHierarchy>(), |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 45 | make_unique<CertificateFetcherAppCert>(face)); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | class AppCertFetcherFixture : public DbTestData |
| 49 | { |
| 50 | public: |
| 51 | AppCertFetcherFixture() |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 52 | : m_validatorFace(m_io, m_keyChain, {true, true}) |
| 53 | , m_validator(makeValidatorAppCert(m_validatorFace)) |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 54 | { |
| 55 | // build the data and certificate for this test |
| 56 | buildAppCertAndData(); |
| 57 | |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 58 | auto serverValidator = NdnsValidatorBuilder::create(m_validatorFace, 10, 0, |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 59 | UNIT_TESTS_TMPDIR "/validator.conf"); |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 60 | // 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 Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 66 | // 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 Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 70 | 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 Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 74 | }; |
| 75 | addServer(m_testName); |
| 76 | addServer(m_netName); |
| 77 | addServer(m_ndnsimName); |
| 78 | advanceClocks(time::milliseconds(10), 1); |
| 79 | } |
| 80 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 81 | private: |
| 82 | void |
| 83 | buildAppCertAndData() |
| 84 | { |
| 85 | // create NDNS-stored certificate and the signed data |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 86 | Identity ndnsimIdentity = m_keyChain.createIdentity(m_ndnsimName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 87 | 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 | |
| 108 | public: |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 109 | util::DummyClientFace m_validatorFace; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 110 | unique_ptr<security::Validator> m_validator; |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 111 | std::vector<unique_ptr<util::DummyClientFace>> m_serverFaces; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 112 | std::vector<shared_ptr<ndns::NameServer>> m_servers; |
| 113 | Data m_appCertSignedData; |
| 114 | }; |
| 115 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 116 | BOOST_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 Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 124 | [&] (const Data& data, const security::ValidationError& str) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 125 | hasValidated = true; |
| 126 | BOOST_CHECK(false); |
| 127 | }); |
| 128 | advanceClocks(time::milliseconds(10), 1000); |
| 129 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_SUITE_END() |
| 133 | |
| 134 | } // namespace tests |
| 135 | } // namespace ndns |
| 136 | } // namespace ndn |