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