blob: 2ac878a839068657c867d4c871405f078cd961bb [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/*
Davide Pesavento28229df2020-01-04 15:11:25 -05003 * Copyright (c) 2014-2020, 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"
Yumin Xia9d110ce2016-11-15 14:03:14 -080021#include "daemon/rrset-factory.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080022#include "util/cert-helper.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080023#include "ndns-enum.hpp"
24#include "ndns-label.hpp"
25#include "ndns-tlv.hpp"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050026
27#include "boost-test.hpp"
28#include "key-chain-fixture.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080029
Yumin Xia55a7cc42017-05-14 18:43:34 -070030#include <random>
31
Jiewen Tan870b29b2014-11-17 19:09:49 -080032#include <boost/algorithm/string/replace.hpp>
Yumin Xia55a7cc42017-05-14 18:43:34 -070033#include <boost/range/adaptors.hpp>
Davide Pesavento28229df2020-01-04 15:11:25 -050034#if BOOST_VERSION >= 105900
35#include <boost/test/tools/output_test_stream.hpp>
36#else
37#include <boost/test/output_test_stream.hpp>
38#endif
Jiewen Tan870b29b2014-11-17 19:09:49 -080039
Davide Pesavento28229df2020-01-04 15:11:25 -050040#include <ndn-cxx/security/transform.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080041#include <ndn-cxx/util/io.hpp>
42#include <ndn-cxx/util/regex.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080043
Jiewen Tan870b29b2014-11-17 19:09:49 -080044namespace ndn {
45namespace ndns {
46namespace tests {
47
Davide Pesaventobdd88c12020-11-26 00:35:08 -050048using boost::test_tools::output_test_stream;
49
50const auto TEST_DATABASE = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool.db";
51const auto TEST_CERTDIR = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool_certs";
52const Name FAKE_ROOT("/fake-root/123456789");
Jiewen Tan870b29b2014-11-17 19:09:49 -080053
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080054/**
55 * @brief Recursive copy a directory using Boost Filesystem
56 *
57 * Based on from http://stackoverflow.com/q/8593608/2150331
58 */
Davide Pesaventobdd88c12020-11-26 00:35:08 -050059static void
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080060copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
61{
62 namespace fs = boost::filesystem;
63
64 fs::create_directory(destination);
65 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
66 fs::path current(file->path());
67 if (is_directory(current)) {
68 copyDir(current, destination / current.filename());
69 }
70 else {
Eric Newberry9edaf262018-06-07 23:44:57 -070071 copy_file(current, destination / current.filename());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080072 }
73 }
74}
75
76class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080077{
78public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080079 TestHome()
80 {
81 if (std::getenv("HOME"))
82 m_origHome = std::getenv("HOME");
83
Davide Pesaventobdd88c12020-11-26 00:35:08 -050084 auto p = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "tests" / "unit" / "mgmt";
85 setenv("HOME", p.c_str(), 1);
86 boost::filesystem::remove_all(p);
87 boost::filesystem::create_directories(p);
88 copyDir("tests/unit/mgmt/.ndn", p / ".ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080089 }
90
91 ~TestHome()
92 {
93 if (!m_origHome.empty())
Davide Pesaventobdd88c12020-11-26 00:35:08 -050094 setenv("HOME", m_origHome.data(), 1);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080095 else
96 unsetenv("HOME");
97 }
98
99protected:
100 std::string m_origHome;
101};
102
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500103class ManagementToolFixture : public TestHome, public KeyChainFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800104{
105public:
106 class Error : public std::runtime_error
107 {
108 public:
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500109 using std::runtime_error::runtime_error;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800110 };
111
112 class PreviousStateCleaner
113 {
114 public:
115 PreviousStateCleaner()
116 {
117 boost::filesystem::remove(TEST_DATABASE);
118 boost::filesystem::remove_all(TEST_CERTDIR);
119 }
120 };
121
Jiewen Tan870b29b2014-11-17 19:09:49 -0800122 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800123 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800124 , m_dbMgr(TEST_DATABASE.string().c_str())
125 {
126 boost::filesystem::create_directory(TEST_CERTDIR);
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500127 Identity root = m_keyChain.createIdentity("NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800128 Key ksk = root.getDefaultKey();
129 m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName());
130 Certificate kskCert = CertHelper::createCertificate(m_keyChain, ksk, ksk, "CERT");
131 m_keyChain.addCertificate(ksk, kskCert);
132 rootKsk = kskCert.getName();
133
134 Key dsk = m_keyChain.createKey(root);
135 // replace rootDsk's default cert with ksk-signing cert
136 m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName());
137 Certificate dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT");
138 m_keyChain.addCertificate(dsk, dskCert);
139 rootDsk = dskCert.getName();
140
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500141 Identity other = m_keyChain.createIdentity("/ndns-test/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800142 Key otherKskKey = other.getDefaultKey();
143 m_keyChain.deleteCertificate(otherKskKey, otherKskKey.getDefaultCertificate().getName());
144 Certificate otherKskCert = CertHelper::createCertificate(m_keyChain, otherKskKey, otherKskKey, "CERT");
145 m_keyChain.addCertificate(otherKskKey, otherKskCert);
146 otherKsk = otherKskCert.getName();
147
148 // replace rootDsk's default cert with ksk-signing cert
149 Key otherDskKey = m_keyChain.createKey(other);
150 m_keyChain.deleteCertificate(otherDskKey, otherDskKey.getDefaultCertificate().getName());
151 Certificate otherDskCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
152 m_keyChain.addCertificate(otherDskKey, otherDskCert);
153 otherDsk = otherDskCert.getName();
154
155 Certificate rootDkeyCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
156 m_keyChain.addCertificate(otherDskKey, rootDkeyCert);
157 rootDkey = rootDkeyCert.getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800158 }
159
160 ~ManagementToolFixture()
161 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800162 }
163
Yumin Xia2c509c22017-02-09 14:37:36 -0800164 std::vector<Certificate>
165 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800166 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800167 Zone zone(zoneName);
168 std::vector<Certificate> certs;
169 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
170 // ksk are always the first key
171 certs.push_back(Certificate(zoneInfo["ksk"]));
172 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800173 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800174 }
175
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800176 Rrset
177 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800178 {
179 Rrset rrset(&zone);
180 rrset.setLabel(label);
181 rrset.setType(type);
182
183 if (!m_dbMgr.find(rrset))
Yumin Xia2c509c22017-02-09 14:37:36 -0800184 BOOST_THROW_EXCEPTION(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800185 else
186 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800187 }
188
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800189 Name
190 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800191 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800192 size_t zoneNameSize = zone.getName().size();
193 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800194 }
195
Yumin Xia2c509c22017-02-09 14:37:36 -0800196 Certificate
197 findCertFromIdentity(const Name& identityName, const Name& certName)
198 {
199 Certificate rtn;
200 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
201 for (const auto& key : identity.getKeys()) {
202 for (const auto& cert : key.getCertificates()) {
203 if (cert.getName() == certName) {
204 rtn = cert;
205 return rtn;
206 }
207 }
208 }
209 BOOST_THROW_EXCEPTION(Error("Certificate not found in keyChain"));
210 return rtn;
211 }
212
213 Certificate
214 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800215 {
216 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800217 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800218 cert.wireDecode(rrset.getData());
219 return cert;
220 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800221
Yumin Xia2c509c22017-02-09 14:37:36 -0800222 Certificate
223 findDkeyFromDb(const Name& zoneName)
224 {
225 Zone zone(zoneName);
226 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
227 return Certificate(zoneInfo["dkey"]);
228 }
229
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800230 Response
231 findResponse(Zone& zone, const Name& label, const name::Component& type)
232 {
233 Rrset rrset = findRrSet(zone, label, type);
234 Data data(rrset.getData());
235 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700236 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800237 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800238 }
239
240public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800241 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800242 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800243 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800244
245 // Names of pre-created certificates
246 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
247 // constructor.
248 Name rootKsk;
249 Name rootDsk;
250 Name otherKsk;
251 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800252 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800253};
254
Yumin Xia9d110ce2016-11-15 14:03:14 -0800255BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
256
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800257// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
258// {
259// using time::seconds;
260
261// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
262// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
263// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
264// auto kskCert = m_keyChain
265// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
266// time::fromUnixTimestamp(seconds(2147483648)), {});
267// if (parentCert.empty()) {
268// m_keyChain.selfSign(*kskCert);
269// }
270// else {
271// m_keyChain.sign(*kskCert, parentCert);
272// }
273// m_keyChain.addCertificate(*kskCert);
274
275// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
276// auto dskCert = m_keyChain
277// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
278// time::fromUnixTimestamp(seconds(2147483648)), {});
279// m_keyChain.sign(*dskCert, kskCert->getName());
280// m_keyChain.addCertificate(*dskCert);
281
282// return dskCert->getName();
283// };
284
285// Name rootDsk = generateCerts(ROOT_ZONE);
286// generateCerts("/ndns-test", rootDsk);
287
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500288// copyDir(UNIT_TESTS_TMPDIR "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800289// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
290// }
291
Yumin Xia55a7cc42017-05-14 18:43:34 -0700292BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
293{
294 // the correctness of our DoE design rely on the ordering of SQLite
295 // this unit test make sure that our label::isSmallerInLabelOrder
296 // is the same as the ordering of BLOB in SQLite
297
298 std::random_device seed;
299 std::mt19937 gen(seed());
300
301 auto genRandomString = [&] (int length) -> std::string {
302 std::string charset =
303 "0123456789"
304 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
305 "abcdefghijklmnopqrstuvwxyz";
306 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
307
308 std::string str(length, 0);
309 std::generate_n(str.begin(), length, [&] { return charset[dist(gen)];} );
310 return str;
311 };
312
313 auto genRandomLabel = [&]() -> Name {
314 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
315 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
316 Name nm;
317 size_t length = numberOfLabelsDist(gen);
318 for (size_t i = 0; i < length; i++) {
319 nm.append(genRandomString(labelSizeDist(gen)));
320 }
321 return nm;
322 };
323
324 const size_t NGENERATED_LABELS = 10;
325
326 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
327 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
328 m_keyChain, DEFAULT_CERT);
329 rrsetFactory.checkZoneKey();
330 std::vector<Rrset> rrsets;
331 std::vector<Name> names;
332 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
333 Name randomLabel = genRandomLabel();
334 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
335 VERSION_USE_UNIX_TIMESTAMP,
336 DEFAULT_CACHE_TTL,
337 {});
338
339 rrsets.push_back(randomTxt);
340 m_dbMgr.insert(randomTxt);
341 names.push_back(randomLabel);
342 }
343
344 std::sort(rrsets.begin(), rrsets.end());
345
346 using boost::adaptors::filtered;
347 using boost::adaptors::transformed;
348
349 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
350 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
351 return rrset.getType() == label::TXT_RR_TYPE;
352 });
353
354 auto extractLabel = [] (const Rrset& rr) {
355 return rr.getLabel();
356 };
357 auto expected = rrsets | transformed(extractLabel);
358 auto actual = fromDbFiltered | transformed(extractLabel);
359
360 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
361 actual.begin(), actual.end());
362}
363
Yumin Xia9d110ce2016-11-15 14:03:14 -0800364BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800365{
Yumin Xia2c509c22017-02-09 14:37:36 -0800366 // creating root_zone need a rootDkey
367 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
368 time::seconds(4600),
369 time::seconds(4600),
370 rootKsk, rootDsk), ndns::ManagementTool::Error);
371
372 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
373 time::seconds(4600),
374 time::seconds(4600),
375 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800376
377 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800378 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800379 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800380 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
381 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
382 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
383 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
384
385 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
386 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800387
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800388 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
389 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800390}
391
Yumin Xia9d110ce2016-11-15 14:03:14 -0800392BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800393{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800394 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800395 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800396
Yumin Xia2c509c22017-02-09 14:37:36 -0800397 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700398 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800399 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800400
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800401 // will generate keys automatically
402 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800403 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800404
Yumin Xia2c509c22017-02-09 14:37:36 -0800405 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800406 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800407
Yumin Xia2c509c22017-02-09 14:37:36 -0800408 const Name& ksk = certs[0].getName();
409 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800410
411 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800412 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800413 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
414 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
415 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
416
417 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
418 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800419
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800420 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800421
422 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800423 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
424}
425
Yumin Xia9d110ce2016-11-15 14:03:14 -0800426BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800427{
428 Name parentZoneName("/ndns-test");
429 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700430 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800431
432 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800433 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800434
Yumin Xia2c509c22017-02-09 14:37:36 -0800435 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800436 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800437
Yumin Xia2c509c22017-02-09 14:37:36 -0800438 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800439
440 // Check zone ttl
441 Zone zone(zoneName);
442 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
443 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
444
Yumin Xia2c509c22017-02-09 14:37:36 -0800445 // check dkey name
446 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
447 Certificate dkey = findDkeyFromDb(zoneName);
448 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
449
450 // TODO: check signing hierarchy
451
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800452 // Check dsk rrset ttl
453 Rrset rrset;
454 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
455 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
456
457 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800458 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
459 time::system_clock::TimePoint beg,end;
460 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800461
Yumin Xia2c509c22017-02-09 14:37:36 -0800462 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
463 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
464 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800465 m_tool.deleteZone(zoneName);
466}
467
Yumin Xia9d110ce2016-11-15 14:03:14 -0800468BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800469{
470 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
471 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
472
Yumin Xia2c509c22017-02-09 14:37:36 -0800473 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800474 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800475
Yumin Xia2c509c22017-02-09 14:37:36 -0800476 const Name& ksk = certs[0].getName();
477 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800478
479 m_tool.deleteZone("/net/ndnsim");
480 // identity will still exist after the zone is deleted
481
482 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
483
484 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
485
486 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
487 time::seconds(1), time::days(1), ksk, dsk));
488 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
489 m_tool.deleteZone("/net/ndnsim");
490
491 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
492 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800493
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800494 m_tool.deleteZone("/net/ndnsim");
495
496 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
497 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800498 m_tool.deleteZone("/net/ndnsim");
499
500 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
501 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800502 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800503
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500504 Identity id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800505 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800506 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800507 time::seconds(1), time::days(1), cert.getName()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800508
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500509 id = m_keyChain.createIdentity("/com/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800510 cert = id.getDefaultKey().getDefaultCertificate();
511
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800512 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800513 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800514 ndns::ManagementTool::Error);
515
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500516 id = m_keyChain.createIdentity("/net/ndnsim/www/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800517 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800518 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800519 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800520 ndns::ManagementTool::Error);
521
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500522 id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800523 cert = id.getDefaultKey().getDefaultCertificate();
524 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800525 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800526 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800527 ndns::ManagementTool::Error);
528
Yumin Xia2c509c22017-02-09 14:37:36 -0800529 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800530 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
531
532 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800533 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800534}
535
536class OutputTester
537{
538public:
539 OutputTester()
540 : savedBuf(std::clog.rdbuf())
541 {
542 std::cout.rdbuf(buffer.rdbuf());
543 }
544
545 ~OutputTester()
546 {
547 std::cout.rdbuf(savedBuf);
548 }
549
550public:
551 std::stringstream buffer;
552 std::streambuf* savedBuf;
553};
554
Yumin Xia2c509c22017-02-09 14:37:36 -0800555// BOOST_AUTO_TEST_CASE(ExportCertificate)
556// {
557// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800558
Yumin Xia2c509c22017-02-09 14:37:36 -0800559// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
560// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800561
Yumin Xia2c509c22017-02-09 14:37:36 -0800562// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
563// // doesn't check the zone, export from KeyChain directly
564// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
565// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800566
Yumin Xia2c509c22017-02-09 14:37:36 -0800567// std::string dskValue =
568// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
569// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
570// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
571// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
572// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
573// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
574// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
575// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
576// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
577// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
578// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
579// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
580// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
581// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
582// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
583// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800584
Yumin Xia2c509c22017-02-09 14:37:36 -0800585// {
586// std::ifstream ifs(outputFile.c_str());
587// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
588// std::istreambuf_iterator<char>());
589// BOOST_CHECK_EQUAL(actualValue, dskValue);
590// }
591// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800592
Yumin Xia2c509c22017-02-09 14:37:36 -0800593// // doesn't check the zone, export from KeyChain directly
594// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
595// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800596
Yumin Xia2c509c22017-02-09 14:37:36 -0800597// Name zoneName("/ndns-test");
598// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
599// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800600
Yumin Xia2c509c22017-02-09 14:37:36 -0800601// m_keyChain.deleteCertificate(otherKsk);
602// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800603
Yumin Xia2c509c22017-02-09 14:37:36 -0800604// // retrieve cert from the zone
605// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
606// {
607// std::ifstream ifs(outputFile.c_str());
608// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
609// std::istreambuf_iterator<char>());
610// BOOST_CHECK_EQUAL(actualValue, dskValue);
611// }
612// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800613
Yumin Xia2c509c22017-02-09 14:37:36 -0800614// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
615// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800616
Yumin Xia2c509c22017-02-09 14:37:36 -0800617// // output to std::cout
618// std::string acutalOutput;
619// {
620// OutputTester tester;
621// m_tool.exportCertificate(otherDsk, "-");
622// acutalOutput = tester.buffer.str();
623// }
624// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
625// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800626
Yumin Xia9d110ce2016-11-15 14:03:14 -0800627BOOST_AUTO_TEST_CASE(AddRrset)
628{
629 Name zoneName("/ndns-test");
630 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800631
Yumin Xia9d110ce2016-11-15 14:03:14 -0800632 time::seconds ttl1(4200);
633 time::seconds ttl2(4500);
634 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800635
Yumin Xia9d110ce2016-11-15 14:03:14 -0800636 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
637 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700638 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800639
Yumin Xia9d110ce2016-11-15 14:03:14 -0800640 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
641 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
642 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800643}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800644
Yumin Xia9d110ce2016-11-15 14:03:14 -0800645BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
646{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800647 Name zoneName("/ndns-test");
648 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800649
Yumin Xia9d110ce2016-11-15 14:03:14 -0800650 time::seconds ttl(4200);
651 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800652
Yumin Xia9d110ce2016-11-15 14:03:14 -0800653 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
654 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800655
Yumin Xia9d110ce2016-11-15 14:03:14 -0800656 auto checkRrset = [&zone, &zoneName, this](Name label,
657 name::Component type,
658 NdnsContentType contentType) -> void {
659 Rrset rr1 = findRrSet(zone, label, type);
660 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
661 Response response1;
662 response1.fromData(zoneName, Data(rr1.getData()));
663 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
664 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800665
Yumin Xia9d110ce2016-11-15 14:03:14 -0800666 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800667
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700668 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800669
Yumin Xia9d110ce2016-11-15 14:03:14 -0800670 //add NS NDNS_AUTH and check user-defined ttl
671 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
672 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
673 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800674
Yumin Xia9d110ce2016-11-15 14:03:14 -0800675 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
676 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800677
Yumin Xia9d110ce2016-11-15 14:03:14 -0800678 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700679 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800680 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800681
Yumin Xia9d110ce2016-11-15 14:03:14 -0800682 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
683 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
684 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
685 // check that there is no confliction
686 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800687
Yumin Xia9d110ce2016-11-15 14:03:14 -0800688 // insert a shorter NS, when there are longer NS or TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700689 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800690 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
691 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800692
Yumin Xia9d110ce2016-11-15 14:03:14 -0800693 // insert a longer NS, when there is already a shorter NS
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700694 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800695 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
696 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800697
Yumin Xia9d110ce2016-11-15 14:03:14 -0800698 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700699 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800700 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800701
Yumin Xia9d110ce2016-11-15 14:03:14 -0800702 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700703 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
704 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800705 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
706 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
707 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800708
Yumin Xia9d110ce2016-11-15 14:03:14 -0800709 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700710 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800711 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
712}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800713
Yumin Xia9d110ce2016-11-15 14:03:14 -0800714BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
715{
716 // check pre-condition
717 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800718
Yumin Xia9d110ce2016-11-15 14:03:14 -0800719 // Check: throw if zone not exist
720 std::string certPath = TEST_CERTDIR.string();
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800721 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800722
Yumin Xia9d110ce2016-11-15 14:03:14 -0800723 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800724
Yumin Xia9d110ce2016-11-15 14:03:14 -0800725 // Check: throw if certificate does not match
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800726 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800727
Yumin Xia9d110ce2016-11-15 14:03:14 -0800728 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800729 std::vector<Certificate>&& certs = getCerts(zoneName);
730 const Name& ksk = certs[0].getName();
731 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800732
Yumin Xia2c509c22017-02-09 14:37:36 -0800733 // Check: throw if it's a duplicated certificate
734 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800735}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800736
Yumin Xia9d110ce2016-11-15 14:03:14 -0800737BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
738{
739 Name parentZoneName("/ndns-test");
740 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700741 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800742
Yumin Xia9d110ce2016-11-15 14:03:14 -0800743 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
744 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800745
Yumin Xia2c509c22017-02-09 14:37:36 -0800746 Zone zone(zoneName);
747 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800748
Yumin Xia2c509c22017-02-09 14:37:36 -0800749 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800750 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800751 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800752
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800753 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800754 // Check if child zone's d-key could be inserted correctly
755 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800756}
Jiewen Tand2d21822015-03-19 15:37:03 -0700757
Yumin Xia9d110ce2016-11-15 14:03:14 -0800758BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
759{
760 //check using user provided certificate
761 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700762 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800763 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700764 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800765
Yumin Xia2c509c22017-02-09 14:37:36 -0800766 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
767 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
768 Key dsk = m_keyChain.createKey(id);
769 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800770
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800771 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800772 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
773 m_tool.createZone(zoneName, parentZoneName);
774
Yumin Xia2c509c22017-02-09 14:37:36 -0800775 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800776 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800777 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800778
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800779 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800780 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800781}
782
783BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
784{
785 //check invalid output
786 Name parentZoneName("/ndns-test");
787 Name zoneName = Name(parentZoneName).append("child-zone");
788 m_tool.createZone(zoneName, parentZoneName);
789
790 Name content = "invalid data packet";
791 std::string output = TEST_CERTDIR.string() + "/ss.cert";
792 ndn::io::save(content, output);
793
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800794 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800795}
796
797BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
798{
799 //check version control
800 time::seconds ttl(4200);
801 Name parentZoneName("/ndns-test");
802 Name zoneName = Name(parentZoneName).append("child-zone");
803 m_tool.createZone(zoneName, parentZoneName);
804
805 Name label("/label");
806 uint64_t version = 110;
807
808 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
809 rf.checkZoneKey();
810
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700811 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800812
813 m_tool.addRrset(rrset1);
814 // throw error when adding duplicated rrset with the same version
815 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
816 ndns::ManagementTool::Error);
817 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700818 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800819 // throw error when adding duplicated rrset with older version
820 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
821 ndns::ManagementTool::Error);
822
823 version++;
824 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700825 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800826 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
827
828 Zone zone(zoneName);
829 m_dbMgr.find(zone);
830 Rrset rrset;
831 rrset.setZone(&zone);
832 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700833 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800834 m_dbMgr.find(rrset);
835
836 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
837}
838
839BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
840{
841 //check input with different formats
842 Name parentZoneName("/ndns-test");
843 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800844 Zone parentZone(parentZoneName);
845
846 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800847 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700848
Yumin Xia2c509c22017-02-09 14:37:36 -0800849 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800850 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700851
Yumin Xia918343d2017-03-17 19:04:55 -0700852 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
853
Yumin Xia9d110ce2016-11-15 14:03:14 -0800854 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800855 ndn::io::save(cert, output, ndn::io::BASE64);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800856 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800857 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
858 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700859
Yumin Xia9d110ce2016-11-15 14:03:14 -0800860 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800861 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800862 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800863 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
864 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700865
Yumin Xia9d110ce2016-11-15 14:03:14 -0800866 // hex
Yumin Xia2c509c22017-02-09 14:37:36 -0800867 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800868 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800869 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
870 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700871
Yumin Xia9d110ce2016-11-15 14:03:14 -0800872 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800873 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800874 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800875 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
876 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800877 ndns::ManagementTool::Error);
878}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700879
Yumin Xia9d110ce2016-11-15 14:03:14 -0800880BOOST_AUTO_TEST_CASE(ListAllZones)
881{
Yumin Xia2c509c22017-02-09 14:37:36 -0800882 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800883 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700884
Yumin Xia2c509c22017-02-09 14:37:36 -0800885 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
886 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
887
Yumin Xia9d110ce2016-11-15 14:03:14 -0800888 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800889 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
890 "default-certificate=" + rootDsk.toUri() + "\n"
891 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
892 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700893
Yumin Xia9d110ce2016-11-15 14:03:14 -0800894 output_test_stream testOutput;
895 m_tool.listAllZones(testOutput);
896 BOOST_CHECK(testOutput.is_equal(expectedValue));
897}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700898
Yumin Xia55a7cc42017-05-14 18:43:34 -0700899// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800900
Yumin Xia55a7cc42017-05-14 18:43:34 -0700901// BOOST_AUTO_TEST_CASE(ListZone)
902// {
903// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700904
Yumin Xia55a7cc42017-05-14 18:43:34 -0700905// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
906// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700907
Yumin Xia55a7cc42017-05-14 18:43:34 -0700908// // Add NS with NDNS_RESP
909// Delegation del;
910// del.preference = 10;
911// del.name = Name("/get/link");
912// DelegationList ds = {del};
913// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
914// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700915
Yumin Xia55a7cc42017-05-14 18:43:34 -0700916// // Add NS with NDNS_AUTH
917// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
918// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700919
Yumin Xia55a7cc42017-05-14 18:43:34 -0700920// // Add TXT from file
921// std::string output = TEST_CERTDIR.string() + "/a.rrset";
922// Response re1;
923// re1.setZone("/ndns-test");
924// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
925// re1.setRrLabel("/label2");
926// re1.setRrType(label::TXT_RR_TYPE);
927// re1.setContentType(NDNS_RESP);
928// re1.setVersion(name::Component::fromVersion(654321));
929// re1.addRr("First RR");
930// re1.addRr("Second RR");
931// re1.addRr("Last RR");
932// shared_ptr<Data> data1 = re1.toData();
933// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
934// ndn::io::save(*data1, output);
935// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800936
Yumin Xia55a7cc42017-05-14 18:43:34 -0700937// // Add TXT in normal way
938// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
939// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800940
Yumin Xia55a7cc42017-05-14 18:43:34 -0700941// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800942
Yumin Xia55a7cc42017-05-14 18:43:34 -0700943// output_test_stream testOutput;
944// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800945
Yumin Xia55a7cc42017-05-14 18:43:34 -0700946// std::string expectedValue =
947// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800948
Yumin Xia55a7cc42017-05-14 18:43:34 -0700949// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
950// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
951// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800952
Yumin Xia55a7cc42017-05-14 18:43:34 -0700953// /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
954// Certificate name:
955// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
956// Validity:
957// NotBefore: 20180314T212340
958// NotAfter: 20180324T212340
959// Public key bits:
960// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
961// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
962// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
963// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
964// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
965// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
966// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
967// Signature Information:
968// Signature Type: SignatureSha256WithEcdsa
969// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800970
Yumin Xia55a7cc42017-05-14 18:43:34 -0700971// ; 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
972// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
973// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800974
Yumin Xia55a7cc42017-05-14 18:43:34 -0700975// /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
976// Certificate name:
977// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
978// Validity:
979// NotBefore: 20180314T212340
980// NotAfter: 20180324T212340
981// Public key bits:
982// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
983// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
984// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
985// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
986// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
987// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
988// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
989// Signature Information:
990// Signature Type: SignatureSha256WithEcdsa
991// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800992
Yumin Xia55a7cc42017-05-14 18:43:34 -0700993// ; 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
994// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
995// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800996
Yumin Xia55a7cc42017-05-14 18:43:34 -0700997// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
998// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800999
Yumin Xia55a7cc42017-05-14 18:43:34 -07001000// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1001// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
1002// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001003
Yumin Xia55a7cc42017-05-14 18:43:34 -07001004// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1005// /label2 10 NS NDNS-Auth
1006
1007// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1008// /label2 10 TXT First RR
1009// /label2 10 TXT Second RR
1010// /label2 10 TXT Last RR
1011
1012// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1013// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1014// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1015
1016// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1017// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1018// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1019
1020// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1021// /label3 10 TXT Hello
1022// /label3 10 TXT World
1023
1024// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1025// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1026// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1027
1028// )VALUE";
1029
1030// BOOST_CHECK(testOutput.is_equal(expectedValue));
1031// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001032
Yumin Xia9d110ce2016-11-15 14:03:14 -08001033BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1034{
1035 Name zoneName("/ndns-test");
1036 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1037 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1038 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001039 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001040
Yumin Xia9d110ce2016-11-15 14:03:14 -08001041 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001042
Yumin Xia2c509c22017-02-09 14:37:36 -08001043 std::stringstream os;
1044
1045 using security::transform::base64Encode;
1046 using security::transform::streamSink;
1047 using security::transform::bufferSource;
1048
1049 bufferSource(rrset1.getData().wire(), rrset1.getData().size()) >> base64Encode() >> streamSink(os);
1050
1051 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001052
Yumin Xia9d110ce2016-11-15 14:03:14 -08001053 output_test_stream testOutput;
1054 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001055 BOOST_CHECK(testOutput.is_equal(expectedValue));
1056}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001057
Yumin Xia9d110ce2016-11-15 14:03:14 -08001058BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1059{
1060 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001061
Yumin Xia9d110ce2016-11-15 14:03:14 -08001062 m_tool.createZone(zoneName, ROOT_ZONE);
1063 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1064 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001065
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001066 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001067
Yumin Xia9d110ce2016-11-15 14:03:14 -08001068 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001069
Yumin Xia9d110ce2016-11-15 14:03:14 -08001070 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001071 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001072
Yumin Xia9d110ce2016-11-15 14:03:14 -08001073 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001074
Yumin Xia9d110ce2016-11-15 14:03:14 -08001075 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1076}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001077
1078BOOST_AUTO_TEST_SUITE_END()
1079
1080} // namespace tests
1081} // namespace ndns
1082} // namespace ndn