Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [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 | |
| 20 | #include "database-test-data.hpp" |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 21 | #include "daemon/rrset-factory.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 22 | #include "mgmt/management-tool.hpp" |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 23 | #include "util/cert-helper.hpp" |
| 24 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 25 | #include <ndn-cxx/security/verification-helpers.hpp> |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 27 | namespace fs = boost::filesystem; |
| 28 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace ndns { |
| 31 | namespace tests { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 33 | const fs::path DbTestData::TEST_DATABASE = fs::path(UNIT_TESTS_TMPDIR) / "test-ndns.db"; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 34 | const Name DbTestData::TEST_IDENTITY_NAME("/test19"); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 35 | const fs::path DbTestData::TEST_CERT = fs::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert"; |
| 36 | const fs::path DbTestData::TEST_DKEY_CERT = fs::path(UNIT_TESTS_TMPDIR) / "dkey.cert"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 38 | DbTestData::PreviousStateCleaner::PreviousStateCleaner() |
| 39 | { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 40 | fs::remove(TEST_DATABASE); |
| 41 | fs::remove(TEST_CERT); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 44 | DbTestData::DbTestData() |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 45 | : m_session(TEST_DATABASE.string()) |
| 46 | , m_testName("/test19") |
| 47 | , m_netName("/test19/net") |
| 48 | , m_ndnsimName("/test19/net/ndnsim") |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 49 | { |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 50 | NdnsValidatorBuilder::VALIDATOR_CONF_FILE = (fs::path(UNIT_TESTS_TMPDIR) / "validator.conf").string(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 51 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 52 | ManagementTool tool(TEST_DATABASE.string(), m_keyChain); |
| 53 | // this is how DKEY is added to parent zone in real world. |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 54 | auto addDkeyCertToParent = [&tool] (Zone& dkeyFrom, Zone& dkeyTo) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 55 | Certificate dkeyCert; |
| 56 | dkeyCert = tool.getZoneDkey(dkeyFrom); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 57 | io::save(dkeyCert, TEST_DKEY_CERT.string()); |
| 58 | tool.addRrsetFromFile(dkeyTo.getName(), TEST_DKEY_CERT.string(), |
| 59 | DEFAULT_RR_TTL, DEFAULT_CERT, io::BASE64, true); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 60 | }; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 61 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 62 | Name testName(m_testName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 63 | m_test = tool.createZone(testName, ROOT_ZONE); |
| 64 | // m_test's DKEY is not added to parent zone |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 65 | Name netName(m_netName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 66 | m_net = tool.createZone(netName, testName); |
| 67 | addDkeyCertToParent(m_net, m_test); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 68 | Name ndnsimName(m_ndnsimName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 69 | m_ndnsim = tool.createZone(ndnsimName, netName); |
| 70 | addDkeyCertToParent(m_ndnsim, m_net); |
| 71 | |
| 72 | m_zones.push_back(m_test); |
| 73 | m_zones.push_back(m_net); |
| 74 | m_zones.push_back(m_ndnsim); |
| 75 | |
| 76 | Name identityName = Name(testName).append("NDNS"); |
| 77 | m_identity = CertHelper::getIdentity(m_keyChain, identityName); |
| 78 | m_certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, identityName); |
| 79 | m_cert = CertHelper::getCertificate(m_keyChain, identityName, m_certName); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 80 | BOOST_ASSERT(!m_certName.empty()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 81 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 82 | io::save(m_cert, TEST_CERT.string()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 83 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 84 | int certificateIndex = 0; |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 85 | auto addQueryRrset = [this, &certificateIndex] (const Name& label, Zone& zone, |
| 86 | const name::Component& type) { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 87 | const time::seconds ttl(3000 + 100 * certificateIndex); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 88 | const auto version = name::Component::fromVersion(100 + 1000 * certificateIndex); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 89 | name::Component qType(label::NDNS_ITERATIVE_QUERY); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 90 | NdnsContentType contentType = NDNS_RESP; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 91 | if (type == label::APPCERT_RR_TYPE) { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 92 | contentType = NDNS_KEY; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 93 | } |
| 94 | else if (type == label::NS_RR_TYPE) { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 95 | contentType = NDNS_LINK; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 96 | } |
| 97 | else if (type == label::TXT_RR_TYPE) { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 98 | contentType = NDNS_RESP; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 99 | } |
| 100 | std::ostringstream os; |
| 101 | os << "a fake content: " << (++certificateIndex) << "th"; |
| 102 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 103 | addRrset(zone, label, type, ttl, version, qType, contentType, os.str()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 104 | }; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 105 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 106 | addQueryRrset("net", m_test, label::NS_RR_TYPE); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 107 | addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE); |
| 108 | addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE); |
| 109 | addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 110 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 111 | addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000), |
| 112 | name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, |
| 113 | std::string("")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 114 | |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 115 | // last link is the same as former one |
| 116 | BOOST_ASSERT(!m_links.empty()); |
| 117 | m_links.push_back(m_links.back()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 120 | void |
| 121 | DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type, |
| 122 | const time::seconds& ttl, const name::Component& version, |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 123 | const name::Component& qType, NdnsContentType contentType, const std::string& msg) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 124 | { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 125 | Rrset rrset; |
| 126 | RrsetFactory rf(TEST_DATABASE.string(), zone.getName(), |
| 127 | m_keyChain, m_certName); |
| 128 | rf.onlyCheckZone(); |
| 129 | if (type == label::NS_RR_TYPE) { |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 130 | rrset = rf.generateNsRrset(label, version.toVersion(), ttl, {"/xx"}); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 131 | if (contentType != NDNS_AUTH) { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 132 | // do not add AUTH packet to link |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 133 | m_links.emplace_back(rrset.getData()); |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 134 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 135 | } |
| 136 | else if (type == label::TXT_RR_TYPE) { |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 137 | rrset = rf.generateTxtRrset(label, version.toVersion(), ttl, {}); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 138 | } |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 139 | else if (type == label::APPCERT_RR_TYPE) { |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 140 | rrset = rf.generateCertRrset(label, version.toVersion(), ttl, m_cert); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 141 | } |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 143 | auto data = std::make_shared<Data>(rrset.getData()); |
| 144 | BOOST_VERIFY(security::verifySignature(*data, m_cert)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 145 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 146 | ManagementTool tool(TEST_DATABASE.string(), m_keyChain); |
| 147 | tool.addRrset(rrset); |
| 148 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 149 | m_rrsets.push_back(rrset); |
| 150 | } |
| 151 | |
| 152 | DbTestData::~DbTestData() |
| 153 | { |
| 154 | for (auto& zone : m_zones) |
| 155 | m_session.remove(zone); |
| 156 | |
| 157 | for (auto& rrset : m_rrsets) |
| 158 | m_session.remove(rrset); |
| 159 | |
| 160 | m_session.close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace tests |
| 164 | } // namespace ndns |
| 165 | } // namespace ndn |