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