blob: a8a47ccc2555ad1cc384ff3c359c2c5163fcb2cd [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 Pesavento28229df2020-01-04 15:11:25 -050026#include "test-common.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080027
Yumin Xia55a7cc42017-05-14 18:43:34 -070028#include <random>
29
Jiewen Tan870b29b2014-11-17 19:09:49 -080030#include <boost/algorithm/string/replace.hpp>
Yumin Xia55a7cc42017-05-14 18:43:34 -070031#include <boost/range/adaptors.hpp>
Davide Pesavento28229df2020-01-04 15:11:25 -050032#if BOOST_VERSION >= 105900
33#include <boost/test/tools/output_test_stream.hpp>
34#else
35#include <boost/test/output_test_stream.hpp>
36#endif
Jiewen Tan870b29b2014-11-17 19:09:49 -080037
Davide Pesavento28229df2020-01-04 15:11:25 -050038#include <ndn-cxx/security/transform.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080039#include <ndn-cxx/util/io.hpp>
40#include <ndn-cxx/util/regex.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080041
42using boost::test_tools::output_test_stream;
43
Jiewen Tan870b29b2014-11-17 19:09:49 -080044namespace ndn {
45namespace ndns {
46namespace tests {
47
Jiewen Tan870b29b2014-11-17 19:09:49 -080048static const boost::filesystem::path TEST_DATABASE = TEST_CONFIG_PATH "/management_tool.db";
49static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
50static const Name FAKE_ROOT("/fake-root/123456789");
51
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080052/**
53 * @brief Recursive copy a directory using Boost Filesystem
54 *
55 * Based on from http://stackoverflow.com/q/8593608/2150331
56 */
57void
58copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
59{
60 namespace fs = boost::filesystem;
61
62 fs::create_directory(destination);
63 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
64 fs::path current(file->path());
65 if (is_directory(current)) {
66 copyDir(current, destination / current.filename());
67 }
68 else {
Eric Newberry9edaf262018-06-07 23:44:57 -070069 copy_file(current, destination / current.filename());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080070 }
71 }
72}
73
74class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080075{
76public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080077 TestHome()
78 {
79 if (std::getenv("HOME"))
80 m_origHome = std::getenv("HOME");
81
82 setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1);
83 boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/");
84 boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt");
85 copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn");
86 }
87
88 ~TestHome()
89 {
90 if (!m_origHome.empty())
91 setenv("HOME", m_origHome.c_str(), 1);
92 else
93 unsetenv("HOME");
94 }
95
96protected:
97 std::string m_origHome;
98};
99
100
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800101class ManagementToolFixture : public TestHome, public IdentityManagementFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800102{
103public:
104 class Error : public std::runtime_error
105 {
106 public:
107 explicit
108 Error(const std::string& what)
109 : std::runtime_error(what)
110 {
111 }
112 };
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);
Yumin Xia2c509c22017-02-09 14:37:36 -0800129 Identity root = addIdentity("NDNS");
130 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
143 Identity other = addIdentity("/ndns-test/NDNS");
144 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
162 ~ManagementToolFixture()
163 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800164 }
165
Yumin Xia2c509c22017-02-09 14:37:36 -0800166 std::vector<Certificate>
167 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800168 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800169 Zone zone(zoneName);
170 std::vector<Certificate> certs;
171 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
172 // ksk are always the first key
173 certs.push_back(Certificate(zoneInfo["ksk"]));
174 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800175 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800176 }
177
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800178 Rrset
179 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800180 {
181 Rrset rrset(&zone);
182 rrset.setLabel(label);
183 rrset.setType(type);
184
185 if (!m_dbMgr.find(rrset))
Yumin Xia2c509c22017-02-09 14:37:36 -0800186 BOOST_THROW_EXCEPTION(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800187 else
188 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800189 }
190
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800191 Name
192 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800193 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800194 size_t zoneNameSize = zone.getName().size();
195 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800196 }
197
Yumin Xia2c509c22017-02-09 14:37:36 -0800198 Certificate
199 findCertFromIdentity(const Name& identityName, const Name& certName)
200 {
201 Certificate rtn;
202 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
203 for (const auto& key : identity.getKeys()) {
204 for (const auto& cert : key.getCertificates()) {
205 if (cert.getName() == certName) {
206 rtn = cert;
207 return rtn;
208 }
209 }
210 }
211 BOOST_THROW_EXCEPTION(Error("Certificate not found in keyChain"));
212 return rtn;
213 }
214
215 Certificate
216 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800217 {
218 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800219 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800220 cert.wireDecode(rrset.getData());
221 return cert;
222 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800223
Yumin Xia2c509c22017-02-09 14:37:36 -0800224 Certificate
225 findDkeyFromDb(const Name& zoneName)
226 {
227 Zone zone(zoneName);
228 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
229 return Certificate(zoneInfo["dkey"]);
230 }
231
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800232 Response
233 findResponse(Zone& zone, const Name& label, const name::Component& type)
234 {
235 Rrset rrset = findRrSet(zone, label, type);
236 Data data(rrset.getData());
237 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700238 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800239 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800240 }
241
242public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800243 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800244 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800245 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800246
247 // Names of pre-created certificates
248 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
249 // constructor.
250 Name rootKsk;
251 Name rootDsk;
252 Name otherKsk;
253 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800254 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800255};
256
Yumin Xia9d110ce2016-11-15 14:03:14 -0800257BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
258
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800259// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
260// {
261// using time::seconds;
262
263// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
264// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
265// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
266// auto kskCert = m_keyChain
267// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
268// time::fromUnixTimestamp(seconds(2147483648)), {});
269// if (parentCert.empty()) {
270// m_keyChain.selfSign(*kskCert);
271// }
272// else {
273// m_keyChain.sign(*kskCert, parentCert);
274// }
275// m_keyChain.addCertificate(*kskCert);
276
277// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
278// auto dskCert = m_keyChain
279// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
280// time::fromUnixTimestamp(seconds(2147483648)), {});
281// m_keyChain.sign(*dskCert, kskCert->getName());
282// m_keyChain.addCertificate(*dskCert);
283
284// return dskCert->getName();
285// };
286
287// Name rootDsk = generateCerts(ROOT_ZONE);
288// generateCerts("/ndns-test", rootDsk);
289
290// copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
291// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
292// }
293
Yumin Xia55a7cc42017-05-14 18:43:34 -0700294BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
295{
296 // the correctness of our DoE design rely on the ordering of SQLite
297 // this unit test make sure that our label::isSmallerInLabelOrder
298 // is the same as the ordering of BLOB in SQLite
299
300 std::random_device seed;
301 std::mt19937 gen(seed());
302
303 auto genRandomString = [&] (int length) -> std::string {
304 std::string charset =
305 "0123456789"
306 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
307 "abcdefghijklmnopqrstuvwxyz";
308 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
309
310 std::string str(length, 0);
311 std::generate_n(str.begin(), length, [&] { return charset[dist(gen)];} );
312 return str;
313 };
314
315 auto genRandomLabel = [&]() -> Name {
316 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
317 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
318 Name nm;
319 size_t length = numberOfLabelsDist(gen);
320 for (size_t i = 0; i < length; i++) {
321 nm.append(genRandomString(labelSizeDist(gen)));
322 }
323 return nm;
324 };
325
326 const size_t NGENERATED_LABELS = 10;
327
328 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
329 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
330 m_keyChain, DEFAULT_CERT);
331 rrsetFactory.checkZoneKey();
332 std::vector<Rrset> rrsets;
333 std::vector<Name> names;
334 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
335 Name randomLabel = genRandomLabel();
336 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
337 VERSION_USE_UNIX_TIMESTAMP,
338 DEFAULT_CACHE_TTL,
339 {});
340
341 rrsets.push_back(randomTxt);
342 m_dbMgr.insert(randomTxt);
343 names.push_back(randomLabel);
344 }
345
346 std::sort(rrsets.begin(), rrsets.end());
347
348 using boost::adaptors::filtered;
349 using boost::adaptors::transformed;
350
351 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
352 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
353 return rrset.getType() == label::TXT_RR_TYPE;
354 });
355
356 auto extractLabel = [] (const Rrset& rr) {
357 return rr.getLabel();
358 };
359 auto expected = rrsets | transformed(extractLabel);
360 auto actual = fromDbFiltered | transformed(extractLabel);
361
362 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
363 actual.begin(), actual.end());
364}
365
Yumin Xia9d110ce2016-11-15 14:03:14 -0800366BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800367{
Yumin Xia2c509c22017-02-09 14:37:36 -0800368 // creating root_zone need a rootDkey
369 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
370 time::seconds(4600),
371 time::seconds(4600),
372 rootKsk, rootDsk), ndns::ManagementTool::Error);
373
374 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
375 time::seconds(4600),
376 time::seconds(4600),
377 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800378
379 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800380 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800381 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800382 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
383 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
384 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
385 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
386
387 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
388 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800389
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800390 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
391 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800392}
393
Yumin Xia9d110ce2016-11-15 14:03:14 -0800394BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800395{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800396 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800397 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800398
Yumin Xia2c509c22017-02-09 14:37:36 -0800399 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700400 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800401 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800402
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800403 // will generate keys automatically
404 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800405 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800406
Yumin Xia2c509c22017-02-09 14:37:36 -0800407 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800408 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800409
Yumin Xia2c509c22017-02-09 14:37:36 -0800410 const Name& ksk = certs[0].getName();
411 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800412
413 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800414 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800415 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
416 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
417 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
418
419 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
420 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800421
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800422 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800423
424 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800425 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
426}
427
Yumin Xia9d110ce2016-11-15 14:03:14 -0800428BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800429{
430 Name parentZoneName("/ndns-test");
431 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700432 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800433
434 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800435 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800436
Yumin Xia2c509c22017-02-09 14:37:36 -0800437 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800438 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800439
Yumin Xia2c509c22017-02-09 14:37:36 -0800440 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800441
442 // Check zone ttl
443 Zone zone(zoneName);
444 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
445 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
446
Yumin Xia2c509c22017-02-09 14:37:36 -0800447 // check dkey name
448 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
449 Certificate dkey = findDkeyFromDb(zoneName);
450 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
451
452 // TODO: check signing hierarchy
453
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800454 // Check dsk rrset ttl
455 Rrset rrset;
456 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
457 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
458
459 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800460 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
461 time::system_clock::TimePoint beg,end;
462 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800463
Yumin Xia2c509c22017-02-09 14:37:36 -0800464 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
465 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
466 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800467 m_tool.deleteZone(zoneName);
468}
469
Yumin Xia9d110ce2016-11-15 14:03:14 -0800470BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800471{
472 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
473 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
474
Yumin Xia2c509c22017-02-09 14:37:36 -0800475 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800476 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800477
Yumin Xia2c509c22017-02-09 14:37:36 -0800478 const Name& ksk = certs[0].getName();
479 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800480
481 m_tool.deleteZone("/net/ndnsim");
482 // identity will still exist after the zone is deleted
483
484 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
485
486 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
487
488 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
489 time::seconds(1), time::days(1), ksk, dsk));
490 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
491 m_tool.deleteZone("/net/ndnsim");
492
493 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
494 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800495
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800496 m_tool.deleteZone("/net/ndnsim");
497
498 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
499 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800500 m_tool.deleteZone("/net/ndnsim");
501
502 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
503 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800504 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800505
Yumin Xia2c509c22017-02-09 14:37:36 -0800506 Identity id = addIdentity("/net/ndnsim/NDNS");
507 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800508 BOOST_CHECK_NO_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
Yumin Xia2c509c22017-02-09 14:37:36 -0800511 id = addIdentity("/com/ndnsim/NDNS");
512 cert = id.getDefaultKey().getDefaultCertificate();
513
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
Yumin Xia2c509c22017-02-09 14:37:36 -0800518 id = addIdentity("/net/ndnsim/www/NDNS");
519 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800520 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800521 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800522 ndns::ManagementTool::Error);
523
Yumin Xia2c509c22017-02-09 14:37:36 -0800524 id = addIdentity("/net/ndnsim/NDNS");
525 cert = id.getDefaultKey().getDefaultCertificate();
526 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800527 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800528 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800529 ndns::ManagementTool::Error);
530
Yumin Xia2c509c22017-02-09 14:37:36 -0800531 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800532 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
533
534 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800535 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800536}
537
538class OutputTester
539{
540public:
541 OutputTester()
542 : savedBuf(std::clog.rdbuf())
543 {
544 std::cout.rdbuf(buffer.rdbuf());
545 }
546
547 ~OutputTester()
548 {
549 std::cout.rdbuf(savedBuf);
550 }
551
552public:
553 std::stringstream buffer;
554 std::streambuf* savedBuf;
555};
556
Yumin Xia2c509c22017-02-09 14:37:36 -0800557// BOOST_AUTO_TEST_CASE(ExportCertificate)
558// {
559// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800560
Yumin Xia2c509c22017-02-09 14:37:36 -0800561// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
562// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800563
Yumin Xia2c509c22017-02-09 14:37:36 -0800564// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
565// // doesn't check the zone, export from KeyChain directly
566// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
567// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800568
Yumin Xia2c509c22017-02-09 14:37:36 -0800569// std::string dskValue =
570// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
571// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
572// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
573// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
574// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
575// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
576// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
577// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
578// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
579// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
580// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
581// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
582// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
583// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
584// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
585// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800586
Yumin Xia2c509c22017-02-09 14:37:36 -0800587// {
588// std::ifstream ifs(outputFile.c_str());
589// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
590// std::istreambuf_iterator<char>());
591// BOOST_CHECK_EQUAL(actualValue, dskValue);
592// }
593// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800594
Yumin Xia2c509c22017-02-09 14:37:36 -0800595// // doesn't check the zone, export from KeyChain directly
596// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
597// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800598
Yumin Xia2c509c22017-02-09 14:37:36 -0800599// Name zoneName("/ndns-test");
600// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
601// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800602
Yumin Xia2c509c22017-02-09 14:37:36 -0800603// m_keyChain.deleteCertificate(otherKsk);
604// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800605
Yumin Xia2c509c22017-02-09 14:37:36 -0800606// // retrieve cert from the zone
607// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
608// {
609// std::ifstream ifs(outputFile.c_str());
610// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
611// std::istreambuf_iterator<char>());
612// BOOST_CHECK_EQUAL(actualValue, dskValue);
613// }
614// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800615
Yumin Xia2c509c22017-02-09 14:37:36 -0800616// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
617// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800618
Yumin Xia2c509c22017-02-09 14:37:36 -0800619// // output to std::cout
620// std::string acutalOutput;
621// {
622// OutputTester tester;
623// m_tool.exportCertificate(otherDsk, "-");
624// acutalOutput = tester.buffer.str();
625// }
626// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
627// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800628
Yumin Xia9d110ce2016-11-15 14:03:14 -0800629BOOST_AUTO_TEST_CASE(AddRrset)
630{
631 Name zoneName("/ndns-test");
632 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800633
Yumin Xia9d110ce2016-11-15 14:03:14 -0800634 time::seconds ttl1(4200);
635 time::seconds ttl2(4500);
636 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800637
Yumin Xia9d110ce2016-11-15 14:03:14 -0800638 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
639 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700640 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800641
Yumin Xia9d110ce2016-11-15 14:03:14 -0800642 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
643 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
644 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800645}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800646
Yumin Xia9d110ce2016-11-15 14:03:14 -0800647BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
648{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800649 Name zoneName("/ndns-test");
650 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800651
Yumin Xia9d110ce2016-11-15 14:03:14 -0800652 time::seconds ttl(4200);
653 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800654
Yumin Xia9d110ce2016-11-15 14:03:14 -0800655 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
656 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800657
Yumin Xia9d110ce2016-11-15 14:03:14 -0800658 auto checkRrset = [&zone, &zoneName, this](Name label,
659 name::Component type,
660 NdnsContentType contentType) -> void {
661 Rrset rr1 = findRrSet(zone, label, type);
662 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
663 Response response1;
664 response1.fromData(zoneName, Data(rr1.getData()));
665 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
666 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800667
Yumin Xia9d110ce2016-11-15 14:03:14 -0800668 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800669
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700670 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800671
Yumin Xia9d110ce2016-11-15 14:03:14 -0800672 //add NS NDNS_AUTH and check user-defined ttl
673 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
674 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
675 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800676
Yumin Xia9d110ce2016-11-15 14:03:14 -0800677 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
678 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800679
Yumin Xia9d110ce2016-11-15 14:03:14 -0800680 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700681 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800682 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800683
Yumin Xia9d110ce2016-11-15 14:03:14 -0800684 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
685 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
686 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
687 // check that there is no confliction
688 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800689
Yumin Xia9d110ce2016-11-15 14:03:14 -0800690 // insert a shorter NS, when there are longer NS or TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700691 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800692 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
693 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800694
Yumin Xia9d110ce2016-11-15 14:03:14 -0800695 // insert a longer NS, when there is already a shorter NS
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700696 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800697 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
698 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800699
Yumin Xia9d110ce2016-11-15 14:03:14 -0800700 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700701 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800702 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800703
Yumin Xia9d110ce2016-11-15 14:03:14 -0800704 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700705 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
706 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800707 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
708 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
709 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800710
Yumin Xia9d110ce2016-11-15 14:03:14 -0800711 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700712 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800713 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
714}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800715
Yumin Xia9d110ce2016-11-15 14:03:14 -0800716BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
717{
718 // check pre-condition
719 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800720
Yumin Xia9d110ce2016-11-15 14:03:14 -0800721 // Check: throw if zone not exist
722 std::string certPath = TEST_CERTDIR.string();
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800723 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800724
Yumin Xia9d110ce2016-11-15 14:03:14 -0800725 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800726
Yumin Xia9d110ce2016-11-15 14:03:14 -0800727 // Check: throw if certificate does not match
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800728 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800729
Yumin Xia9d110ce2016-11-15 14:03:14 -0800730 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800731 std::vector<Certificate>&& certs = getCerts(zoneName);
732 const Name& ksk = certs[0].getName();
733 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800734
Yumin Xia2c509c22017-02-09 14:37:36 -0800735 // Check: throw if it's a duplicated certificate
736 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800737}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800738
Yumin Xia9d110ce2016-11-15 14:03:14 -0800739BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
740{
741 Name parentZoneName("/ndns-test");
742 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700743 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800744
Yumin Xia9d110ce2016-11-15 14:03:14 -0800745 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
746 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800747
Yumin Xia2c509c22017-02-09 14:37:36 -0800748 Zone zone(zoneName);
749 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800750
Yumin Xia2c509c22017-02-09 14:37:36 -0800751 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800752 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800753 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800754
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800755 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800756 // Check if child zone's d-key could be inserted correctly
757 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800758}
Jiewen Tand2d21822015-03-19 15:37:03 -0700759
Yumin Xia9d110ce2016-11-15 14:03:14 -0800760BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
761{
762 //check using user provided certificate
763 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700764 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800765 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700766 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800767
Yumin Xia2c509c22017-02-09 14:37:36 -0800768 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
769 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
770 Key dsk = m_keyChain.createKey(id);
771 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800772
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800773 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800774 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
775 m_tool.createZone(zoneName, parentZoneName);
776
Yumin Xia2c509c22017-02-09 14:37:36 -0800777 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800778 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800779 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800780
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800781 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800782 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800783}
784
785BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
786{
787 //check invalid output
788 Name parentZoneName("/ndns-test");
789 Name zoneName = Name(parentZoneName).append("child-zone");
790 m_tool.createZone(zoneName, parentZoneName);
791
792 Name content = "invalid data packet";
793 std::string output = TEST_CERTDIR.string() + "/ss.cert";
794 ndn::io::save(content, output);
795
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800796 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800797}
798
799BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
800{
801 //check version control
802 time::seconds ttl(4200);
803 Name parentZoneName("/ndns-test");
804 Name zoneName = Name(parentZoneName).append("child-zone");
805 m_tool.createZone(zoneName, parentZoneName);
806
807 Name label("/label");
808 uint64_t version = 110;
809
810 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
811 rf.checkZoneKey();
812
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700813 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800814
815 m_tool.addRrset(rrset1);
816 // throw error when adding duplicated rrset with the same version
817 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
818 ndns::ManagementTool::Error);
819 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700820 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800821 // throw error when adding duplicated rrset with older version
822 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
823 ndns::ManagementTool::Error);
824
825 version++;
826 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700827 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800828 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
829
830 Zone zone(zoneName);
831 m_dbMgr.find(zone);
832 Rrset rrset;
833 rrset.setZone(&zone);
834 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700835 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800836 m_dbMgr.find(rrset);
837
838 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
839}
840
841BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
842{
843 //check input with different formats
844 Name parentZoneName("/ndns-test");
845 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800846 Zone parentZone(parentZoneName);
847
848 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800849 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700850
Yumin Xia2c509c22017-02-09 14:37:36 -0800851 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800852 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700853
Yumin Xia918343d2017-03-17 19:04:55 -0700854 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
855
Yumin Xia9d110ce2016-11-15 14:03:14 -0800856 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800857 ndn::io::save(cert, output, ndn::io::BASE64);
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::BASE64));
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 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800863 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
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::NO_ENCODING));
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 // hex
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_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800871 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
872 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700873
Yumin Xia9d110ce2016-11-15 14:03:14 -0800874 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800875 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800876 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800877 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
878 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800879 ndns::ManagementTool::Error);
880}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700881
Yumin Xia9d110ce2016-11-15 14:03:14 -0800882BOOST_AUTO_TEST_CASE(ListAllZones)
883{
Yumin Xia2c509c22017-02-09 14:37:36 -0800884 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800885 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700886
Yumin Xia2c509c22017-02-09 14:37:36 -0800887 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
888 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
889
Yumin Xia9d110ce2016-11-15 14:03:14 -0800890 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800891 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
892 "default-certificate=" + rootDsk.toUri() + "\n"
893 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
894 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700895
Yumin Xia9d110ce2016-11-15 14:03:14 -0800896 output_test_stream testOutput;
897 m_tool.listAllZones(testOutput);
898 BOOST_CHECK(testOutput.is_equal(expectedValue));
899}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700900
Yumin Xia55a7cc42017-05-14 18:43:34 -0700901// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800902
Yumin Xia55a7cc42017-05-14 18:43:34 -0700903// BOOST_AUTO_TEST_CASE(ListZone)
904// {
905// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700906
Yumin Xia55a7cc42017-05-14 18:43:34 -0700907// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
908// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700909
Yumin Xia55a7cc42017-05-14 18:43:34 -0700910// // Add NS with NDNS_RESP
911// Delegation del;
912// del.preference = 10;
913// del.name = Name("/get/link");
914// DelegationList ds = {del};
915// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
916// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700917
Yumin Xia55a7cc42017-05-14 18:43:34 -0700918// // Add NS with NDNS_AUTH
919// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
920// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700921
Yumin Xia55a7cc42017-05-14 18:43:34 -0700922// // Add TXT from file
923// std::string output = TEST_CERTDIR.string() + "/a.rrset";
924// Response re1;
925// re1.setZone("/ndns-test");
926// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
927// re1.setRrLabel("/label2");
928// re1.setRrType(label::TXT_RR_TYPE);
929// re1.setContentType(NDNS_RESP);
930// re1.setVersion(name::Component::fromVersion(654321));
931// re1.addRr("First RR");
932// re1.addRr("Second RR");
933// re1.addRr("Last RR");
934// shared_ptr<Data> data1 = re1.toData();
935// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
936// ndn::io::save(*data1, output);
937// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800938
Yumin Xia55a7cc42017-05-14 18:43:34 -0700939// // Add TXT in normal way
940// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
941// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800942
Yumin Xia55a7cc42017-05-14 18:43:34 -0700943// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800944
Yumin Xia55a7cc42017-05-14 18:43:34 -0700945// output_test_stream testOutput;
946// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800947
Yumin Xia55a7cc42017-05-14 18:43:34 -0700948// std::string expectedValue =
949// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800950
Yumin Xia55a7cc42017-05-14 18:43:34 -0700951// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
952// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
953// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800954
Yumin Xia55a7cc42017-05-14 18:43:34 -0700955// /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
956// Certificate name:
957// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
958// Validity:
959// NotBefore: 20180314T212340
960// NotAfter: 20180324T212340
961// Public key bits:
962// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
963// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
964// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
965// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
966// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
967// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
968// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
969// Signature Information:
970// Signature Type: SignatureSha256WithEcdsa
971// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800972
Yumin Xia55a7cc42017-05-14 18:43:34 -0700973// ; 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
974// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
975// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800976
Yumin Xia55a7cc42017-05-14 18:43:34 -0700977// /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
978// Certificate name:
979// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
980// Validity:
981// NotBefore: 20180314T212340
982// NotAfter: 20180324T212340
983// Public key bits:
984// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
985// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
986// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
987// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
988// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
989// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
990// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
991// Signature Information:
992// Signature Type: SignatureSha256WithEcdsa
993// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800994
Yumin Xia55a7cc42017-05-14 18:43:34 -0700995// ; 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
996// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
997// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800998
Yumin Xia55a7cc42017-05-14 18:43:34 -0700999// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1000// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001001
Yumin Xia55a7cc42017-05-14 18:43:34 -07001002// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1003// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
1004// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001005
Yumin Xia55a7cc42017-05-14 18:43:34 -07001006// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1007// /label2 10 NS NDNS-Auth
1008
1009// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1010// /label2 10 TXT First RR
1011// /label2 10 TXT Second RR
1012// /label2 10 TXT Last RR
1013
1014// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1015// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1016// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1017
1018// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1019// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1020// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1021
1022// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1023// /label3 10 TXT Hello
1024// /label3 10 TXT World
1025
1026// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1027// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1028// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1029
1030// )VALUE";
1031
1032// BOOST_CHECK(testOutput.is_equal(expectedValue));
1033// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001034
Yumin Xia9d110ce2016-11-15 14:03:14 -08001035BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1036{
1037 Name zoneName("/ndns-test");
1038 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1039 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1040 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001041 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001042
Yumin Xia9d110ce2016-11-15 14:03:14 -08001043 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001044
Yumin Xia2c509c22017-02-09 14:37:36 -08001045 std::stringstream os;
1046
1047 using security::transform::base64Encode;
1048 using security::transform::streamSink;
1049 using security::transform::bufferSource;
1050
1051 bufferSource(rrset1.getData().wire(), rrset1.getData().size()) >> base64Encode() >> streamSink(os);
1052
1053 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001054
Yumin Xia9d110ce2016-11-15 14:03:14 -08001055 output_test_stream testOutput;
1056 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001057 BOOST_CHECK(testOutput.is_equal(expectedValue));
1058}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001059
Yumin Xia9d110ce2016-11-15 14:03:14 -08001060BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1061{
1062 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001063
Yumin Xia9d110ce2016-11-15 14:03:14 -08001064 m_tool.createZone(zoneName, ROOT_ZONE);
1065 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1066 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001067
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001068 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001069
Yumin Xia9d110ce2016-11-15 14:03:14 -08001070 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001071
Yumin Xia9d110ce2016-11-15 14:03:14 -08001072 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001073 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001074
Yumin Xia9d110ce2016-11-15 14:03:14 -08001075 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001076
Yumin Xia9d110ce2016-11-15 14:03:14 -08001077 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1078}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001079
1080BOOST_AUTO_TEST_SUITE_END()
1081
1082} // namespace tests
1083} // namespace ndns
1084} // namespace ndn