blob: 97282d1d23369a4cceb81225245edf9c14adb7a7 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -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.
Jiewen Tan870b29b2014-11-17 19:09:49 -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
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080020#include "mgmt/management-tool.hpp"
Davide Pesavento948c50c2020-12-26 21:30:45 -050021
Yumin Xia9d110ce2016-11-15 14:03:14 -080022#include "daemon/rrset-factory.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include "util/cert-helper.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080024#include "ndns-enum.hpp"
25#include "ndns-label.hpp"
26#include "ndns-tlv.hpp"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050027
28#include "boost-test.hpp"
29#include "key-chain-fixture.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080030
Jiewen Tan870b29b2014-11-17 19:09:49 -080031#include <boost/algorithm/string/replace.hpp>
Yumin Xia55a7cc42017-05-14 18:43:34 -070032#include <boost/range/adaptors.hpp>
Davide Pesavento28229df2020-01-04 15:11:25 -050033#include <boost/test/tools/output_test_stream.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080034
Davide Pesavento28229df2020-01-04 15:11:25 -050035#include <ndn-cxx/security/transform.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080036#include <ndn-cxx/util/io.hpp>
Davide Pesavento948c50c2020-12-26 21:30:45 -050037#include <ndn-cxx/util/random.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080038#include <ndn-cxx/util/regex.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080039
Davide Pesavento948c50c2020-12-26 21:30:45 -050040#include <iostream>
41
Jiewen Tan870b29b2014-11-17 19:09:49 -080042namespace ndn {
43namespace ndns {
44namespace tests {
45
Davide Pesaventobdd88c12020-11-26 00:35:08 -050046using boost::test_tools::output_test_stream;
47
48const auto TEST_DATABASE = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool.db";
49const auto TEST_CERTDIR = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool_certs";
50const Name FAKE_ROOT("/fake-root/123456789");
Jiewen Tan870b29b2014-11-17 19:09:49 -080051
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080052/**
53 * @brief Recursive copy a directory using Boost Filesystem
54 *
55 * Based on from http://stackoverflow.com/q/8593608/2150331
56 */
Davide Pesaventobdd88c12020-11-26 00:35:08 -050057static void
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080058copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
59{
60 namespace fs = boost::filesystem;
61
62 fs::create_directory(destination);
63 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
64 fs::path current(file->path());
65 if (is_directory(current)) {
66 copyDir(current, destination / current.filename());
67 }
68 else {
Eric Newberry9edaf262018-06-07 23:44:57 -070069 copy_file(current, destination / current.filename());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080070 }
71 }
72}
73
74class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080075{
76public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080077 TestHome()
78 {
79 if (std::getenv("HOME"))
80 m_origHome = std::getenv("HOME");
81
Davide Pesaventobdd88c12020-11-26 00:35:08 -050082 auto p = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "tests" / "unit" / "mgmt";
83 setenv("HOME", p.c_str(), 1);
84 boost::filesystem::remove_all(p);
85 boost::filesystem::create_directories(p);
86 copyDir("tests/unit/mgmt/.ndn", p / ".ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080087 }
88
89 ~TestHome()
90 {
91 if (!m_origHome.empty())
Davide Pesaventobdd88c12020-11-26 00:35:08 -050092 setenv("HOME", m_origHome.data(), 1);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080093 else
94 unsetenv("HOME");
95 }
96
97protected:
98 std::string m_origHome;
99};
100
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500101class ManagementToolFixture : public TestHome, public KeyChainFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800102{
103public:
104 class Error : public std::runtime_error
105 {
106 public:
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500107 using std::runtime_error::runtime_error;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800108 };
109
110 class PreviousStateCleaner
111 {
112 public:
113 PreviousStateCleaner()
114 {
115 boost::filesystem::remove(TEST_DATABASE);
116 boost::filesystem::remove_all(TEST_CERTDIR);
117 }
118 };
119
Jiewen Tan870b29b2014-11-17 19:09:49 -0800120 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800121 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800122 , m_dbMgr(TEST_DATABASE.string().c_str())
123 {
124 boost::filesystem::create_directory(TEST_CERTDIR);
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500125 Identity root = m_keyChain.createIdentity("NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800126 Key ksk = root.getDefaultKey();
127 m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName());
128 Certificate kskCert = CertHelper::createCertificate(m_keyChain, ksk, ksk, "CERT");
129 m_keyChain.addCertificate(ksk, kskCert);
130 rootKsk = kskCert.getName();
131
132 Key dsk = m_keyChain.createKey(root);
133 // replace rootDsk's default cert with ksk-signing cert
134 m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName());
135 Certificate dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT");
136 m_keyChain.addCertificate(dsk, dskCert);
137 rootDsk = dskCert.getName();
138
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500139 Identity other = m_keyChain.createIdentity("/ndns-test/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800140 Key otherKskKey = other.getDefaultKey();
141 m_keyChain.deleteCertificate(otherKskKey, otherKskKey.getDefaultCertificate().getName());
142 Certificate otherKskCert = CertHelper::createCertificate(m_keyChain, otherKskKey, otherKskKey, "CERT");
143 m_keyChain.addCertificate(otherKskKey, otherKskCert);
144 otherKsk = otherKskCert.getName();
145
146 // replace rootDsk's default cert with ksk-signing cert
147 Key otherDskKey = m_keyChain.createKey(other);
148 m_keyChain.deleteCertificate(otherDskKey, otherDskKey.getDefaultCertificate().getName());
149 Certificate otherDskCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
150 m_keyChain.addCertificate(otherDskKey, otherDskCert);
151 otherDsk = otherDskCert.getName();
152
153 Certificate rootDkeyCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
154 m_keyChain.addCertificate(otherDskKey, rootDkeyCert);
155 rootDkey = rootDkeyCert.getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800156 }
157
Yumin Xia2c509c22017-02-09 14:37:36 -0800158 std::vector<Certificate>
159 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800160 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800161 Zone zone(zoneName);
162 std::vector<Certificate> certs;
163 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
164 // ksk are always the first key
165 certs.push_back(Certificate(zoneInfo["ksk"]));
166 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800167 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800168 }
169
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800170 Rrset
171 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800172 {
173 Rrset rrset(&zone);
174 rrset.setLabel(label);
175 rrset.setType(type);
176
177 if (!m_dbMgr.find(rrset))
Davide Pesavento948c50c2020-12-26 21:30:45 -0500178 NDN_THROW(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800179 else
180 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800181 }
182
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800183 Name
184 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800185 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800186 size_t zoneNameSize = zone.getName().size();
187 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800188 }
189
Yumin Xia2c509c22017-02-09 14:37:36 -0800190 Certificate
191 findCertFromIdentity(const Name& identityName, const Name& certName)
192 {
193 Certificate rtn;
194 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
195 for (const auto& key : identity.getKeys()) {
196 for (const auto& cert : key.getCertificates()) {
197 if (cert.getName() == certName) {
198 rtn = cert;
199 return rtn;
200 }
201 }
202 }
Davide Pesavento948c50c2020-12-26 21:30:45 -0500203 NDN_THROW(Error("Certificate not found in keyChain"));
Yumin Xia2c509c22017-02-09 14:37:36 -0800204 }
205
206 Certificate
207 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800208 {
209 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800210 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800211 cert.wireDecode(rrset.getData());
212 return cert;
213 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800214
Yumin Xia2c509c22017-02-09 14:37:36 -0800215 Certificate
216 findDkeyFromDb(const Name& zoneName)
217 {
218 Zone zone(zoneName);
219 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
220 return Certificate(zoneInfo["dkey"]);
221 }
222
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800223 Response
224 findResponse(Zone& zone, const Name& label, const name::Component& type)
225 {
226 Rrset rrset = findRrSet(zone, label, type);
227 Data data(rrset.getData());
228 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700229 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800230 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800231 }
232
233public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800234 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800235 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800236 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800237
238 // Names of pre-created certificates
239 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
240 // constructor.
241 Name rootKsk;
242 Name rootDsk;
243 Name otherKsk;
244 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800245 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800246};
247
Yumin Xia9d110ce2016-11-15 14:03:14 -0800248BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
249
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800250// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
251// {
252// using time::seconds;
253
254// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
255// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
256// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
257// auto kskCert = m_keyChain
258// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
259// time::fromUnixTimestamp(seconds(2147483648)), {});
260// if (parentCert.empty()) {
261// m_keyChain.selfSign(*kskCert);
262// }
263// else {
264// m_keyChain.sign(*kskCert, parentCert);
265// }
266// m_keyChain.addCertificate(*kskCert);
267
268// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
269// auto dskCert = m_keyChain
270// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
271// time::fromUnixTimestamp(seconds(2147483648)), {});
272// m_keyChain.sign(*dskCert, kskCert->getName());
273// m_keyChain.addCertificate(*dskCert);
274
275// return dskCert->getName();
276// };
277
278// Name rootDsk = generateCerts(ROOT_ZONE);
279// generateCerts("/ndns-test", rootDsk);
280
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500281// copyDir(UNIT_TESTS_TMPDIR "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800282// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
283// }
284
Yumin Xia55a7cc42017-05-14 18:43:34 -0700285BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
286{
287 // the correctness of our DoE design rely on the ordering of SQLite
288 // this unit test make sure that our label::isSmallerInLabelOrder
289 // is the same as the ordering of BLOB in SQLite
290
Davide Pesavento948c50c2020-12-26 21:30:45 -0500291 auto genRandomString = [] (size_t length) {
292 static const std::string charset = "0123456789"
293 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
294 "abcdefghijklmnopqrstuvwxyz";
Yumin Xia55a7cc42017-05-14 18:43:34 -0700295 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
296
297 std::string str(length, 0);
Davide Pesavento948c50c2020-12-26 21:30:45 -0500298 std::generate_n(str.begin(), length,
299 [&] { return charset[dist(random::getRandomNumberEngine())]; });
Yumin Xia55a7cc42017-05-14 18:43:34 -0700300 return str;
301 };
302
Davide Pesavento948c50c2020-12-26 21:30:45 -0500303 auto genRandomLabel = [&] {
Yumin Xia55a7cc42017-05-14 18:43:34 -0700304 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
305 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
Davide Pesavento948c50c2020-12-26 21:30:45 -0500306
Yumin Xia55a7cc42017-05-14 18:43:34 -0700307 Name nm;
Davide Pesavento948c50c2020-12-26 21:30:45 -0500308 size_t length = numberOfLabelsDist(random::getRandomNumberEngine());
Yumin Xia55a7cc42017-05-14 18:43:34 -0700309 for (size_t i = 0; i < length; i++) {
Davide Pesavento948c50c2020-12-26 21:30:45 -0500310 nm.append(genRandomString(labelSizeDist(random::getRandomNumberEngine())));
Yumin Xia55a7cc42017-05-14 18:43:34 -0700311 }
312 return nm;
313 };
314
315 const size_t NGENERATED_LABELS = 10;
316
317 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
318 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
319 m_keyChain, DEFAULT_CERT);
320 rrsetFactory.checkZoneKey();
321 std::vector<Rrset> rrsets;
322 std::vector<Name> names;
323 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
324 Name randomLabel = genRandomLabel();
325 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
326 VERSION_USE_UNIX_TIMESTAMP,
327 DEFAULT_CACHE_TTL,
328 {});
329
330 rrsets.push_back(randomTxt);
331 m_dbMgr.insert(randomTxt);
332 names.push_back(randomLabel);
333 }
334
335 std::sort(rrsets.begin(), rrsets.end());
336
337 using boost::adaptors::filtered;
338 using boost::adaptors::transformed;
339
340 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
341 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
342 return rrset.getType() == label::TXT_RR_TYPE;
343 });
344
345 auto extractLabel = [] (const Rrset& rr) {
346 return rr.getLabel();
347 };
348 auto expected = rrsets | transformed(extractLabel);
349 auto actual = fromDbFiltered | transformed(extractLabel);
350
351 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
352 actual.begin(), actual.end());
353}
354
Yumin Xia9d110ce2016-11-15 14:03:14 -0800355BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800356{
Yumin Xia2c509c22017-02-09 14:37:36 -0800357 // creating root_zone need a rootDkey
358 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
359 time::seconds(4600),
360 time::seconds(4600),
361 rootKsk, rootDsk), ndns::ManagementTool::Error);
362
363 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
364 time::seconds(4600),
365 time::seconds(4600),
366 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800367
368 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800369 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800370 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800371 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
372 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
373 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
374 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
375
376 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
377 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800378
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800379 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
380 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800381}
382
Yumin Xia9d110ce2016-11-15 14:03:14 -0800383BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800384{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800385 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800386 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800387
Yumin Xia2c509c22017-02-09 14:37:36 -0800388 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700389 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800390 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800391
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800392 // will generate keys automatically
393 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800394 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800395
Yumin Xia2c509c22017-02-09 14:37:36 -0800396 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800397 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800398
Yumin Xia2c509c22017-02-09 14:37:36 -0800399 const Name& ksk = certs[0].getName();
400 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800401
402 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800403 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800404 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
405 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
406 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
407
408 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
409 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800410
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800411 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800412
413 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800414 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
415}
416
Yumin Xia9d110ce2016-11-15 14:03:14 -0800417BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800418{
419 Name parentZoneName("/ndns-test");
420 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700421 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800422
423 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800424 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800425
Yumin Xia2c509c22017-02-09 14:37:36 -0800426 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800427 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800428
Yumin Xia2c509c22017-02-09 14:37:36 -0800429 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800430
431 // Check zone ttl
432 Zone zone(zoneName);
433 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
434 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
435
Yumin Xia2c509c22017-02-09 14:37:36 -0800436 // check dkey name
437 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
438 Certificate dkey = findDkeyFromDb(zoneName);
439 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
440
441 // TODO: check signing hierarchy
442
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800443 // Check dsk rrset ttl
444 Rrset rrset;
445 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
446 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
447
448 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800449 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
450 time::system_clock::TimePoint beg,end;
451 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800452
Yumin Xia2c509c22017-02-09 14:37:36 -0800453 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
454 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
455 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800456 m_tool.deleteZone(zoneName);
457}
458
Yumin Xia9d110ce2016-11-15 14:03:14 -0800459BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800460{
461 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
462 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
463
Yumin Xia2c509c22017-02-09 14:37:36 -0800464 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800465 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800466
Yumin Xia2c509c22017-02-09 14:37:36 -0800467 const Name& ksk = certs[0].getName();
468 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800469
470 m_tool.deleteZone("/net/ndnsim");
471 // identity will still exist after the zone is deleted
472
473 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
474
475 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
476
477 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
478 time::seconds(1), time::days(1), ksk, dsk));
479 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
480 m_tool.deleteZone("/net/ndnsim");
481
482 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
483 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800484
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800485 m_tool.deleteZone("/net/ndnsim");
486
487 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
488 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800489 m_tool.deleteZone("/net/ndnsim");
490
491 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
492 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800493 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800494
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500495 Identity id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800496 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800497 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800498 time::seconds(1), time::days(1), cert.getName()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800499
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500500 id = m_keyChain.createIdentity("/com/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800501 cert = id.getDefaultKey().getDefaultCertificate();
502
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800503 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800504 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800505 ndns::ManagementTool::Error);
506
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500507 id = m_keyChain.createIdentity("/net/ndnsim/www/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800508 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800509 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800510 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800511 ndns::ManagementTool::Error);
512
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500513 id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800514 cert = id.getDefaultKey().getDefaultCertificate();
515 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800516 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800517 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800518 ndns::ManagementTool::Error);
519
Yumin Xia2c509c22017-02-09 14:37:36 -0800520 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800521 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
522
523 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800524 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800525}
526
527class OutputTester
528{
529public:
530 OutputTester()
531 : savedBuf(std::clog.rdbuf())
532 {
533 std::cout.rdbuf(buffer.rdbuf());
534 }
535
536 ~OutputTester()
537 {
538 std::cout.rdbuf(savedBuf);
539 }
540
541public:
542 std::stringstream buffer;
543 std::streambuf* savedBuf;
544};
545
Yumin Xia2c509c22017-02-09 14:37:36 -0800546// BOOST_AUTO_TEST_CASE(ExportCertificate)
547// {
548// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800549
Yumin Xia2c509c22017-02-09 14:37:36 -0800550// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
551// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800552
Yumin Xia2c509c22017-02-09 14:37:36 -0800553// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
554// // doesn't check the zone, export from KeyChain directly
555// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
556// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800557
Yumin Xia2c509c22017-02-09 14:37:36 -0800558// std::string dskValue =
559// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
560// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
561// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
562// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
563// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
564// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
565// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
566// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
567// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
568// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
569// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
570// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
571// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
572// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
573// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
574// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800575
Yumin Xia2c509c22017-02-09 14:37:36 -0800576// {
577// std::ifstream ifs(outputFile.c_str());
578// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
579// std::istreambuf_iterator<char>());
580// BOOST_CHECK_EQUAL(actualValue, dskValue);
581// }
582// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800583
Yumin Xia2c509c22017-02-09 14:37:36 -0800584// // doesn't check the zone, export from KeyChain directly
585// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
586// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800587
Yumin Xia2c509c22017-02-09 14:37:36 -0800588// Name zoneName("/ndns-test");
589// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
590// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800591
Yumin Xia2c509c22017-02-09 14:37:36 -0800592// m_keyChain.deleteCertificate(otherKsk);
593// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800594
Yumin Xia2c509c22017-02-09 14:37:36 -0800595// // retrieve cert from the zone
596// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
597// {
598// std::ifstream ifs(outputFile.c_str());
599// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
600// std::istreambuf_iterator<char>());
601// BOOST_CHECK_EQUAL(actualValue, dskValue);
602// }
603// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800604
Yumin Xia2c509c22017-02-09 14:37:36 -0800605// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
606// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800607
Yumin Xia2c509c22017-02-09 14:37:36 -0800608// // output to std::cout
609// std::string acutalOutput;
610// {
611// OutputTester tester;
612// m_tool.exportCertificate(otherDsk, "-");
613// acutalOutput = tester.buffer.str();
614// }
615// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
616// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800617
Yumin Xia9d110ce2016-11-15 14:03:14 -0800618BOOST_AUTO_TEST_CASE(AddRrset)
619{
620 Name zoneName("/ndns-test");
621 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800622
Yumin Xia9d110ce2016-11-15 14:03:14 -0800623 time::seconds ttl1(4200);
624 time::seconds ttl2(4500);
625 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800626
Yumin Xia9d110ce2016-11-15 14:03:14 -0800627 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
628 rf.checkZoneKey();
Junxiao Shi81e98762022-01-11 18:17:24 +0000629 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -0800630
Yumin Xia9d110ce2016-11-15 14:03:14 -0800631 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
632 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
633 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800634}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800635
Yumin Xia9d110ce2016-11-15 14:03:14 -0800636BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
637{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800638 Name zoneName("/ndns-test");
639 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800640
Yumin Xia9d110ce2016-11-15 14:03:14 -0800641 time::seconds ttl(4200);
642 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800643
Yumin Xia9d110ce2016-11-15 14:03:14 -0800644 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
645 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800646
Yumin Xia9d110ce2016-11-15 14:03:14 -0800647 auto checkRrset = [&zone, &zoneName, this](Name label,
648 name::Component type,
649 NdnsContentType contentType) -> void {
650 Rrset rr1 = findRrSet(zone, label, type);
651 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
652 Response response1;
653 response1.fromData(zoneName, Data(rr1.getData()));
654 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
655 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800656
Yumin Xia9d110ce2016-11-15 14:03:14 -0800657 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800658
Junxiao Shi81e98762022-01-11 18:17:24 +0000659 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -0800660
Yumin Xia9d110ce2016-11-15 14:03:14 -0800661 //add NS NDNS_AUTH and check user-defined ttl
662 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
663 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
664 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800665
Yumin Xia9d110ce2016-11-15 14:03:14 -0800666 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
667 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800668
Yumin Xia9d110ce2016-11-15 14:03:14 -0800669 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700670 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800671 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800672
Yumin Xia9d110ce2016-11-15 14:03:14 -0800673 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
674 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
675 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
676 // check that there is no confliction
677 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800678
Yumin Xia9d110ce2016-11-15 14:03:14 -0800679 // insert a shorter NS, when there are longer NS or TXT
Junxiao Shi81e98762022-01-11 18:17:24 +0000680 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800681 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
682 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800683
Yumin Xia9d110ce2016-11-15 14:03:14 -0800684 // insert a longer NS, when there is already a shorter NS
Junxiao Shi81e98762022-01-11 18:17:24 +0000685 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800686 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
687 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800688
Yumin Xia9d110ce2016-11-15 14:03:14 -0800689 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700690 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800691 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800692
Yumin Xia9d110ce2016-11-15 14:03:14 -0800693 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700694 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
Junxiao Shi81e98762022-01-11 18:17:24 +0000695 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800696 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
697 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
698 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800699
Yumin Xia9d110ce2016-11-15 14:03:14 -0800700 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700701 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800702 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
703}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800704
Yumin Xia9d110ce2016-11-15 14:03:14 -0800705BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
706{
707 // check pre-condition
708 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800709
Yumin Xia9d110ce2016-11-15 14:03:14 -0800710 // Check: throw if zone not exist
711 std::string certPath = TEST_CERTDIR.string();
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800712 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800713
Yumin Xia9d110ce2016-11-15 14:03:14 -0800714 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800715
Yumin Xia9d110ce2016-11-15 14:03:14 -0800716 // Check: throw if certificate does not match
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800717 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800718
Yumin Xia9d110ce2016-11-15 14:03:14 -0800719 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800720 std::vector<Certificate>&& certs = getCerts(zoneName);
721 const Name& ksk = certs[0].getName();
722 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800723
Yumin Xia2c509c22017-02-09 14:37:36 -0800724 // Check: throw if it's a duplicated certificate
725 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800726}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800727
Yumin Xia9d110ce2016-11-15 14:03:14 -0800728BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
729{
730 Name parentZoneName("/ndns-test");
731 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700732 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800733
Yumin Xia9d110ce2016-11-15 14:03:14 -0800734 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
735 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800736
Yumin Xia2c509c22017-02-09 14:37:36 -0800737 Zone zone(zoneName);
738 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800739
Yumin Xia2c509c22017-02-09 14:37:36 -0800740 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800741 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800742 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800743
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800744 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800745 // Check if child zone's d-key could be inserted correctly
746 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800747}
Jiewen Tand2d21822015-03-19 15:37:03 -0700748
Yumin Xia9d110ce2016-11-15 14:03:14 -0800749BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
750{
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500751 // check using user provided certificate
Yumin Xia9d110ce2016-11-15 14:03:14 -0800752 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700753 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800754 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700755 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800756
Yumin Xia2c509c22017-02-09 14:37:36 -0800757 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
758 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
759 Key dsk = m_keyChain.createKey(id);
760 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800761
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800762 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800763 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
764 m_tool.createZone(zoneName, parentZoneName);
765
Yumin Xia2c509c22017-02-09 14:37:36 -0800766 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800767 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800768 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800769
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800770 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800771 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800772}
773
774BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
775{
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500776 // check invalid output
Yumin Xia9d110ce2016-11-15 14:03:14 -0800777 Name parentZoneName("/ndns-test");
778 Name zoneName = Name(parentZoneName).append("child-zone");
779 m_tool.createZone(zoneName, parentZoneName);
780
781 Name content = "invalid data packet";
782 std::string output = TEST_CERTDIR.string() + "/ss.cert";
783 ndn::io::save(content, output);
784
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800785 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800786}
787
788BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
789{
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500790 // check version control
Yumin Xia9d110ce2016-11-15 14:03:14 -0800791 time::seconds ttl(4200);
792 Name parentZoneName("/ndns-test");
793 Name zoneName = Name(parentZoneName).append("child-zone");
794 m_tool.createZone(zoneName, parentZoneName);
795
796 Name label("/label");
797 uint64_t version = 110;
798
799 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
800 rf.checkZoneKey();
801
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700802 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800803
804 m_tool.addRrset(rrset1);
805 // throw error when adding duplicated rrset with the same version
806 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
807 ndns::ManagementTool::Error);
808 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700809 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800810 // throw error when adding duplicated rrset with older version
811 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
812 ndns::ManagementTool::Error);
813
814 version++;
815 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700816 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800817 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
818
819 Zone zone(zoneName);
820 m_dbMgr.find(zone);
821 Rrset rrset;
822 rrset.setZone(&zone);
823 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700824 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800825 m_dbMgr.find(rrset);
826
827 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
828}
829
830BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
831{
Davide Pesavento7f27ec12022-03-10 20:10:54 -0500832 // check input with different formats
Yumin Xia9d110ce2016-11-15 14:03:14 -0800833 Name parentZoneName("/ndns-test");
834 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800835 Zone parentZone(parentZoneName);
836
837 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800838 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700839
Yumin Xia2c509c22017-02-09 14:37:36 -0800840 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800841 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700842
Yumin Xia918343d2017-03-17 19:04:55 -0700843 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
844
Yumin Xia9d110ce2016-11-15 14:03:14 -0800845 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800846 ndn::io::save(cert, output, ndn::io::BASE64);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800847 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800848 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
849 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700850
Yumin Xia9d110ce2016-11-15 14:03:14 -0800851 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800852 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800853 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800854 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
855 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700856
Yumin Xia9d110ce2016-11-15 14:03:14 -0800857 // hex
Yumin Xia2c509c22017-02-09 14:37:36 -0800858 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800859 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800860 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
861 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700862
Yumin Xia9d110ce2016-11-15 14:03:14 -0800863 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800864 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800865 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800866 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
867 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800868 ndns::ManagementTool::Error);
869}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700870
Yumin Xia9d110ce2016-11-15 14:03:14 -0800871BOOST_AUTO_TEST_CASE(ListAllZones)
872{
Yumin Xia2c509c22017-02-09 14:37:36 -0800873 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800874 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700875
Yumin Xia2c509c22017-02-09 14:37:36 -0800876 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
877 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
878
Yumin Xia9d110ce2016-11-15 14:03:14 -0800879 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800880 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
881 "default-certificate=" + rootDsk.toUri() + "\n"
882 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
883 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700884
Yumin Xia9d110ce2016-11-15 14:03:14 -0800885 output_test_stream testOutput;
886 m_tool.listAllZones(testOutput);
887 BOOST_CHECK(testOutput.is_equal(expectedValue));
888}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700889
Yumin Xia55a7cc42017-05-14 18:43:34 -0700890// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800891
Yumin Xia55a7cc42017-05-14 18:43:34 -0700892// BOOST_AUTO_TEST_CASE(ListZone)
893// {
894// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700895
Yumin Xia55a7cc42017-05-14 18:43:34 -0700896// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
897// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700898
Yumin Xia55a7cc42017-05-14 18:43:34 -0700899// // Add NS with NDNS_RESP
900// Delegation del;
901// del.preference = 10;
902// del.name = Name("/get/link");
903// DelegationList ds = {del};
904// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
905// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700906
Yumin Xia55a7cc42017-05-14 18:43:34 -0700907// // Add NS with NDNS_AUTH
908// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
909// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700910
Yumin Xia55a7cc42017-05-14 18:43:34 -0700911// // Add TXT from file
912// std::string output = TEST_CERTDIR.string() + "/a.rrset";
913// Response re1;
914// re1.setZone("/ndns-test");
915// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
916// re1.setRrLabel("/label2");
917// re1.setRrType(label::TXT_RR_TYPE);
918// re1.setContentType(NDNS_RESP);
919// re1.setVersion(name::Component::fromVersion(654321));
920// re1.addRr("First RR");
921// re1.addRr("Second RR");
922// re1.addRr("Last RR");
923// shared_ptr<Data> data1 = re1.toData();
924// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
925// ndn::io::save(*data1, output);
926// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800927
Yumin Xia55a7cc42017-05-14 18:43:34 -0700928// // Add TXT in normal way
929// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
930// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800931
Yumin Xia55a7cc42017-05-14 18:43:34 -0700932// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800933
Yumin Xia55a7cc42017-05-14 18:43:34 -0700934// output_test_stream testOutput;
935// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800936
Yumin Xia55a7cc42017-05-14 18:43:34 -0700937// std::string expectedValue =
938// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800939
Yumin Xia55a7cc42017-05-14 18:43:34 -0700940// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
941// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
942// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800943
Yumin Xia55a7cc42017-05-14 18:43:34 -0700944// /KEY/%12%05t%90%3AjvA 10 CERT ; content-type=KEY version=%FD%00%00%01b%26e%AEY signed-by=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
945// Certificate name:
946// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
947// Validity:
948// NotBefore: 20180314T212340
949// NotAfter: 20180324T212340
950// Public key bits:
951// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
952// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
953// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
954// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
955// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
956// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
957// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
958// Signature Information:
959// Signature Type: SignatureSha256WithEcdsa
960// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800961
Yumin Xia55a7cc42017-05-14 18:43:34 -0700962// ; rrset=/KEY/%12%05t%90%3AjvA/CERT type=DOE version=%FD%00%00%01b%26e%AE%91 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
963// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
964// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800965
Yumin Xia55a7cc42017-05-14 18:43:34 -0700966// /KEY/%8D%1Dj%1E%BE%B0%2A%E4 10 CERT ; content-type=KEY version=%FD%00%00%01b%26e%AEX signed-by=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
967// Certificate name:
968// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
969// Validity:
970// NotBefore: 20180314T212340
971// NotAfter: 20180324T212340
972// Public key bits:
973// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
974// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
975// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
976// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
977// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
978// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
979// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
980// Signature Information:
981// Signature Type: SignatureSha256WithEcdsa
982// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800983
Yumin Xia55a7cc42017-05-14 18:43:34 -0700984// ; rrset=/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT type=DOE version=%FD%00%00%01b%26e%AE%93 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
985// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
986// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800987
Yumin Xia55a7cc42017-05-14 18:43:34 -0700988// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
989// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800990
Yumin Xia55a7cc42017-05-14 18:43:34 -0700991// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
992// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
993// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800994
Yumin Xia55a7cc42017-05-14 18:43:34 -0700995// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
996// /label2 10 NS NDNS-Auth
997
998// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
999// /label2 10 TXT First RR
1000// /label2 10 TXT Second RR
1001// /label2 10 TXT Last RR
1002
1003// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1004// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1005// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1006
1007// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1008// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1009// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1010
1011// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1012// /label3 10 TXT Hello
1013// /label3 10 TXT World
1014
1015// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1016// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1017// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1018
1019// )VALUE";
1020
1021// BOOST_CHECK(testOutput.is_equal(expectedValue));
1022// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001023
Yumin Xia9d110ce2016-11-15 14:03:14 -08001024BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1025{
1026 Name zoneName("/ndns-test");
1027 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1028 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1029 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001030 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001031
Yumin Xia9d110ce2016-11-15 14:03:14 -08001032 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001033
Yumin Xia2c509c22017-02-09 14:37:36 -08001034 std::stringstream os;
1035
1036 using security::transform::base64Encode;
1037 using security::transform::streamSink;
1038 using security::transform::bufferSource;
1039
Davide Pesavento98026122022-03-14 22:00:03 -04001040 bufferSource(rrset1.getData()) >> base64Encode() >> streamSink(os);
Yumin Xia2c509c22017-02-09 14:37:36 -08001041
1042 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001043
Yumin Xia9d110ce2016-11-15 14:03:14 -08001044 output_test_stream testOutput;
1045 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001046 BOOST_CHECK(testOutput.is_equal(expectedValue));
1047}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001048
Yumin Xia9d110ce2016-11-15 14:03:14 -08001049BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1050{
1051 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001052
Yumin Xia9d110ce2016-11-15 14:03:14 -08001053 m_tool.createZone(zoneName, ROOT_ZONE);
1054 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1055 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001056
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001057 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001058
Yumin Xia9d110ce2016-11-15 14:03:14 -08001059 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001060
Yumin Xia9d110ce2016-11-15 14:03:14 -08001061 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001062 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001063
Yumin Xia9d110ce2016-11-15 14:03:14 -08001064 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001065
Yumin Xia9d110ce2016-11-15 14:03:14 -08001066 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1067}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001068
1069BOOST_AUTO_TEST_SUITE_END()
1070
1071} // namespace tests
1072} // namespace ndns
1073} // namespace ndn