blob: f77c5bee075da48e59079c6dbaa36da3472ef17a [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
Davide Pesaventobdd88c12020-11-26 00:35:08 -050033const fs::path DbTestData::TEST_DATABASE = fs::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070034const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Davide Pesaventobdd88c12020-11-26 00:35:08 -050035const fs::path DbTestData::TEST_CERT = fs::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert";
36const fs::path DbTestData::TEST_DKEY_CERT = fs::path(UNIT_TESTS_TMPDIR) / "dkey.cert";
Shock Jiang3016c982014-11-11 11:35:17 -080037
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080038DbTestData::PreviousStateCleaner::PreviousStateCleaner()
39{
Davide Pesaventobdd88c12020-11-26 00:35:08 -050040 fs::remove(TEST_DATABASE);
41 fs::remove(TEST_CERT);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080042}
43
Shock Jiang3016c982014-11-11 11:35:17 -080044DbTestData::DbTestData()
Davide Pesavento38fd3982022-04-18 22:22:02 -040045 : m_session(TEST_DATABASE.string())
46 , m_testName("/test19")
47 , m_netName("/test19/net")
48 , m_ndnsimName("/test19/net/ndnsim")
Shock Jiang3016c982014-11-11 11:35:17 -080049{
Davide Pesaventobdd88c12020-11-26 00:35:08 -050050 NdnsValidatorBuilder::VALIDATOR_CONF_FILE = (fs::path(UNIT_TESTS_TMPDIR) / "validator.conf").string();
Shock Jiang3016c982014-11-11 11:35:17 -080051
Yumin Xia2c509c22017-02-09 14:37:36 -080052 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
53 // this is how DKEY is added to parent zone in real world.
Davide Pesaventobdd88c12020-11-26 00:35:08 -050054 auto addDkeyCertToParent = [&tool] (Zone& dkeyFrom, Zone& dkeyTo) {
Yumin Xia2c509c22017-02-09 14:37:36 -080055 Certificate dkeyCert;
56 dkeyCert = tool.getZoneDkey(dkeyFrom);
Davide Pesaventobdd88c12020-11-26 00:35:08 -050057 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 Xia2c509c22017-02-09 14:37:36 -080060 };
Shock Jiang3016c982014-11-11 11:35:17 -080061
Yumin Xiafa2bce72017-04-09 16:20:25 -070062 Name testName(m_testName);
Yumin Xia2c509c22017-02-09 14:37:36 -080063 m_test = tool.createZone(testName, ROOT_ZONE);
64 // m_test's DKEY is not added to parent zone
Yumin Xiafa2bce72017-04-09 16:20:25 -070065 Name netName(m_netName);
Yumin Xia2c509c22017-02-09 14:37:36 -080066 m_net = tool.createZone(netName, testName);
67 addDkeyCertToParent(m_net, m_test);
Yumin Xiafa2bce72017-04-09 16:20:25 -070068 Name ndnsimName(m_ndnsimName);
Yumin Xia2c509c22017-02-09 14:37:36 -080069 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 Pesavento38fd3982022-04-18 22:22:02 -040080 BOOST_ASSERT(!m_certName.empty());
Yumin Xia2c509c22017-02-09 14:37:36 -080081
Davide Pesaventobdd88c12020-11-26 00:35:08 -050082 io::save(m_cert, TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080083
Shock Jiang3016c982014-11-11 11:35:17 -080084 int certificateIndex = 0;
Davide Pesaventobdd88c12020-11-26 00:35:08 -050085 auto addQueryRrset = [this, &certificateIndex] (const Name& label, Zone& zone,
86 const name::Component& type) {
Shock Jiang3016c982014-11-11 11:35:17 -080087 const time::seconds ttl(3000 + 100 * certificateIndex);
Davide Pesavento38fd3982022-04-18 22:22:02 -040088 const auto version = name::Component::fromVersion(100 + 1000 * certificateIndex);
Shock Jiang3016c982014-11-11 11:35:17 -080089 name::Component qType(label::NDNS_ITERATIVE_QUERY);
Yumin Xiaa484ba72016-11-10 20:40:12 -080090 NdnsContentType contentType = NDNS_RESP;
Yumin Xiad4e8ce52017-03-17 19:56:52 -070091 if (type == label::APPCERT_RR_TYPE) {
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080092 contentType = NDNS_KEY;
Yumin Xia2c509c22017-02-09 14:37:36 -080093 }
94 else if (type == label::NS_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -080095 contentType = NDNS_LINK;
Yumin Xia2c509c22017-02-09 14:37:36 -080096 }
97 else if (type == label::TXT_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -080098 contentType = NDNS_RESP;
Shock Jiang3016c982014-11-11 11:35:17 -080099 }
100 std::ostringstream os;
101 os << "a fake content: " << (++certificateIndex) << "th";
102
Yumin Xiaa484ba72016-11-10 20:40:12 -0800103 addRrset(zone, label, type, ttl, version, qType, contentType, os.str());
Shock Jiang3016c982014-11-11 11:35:17 -0800104 };
Shock Jiangcde28712014-10-19 21:17:20 -0700105
Yumin Xia2c509c22017-02-09 14:37:36 -0800106 addQueryRrset("net", m_test, label::NS_RR_TYPE);
Shock Jiangcde28712014-10-19 21:17:20 -0700107 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 Jiang3016c982014-11-11 11:35:17 -0800110
Shock Jiangcde28712014-10-19 21:17:20 -0700111 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 Jiang3016c982014-11-11 11:35:17 -0800114
Yumin Xia4e561892016-10-21 10:48:01 -0700115 // last link is the same as former one
116 BOOST_ASSERT(!m_links.empty());
117 m_links.push_back(m_links.back());
Shock Jiang3016c982014-11-11 11:35:17 -0800118}
119
Shock Jiang3016c982014-11-11 11:35:17 -0800120void
121DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
122 const time::seconds& ttl, const name::Component& version,
Yumin Xiaa484ba72016-11-10 20:40:12 -0800123 const name::Component& qType, NdnsContentType contentType, const std::string& msg)
Shock Jiang3016c982014-11-11 11:35:17 -0800124{
Yumin Xia4e561892016-10-21 10:48:01 -0700125 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 Shi81e98762022-01-11 18:17:24 +0000130 rrset = rf.generateNsRrset(label, version.toVersion(), ttl, {"/xx"});
Yumin Xiaa484ba72016-11-10 20:40:12 -0800131 if (contentType != NDNS_AUTH) {
Yumin Xia4e561892016-10-21 10:48:01 -0700132 // do not add AUTH packet to link
Davide Pesavento35e93542022-03-07 21:47:03 -0500133 m_links.emplace_back(rrset.getData());
Yumin Xia4e561892016-10-21 10:48:01 -0700134 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800135 }
136 else if (type == label::TXT_RR_TYPE) {
Davide Pesavento35e93542022-03-07 21:47:03 -0500137 rrset = rf.generateTxtRrset(label, version.toVersion(), ttl, {});
Yumin Xia2c509c22017-02-09 14:37:36 -0800138 }
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700139 else if (type == label::APPCERT_RR_TYPE) {
Davide Pesavento35e93542022-03-07 21:47:03 -0500140 rrset = rf.generateCertRrset(label, version.toVersion(), ttl, m_cert);
Shock Jiang3016c982014-11-11 11:35:17 -0800141 }
Yumin Xia4e561892016-10-21 10:48:01 -0700142
Davide Pesavento35e93542022-03-07 21:47:03 -0500143 auto data = std::make_shared<Data>(rrset.getData());
144 BOOST_VERIFY(security::verifySignature(*data, m_cert));
Shock Jiang3016c982014-11-11 11:35:17 -0800145
Yumin Xia55a7cc42017-05-14 18:43:34 -0700146 ManagementTool tool(TEST_DATABASE.string(), m_keyChain);
147 tool.addRrset(rrset);
148
Shock Jiang3016c982014-11-11 11:35:17 -0800149 m_rrsets.push_back(rrset);
150}
151
152DbTestData::~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 Jiang3016c982014-11-11 11:35:17 -0800161}
162
163} // namespace tests
164} // namespace ndns
165} // namespace ndn