blob: 054a8cfc3983c6ac15ca8ca09ac95fd7283b70c5 [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"
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#if BOOST_VERSION >= 105900
34#include <boost/test/tools/output_test_stream.hpp>
35#else
36#include <boost/test/output_test_stream.hpp>
37#endif
Jiewen Tan870b29b2014-11-17 19:09:49 -080038
Davide Pesavento28229df2020-01-04 15:11:25 -050039#include <ndn-cxx/security/transform.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080040#include <ndn-cxx/util/io.hpp>
Davide Pesavento948c50c2020-12-26 21:30:45 -050041#include <ndn-cxx/util/random.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080042#include <ndn-cxx/util/regex.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080043
Davide Pesavento948c50c2020-12-26 21:30:45 -050044#include <iostream>
45
Jiewen Tan870b29b2014-11-17 19:09:49 -080046namespace ndn {
47namespace ndns {
48namespace tests {
49
Davide Pesaventobdd88c12020-11-26 00:35:08 -050050using boost::test_tools::output_test_stream;
51
52const auto TEST_DATABASE = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool.db";
53const auto TEST_CERTDIR = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "management_tool_certs";
54const Name FAKE_ROOT("/fake-root/123456789");
Jiewen Tan870b29b2014-11-17 19:09:49 -080055
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080056/**
57 * @brief Recursive copy a directory using Boost Filesystem
58 *
59 * Based on from http://stackoverflow.com/q/8593608/2150331
60 */
Davide Pesaventobdd88c12020-11-26 00:35:08 -050061static void
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080062copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
63{
64 namespace fs = boost::filesystem;
65
66 fs::create_directory(destination);
67 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
68 fs::path current(file->path());
69 if (is_directory(current)) {
70 copyDir(current, destination / current.filename());
71 }
72 else {
Eric Newberry9edaf262018-06-07 23:44:57 -070073 copy_file(current, destination / current.filename());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080074 }
75 }
76}
77
78class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080079{
80public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080081 TestHome()
82 {
83 if (std::getenv("HOME"))
84 m_origHome = std::getenv("HOME");
85
Davide Pesaventobdd88c12020-11-26 00:35:08 -050086 auto p = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "tests" / "unit" / "mgmt";
87 setenv("HOME", p.c_str(), 1);
88 boost::filesystem::remove_all(p);
89 boost::filesystem::create_directories(p);
90 copyDir("tests/unit/mgmt/.ndn", p / ".ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080091 }
92
93 ~TestHome()
94 {
95 if (!m_origHome.empty())
Davide Pesaventobdd88c12020-11-26 00:35:08 -050096 setenv("HOME", m_origHome.data(), 1);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080097 else
98 unsetenv("HOME");
99 }
100
101protected:
102 std::string m_origHome;
103};
104
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500105class ManagementToolFixture : public TestHome, public KeyChainFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800106{
107public:
108 class Error : public std::runtime_error
109 {
110 public:
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500111 using std::runtime_error::runtime_error;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800112 };
113
114 class PreviousStateCleaner
115 {
116 public:
117 PreviousStateCleaner()
118 {
119 boost::filesystem::remove(TEST_DATABASE);
120 boost::filesystem::remove_all(TEST_CERTDIR);
121 }
122 };
123
Jiewen Tan870b29b2014-11-17 19:09:49 -0800124 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800125 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800126 , m_dbMgr(TEST_DATABASE.string().c_str())
127 {
128 boost::filesystem::create_directory(TEST_CERTDIR);
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500129 Identity root = m_keyChain.createIdentity("NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800130 Key ksk = root.getDefaultKey();
131 m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName());
132 Certificate kskCert = CertHelper::createCertificate(m_keyChain, ksk, ksk, "CERT");
133 m_keyChain.addCertificate(ksk, kskCert);
134 rootKsk = kskCert.getName();
135
136 Key dsk = m_keyChain.createKey(root);
137 // replace rootDsk's default cert with ksk-signing cert
138 m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName());
139 Certificate dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT");
140 m_keyChain.addCertificate(dsk, dskCert);
141 rootDsk = dskCert.getName();
142
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500143 Identity other = m_keyChain.createIdentity("/ndns-test/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800144 Key otherKskKey = other.getDefaultKey();
145 m_keyChain.deleteCertificate(otherKskKey, otherKskKey.getDefaultCertificate().getName());
146 Certificate otherKskCert = CertHelper::createCertificate(m_keyChain, otherKskKey, otherKskKey, "CERT");
147 m_keyChain.addCertificate(otherKskKey, otherKskCert);
148 otherKsk = otherKskCert.getName();
149
150 // replace rootDsk's default cert with ksk-signing cert
151 Key otherDskKey = m_keyChain.createKey(other);
152 m_keyChain.deleteCertificate(otherDskKey, otherDskKey.getDefaultCertificate().getName());
153 Certificate otherDskCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
154 m_keyChain.addCertificate(otherDskKey, otherDskCert);
155 otherDsk = otherDskCert.getName();
156
157 Certificate rootDkeyCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
158 m_keyChain.addCertificate(otherDskKey, rootDkeyCert);
159 rootDkey = rootDkeyCert.getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800160 }
161
Yumin Xia2c509c22017-02-09 14:37:36 -0800162 std::vector<Certificate>
163 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800164 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800165 Zone zone(zoneName);
166 std::vector<Certificate> certs;
167 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
168 // ksk are always the first key
169 certs.push_back(Certificate(zoneInfo["ksk"]));
170 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800171 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800172 }
173
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800174 Rrset
175 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800176 {
177 Rrset rrset(&zone);
178 rrset.setLabel(label);
179 rrset.setType(type);
180
181 if (!m_dbMgr.find(rrset))
Davide Pesavento948c50c2020-12-26 21:30:45 -0500182 NDN_THROW(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800183 else
184 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800185 }
186
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800187 Name
188 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800189 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800190 size_t zoneNameSize = zone.getName().size();
191 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800192 }
193
Yumin Xia2c509c22017-02-09 14:37:36 -0800194 Certificate
195 findCertFromIdentity(const Name& identityName, const Name& certName)
196 {
197 Certificate rtn;
198 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
199 for (const auto& key : identity.getKeys()) {
200 for (const auto& cert : key.getCertificates()) {
201 if (cert.getName() == certName) {
202 rtn = cert;
203 return rtn;
204 }
205 }
206 }
Davide Pesavento948c50c2020-12-26 21:30:45 -0500207 NDN_THROW(Error("Certificate not found in keyChain"));
Yumin Xia2c509c22017-02-09 14:37:36 -0800208 return rtn;
209 }
210
211 Certificate
212 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800213 {
214 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800215 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800216 cert.wireDecode(rrset.getData());
217 return cert;
218 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800219
Yumin Xia2c509c22017-02-09 14:37:36 -0800220 Certificate
221 findDkeyFromDb(const Name& zoneName)
222 {
223 Zone zone(zoneName);
224 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
225 return Certificate(zoneInfo["dkey"]);
226 }
227
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800228 Response
229 findResponse(Zone& zone, const Name& label, const name::Component& type)
230 {
231 Rrset rrset = findRrSet(zone, label, type);
232 Data data(rrset.getData());
233 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700234 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800235 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800236 }
237
238public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800239 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800240 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800241 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800242
243 // Names of pre-created certificates
244 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
245 // constructor.
246 Name rootKsk;
247 Name rootDsk;
248 Name otherKsk;
249 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800250 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800251};
252
Yumin Xia9d110ce2016-11-15 14:03:14 -0800253BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
254
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800255// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
256// {
257// using time::seconds;
258
259// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
260// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
261// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
262// auto kskCert = m_keyChain
263// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
264// time::fromUnixTimestamp(seconds(2147483648)), {});
265// if (parentCert.empty()) {
266// m_keyChain.selfSign(*kskCert);
267// }
268// else {
269// m_keyChain.sign(*kskCert, parentCert);
270// }
271// m_keyChain.addCertificate(*kskCert);
272
273// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
274// auto dskCert = m_keyChain
275// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
276// time::fromUnixTimestamp(seconds(2147483648)), {});
277// m_keyChain.sign(*dskCert, kskCert->getName());
278// m_keyChain.addCertificate(*dskCert);
279
280// return dskCert->getName();
281// };
282
283// Name rootDsk = generateCerts(ROOT_ZONE);
284// generateCerts("/ndns-test", rootDsk);
285
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500286// copyDir(UNIT_TESTS_TMPDIR "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800287// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
288// }
289
Yumin Xia55a7cc42017-05-14 18:43:34 -0700290BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
291{
292 // the correctness of our DoE design rely on the ordering of SQLite
293 // this unit test make sure that our label::isSmallerInLabelOrder
294 // is the same as the ordering of BLOB in SQLite
295
Davide Pesavento948c50c2020-12-26 21:30:45 -0500296 auto genRandomString = [] (size_t length) {
297 static const std::string charset = "0123456789"
298 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
299 "abcdefghijklmnopqrstuvwxyz";
Yumin Xia55a7cc42017-05-14 18:43:34 -0700300 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
301
302 std::string str(length, 0);
Davide Pesavento948c50c2020-12-26 21:30:45 -0500303 std::generate_n(str.begin(), length,
304 [&] { return charset[dist(random::getRandomNumberEngine())]; });
Yumin Xia55a7cc42017-05-14 18:43:34 -0700305 return str;
306 };
307
Davide Pesavento948c50c2020-12-26 21:30:45 -0500308 auto genRandomLabel = [&] {
Yumin Xia55a7cc42017-05-14 18:43:34 -0700309 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
310 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
Davide Pesavento948c50c2020-12-26 21:30:45 -0500311
Yumin Xia55a7cc42017-05-14 18:43:34 -0700312 Name nm;
Davide Pesavento948c50c2020-12-26 21:30:45 -0500313 size_t length = numberOfLabelsDist(random::getRandomNumberEngine());
Yumin Xia55a7cc42017-05-14 18:43:34 -0700314 for (size_t i = 0; i < length; i++) {
Davide Pesavento948c50c2020-12-26 21:30:45 -0500315 nm.append(genRandomString(labelSizeDist(random::getRandomNumberEngine())));
Yumin Xia55a7cc42017-05-14 18:43:34 -0700316 }
317 return nm;
318 };
319
320 const size_t NGENERATED_LABELS = 10;
321
322 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
323 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
324 m_keyChain, DEFAULT_CERT);
325 rrsetFactory.checkZoneKey();
326 std::vector<Rrset> rrsets;
327 std::vector<Name> names;
328 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
329 Name randomLabel = genRandomLabel();
330 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
331 VERSION_USE_UNIX_TIMESTAMP,
332 DEFAULT_CACHE_TTL,
333 {});
334
335 rrsets.push_back(randomTxt);
336 m_dbMgr.insert(randomTxt);
337 names.push_back(randomLabel);
338 }
339
340 std::sort(rrsets.begin(), rrsets.end());
341
342 using boost::adaptors::filtered;
343 using boost::adaptors::transformed;
344
345 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
346 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
347 return rrset.getType() == label::TXT_RR_TYPE;
348 });
349
350 auto extractLabel = [] (const Rrset& rr) {
351 return rr.getLabel();
352 };
353 auto expected = rrsets | transformed(extractLabel);
354 auto actual = fromDbFiltered | transformed(extractLabel);
355
356 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
357 actual.begin(), actual.end());
358}
359
Yumin Xia9d110ce2016-11-15 14:03:14 -0800360BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800361{
Yumin Xia2c509c22017-02-09 14:37:36 -0800362 // creating root_zone need a rootDkey
363 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
364 time::seconds(4600),
365 time::seconds(4600),
366 rootKsk, rootDsk), ndns::ManagementTool::Error);
367
368 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
369 time::seconds(4600),
370 time::seconds(4600),
371 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800372
373 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800374 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800375 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800376 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
377 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
378 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
379 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
380
381 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
382 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800383
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800384 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
385 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800386}
387
Yumin Xia9d110ce2016-11-15 14:03:14 -0800388BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800389{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800390 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800391 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800392
Yumin Xia2c509c22017-02-09 14:37:36 -0800393 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700394 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800395 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800396
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800397 // will generate keys automatically
398 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800399 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800400
Yumin Xia2c509c22017-02-09 14:37:36 -0800401 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800402 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800403
Yumin Xia2c509c22017-02-09 14:37:36 -0800404 const Name& ksk = certs[0].getName();
405 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800406
407 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800408 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800409 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
410 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
411 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
412
413 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
414 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800415
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800416 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800417
418 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800419 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
420}
421
Yumin Xia9d110ce2016-11-15 14:03:14 -0800422BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800423{
424 Name parentZoneName("/ndns-test");
425 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700426 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800427
428 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800429 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800430
Yumin Xia2c509c22017-02-09 14:37:36 -0800431 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800432 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800433
Yumin Xia2c509c22017-02-09 14:37:36 -0800434 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800435
436 // Check zone ttl
437 Zone zone(zoneName);
438 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
439 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
440
Yumin Xia2c509c22017-02-09 14:37:36 -0800441 // check dkey name
442 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
443 Certificate dkey = findDkeyFromDb(zoneName);
444 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
445
446 // TODO: check signing hierarchy
447
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800448 // Check dsk rrset ttl
449 Rrset rrset;
450 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
451 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
452
453 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800454 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
455 time::system_clock::TimePoint beg,end;
456 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800457
Yumin Xia2c509c22017-02-09 14:37:36 -0800458 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
459 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
460 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800461 m_tool.deleteZone(zoneName);
462}
463
Yumin Xia9d110ce2016-11-15 14:03:14 -0800464BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800465{
466 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
467 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
468
Yumin Xia2c509c22017-02-09 14:37:36 -0800469 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800470 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800471
Yumin Xia2c509c22017-02-09 14:37:36 -0800472 const Name& ksk = certs[0].getName();
473 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800474
475 m_tool.deleteZone("/net/ndnsim");
476 // identity will still exist after the zone is deleted
477
478 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
479
480 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
481
482 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
483 time::seconds(1), time::days(1), ksk, dsk));
484 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
485 m_tool.deleteZone("/net/ndnsim");
486
487 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
488 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800489
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800490 m_tool.deleteZone("/net/ndnsim");
491
492 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
493 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800494 m_tool.deleteZone("/net/ndnsim");
495
496 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
497 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800498 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800499
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500500 Identity id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800501 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800502 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800503 time::seconds(1), time::days(1), cert.getName()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800504
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500505 id = m_keyChain.createIdentity("/com/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800506 cert = id.getDefaultKey().getDefaultCertificate();
507
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800508 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800509 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800510 ndns::ManagementTool::Error);
511
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500512 id = m_keyChain.createIdentity("/net/ndnsim/www/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800513 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800514 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800515 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800516 ndns::ManagementTool::Error);
517
Davide Pesaventobdd88c12020-11-26 00:35:08 -0500518 id = m_keyChain.createIdentity("/net/ndnsim/NDNS");
Yumin Xia2c509c22017-02-09 14:37:36 -0800519 cert = id.getDefaultKey().getDefaultCertificate();
520 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800521 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800522 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800523 ndns::ManagementTool::Error);
524
Yumin Xia2c509c22017-02-09 14:37:36 -0800525 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800526 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
527
528 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800529 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800530}
531
532class OutputTester
533{
534public:
535 OutputTester()
536 : savedBuf(std::clog.rdbuf())
537 {
538 std::cout.rdbuf(buffer.rdbuf());
539 }
540
541 ~OutputTester()
542 {
543 std::cout.rdbuf(savedBuf);
544 }
545
546public:
547 std::stringstream buffer;
548 std::streambuf* savedBuf;
549};
550
Yumin Xia2c509c22017-02-09 14:37:36 -0800551// BOOST_AUTO_TEST_CASE(ExportCertificate)
552// {
553// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800554
Yumin Xia2c509c22017-02-09 14:37:36 -0800555// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
556// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800557
Yumin Xia2c509c22017-02-09 14:37:36 -0800558// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
559// // doesn't check the zone, export from KeyChain directly
560// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
561// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800562
Yumin Xia2c509c22017-02-09 14:37:36 -0800563// std::string dskValue =
564// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
565// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
566// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
567// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
568// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
569// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
570// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
571// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
572// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
573// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
574// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
575// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
576// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
577// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
578// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
579// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800580
Yumin Xia2c509c22017-02-09 14:37:36 -0800581// {
582// std::ifstream ifs(outputFile.c_str());
583// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
584// std::istreambuf_iterator<char>());
585// BOOST_CHECK_EQUAL(actualValue, dskValue);
586// }
587// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800588
Yumin Xia2c509c22017-02-09 14:37:36 -0800589// // doesn't check the zone, export from KeyChain directly
590// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
591// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800592
Yumin Xia2c509c22017-02-09 14:37:36 -0800593// Name zoneName("/ndns-test");
594// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
595// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800596
Yumin Xia2c509c22017-02-09 14:37:36 -0800597// m_keyChain.deleteCertificate(otherKsk);
598// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800599
Yumin Xia2c509c22017-02-09 14:37:36 -0800600// // retrieve cert from the zone
601// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
602// {
603// std::ifstream ifs(outputFile.c_str());
604// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
605// std::istreambuf_iterator<char>());
606// BOOST_CHECK_EQUAL(actualValue, dskValue);
607// }
608// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800609
Yumin Xia2c509c22017-02-09 14:37:36 -0800610// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
611// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800612
Yumin Xia2c509c22017-02-09 14:37:36 -0800613// // output to std::cout
614// std::string acutalOutput;
615// {
616// OutputTester tester;
617// m_tool.exportCertificate(otherDsk, "-");
618// acutalOutput = tester.buffer.str();
619// }
620// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
621// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800622
Yumin Xia9d110ce2016-11-15 14:03:14 -0800623BOOST_AUTO_TEST_CASE(AddRrset)
624{
625 Name zoneName("/ndns-test");
626 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800627
Yumin Xia9d110ce2016-11-15 14:03:14 -0800628 time::seconds ttl1(4200);
629 time::seconds ttl2(4500);
630 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800631
Yumin Xia9d110ce2016-11-15 14:03:14 -0800632 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
633 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700634 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800635
Yumin Xia9d110ce2016-11-15 14:03:14 -0800636 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
637 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
638 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800639}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800640
Yumin Xia9d110ce2016-11-15 14:03:14 -0800641BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
642{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800643 Name zoneName("/ndns-test");
644 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800645
Yumin Xia9d110ce2016-11-15 14:03:14 -0800646 time::seconds ttl(4200);
647 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800648
Yumin Xia9d110ce2016-11-15 14:03:14 -0800649 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
650 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800651
Yumin Xia9d110ce2016-11-15 14:03:14 -0800652 auto checkRrset = [&zone, &zoneName, this](Name label,
653 name::Component type,
654 NdnsContentType contentType) -> void {
655 Rrset rr1 = findRrSet(zone, label, type);
656 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
657 Response response1;
658 response1.fromData(zoneName, Data(rr1.getData()));
659 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
660 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800661
Yumin Xia9d110ce2016-11-15 14:03:14 -0800662 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800663
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700664 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800665
Yumin Xia9d110ce2016-11-15 14:03:14 -0800666 //add NS NDNS_AUTH and check user-defined ttl
667 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
668 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
669 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800670
Yumin Xia9d110ce2016-11-15 14:03:14 -0800671 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
672 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800673
Yumin Xia9d110ce2016-11-15 14:03:14 -0800674 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700675 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800676 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800677
Yumin Xia9d110ce2016-11-15 14:03:14 -0800678 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
679 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
680 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
681 // check that there is no confliction
682 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800683
Yumin Xia9d110ce2016-11-15 14:03:14 -0800684 // insert a shorter NS, when there are longer NS or TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700685 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800686 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
687 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800688
Yumin Xia9d110ce2016-11-15 14:03:14 -0800689 // insert a longer NS, when there is already a shorter NS
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700690 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800691 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
692 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800693
Yumin Xia9d110ce2016-11-15 14:03:14 -0800694 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700695 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800696 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800697
Yumin Xia9d110ce2016-11-15 14:03:14 -0800698 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700699 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
700 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800701 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
702 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
703 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800704
Yumin Xia9d110ce2016-11-15 14:03:14 -0800705 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700706 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800707 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
708}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800709
Yumin Xia9d110ce2016-11-15 14:03:14 -0800710BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
711{
712 // check pre-condition
713 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800714
Yumin Xia9d110ce2016-11-15 14:03:14 -0800715 // Check: throw if zone not exist
716 std::string certPath = TEST_CERTDIR.string();
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 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800720
Yumin Xia9d110ce2016-11-15 14:03:14 -0800721 // Check: throw if certificate does not match
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800722 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800723
Yumin Xia9d110ce2016-11-15 14:03:14 -0800724 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800725 std::vector<Certificate>&& certs = getCerts(zoneName);
726 const Name& ksk = certs[0].getName();
727 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800728
Yumin Xia2c509c22017-02-09 14:37:36 -0800729 // Check: throw if it's a duplicated certificate
730 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800731}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800732
Yumin Xia9d110ce2016-11-15 14:03:14 -0800733BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
734{
735 Name parentZoneName("/ndns-test");
736 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700737 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800738
Yumin Xia9d110ce2016-11-15 14:03:14 -0800739 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
740 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800741
Yumin Xia2c509c22017-02-09 14:37:36 -0800742 Zone zone(zoneName);
743 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800744
Yumin Xia2c509c22017-02-09 14:37:36 -0800745 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800746 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800747 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800748
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800749 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800750 // Check if child zone's d-key could be inserted correctly
751 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800752}
Jiewen Tand2d21822015-03-19 15:37:03 -0700753
Yumin Xia9d110ce2016-11-15 14:03:14 -0800754BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
755{
756 //check using user provided certificate
757 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700758 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800759 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700760 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800761
Yumin Xia2c509c22017-02-09 14:37:36 -0800762 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
763 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
764 Key dsk = m_keyChain.createKey(id);
765 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800766
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800767 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800768 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
769 m_tool.createZone(zoneName, parentZoneName);
770
Yumin Xia2c509c22017-02-09 14:37:36 -0800771 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800772 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800773 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800774
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800775 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800776 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800777}
778
779BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
780{
781 //check invalid output
782 Name parentZoneName("/ndns-test");
783 Name zoneName = Name(parentZoneName).append("child-zone");
784 m_tool.createZone(zoneName, parentZoneName);
785
786 Name content = "invalid data packet";
787 std::string output = TEST_CERTDIR.string() + "/ss.cert";
788 ndn::io::save(content, output);
789
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800790 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800791}
792
793BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
794{
795 //check version control
796 time::seconds ttl(4200);
797 Name parentZoneName("/ndns-test");
798 Name zoneName = Name(parentZoneName).append("child-zone");
799 m_tool.createZone(zoneName, parentZoneName);
800
801 Name label("/label");
802 uint64_t version = 110;
803
804 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
805 rf.checkZoneKey();
806
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700807 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800808
809 m_tool.addRrset(rrset1);
810 // throw error when adding duplicated rrset with the same version
811 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
812 ndns::ManagementTool::Error);
813 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700814 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800815 // throw error when adding duplicated rrset with older version
816 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
817 ndns::ManagementTool::Error);
818
819 version++;
820 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700821 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800822 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
823
824 Zone zone(zoneName);
825 m_dbMgr.find(zone);
826 Rrset rrset;
827 rrset.setZone(&zone);
828 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700829 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800830 m_dbMgr.find(rrset);
831
832 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
833}
834
835BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
836{
837 //check input with different formats
838 Name parentZoneName("/ndns-test");
839 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800840 Zone parentZone(parentZoneName);
841
842 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800843 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700844
Yumin Xia2c509c22017-02-09 14:37:36 -0800845 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800846 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700847
Yumin Xia918343d2017-03-17 19:04:55 -0700848 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
849
Yumin Xia9d110ce2016-11-15 14:03:14 -0800850 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800851 ndn::io::save(cert, output, ndn::io::BASE64);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800852 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800853 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
854 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700855
Yumin Xia9d110ce2016-11-15 14:03:14 -0800856 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800857 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800858 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800859 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
860 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700861
Yumin Xia9d110ce2016-11-15 14:03:14 -0800862 // hex
Yumin Xia2c509c22017-02-09 14:37:36 -0800863 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800864 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800865 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
866 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700867
Yumin Xia9d110ce2016-11-15 14:03:14 -0800868 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800869 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800870 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800871 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
872 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800873 ndns::ManagementTool::Error);
874}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700875
Yumin Xia9d110ce2016-11-15 14:03:14 -0800876BOOST_AUTO_TEST_CASE(ListAllZones)
877{
Yumin Xia2c509c22017-02-09 14:37:36 -0800878 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800879 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700880
Yumin Xia2c509c22017-02-09 14:37:36 -0800881 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
882 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
883
Yumin Xia9d110ce2016-11-15 14:03:14 -0800884 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800885 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
886 "default-certificate=" + rootDsk.toUri() + "\n"
887 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
888 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700889
Yumin Xia9d110ce2016-11-15 14:03:14 -0800890 output_test_stream testOutput;
891 m_tool.listAllZones(testOutput);
892 BOOST_CHECK(testOutput.is_equal(expectedValue));
893}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700894
Yumin Xia55a7cc42017-05-14 18:43:34 -0700895// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800896
Yumin Xia55a7cc42017-05-14 18:43:34 -0700897// BOOST_AUTO_TEST_CASE(ListZone)
898// {
899// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700900
Yumin Xia55a7cc42017-05-14 18:43:34 -0700901// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
902// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700903
Yumin Xia55a7cc42017-05-14 18:43:34 -0700904// // Add NS with NDNS_RESP
905// Delegation del;
906// del.preference = 10;
907// del.name = Name("/get/link");
908// DelegationList ds = {del};
909// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
910// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700911
Yumin Xia55a7cc42017-05-14 18:43:34 -0700912// // Add NS with NDNS_AUTH
913// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
914// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700915
Yumin Xia55a7cc42017-05-14 18:43:34 -0700916// // Add TXT from file
917// std::string output = TEST_CERTDIR.string() + "/a.rrset";
918// Response re1;
919// re1.setZone("/ndns-test");
920// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
921// re1.setRrLabel("/label2");
922// re1.setRrType(label::TXT_RR_TYPE);
923// re1.setContentType(NDNS_RESP);
924// re1.setVersion(name::Component::fromVersion(654321));
925// re1.addRr("First RR");
926// re1.addRr("Second RR");
927// re1.addRr("Last RR");
928// shared_ptr<Data> data1 = re1.toData();
929// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
930// ndn::io::save(*data1, output);
931// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800932
Yumin Xia55a7cc42017-05-14 18:43:34 -0700933// // Add TXT in normal way
934// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
935// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800936
Yumin Xia55a7cc42017-05-14 18:43:34 -0700937// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800938
Yumin Xia55a7cc42017-05-14 18:43:34 -0700939// output_test_stream testOutput;
940// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800941
Yumin Xia55a7cc42017-05-14 18:43:34 -0700942// std::string expectedValue =
943// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800944
Yumin Xia55a7cc42017-05-14 18:43:34 -0700945// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
946// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
947// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800948
Yumin Xia55a7cc42017-05-14 18:43:34 -0700949// /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
950// Certificate name:
951// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
952// Validity:
953// NotBefore: 20180314T212340
954// NotAfter: 20180324T212340
955// Public key bits:
956// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
957// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
958// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
959// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
960// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
961// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
962// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
963// Signature Information:
964// Signature Type: SignatureSha256WithEcdsa
965// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800966
Yumin Xia55a7cc42017-05-14 18:43:34 -0700967// ; 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
968// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
969// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800970
Yumin Xia55a7cc42017-05-14 18:43:34 -0700971// /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
972// Certificate name:
973// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
974// Validity:
975// NotBefore: 20180314T212340
976// NotAfter: 20180324T212340
977// Public key bits:
978// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
979// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
980// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
981// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
982// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
983// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
984// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
985// Signature Information:
986// Signature Type: SignatureSha256WithEcdsa
987// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800988
Yumin Xia55a7cc42017-05-14 18:43:34 -0700989// ; 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
990// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
991// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800992
Yumin Xia55a7cc42017-05-14 18:43:34 -0700993// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
994// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800995
Yumin Xia55a7cc42017-05-14 18:43:34 -0700996// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
997// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
998// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800999
Yumin Xia55a7cc42017-05-14 18:43:34 -07001000// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1001// /label2 10 NS NDNS-Auth
1002
1003// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1004// /label2 10 TXT First RR
1005// /label2 10 TXT Second RR
1006// /label2 10 TXT Last RR
1007
1008// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1009// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1010// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1011
1012// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1013// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1014// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1015
1016// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1017// /label3 10 TXT Hello
1018// /label3 10 TXT World
1019
1020// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1021// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1022// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1023
1024// )VALUE";
1025
1026// BOOST_CHECK(testOutput.is_equal(expectedValue));
1027// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001028
Yumin Xia9d110ce2016-11-15 14:03:14 -08001029BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1030{
1031 Name zoneName("/ndns-test");
1032 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1033 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1034 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001035 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001036
Yumin Xia9d110ce2016-11-15 14:03:14 -08001037 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001038
Yumin Xia2c509c22017-02-09 14:37:36 -08001039 std::stringstream os;
1040
1041 using security::transform::base64Encode;
1042 using security::transform::streamSink;
1043 using security::transform::bufferSource;
1044
1045 bufferSource(rrset1.getData().wire(), rrset1.getData().size()) >> base64Encode() >> streamSink(os);
1046
1047 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001048
Yumin Xia9d110ce2016-11-15 14:03:14 -08001049 output_test_stream testOutput;
1050 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001051 BOOST_CHECK(testOutput.is_equal(expectedValue));
1052}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001053
Yumin Xia9d110ce2016-11-15 14:03:14 -08001054BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1055{
1056 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001057
Yumin Xia9d110ce2016-11-15 14:03:14 -08001058 m_tool.createZone(zoneName, ROOT_ZONE);
1059 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1060 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001061
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001062 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001063
Yumin Xia9d110ce2016-11-15 14:03:14 -08001064 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001065
Yumin Xia9d110ce2016-11-15 14:03:14 -08001066 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001067 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001068
Yumin Xia9d110ce2016-11-15 14:03:14 -08001069 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001070
Yumin Xia9d110ce2016-11-15 14:03:14 -08001071 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1072}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001073
1074BOOST_AUTO_TEST_SUITE_END()
1075
1076} // namespace tests
1077} // namespace ndns
1078} // namespace ndn