blob: 8f042f2b7a1c343cdb90254536a8ddba124ee883 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia9d110ce2016-11-15 14:03:14 -08003 * Copyright (c) 2014-2017, 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"
Jiewen Tan870b29b2014-11-17 19:09:49 -080022
23#include "ndns-enum.hpp"
24#include "ndns-label.hpp"
25#include "ndns-tlv.hpp"
26
Jiewen Tan870b29b2014-11-17 19:09:49 -080027#include <boost/algorithm/string/replace.hpp>
28
Jiewen Tan870b29b2014-11-17 19:09:49 -080029#include <ndn-cxx/util/io.hpp>
30#include <ndn-cxx/util/regex.hpp>
31
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080032#include "test-common.hpp"
33
34using boost::test_tools::output_test_stream;
35
Jiewen Tan870b29b2014-11-17 19:09:49 -080036namespace ndn {
37namespace ndns {
38namespace tests {
39
Jiewen Tan870b29b2014-11-17 19:09:49 -080040static const boost::filesystem::path TEST_DATABASE = TEST_CONFIG_PATH "/management_tool.db";
41static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
42static const Name FAKE_ROOT("/fake-root/123456789");
43
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080044/**
45 * @brief Recursive copy a directory using Boost Filesystem
46 *
47 * Based on from http://stackoverflow.com/q/8593608/2150331
48 */
49void
50copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
51{
52 namespace fs = boost::filesystem;
53
54 fs::create_directory(destination);
55 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
56 fs::path current(file->path());
57 if (is_directory(current)) {
58 copyDir(current, destination / current.filename());
59 }
60 else {
61 // cannot use fs::copy_file, see https://svn.boost.org/trac/boost/ticket/10038
62 // fs::copy works, as it doesn't use problematic private API
63 copy(current, destination / current.filename());
64 }
65 }
66}
67
68class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080069{
70public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080071 TestHome()
72 {
73 if (std::getenv("HOME"))
74 m_origHome = std::getenv("HOME");
75
76 setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1);
77 boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/");
78 boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt");
79 copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn");
80 }
81
82 ~TestHome()
83 {
84 if (!m_origHome.empty())
85 setenv("HOME", m_origHome.c_str(), 1);
86 else
87 unsetenv("HOME");
88 }
89
90protected:
91 std::string m_origHome;
92};
93
94
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080095class ManagementToolFixture : public TestHome, public IdentityManagementFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080096{
97public:
98 class Error : public std::runtime_error
99 {
100 public:
101 explicit
102 Error(const std::string& what)
103 : std::runtime_error(what)
104 {
105 }
106 };
107
108 class PreviousStateCleaner
109 {
110 public:
111 PreviousStateCleaner()
112 {
113 boost::filesystem::remove(TEST_DATABASE);
114 boost::filesystem::remove_all(TEST_CERTDIR);
115 }
116 };
117
Jiewen Tan870b29b2014-11-17 19:09:49 -0800118 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800119 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800120 , m_dbMgr(TEST_DATABASE.string().c_str())
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800121
122 , rootKsk("/KEY/ksk-1416974006376/ID-CERT/%FD%00%00%01I%EA%3Bx%BD")
123 , rootDsk("/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28")
124
125 , otherKsk("/ndns-test/KEY/ksk-1416974006577/ID-CERT/%FD%00%00%01I%EA%3By%7F")
126 , otherDsk("/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E")
Jiewen Tan870b29b2014-11-17 19:09:49 -0800127 {
128 boost::filesystem::create_directory(TEST_CERTDIR);
129 }
130
131 ~ManagementToolFixture()
132 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800133 }
134
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800135 std::vector<Name>
136 getKeys(const Name& identity)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800137 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800138 std::vector<Name> keys;
139 m_keyChain.getAllKeyNamesOfIdentity(identity, keys, false);
140 m_keyChain.getAllKeyNamesOfIdentity(identity, keys, true);
141 return keys;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800142 }
143
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800144 std::vector<Name>
145 getCerts(const Name& identity)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800146 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800147 std::vector<Name> certs;
148 for (auto&& name : getKeys(identity)) {
149 m_keyChain.getAllCertificateNamesOfKey(name, certs, false);
150 m_keyChain.getAllCertificateNamesOfKey(name, certs, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800151 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800152 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800153 }
154
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800155 Rrset
156 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800157 {
158 Rrset rrset(&zone);
159 rrset.setLabel(label);
160 rrset.setType(type);
161
162 if (!m_dbMgr.find(rrset))
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800163 throw Error("Record not found");
164 else
165 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800166 }
167
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800168 Name
169 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800170 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800171 size_t zoneNameSize = zone.getName().size();
172 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800173 }
174
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800175 IdentityCertificate
176 findIdCert(Zone& zone, const Name& fullName)
177 {
178 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
179 IdentityCertificate cert;
180 cert.wireDecode(rrset.getData());
181 return cert;
182 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800183
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800184 Response
185 findResponse(Zone& zone, const Name& label, const name::Component& type)
186 {
187 Rrset rrset = findRrSet(zone, label, type);
188 Data data(rrset.getData());
189 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700190 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800191 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800192 }
193
194public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800195 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800196 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800197 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800198
199 // Names of pre-created certificates
200 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
201 // constructor.
202 Name rootKsk;
203 Name rootDsk;
204 Name otherKsk;
205 Name otherDsk;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800206};
207
Yumin Xia9d110ce2016-11-15 14:03:14 -0800208BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
209
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800210// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
211// {
212// using time::seconds;
213
214// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
215// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
216// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
217// auto kskCert = m_keyChain
218// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
219// time::fromUnixTimestamp(seconds(2147483648)), {});
220// if (parentCert.empty()) {
221// m_keyChain.selfSign(*kskCert);
222// }
223// else {
224// m_keyChain.sign(*kskCert, parentCert);
225// }
226// m_keyChain.addCertificate(*kskCert);
227
228// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
229// auto dskCert = m_keyChain
230// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
231// time::fromUnixTimestamp(seconds(2147483648)), {});
232// m_keyChain.sign(*dskCert, kskCert->getName());
233// m_keyChain.addCertificate(*dskCert);
234
235// return dskCert->getName();
236// };
237
238// Name rootDsk = generateCerts(ROOT_ZONE);
239// generateCerts("/ndns-test", rootDsk);
240
241// copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
242// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
243// }
244
Yumin Xia9d110ce2016-11-15 14:03:14 -0800245BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800246{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800247 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(4600), time::seconds(4600),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800248 rootKsk, rootDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800249
250 Zone zone(ROOT_ZONE);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800251 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
252 BOOST_REQUIRE_NO_THROW(findIdCert(zone, rootDsk));
253 BOOST_CHECK_EQUAL(findIdCert(zone, rootDsk).getName(), rootDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800254
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800255 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
256 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800257}
258
Yumin Xia9d110ce2016-11-15 14:03:14 -0800259BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800260{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800261 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800262 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800263
264 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), false);
265
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800266 // will generate keys automatically
267 m_tool.createZone(zoneName, parentZoneName);
268 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800269
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800270 std::vector<Name>&& certs = getCerts(zoneName);
271 BOOST_REQUIRE_EQUAL(certs.size(), 2);
272 std::sort(certs.begin(), certs.end());
273
274 // Name& ksk = certs[0];
275 Name& dsk = certs[1];
Jiewen Tan870b29b2014-11-17 19:09:49 -0800276
277 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800278 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
279 BOOST_REQUIRE_NO_THROW(findIdCert(zone, dsk));
280 BOOST_CHECK_EQUAL(findIdCert(zone, dsk).getName(), dsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800281
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800282 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800283
284 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800285 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
286}
287
Yumin Xia9d110ce2016-11-15 14:03:14 -0800288BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800289{
290 Name parentZoneName("/ndns-test");
291 Name zoneName = Name(parentZoneName).append("child-zone");
292
293 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
294 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true);
295
296 std::vector<Name>&& certs = getCerts(zoneName);
297 BOOST_REQUIRE_EQUAL(certs.size(), 2);
298 std::sort(certs.begin(), certs.end());
299
300 // Name& ksk = certs[0];
301 Name& dsk = certs[1];
302
303 // Check zone ttl
304 Zone zone(zoneName);
305 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
306 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
307
308 // Check dsk rrset ttl
309 Rrset rrset;
310 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
311 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
312
313 // Check certificate freshnessPeriod and validity
314 IdentityCertificate cert;
315 BOOST_REQUIRE_NO_THROW(cert = findIdCert(zone, dsk));
316 BOOST_CHECK_EQUAL(cert.getMetaInfo().getFreshnessPeriod(), time::seconds(4200));
317 BOOST_CHECK_EQUAL(cert.getNotAfter() - cert.getNotBefore(), time::days(30));
318
319 m_tool.deleteZone(zoneName);
320}
321
Yumin Xia9d110ce2016-11-15 14:03:14 -0800322BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800323{
324 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
325 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
326
327 std::vector<Name>&& certs = getCerts("/net/ndnsim");
328 BOOST_REQUIRE_EQUAL(certs.size(), 2);
329 std::sort(certs.begin(), certs.end());
330
331 Name& ksk = certs[0];
332 Name& dsk = certs[1];
333
334 m_tool.deleteZone("/net/ndnsim");
335 // identity will still exist after the zone is deleted
336
337 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
338
339 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
340
341 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
342 time::seconds(1), time::days(1), ksk, dsk));
343 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
344 m_tool.deleteZone("/net/ndnsim");
345
Jiewen Tan01693fd2015-03-25 20:34:45 -0700346 // no ksk and dsk will be generated
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800347 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
348 time::seconds(1), time::days(1), Name(), dsk));
Jiewen Tan01693fd2015-03-25 20:34:45 -0700349 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800350 m_tool.deleteZone("/net/ndnsim");
351
352 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
353 time::seconds(1), time::days(1), ksk, Name()));
Jiewen Tan01693fd2015-03-25 20:34:45 -0700354 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 3);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800355 m_tool.deleteZone("/net/ndnsim");
356
357 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
358 time::seconds(1), time::days(1), "/com/ndnsim"),
359 ndns::ManagementTool::Error);
360
361 m_keyChain.deleteIdentity("/net/ndnsim");
362 Name cert = m_keyChain.createIdentity("/net/ndnsim");
363 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
364 time::seconds(1), time::days(1), cert));
365
366 cert = m_keyChain.createIdentity("/com/ndnsim");
367 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
368 time::seconds(1), time::days(1), cert),
369 ndns::ManagementTool::Error);
370
371 cert = m_keyChain.createIdentity("/net/ndnsim/www");
372 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
373 time::seconds(1), time::days(1), cert),
374 ndns::ManagementTool::Error);
375
376 cert = m_keyChain.createIdentity("/net/ndnsim");
377 m_keyChain.deleteKeyPairInTpm(m_keyChain.getCertificate(cert)->getPublicKeyName());
378 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
379 time::seconds(1), time::days(1), cert),
380 ndns::ManagementTool::Error);
381
382 // for root zone special case (requires a valid KSK to be specified)
383 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
384
385 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
386 rootKsk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800387}
388
389class OutputTester
390{
391public:
392 OutputTester()
393 : savedBuf(std::clog.rdbuf())
394 {
395 std::cout.rdbuf(buffer.rdbuf());
396 }
397
398 ~OutputTester()
399 {
400 std::cout.rdbuf(savedBuf);
401 }
402
403public:
404 std::stringstream buffer;
405 std::streambuf* savedBuf;
406};
407
Yumin Xia9d110ce2016-11-15 14:03:14 -0800408BOOST_AUTO_TEST_CASE(ExportCertificate)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800409{
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800410 std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800411
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800412 BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
413 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800414
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800415 BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
416 // doesn't check the zone, export from KeyChain directly
417 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
418 BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800419
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800420 std::string dskValue =
421 "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
422 "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
423 "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
424 "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
425 "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
426 "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
427 "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
428 "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
429 "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
430 "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
431 "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
432 "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
433 "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
434 "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
435 "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
436 "8UNqhMpjAZwW6pdvOZADVg==\n";
437
438 {
439 std::ifstream ifs(outputFile.c_str());
440 std::string actualValue((std::istreambuf_iterator<char>(ifs)),
441 std::istreambuf_iterator<char>());
442 BOOST_CHECK_EQUAL(actualValue, dskValue);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800443 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800444 boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800445
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800446 // doesn't check the zone, export from KeyChain directly
447 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
448 boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800449
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800450 Name zoneName("/ndns-test");
451 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
452 otherKsk, otherDsk);
453
454 m_keyChain.deleteCertificate(otherKsk);
455 m_keyChain.deleteCertificate(otherDsk);
456
457 // retrieve cert from the zone
458 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
459 {
460 std::ifstream ifs(outputFile.c_str());
461 std::string actualValue((std::istreambuf_iterator<char>(ifs)),
462 std::istreambuf_iterator<char>());
463 BOOST_CHECK_EQUAL(actualValue, dskValue);
464 }
465 boost::filesystem::remove(outputFile);
466
467 BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
468 ndns::ManagementTool::Error);
469
470 // output to std::cout
Jiewen Tan870b29b2014-11-17 19:09:49 -0800471 std::string acutalOutput;
472 {
473 OutputTester tester;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800474 m_tool.exportCertificate(otherDsk, "-");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800475 acutalOutput = tester.buffer.str();
476 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800477 BOOST_CHECK_EQUAL(acutalOutput, dskValue);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800478}
479
Yumin Xia9d110ce2016-11-15 14:03:14 -0800480BOOST_AUTO_TEST_CASE(AddRrset)
481{
482 Name zoneName("/ndns-test");
483 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800484
Yumin Xia9d110ce2016-11-15 14:03:14 -0800485 time::seconds ttl1(4200);
486 time::seconds ttl2(4500);
487 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800488
Yumin Xia9d110ce2016-11-15 14:03:14 -0800489 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
490 rf.checkZoneKey();
491 Rrset rrset1 = rf.generateNsRrset("/l1", label::NS_RR_TYPE, 7654, ttl2, Link::DelegationSet());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800492
Yumin Xia9d110ce2016-11-15 14:03:14 -0800493 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
494 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
495 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800496
Yumin Xia9d110ce2016-11-15 14:03:14 -0800497 Rrset rrset3 = rf.generateNsRrset("/l1/l2/l3", label::NS_RR_TYPE, 7654, ttl2, Link::DelegationSet());
498 BOOST_CHECK_THROW(m_tool.addRrset(rrset3), ndns::ManagementTool::Error);
499}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800500
Yumin Xia9d110ce2016-11-15 14:03:14 -0800501BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
502{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800503
Yumin Xia9d110ce2016-11-15 14:03:14 -0800504 Name zoneName("/ndns-test");
505 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800506
Yumin Xia9d110ce2016-11-15 14:03:14 -0800507 time::seconds ttl(4200);
508 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800509
Yumin Xia9d110ce2016-11-15 14:03:14 -0800510 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
511 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800512
Yumin Xia9d110ce2016-11-15 14:03:14 -0800513 auto checkRrset = [&zone, &zoneName, this](Name label,
514 name::Component type,
515 NdnsContentType contentType) -> void {
516 Rrset rr1 = findRrSet(zone, label, type);
517 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
518 Response response1;
519 response1.fromData(zoneName, Data(rr1.getData()));
520 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
521 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800522
Yumin Xia9d110ce2016-11-15 14:03:14 -0800523 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800524
Yumin Xia9d110ce2016-11-15 14:03:14 -0800525 Rrset rrset1 = rf.generateNsRrset(labelName, label::NS_RR_TYPE, 7654, ttl, Link::DelegationSet());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800526
Yumin Xia9d110ce2016-11-15 14:03:14 -0800527 //add NS NDNS_AUTH and check user-defined ttl
528 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
529 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
530 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800531
Yumin Xia9d110ce2016-11-15 14:03:14 -0800532 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
533 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800534
Yumin Xia9d110ce2016-11-15 14:03:14 -0800535 // insert a same-name rrset with TXT type
536 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", label::TXT_RR_TYPE, 7654, ttl, std::vector<std::string>());
537 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800538
Yumin Xia9d110ce2016-11-15 14:03:14 -0800539 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
540 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
541 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
542 // check that there is no confliction
543 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800544
Yumin Xia9d110ce2016-11-15 14:03:14 -0800545 // insert a shorter NS, when there are longer NS or TXT
546 Rrset shorterNs = rf.generateNsRrset("/l1/l2", label::NS_RR_TYPE, 7654, ttl, Link::DelegationSet());
547 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
548 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800549
Yumin Xia9d110ce2016-11-15 14:03:14 -0800550 // insert a longer NS, when there is already a shorter NS
551 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", label::NS_RR_TYPE, 7654, ttl, Link::DelegationSet());
552 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
553 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800554
Yumin Xia9d110ce2016-11-15 14:03:14 -0800555 // insert a smaller TXT, when there are longer NS and TXT
556 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", label::TXT_RR_TYPE, 7654, ttl, std::vector<std::string>());
557 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800558
Yumin Xia9d110ce2016-11-15 14:03:14 -0800559 // insert a smaller NS, when there is long TXT
560 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", label::TXT_RR_TYPE, 7654, ttl, std::vector<std::string>());
561 Rrset smallerNs = rf.generateNsRrset("/k1/k2", label::NS_RR_TYPE, 7654, ttl, Link::DelegationSet());
562 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
563 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
564 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800565
Yumin Xia9d110ce2016-11-15 14:03:14 -0800566 // inserting a longer TXT, when there is shoter TXT
567 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", label::TXT_RR_TYPE, 7654, ttl, std::vector<std::string>());
568 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
569}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800570
Yumin Xia9d110ce2016-11-15 14:03:14 -0800571BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
572{
573 // check pre-condition
574 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800575
Yumin Xia9d110ce2016-11-15 14:03:14 -0800576 // Check: throw if zone not exist
577 std::string certPath = TEST_CERTDIR.string();
578 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800579
Yumin Xia9d110ce2016-11-15 14:03:14 -0800580 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800581
Yumin Xia9d110ce2016-11-15 14:03:14 -0800582 // Check: throw if certificate does not match
583 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800584
Yumin Xia9d110ce2016-11-15 14:03:14 -0800585 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
586 m_tool.exportCertificate(otherKsk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800587
Yumin Xia9d110ce2016-11-15 14:03:14 -0800588 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, rightCertPath));
589}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800590
Yumin Xia9d110ce2016-11-15 14:03:14 -0800591BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
592{
593 Name parentZoneName("/ndns-test");
594 Name zoneName("/ndns-test/child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800595
Yumin Xia9d110ce2016-11-15 14:03:14 -0800596 Zone parentZone(parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800597
Yumin Xia9d110ce2016-11-15 14:03:14 -0800598 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
599 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800600
Yumin Xia9d110ce2016-11-15 14:03:14 -0800601 std::vector<Name>&& certs = getCerts(zoneName);
602 BOOST_REQUIRE_EQUAL(certs.size(), 2);
603 std::sort(certs.begin(), certs.end());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800604
Yumin Xia9d110ce2016-11-15 14:03:14 -0800605 Name& ksk = certs[0];
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800606
Yumin Xia9d110ce2016-11-15 14:03:14 -0800607 std::string output = TEST_CERTDIR.string() + "/ss.cert";
608 m_tool.exportCertificate(ksk, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800609
Yumin Xia9d110ce2016-11-15 14:03:14 -0800610 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output));
611 BOOST_CHECK_NO_THROW(findIdCert(parentZone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800612
Yumin Xia9d110ce2016-11-15 14:03:14 -0800613 // Add KSK ID-CERT with illegal name and convert it
614 Name iZoneName = Name(parentZoneName).append("illegal");
615 Name illegalCertName = m_keyChain.createIdentity(iZoneName);
616 m_tool.exportCertificate(illegalCertName, output);
617 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800618
Yumin Xia9d110ce2016-11-15 14:03:14 -0800619 Name legalCertName = Name(parentZoneName).append("KEY")
620 .append("illegal")
621 .append(illegalCertName.getSubName(3));
622 BOOST_CHECK_NO_THROW(findIdCert(parentZone, legalCertName));
623}
Jiewen Tand2d21822015-03-19 15:37:03 -0700624
Yumin Xia9d110ce2016-11-15 14:03:14 -0800625BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
626{
627 //check using user provided certificate
628 Name parentZoneName("/ndns-test");
629 Name zoneName("/ndns-test/child-zone");
630
631 Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
632 shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName);
633 m_keyChain.addCertificateAsKeyDefault(*dskCert);
634
635 // check addRrSet1
636 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
637 m_tool.createZone(zoneName, parentZoneName);
638
639 std::vector<Name>&& certs = getCerts(zoneName);
640 BOOST_REQUIRE_EQUAL(certs.size(), 2);
641 std::sort(certs.begin(), certs.end());
642
643 Name& ksk = certs[0];
644 // Name& dsk = certs[1];
645
646 std::string output = TEST_CERTDIR.string() + "/ss.cert";
647 m_tool.exportCertificate(ksk, output);
648
649 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output, time::seconds(4600),
650 dskCert->getName()));
651}
652
653BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
654{
655 //check invalid output
656 Name parentZoneName("/ndns-test");
657 Name zoneName = Name(parentZoneName).append("child-zone");
658 m_tool.createZone(zoneName, parentZoneName);
659
660 Name content = "invalid data packet";
661 std::string output = TEST_CERTDIR.string() + "/ss.cert";
662 ndn::io::save(content, output);
663
664 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, output), ndns::ManagementTool::Error);
665}
666
667BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
668{
669 //check version control
670 time::seconds ttl(4200);
671 Name parentZoneName("/ndns-test");
672 Name zoneName = Name(parentZoneName).append("child-zone");
673 m_tool.createZone(zoneName, parentZoneName);
674
675 Name label("/label");
676 uint64_t version = 110;
677
678 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
679 rf.checkZoneKey();
680
681 Rrset rrset1 = rf.generateTxtRrset(label, label::NS_RR_TYPE, version, ttl, {});
682
683 m_tool.addRrset(rrset1);
684 // throw error when adding duplicated rrset with the same version
685 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
686 ndns::ManagementTool::Error);
687 version--;
688 Rrset rrset2 = rf.generateTxtRrset(label, label::NS_RR_TYPE, version, ttl, {});
689 // throw error when adding duplicated rrset with older version
690 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
691 ndns::ManagementTool::Error);
692
693 version++;
694 version++;
695 Rrset rrset3 = rf.generateTxtRrset(label, label::NS_RR_TYPE, version, ttl, {});
696 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
697
698 Zone zone(zoneName);
699 m_dbMgr.find(zone);
700 Rrset rrset;
701 rrset.setZone(&zone);
702 rrset.setLabel(label);
703 rrset.setType(label::NS_RR_TYPE);
704 m_dbMgr.find(rrset);
705
706 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
707}
708
709BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
710{
711 //check input with different formats
712 Name parentZoneName("/ndns-test");
713 Name zoneName = Name(parentZoneName).append("child-zone");
714 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700715
Yumin Xia9d110ce2016-11-15 14:03:14 -0800716 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700717
Yumin Xia9d110ce2016-11-15 14:03:14 -0800718 // base64
719 Name dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
720 shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700721
Yumin Xia9d110ce2016-11-15 14:03:14 -0800722 ndn::io::save(*dskCert, output, ndn::io::BASE64);
723 BOOST_CHECK_NO_THROW(
724 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700725
Yumin Xia9d110ce2016-11-15 14:03:14 -0800726 // raw
727 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
728 dskCert = m_keyChain.selfSign(dskName);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700729
Yumin Xia9d110ce2016-11-15 14:03:14 -0800730 ndn::io::save(*dskCert, output, ndn::io::NO_ENCODING);
731 BOOST_CHECK_NO_THROW(
732 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700733
Yumin Xia9d110ce2016-11-15 14:03:14 -0800734 // hex
735 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
736 dskCert = m_keyChain.selfSign(dskName);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700737
Yumin Xia9d110ce2016-11-15 14:03:14 -0800738 ndn::io::save(*dskCert, output, ndn::io::HEX);
739 BOOST_CHECK_NO_THROW(
740 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700741
Yumin Xia9d110ce2016-11-15 14:03:14 -0800742 // incorrect encoding input
743 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
744 dskCert = m_keyChain.selfSign(dskName);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700745
Yumin Xia9d110ce2016-11-15 14:03:14 -0800746 ndn::io::save(*dskCert, output, ndn::io::HEX);
747 BOOST_CHECK_THROW(
748 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
749 static_cast<ndn::io::IoEncoding>(127)),
750 ndns::ManagementTool::Error);
751}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700752
Yumin Xia9d110ce2016-11-15 14:03:14 -0800753BOOST_AUTO_TEST_CASE(ListAllZones)
754{
755 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk);
756 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700757
Yumin Xia9d110ce2016-11-15 14:03:14 -0800758 std::string expectedValue =
759 "/ ; default-ttl=1 default-key=/dsk-1416974006466 "
760 "default-certificate=/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28\n"
761 "/ndns-test ; default-ttl=10 default-key=/ndns-test/dsk-1416974006659 "
762 "default-certificate=/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700763
Yumin Xia9d110ce2016-11-15 14:03:14 -0800764 output_test_stream testOutput;
765 m_tool.listAllZones(testOutput);
766 BOOST_CHECK(testOutput.is_equal(expectedValue));
767}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700768
Yumin Xia9d110ce2016-11-15 14:03:14 -0800769BOOST_AUTO_TEST_CASE(ListZone)
770{
771 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700772
Yumin Xia9d110ce2016-11-15 14:03:14 -0800773 RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
774 rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700775
Yumin Xia9d110ce2016-11-15 14:03:14 -0800776 // Add NS with NDNS_RESP
Jiewen Tan74d745c2015-03-20 01:40:41 -0700777
Yumin Xia9d110ce2016-11-15 14:03:14 -0800778 Link::DelegationSet ds = {std::pair<uint32_t, Name>(10,"/get/link")};
779 Rrset rrset1 = rf.generateNsRrset("/label1", label::NS_RR_TYPE, 100, DEFAULT_RR_TTL, ds);
780 m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700781
Yumin Xia9d110ce2016-11-15 14:03:14 -0800782 // Add NS with NDNS_AUTH
783 Rrset rrset2 = rf.generateAuthRrset("/label2", label::NS_RR_TYPE, 100000, DEFAULT_RR_TTL);
784 m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700785
Yumin Xia9d110ce2016-11-15 14:03:14 -0800786 // Add TXT from file
787 std::string output = TEST_CERTDIR.string() + "/a.rrset";
788 Response re1;
789 re1.setZone("/ndns-test");
790 re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
791 re1.setRrLabel("/label2");
792 re1.setRrType(label::TXT_RR_TYPE);
793 re1.setContentType(NDNS_RESP);
794 re1.setVersion(name::Component::fromVersion(654321));
795 re1.addRr("First RR");
796 re1.addRr("Second RR");
797 re1.addRr("Last RR");
798 shared_ptr<Data> data1 = re1.toData();
799 m_keyChain.sign(*data1, otherDsk);
800 ndn::io::save(*data1, output);
801 m_tool.addRrSet("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800802
Yumin Xia9d110ce2016-11-15 14:03:14 -0800803 // Add TXT in normal way
804 Rrset rrset3 = rf.generateTxtRrset("/label3", label::TXT_RR_TYPE, 3333, DEFAULT_RR_TTL, {"Hello", "World"});
805 m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800806
Yumin Xia9d110ce2016-11-15 14:03:14 -0800807 output_test_stream testOutput;
808 m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800809
Yumin Xia9d110ce2016-11-15 14:03:14 -0800810 std::string expectedValue =
811 R"VALUE(; Zone /ndns-test
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800812
Yumin Xia9d110ce2016-11-15 14:03:14 -0800813; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT
814/label1 10 NS 10,/get/link;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800815
Yumin Xia9d110ce2016-11-15 14:03:14 -0800816; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT
817/label2 10 NS NDNS-Auth
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800818
Yumin Xia9d110ce2016-11-15 14:03:14 -0800819; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT
820/label2 10 TXT First RR
821/label2 10 TXT Second RR
822/label2 10 TXT Last RR
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800823
Yumin Xia9d110ce2016-11-15 14:03:14 -0800824; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT
825/label3 10 TXT Hello
826/label3 10 TXT World
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800827
Yumin Xia9d110ce2016-11-15 14:03:14 -0800828/dsk-1416974006659 10 ID-CERT ; content-type=KEY version=%FD%00%00%01I%EA%3Bz%0E signed-by=/ndns-test/KEY/ksk-1416974006577/ID-CERT
829; Certificate name:
830; /ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E
831; Validity:
832; NotBefore: 19700101T000000
833; NotAfter: 20380119T031408
834; Subject Description:
835; 2.5.4.41: /ndns-test
836; Public key bits: (RSA)
837; MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAyBVC+xc/JpscSE/JdxbV
838; pvgrh/fokNFI/2t9D5inuIFr7cc4W+LyJ4GG1xr9olMx7MHamJU1Xg3VunjhSjL8
839; mOaeXlbS6gxWexBCtNK6U4euPB4wks/gMIKdp24mAAFb4T+mBfjcRgR+NdrjyO5C
840; 2OqM8qbDZmD/iuEmE6GPXnuMS0o6s13yzMj9YfDh3Df2jZnHESZcmG5Qpgg22T58
841; 7t7bRx8Ha2EC3hb29AeYKwgEKDx8JH8ZBJ80AQP321HbyjXWshJLomzy5SJZo9nA
842; bZOYlZPCQkomz92Zc9+kpLNQwDvtRLwkZ46B+b2JpKTFARbnvugONCEBuG6zNgoi
843; EQIB
844; Signature Information:
845; Signature Type: Unknown Signature Type
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800846
Yumin Xia9d110ce2016-11-15 14:03:14 -0800847)VALUE";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800848
Yumin Xia9d110ce2016-11-15 14:03:14 -0800849 BOOST_CHECK(testOutput.is_equal(expectedValue));
850}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800851
Yumin Xia9d110ce2016-11-15 14:03:14 -0800852BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
853{
854 Name zoneName("/ndns-test");
855 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
856 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
857 rf.checkZoneKey();
858 Rrset rrset1 = rf.generateTxtRrset("/label", label::TXT_RR_TYPE, 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -0800859
Yumin Xia9d110ce2016-11-15 14:03:14 -0800860 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800861
Yumin Xia9d110ce2016-11-15 14:03:14 -0800862 std::string expectedValue =
863 R"VALUE(Bv0BeAchCAluZG5zLXRlc3QIBE5ETlMIBWxhYmVsCANUWFQIAv1kFAgYAgQ/GQID
8646BUQvwZWYWx1ZTG/BlZhbHVlMhYzGwEBHC4HLAgJbmRucy10ZXN0CANLRVkIEWRz
865ay0xNDE2OTc0MDA2NjU5CAdJRC1DRVJUF/0BAL7Phidi+mM5cWM6alaV38qpEd+D
866kV1bHEO1BT7jsjfxW8INS7OJVUbr5ducBDTjzCp9dYjKncKv0f3hcZIX7fl9/msL
8676FuCKqrYgEZIgSD3q6DFzh04FUjrMJvqZp1D3LBh1yIKARA9TI0C6TKrlOT40iuY
868wvifmpSna7gOuh1k+qvKvx+/Y6csCw9WVLxnW12/AJdlfv3PPPnDlKkN7DozUV+s
869c7Jf+hhhZDntij+fMYBVgk0Ub/udOJrznlcZKW6C7YK57wq806kO3430gLQBEkGC
870NuOojYCk2k4Skp830cvIdy1Ld5lY1qrBZOIKR38KIy8jchP9+MEB88jvXrY=
871)VALUE";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800872
Yumin Xia9d110ce2016-11-15 14:03:14 -0800873 output_test_stream testOutput;
874 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
875 BOOST_CHECK(testOutput.check_length(expectedValue.length(), false));
876 BOOST_CHECK(testOutput.is_equal(expectedValue));
877}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800878
Yumin Xia9d110ce2016-11-15 14:03:14 -0800879BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
880{
881 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800882
Yumin Xia9d110ce2016-11-15 14:03:14 -0800883 m_tool.createZone(zoneName, ROOT_ZONE);
884 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
885 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800886
Yumin Xia9d110ce2016-11-15 14:03:14 -0800887 Rrset rrset1 = rf.generateTxtRrset("/label", label::NS_RR_TYPE, 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -0800888
Yumin Xia9d110ce2016-11-15 14:03:14 -0800889 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800890
Yumin Xia9d110ce2016-11-15 14:03:14 -0800891 Zone zone(zoneName);
892 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800893
Yumin Xia9d110ce2016-11-15 14:03:14 -0800894 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800895
Yumin Xia9d110ce2016-11-15 14:03:14 -0800896 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
897}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800898
899BOOST_AUTO_TEST_SUITE_END()
900
901} // namespace tests
902} // namespace ndns
903} // namespace ndn