blob: 9188eff65bf4144cafa1930ff7c130b5435391d6 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Junxiao Shi81e98762022-01-11 18:17:24 +00003 * Copyright (c) 2014-2022, Regents of the University of California.
Shock Jiang3016c982014-11-11 11:35:17 -08004 *
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 Xia4e561892016-10-21 10:48:01 -070021#include "daemon/rrset-factory.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080022#include "mgmt/management-tool.hpp"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050023#include "util/cert-helper.hpp"
24
Yumin Xia2c509c22017-02-09 14:37:36 -080025#include <ndn-cxx/security/verification-helpers.hpp>
Shock Jiang3016c982014-11-11 11:35:17 -080026
Davide Pesaventobdd88c12020-11-26 00:35:08 -050027namespace fs = boost::filesystem;
28
Shock Jiang3016c982014-11-11 11:35:17 -080029namespace ndn {
30namespace ndns {
31namespace tests {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080032
Alexander Afanasyev08d18742018-03-15 16:31:28 -040033NDNS_LOG_INIT(TestFakeData);
Shock Jiang3016c982014-11-11 11:35:17 -080034
Davide Pesaventobdd88c12020-11-26 00:35:08 -050035const fs::path DbTestData::TEST_DATABASE = fs::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070036const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Davide Pesaventobdd88c12020-11-26 00:35:08 -050037const fs::path DbTestData::TEST_CERT = fs::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert";
38const fs::path DbTestData::TEST_DKEY_CERT = fs::path(UNIT_TESTS_TMPDIR) / "dkey.cert";
Shock Jiang3016c982014-11-11 11:35:17 -080039
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080040DbTestData::PreviousStateCleaner::PreviousStateCleaner()
41{
Davide Pesaventobdd88c12020-11-26 00:35:08 -050042 fs::remove(TEST_DATABASE);
43 fs::remove(TEST_CERT);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080044}
45
Shock Jiang3016c982014-11-11 11:35:17 -080046DbTestData::DbTestData()
Yumin Xiafa2bce72017-04-09 16:20:25 -070047 : m_session(TEST_DATABASE.string()),
48 m_testName("/test19"),
49 m_netName("/test19/net"),
50 m_ndnsimName("/test19/net/ndnsim")
Shock Jiang3016c982014-11-11 11:35:17 -080051{
52 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080053
Davide Pesaventobdd88c12020-11-26 00:35:08 -050054 NdnsValidatorBuilder::VALIDATOR_CONF_FILE = (fs::path(UNIT_TESTS_TMPDIR) / "validator.conf").string();
Shock Jiang3016c982014-11-11 11:35:17 -080055
Yumin Xia2c509c22017-02-09 14:37:36 -080056 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
57 // this is how DKEY is added to parent zone in real world.
Davide Pesaventobdd88c12020-11-26 00:35:08 -050058 auto addDkeyCertToParent = [&tool] (Zone& dkeyFrom, Zone& dkeyTo) {
Yumin Xia2c509c22017-02-09 14:37:36 -080059 Certificate dkeyCert;
60 dkeyCert = tool.getZoneDkey(dkeyFrom);
Davide Pesaventobdd88c12020-11-26 00:35:08 -050061 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 Xia2c509c22017-02-09 14:37:36 -080064 };
Shock Jiang3016c982014-11-11 11:35:17 -080065
Yumin Xiafa2bce72017-04-09 16:20:25 -070066 Name testName(m_testName);
Yumin Xia2c509c22017-02-09 14:37:36 -080067 m_test = tool.createZone(testName, ROOT_ZONE);
68 // m_test's DKEY is not added to parent zone
Yumin Xiafa2bce72017-04-09 16:20:25 -070069 Name netName(m_netName);
Yumin Xia2c509c22017-02-09 14:37:36 -080070 m_net = tool.createZone(netName, testName);
71 addDkeyCertToParent(m_net, m_test);
Yumin Xiafa2bce72017-04-09 16:20:25 -070072 Name ndnsimName(m_ndnsimName);
Yumin Xia2c509c22017-02-09 14:37:36 -080073 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 Pesaventobdd88c12020-11-26 00:35:08 -050085 io::save(m_cert, TEST_CERT.string());
Shock Jiangcde28712014-10-19 21:17:20 -070086 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080087
Davide Pesavento35e93542022-03-07 21:47:03 -050088 BOOST_ASSERT(!m_certName.empty());
Shock Jiang3016c982014-11-11 11:35:17 -080089 NDNS_LOG_TRACE("test certName: " << m_certName);
90
Shock Jiang3016c982014-11-11 11:35:17 -080091 int certificateIndex = 0;
Davide Pesaventobdd88c12020-11-26 00:35:08 -050092 auto addQueryRrset = [this, &certificateIndex] (const Name& label, Zone& zone,
93 const name::Component& type) {
Shock Jiang3016c982014-11-11 11:35:17 -080094 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 Xiaa484ba72016-11-10 20:40:12 -080097 NdnsContentType contentType = NDNS_RESP;
Yumin Xiad4e8ce52017-03-17 19:56:52 -070098 if (type == label::APPCERT_RR_TYPE) {
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080099 contentType = NDNS_KEY;
Yumin Xia2c509c22017-02-09 14:37:36 -0800100 }
101 else if (type == label::NS_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800102 contentType = NDNS_LINK;
Yumin Xia2c509c22017-02-09 14:37:36 -0800103 }
104 else if (type == label::TXT_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800105 contentType = NDNS_RESP;
Shock Jiang3016c982014-11-11 11:35:17 -0800106 }
107 std::ostringstream os;
108 os << "a fake content: " << (++certificateIndex) << "th";
109
Yumin Xiaa484ba72016-11-10 20:40:12 -0800110 addRrset(zone, label, type, ttl, version, qType, contentType, os.str());
Shock Jiang3016c982014-11-11 11:35:17 -0800111 };
Shock Jiangcde28712014-10-19 21:17:20 -0700112
Yumin Xia2c509c22017-02-09 14:37:36 -0800113 addQueryRrset("net", m_test, label::NS_RR_TYPE);
Shock Jiangcde28712014-10-19 21:17:20 -0700114 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 Jiang3016c982014-11-11 11:35:17 -0800117
Shock Jiangcde28712014-10-19 21:17:20 -0700118 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 Jiang3016c982014-11-11 11:35:17 -0800121
Yumin Xia4e561892016-10-21 10:48:01 -0700122 // last link is the same as former one
123 BOOST_ASSERT(!m_links.empty());
124 m_links.push_back(m_links.back());
125
Shock Jiang3016c982014-11-11 11:35:17 -0800126 NDNS_LOG_INFO("insert testing data: OK");
127}
128
Shock Jiang3016c982014-11-11 11:35:17 -0800129void
130DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
131 const time::seconds& ttl, const name::Component& version,
Yumin Xiaa484ba72016-11-10 20:40:12 -0800132 const name::Component& qType, NdnsContentType contentType, const std::string& msg)
Shock Jiang3016c982014-11-11 11:35:17 -0800133{
Yumin Xia4e561892016-10-21 10:48:01 -0700134 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 Shi81e98762022-01-11 18:17:24 +0000139 rrset = rf.generateNsRrset(label, version.toVersion(), ttl, {"/xx"});
Yumin Xiaa484ba72016-11-10 20:40:12 -0800140 if (contentType != NDNS_AUTH) {
Yumin Xia4e561892016-10-21 10:48:01 -0700141 // do not add AUTH packet to link
Davide Pesavento35e93542022-03-07 21:47:03 -0500142 m_links.emplace_back(rrset.getData());
Yumin Xia4e561892016-10-21 10:48:01 -0700143 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800144 }
145 else if (type == label::TXT_RR_TYPE) {
Davide Pesavento35e93542022-03-07 21:47:03 -0500146 rrset = rf.generateTxtRrset(label, version.toVersion(), ttl, {});
Yumin Xia2c509c22017-02-09 14:37:36 -0800147 }
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700148 else if (type == label::APPCERT_RR_TYPE) {
Davide Pesavento35e93542022-03-07 21:47:03 -0500149 rrset = rf.generateCertRrset(label, version.toVersion(), ttl, m_cert);
Shock Jiang3016c982014-11-11 11:35:17 -0800150 }
Yumin Xia4e561892016-10-21 10:48:01 -0700151
Davide Pesavento35e93542022-03-07 21:47:03 -0500152 auto data = std::make_shared<Data>(rrset.getData());
153 BOOST_VERIFY(security::verifySignature(*data, m_cert));
Shock Jiang3016c982014-11-11 11:35:17 -0800154
Yumin Xia55a7cc42017-05-14 18:43:34 -0700155 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
156 tool.addRrset(rrset);
157
Shock Jiang3016c982014-11-11 11:35:17 -0800158 m_rrsets.push_back(rrset);
159}
160
161DbTestData::~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 Jiang3016c982014-11-11 11:35:17 -0800170}
171
172} // namespace tests
173} // namespace ndns
174} // namespace ndn