blob: 459e9ebb5de7e315e6869139d0307c0fb98c9047 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, 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
20#include "../../src/mgmt/management-tool.hpp"
21
22#include "../../boost-test.hpp"
23#include <boost/test/output_test_stream.hpp>
24using boost::test_tools::output_test_stream;
25
26#include "ndns-enum.hpp"
27#include "ndns-label.hpp"
28#include "ndns-tlv.hpp"
29
30#include <vector>
31#include <iostream>
32#include <fstream>
33#include <string>
34
35#include <boost/filesystem.hpp>
36#include <boost/algorithm/string/replace.hpp>
37
38#include <ndn-cxx/security/key-chain.hpp>
39#include <ndn-cxx/security/validator.hpp>
40#include <ndn-cxx/util/io.hpp>
41#include <ndn-cxx/util/regex.hpp>
42
43namespace ndn {
44namespace ndns {
45namespace tests {
46
47BOOST_AUTO_TEST_SUITE(ManagementTool)
48
49static const boost::filesystem::path TEST_DATABASE = TEST_CONFIG_PATH "/management_tool.db";
50static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
51static const Name FAKE_ROOT("/fake-root/123456789");
52
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080053
54/**
55 * @brief Recursive copy a directory using Boost Filesystem
56 *
57 * Based on from http://stackoverflow.com/q/8593608/2150331
58 */
59void
60copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
61{
62 namespace fs = boost::filesystem;
63
64 fs::create_directory(destination);
65 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
66 fs::path current(file->path());
67 if (is_directory(current)) {
68 copyDir(current, destination / current.filename());
69 }
70 else {
71 // cannot use fs::copy_file, see https://svn.boost.org/trac/boost/ticket/10038
72 // fs::copy works, as it doesn't use problematic private API
73 copy(current, destination / current.filename());
74 }
75 }
76}
77
78class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080079{
80public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080081 TestHome()
82 {
83 if (std::getenv("HOME"))
84 m_origHome = std::getenv("HOME");
85
86 setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1);
87 boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/");
88 boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt");
89 copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn");
90 }
91
92 ~TestHome()
93 {
94 if (!m_origHome.empty())
95 setenv("HOME", m_origHome.c_str(), 1);
96 else
97 unsetenv("HOME");
98 }
99
100protected:
101 std::string m_origHome;
102};
103
104
105class ManagementToolFixture : public TestHome
106{
107public:
108 class Error : public std::runtime_error
109 {
110 public:
111 explicit
112 Error(const std::string& what)
113 : std::runtime_error(what)
114 {
115 }
116 };
117
118 class PreviousStateCleaner
119 {
120 public:
121 PreviousStateCleaner()
122 {
123 boost::filesystem::remove(TEST_DATABASE);
124 boost::filesystem::remove_all(TEST_CERTDIR);
125 }
126 };
127
Jiewen Tan870b29b2014-11-17 19:09:49 -0800128 ManagementToolFixture()
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800129 : m_keyChain("sqlite3", "file")
130 , m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800131 , m_dbMgr(TEST_DATABASE.string().c_str())
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800132
133 , rootKsk("/KEY/ksk-1416974006376/ID-CERT/%FD%00%00%01I%EA%3Bx%BD")
134 , rootDsk("/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28")
135
136 , otherKsk("/ndns-test/KEY/ksk-1416974006577/ID-CERT/%FD%00%00%01I%EA%3By%7F")
137 , otherDsk("/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E")
Jiewen Tan870b29b2014-11-17 19:09:49 -0800138 {
139 boost::filesystem::create_directory(TEST_CERTDIR);
140 }
141
142 ~ManagementToolFixture()
143 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800144 }
145
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800146 std::vector<Name>
147 getKeys(const Name& identity)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800148 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800149 std::vector<Name> keys;
150 m_keyChain.getAllKeyNamesOfIdentity(identity, keys, false);
151 m_keyChain.getAllKeyNamesOfIdentity(identity, keys, true);
152 return keys;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800153 }
154
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800155 std::vector<Name>
156 getCerts(const Name& identity)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800157 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800158 std::vector<Name> certs;
159 for (auto&& name : getKeys(identity)) {
160 m_keyChain.getAllCertificateNamesOfKey(name, certs, false);
161 m_keyChain.getAllCertificateNamesOfKey(name, certs, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800162 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800163 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800164 }
165
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800166 Rrset
167 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800168 {
169 Rrset rrset(&zone);
170 rrset.setLabel(label);
171 rrset.setType(type);
172
173 if (!m_dbMgr.find(rrset))
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800174 throw Error("Record not found");
175 else
176 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800177 }
178
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800179 Name
180 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800181 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800182 size_t zoneNameSize = zone.getName().size();
183 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800184 }
185
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800186 IdentityCertificate
187 findIdCert(Zone& zone, const Name& fullName)
188 {
189 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
190 IdentityCertificate cert;
191 cert.wireDecode(rrset.getData());
192 return cert;
193 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800194
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800195 Response
196 findResponse(Zone& zone, const Name& label, const name::Component& type)
197 {
198 Rrset rrset = findRrSet(zone, label, type);
199 Data data(rrset.getData());
200 Response resp;
201 resp.fromData("", zone.getName(), data);
202 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800203 }
204
205public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800206 PreviousStateCleaner cleaner; // must be first variable
Jiewen Tan870b29b2014-11-17 19:09:49 -0800207 ndn::KeyChain m_keyChain;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800208 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800209 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800210
211 // Names of pre-created certificates
212 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
213 // constructor.
214 Name rootKsk;
215 Name rootDsk;
216 Name otherKsk;
217 Name otherDsk;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800218};
219
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800220// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
221// {
222// using time::seconds;
223
224// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
225// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
226// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
227// auto kskCert = m_keyChain
228// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
229// time::fromUnixTimestamp(seconds(2147483648)), {});
230// if (parentCert.empty()) {
231// m_keyChain.selfSign(*kskCert);
232// }
233// else {
234// m_keyChain.sign(*kskCert, parentCert);
235// }
236// m_keyChain.addCertificate(*kskCert);
237
238// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
239// auto dskCert = m_keyChain
240// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
241// time::fromUnixTimestamp(seconds(2147483648)), {});
242// m_keyChain.sign(*dskCert, kskCert->getName());
243// m_keyChain.addCertificate(*dskCert);
244
245// return dskCert->getName();
246// };
247
248// Name rootDsk = generateCerts(ROOT_ZONE);
249// generateCerts("/ndns-test", rootDsk);
250
251// copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
252// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
253// }
254
255BOOST_FIXTURE_TEST_CASE(CreateDeleteRootZone, ManagementToolFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800256{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800257 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(4600), time::seconds(4600),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800258 rootKsk, rootDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800259
260 Zone zone(ROOT_ZONE);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800261 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
262 BOOST_REQUIRE_NO_THROW(findIdCert(zone, rootDsk));
263 BOOST_CHECK_EQUAL(findIdCert(zone, rootDsk).getName(), rootDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800264
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800265 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
266 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800267}
268
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800269BOOST_FIXTURE_TEST_CASE(CreateDeleteChildZone, ManagementToolFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800270{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800271 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800272 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800273
274 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), false);
275
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800276 // will generate keys automatically
277 m_tool.createZone(zoneName, parentZoneName);
278 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800279
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800280 std::vector<Name>&& certs = getCerts(zoneName);
281 BOOST_REQUIRE_EQUAL(certs.size(), 2);
282 std::sort(certs.begin(), certs.end());
283
284 // Name& ksk = certs[0];
285 Name& dsk = certs[1];
Jiewen Tan870b29b2014-11-17 19:09:49 -0800286
287 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800288 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
289 BOOST_REQUIRE_NO_THROW(findIdCert(zone, dsk));
290 BOOST_CHECK_EQUAL(findIdCert(zone, dsk).getName(), dsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800291
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800292 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800293
294 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800295 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
296}
297
298BOOST_FIXTURE_TEST_CASE(CreateZoneWithTtl, ManagementToolFixture)
299{
300 Name parentZoneName("/ndns-test");
301 Name zoneName = Name(parentZoneName).append("child-zone");
302
303 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
304 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true);
305
306 std::vector<Name>&& certs = getCerts(zoneName);
307 BOOST_REQUIRE_EQUAL(certs.size(), 2);
308 std::sort(certs.begin(), certs.end());
309
310 // Name& ksk = certs[0];
311 Name& dsk = certs[1];
312
313 // Check zone ttl
314 Zone zone(zoneName);
315 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
316 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
317
318 // Check dsk rrset ttl
319 Rrset rrset;
320 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
321 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
322
323 // Check certificate freshnessPeriod and validity
324 IdentityCertificate cert;
325 BOOST_REQUIRE_NO_THROW(cert = findIdCert(zone, dsk));
326 BOOST_CHECK_EQUAL(cert.getMetaInfo().getFreshnessPeriod(), time::seconds(4200));
327 BOOST_CHECK_EQUAL(cert.getNotAfter() - cert.getNotBefore(), time::days(30));
328
329 m_tool.deleteZone(zoneName);
330}
331
332BOOST_FIXTURE_TEST_CASE(ZoneCreatePreconditions, ManagementToolFixture)
333{
334 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
335 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
336
337 std::vector<Name>&& certs = getCerts("/net/ndnsim");
338 BOOST_REQUIRE_EQUAL(certs.size(), 2);
339 std::sort(certs.begin(), certs.end());
340
341 Name& ksk = certs[0];
342 Name& dsk = certs[1];
343
344 m_tool.deleteZone("/net/ndnsim");
345 // identity will still exist after the zone is deleted
346
347 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
348
349 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
350
351 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
352 time::seconds(1), time::days(1), ksk, dsk));
353 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
354 m_tool.deleteZone("/net/ndnsim");
355
Jiewen Tan01693fd2015-03-25 20:34:45 -0700356 // no ksk and dsk will be generated
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800357 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
358 time::seconds(1), time::days(1), Name(), dsk));
Jiewen Tan01693fd2015-03-25 20:34:45 -0700359 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800360 m_tool.deleteZone("/net/ndnsim");
361
362 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
363 time::seconds(1), time::days(1), ksk, Name()));
Jiewen Tan01693fd2015-03-25 20:34:45 -0700364 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 3);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800365 m_tool.deleteZone("/net/ndnsim");
366
367 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
368 time::seconds(1), time::days(1), "/com/ndnsim"),
369 ndns::ManagementTool::Error);
370
371 m_keyChain.deleteIdentity("/net/ndnsim");
372 Name cert = m_keyChain.createIdentity("/net/ndnsim");
373 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
374 time::seconds(1), time::days(1), cert));
375
376 cert = m_keyChain.createIdentity("/com/ndnsim");
377 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
378 time::seconds(1), time::days(1), cert),
379 ndns::ManagementTool::Error);
380
381 cert = m_keyChain.createIdentity("/net/ndnsim/www");
382 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
383 time::seconds(1), time::days(1), cert),
384 ndns::ManagementTool::Error);
385
386 cert = m_keyChain.createIdentity("/net/ndnsim");
387 m_keyChain.deleteKeyPairInTpm(m_keyChain.getCertificate(cert)->getPublicKeyName());
388 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
389 time::seconds(1), time::days(1), cert),
390 ndns::ManagementTool::Error);
391
392 // for root zone special case (requires a valid KSK to be specified)
393 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
394
395 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
396 rootKsk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800397}
398
399class OutputTester
400{
401public:
402 OutputTester()
403 : savedBuf(std::clog.rdbuf())
404 {
405 std::cout.rdbuf(buffer.rdbuf());
406 }
407
408 ~OutputTester()
409 {
410 std::cout.rdbuf(savedBuf);
411 }
412
413public:
414 std::stringstream buffer;
415 std::streambuf* savedBuf;
416};
417
418BOOST_FIXTURE_TEST_CASE(ExportCertificate, ManagementToolFixture)
419{
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800420 std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800421
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800422 BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
423 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800424
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800425 BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
426 // doesn't check the zone, export from KeyChain directly
427 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
428 BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800429
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800430 std::string dskValue =
431 "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
432 "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
433 "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
434 "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
435 "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
436 "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
437 "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
438 "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
439 "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
440 "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
441 "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
442 "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
443 "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
444 "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
445 "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
446 "8UNqhMpjAZwW6pdvOZADVg==\n";
447
448 {
449 std::ifstream ifs(outputFile.c_str());
450 std::string actualValue((std::istreambuf_iterator<char>(ifs)),
451 std::istreambuf_iterator<char>());
452 BOOST_CHECK_EQUAL(actualValue, dskValue);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800453 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800454 boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800455
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800456 // doesn't check the zone, export from KeyChain directly
457 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
458 boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800459
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800460 Name zoneName("/ndns-test");
461 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
462 otherKsk, otherDsk);
463
464 m_keyChain.deleteCertificate(otherKsk);
465 m_keyChain.deleteCertificate(otherDsk);
466
467 // retrieve cert from the zone
468 BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
469 {
470 std::ifstream ifs(outputFile.c_str());
471 std::string actualValue((std::istreambuf_iterator<char>(ifs)),
472 std::istreambuf_iterator<char>());
473 BOOST_CHECK_EQUAL(actualValue, dskValue);
474 }
475 boost::filesystem::remove(outputFile);
476
477 BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
478 ndns::ManagementTool::Error);
479
480 // output to std::cout
Jiewen Tan870b29b2014-11-17 19:09:49 -0800481 std::string acutalOutput;
482 {
483 OutputTester tester;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800484 m_tool.exportCertificate(otherDsk, "-");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800485 acutalOutput = tester.buffer.str();
486 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800487 BOOST_CHECK_EQUAL(acutalOutput, dskValue);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800488}
489
490BOOST_FIXTURE_TEST_CASE(AddRrSet1, ManagementToolFixture)
491{
492 // check pre-condition
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800493 BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_RESP),
Jiewen Tan870b29b2014-11-17 19:09:49 -0800494 ndns::ManagementTool::Error);
495
496 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800497 Zone zone(zoneName);
498 m_dbMgr.insert(zone);
499
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800500 BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_UNKNOWN),
Jiewen Tan870b29b2014-11-17 19:09:49 -0800501 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800502 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, "/test", label::CERT_RR_TYPE, NDNS_RAW),
Jiewen Tan870b29b2014-11-17 19:09:49 -0800503 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800504 BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_RAW),
Jiewen Tan870b29b2014-11-17 19:09:49 -0800505 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800506 BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::TXT_RR_TYPE, NDNS_RAW),
Jiewen Tan870b29b2014-11-17 19:09:49 -0800507 ndns::ManagementTool::Error);
508
509 m_dbMgr.remove(zone);
510}
511
512BOOST_FIXTURE_TEST_CASE(AddRrSet2, ManagementToolFixture)
513{
514 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800515 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800516
Jiewen Tan870b29b2014-11-17 19:09:49 -0800517 uint64_t version = 1234;
518 time::seconds ttl1(4200);
519 time::seconds ttl2(4500);
520 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
521
522 //add NS NDNS_AUTH and check user-defined ttl
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800523 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l1", label::NS_RR_TYPE, NDNS_AUTH, 7654,
Jiewen Tan870b29b2014-11-17 19:09:49 -0800524 {}, DEFAULT_CERT, ttl2));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800525 Response response;
526 BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l1", label::NS_RR_TYPE));
527 BOOST_CHECK_EQUAL(response.getNdnsType(), NDNS_AUTH);
528 BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(7654));
529 BOOST_CHECK_EQUAL(response.getFreshnessPeriod(), ttl2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800530
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800531 // add NS NDNS_RESP and check default ttl
532 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l2", label::NS_RR_TYPE, NDNS_RESP, 15));
533 BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", label::NS_RR_TYPE));
534 BOOST_CHECK_EQUAL(response.getNdnsType(), NDNS_RESP);
535 BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(15));
536 BOOST_CHECK_EQUAL(response.getFreshnessPeriod(), ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800537
538 //add TXT NDNS_RESP and check rr
539 std::string test = "oops";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800540 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l2", label::TXT_RR_TYPE, NDNS_RESP, version,
541 {"oops", "again"}));
542 BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800543
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800544 BOOST_REQUIRE_EQUAL(response.getRrs().size(), 2);
545 {
546 const Block& block = response.getRrs()[0];
547 std::string someString(reinterpret_cast<const char*>(block.value()), block.value_size());
548 BOOST_CHECK_EQUAL("oops", someString);
549 }
550
551 {
552 const Block& block = response.getRrs()[1];
553 std::string someString(reinterpret_cast<const char*>(block.value()), block.value_size());
554 BOOST_CHECK_EQUAL("again", someString);
555 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800556
557 //add user defined type
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800558 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, "/l2", name::Component("USER-DEFINED"), NDNS_RAW,
559 1112223, {"10.10.0.1", "SECOND_INVALID_DATA"}),
560 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800561
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800562 m_tool.addRrSet(zoneName, "/l2", name::Component("USER-DEFINED"), NDNS_RAW, 1112223,
563 {"10.10.0.1"});
564 BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", name::Component("USER-DEFINED")));
565 BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(1112223));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800566
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800567 std::string actualValue(reinterpret_cast<const char*>(response.getAppContent().value()),
568 response.getAppContent().value_size());
569 BOOST_CHECK_EQUAL(actualValue, "10.10.0.1");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800570}
571
572BOOST_FIXTURE_TEST_CASE(AddRrSet3, ManagementToolFixture)
573{
574 // check pre-condition
575 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800576
Jiewen Tan870b29b2014-11-17 19:09:49 -0800577 std::string certPath = TEST_CERTDIR.string();
578 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error);
579
580 m_tool.createZone(zoneName, ROOT_ZONE);
581 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800582}
583
584BOOST_FIXTURE_TEST_CASE(AddRrSet4, ManagementToolFixture)
585{
586 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800587 Name zoneName = Name(parentZoneName).append("/child-zone");
588
589 Zone parentZone(parentZoneName);
590
591 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800592 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800593
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800594 std::vector<Name>&& certs = getCerts(zoneName);
595 BOOST_REQUIRE_EQUAL(certs.size(), 2);
596 std::sort(certs.begin(), certs.end());
597
598 Name& ksk = certs[0];
599 // Name& dsk = certs[1];
600
Jiewen Tan870b29b2014-11-17 19:09:49 -0800601 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800602 m_tool.exportCertificate(ksk, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800603
Jiewen Tan870b29b2014-11-17 19:09:49 -0800604 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800605 BOOST_CHECK_NO_THROW(findIdCert(parentZone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800606
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800607 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, "/child-zone",
608 label::NS_RR_TYPE, NDNS_RESP));
609 BOOST_CHECK_NO_THROW(findRrSet(parentZone, "/child-zone", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800610
611 //add KSK ID-CERT with illegal name and convert it
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800612 Name iZoneName = Name(parentZoneName).append("illegal");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800613 Name illegalCertName = m_keyChain.createIdentity(iZoneName);
614 m_tool.exportCertificate(illegalCertName, output);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800615 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800616
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800617 Name legalCertName =
618 Name(parentZoneName)
619 .append("KEY")
620 .append("illegal")
621 .append(illegalCertName.getSubName(3));
622 BOOST_CHECK_NO_THROW(findIdCert(parentZone, legalCertName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800623}
624
625BOOST_FIXTURE_TEST_CASE(AddRrSet5, ManagementToolFixture)
626{
627 //check using user provided certificate
628 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800629 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800630
631 Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
632 shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName);
633 m_keyChain.addCertificateAsKeyDefault(*dskCert);
634
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800635 // check addRrSet1
636 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800637 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800638
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800639 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);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800648
649 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output, time::seconds(4600),
650 dskCert->getName()));
651
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800652 // check addRrSet2
Jiewen Tan870b29b2014-11-17 19:09:49 -0800653 Name label1("/net/ndnsim1");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800654 BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, "/l1", label::NS_RR_TYPE, NDNS_AUTH, -1, {},
Jiewen Tan870b29b2014-11-17 19:09:49 -0800655 dskCert->getName()));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800656}
657
Jiewen Tand2d21822015-03-19 15:37:03 -0700658BOOST_FIXTURE_TEST_CASE(AddRrSet6, ManagementToolFixture)
659{
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700660 //check invalid output
Jiewen Tand2d21822015-03-19 15:37:03 -0700661 Name parentZoneName("/ndns-test");
662 Name zoneName = Name(parentZoneName).append("child-zone");
663 m_tool.createZone(zoneName, parentZoneName);
664
Jiewen Tand2d21822015-03-19 15:37:03 -0700665 Name content = "invalid data packet";
666 std::string output = TEST_CERTDIR.string() + "/ss.cert";
667 ndn::io::save(content, output);
668
669 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, output), ndns::ManagementTool::Error);
670}
671
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700672BOOST_FIXTURE_TEST_CASE(AddRrSet7, ManagementToolFixture)
673{
674 //check version control
675 Name parentZoneName("/ndns-test");
676 Name zoneName = Name(parentZoneName).append("child-zone");
677 m_tool.createZone(zoneName, parentZoneName);
678
679 Name label("/label");
680 uint64_t version = 110;
681
682 m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version);
683 // throw error when adding duplicated rrset with the same version
684 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version),
685 ndns::ManagementTool::Error);
686 version--;
687 // throw error when adding duplicated rrset with older version
688 BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version),
689 ndns::ManagementTool::Error);
690
691 version++;
692 version++;
693 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version));
694
695 Zone zone(zoneName);
696 m_dbMgr.find(zone);
697 Rrset rrset;
698 rrset.setZone(&zone);
699 rrset.setLabel(label);
700 rrset.setType(label::NS_RR_TYPE);
701 m_dbMgr.find(rrset);
702
703 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
704}
705
Jiewen Tan74d745c2015-03-20 01:40:41 -0700706BOOST_FIXTURE_TEST_CASE(AddRrSet8, ManagementToolFixture)
707{
708 //check input with different formats
709 Name parentZoneName("/ndns-test");
710 Name zoneName = Name(parentZoneName).append("child-zone");
711 m_tool.createZone(zoneName, parentZoneName);
712
713 std::string output = TEST_CERTDIR.string() + "/a.cert";
714
715 // base64
716 Name dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
717 shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName);
718
Junxiao Shidc2df7c2016-08-31 12:53:14 +0000719 ndn::io::save(*dskCert, output, ndn::io::BASE64);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700720 BOOST_CHECK_NO_THROW(
Junxiao Shidc2df7c2016-08-31 12:53:14 +0000721 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
Jiewen Tan74d745c2015-03-20 01:40:41 -0700722
723 // raw
724 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
725 dskCert = m_keyChain.selfSign(dskName);
726
727 ndn::io::save(*dskCert, output, ndn::io::NO_ENCODING);
728 BOOST_CHECK_NO_THROW(
729 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
730
731 // hex
732 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
733 dskCert = m_keyChain.selfSign(dskName);
734
735 ndn::io::save(*dskCert, output, ndn::io::HEX);
736 BOOST_CHECK_NO_THROW(
737 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
738
739 // incorrect encoding input
740 dskName = m_keyChain.generateRsaKeyPair(zoneName, false);
741 dskCert = m_keyChain.selfSign(dskName);
742
743 ndn::io::save(*dskCert, output, ndn::io::HEX);
744 BOOST_CHECK_THROW(
745 m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
746 static_cast<ndn::io::IoEncoding>(127)),
747 ndns::ManagementTool::Error);
748}
749
Jiewen Tan870b29b2014-11-17 19:09:49 -0800750BOOST_FIXTURE_TEST_CASE(ListAllZones, ManagementToolFixture)
751{
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800752 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk);
753 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800754
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800755 std::string expectedValue =
756 "/ ; default-ttl=1 default-key=/dsk-1416974006466 "
757 "default-certificate=/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28\n"
758 "/ndns-test ; default-ttl=10 default-key=/ndns-test/dsk-1416974006659 "
759 "default-certificate=/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E\n";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800760
Jiewen Tan870b29b2014-11-17 19:09:49 -0800761 output_test_stream testOutput;
762 m_tool.listAllZones(testOutput);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800763 BOOST_CHECK(testOutput.is_equal(expectedValue));
764}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800765
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800766BOOST_FIXTURE_TEST_CASE(ListZone, ManagementToolFixture)
767{
768 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
769
770 // Add NS with NDNS_RESP
771 m_tool.addRrSet("/ndns-test", "/label1", label::NS_RR_TYPE, NDNS_RESP, 100);
772
773 // Add NS with NDNS_AUTH
774 m_tool.addRrSet("/ndns-test", "/label2", label::NS_RR_TYPE, NDNS_AUTH, 100000);
775
776 // Add TXT from file
777 std::string output = TEST_CERTDIR.string() + "/a.rrset";
778 Response re1;
779 re1.setZone("/ndns-test");
780 re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
781 re1.setRrLabel("/label2");
782 re1.setRrType(label::TXT_RR_TYPE);
783 re1.setNdnsType(NDNS_RESP);
784 re1.setVersion(name::Component::fromVersion(654321));
785 re1.addRr("First RR");
786 re1.addRr("Second RR");
787 re1.addRr("Last RR");
788 shared_ptr<Data> data1 = re1.toData();
789 m_keyChain.sign(*data1, otherDsk);
790 ndn::io::save(*data1, output);
791 m_tool.addRrSet("/ndns-test", output);
792
793 // Add TXT in normal way
794 m_tool.addRrSet("/ndns-test", "/label3", label::TXT_RR_TYPE, NDNS_RESP, 3333,
795 {"Hello", "World"}, otherDsk);
796
797 // Add User-Defined
798 Response re2;
799 re2.setZone("/ndns-test");
800 re2.setQueryType(label::NDNS_ITERATIVE_QUERY);
801 re2.setRrLabel("/label4");
802 re2.setRrType(name::Component("USER-DEFINED"));
803 re2.setNdnsType(NDNS_RAW);
804 re2.setVersion(name::Component::fromVersion(1234567));
Junxiao Shi767f35c2016-07-23 01:54:42 +0000805 re2.setAppContent(makeBinaryBlock(ndn::tlv::Content, "Hello", sizeof("Hello")));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800806 shared_ptr<Data> data2 = re2.toData();
807 m_keyChain.sign(*data2, otherDsk);
808 ndn::io::save(*data2, output);
809 m_tool.addRrSet("/ndns-test", output);
810
811 output_test_stream testOutput;
812 m_tool.listZone("/ndns-test", testOutput, true);
813
814 std::string expectedValue =
815 "; Zone /ndns-test\n"
816 "\n"
817 "; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n"
818 "/label1 10 NS \n"
819 "\n"
820 "/label2 10 NS ; content-type=NDNS-Auth version=%FD%00%01%86%A0 "
821 "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n"
822 "\n"
823 "; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 "
824 "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n"
825 "/label2 10 TXT First RR\n"
826 "/label2 10 TXT Second RR\n"
827 "/label2 10 TXT Last RR\n"
828 "\n"
829 "; rrset=/label3 type=TXT version=%FD%0D%05 "
830 "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n"
831 "/label3 10 TXT Hello\n"
832 "/label3 10 TXT World\n"
833 "\n"
834 "/label4 10 USER-DEFINED ; content-type=NDNS-Raw version=%FD%00%12%D6%87 "
835 "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n"
836 "; FQZIZWxsbwA=\n"
837 "\n"
838 "/dsk-1416974006659 10 ID-CERT ; content-type=NDNS-Raw version=%FD%00%00%01I%EA%3Bz%0E "
839 "signed-by=/ndns-test/KEY/ksk-1416974006577/ID-CERT\n"
Jiewen Tand1dd86d2015-03-20 10:26:28 -0700840 "; Certificate name:\n"
841 "; /ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E\n"
842 "; Validity:\n"
843 "; NotBefore: 19700101T000000\n"
844 "; NotAfter: 20380119T031408\n"
845 "; Subject Description:\n"
846 "; 2.5.4.41: /ndns-test\n"
847 "; Public key bits: (RSA)\n"
848 "; MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAyBVC+xc/JpscSE/JdxbV\n"
849 "; pvgrh/fokNFI/2t9D5inuIFr7cc4W+LyJ4GG1xr9olMx7MHamJU1Xg3VunjhSjL8\n"
850 "; mOaeXlbS6gxWexBCtNK6U4euPB4wks/gMIKdp24mAAFb4T+mBfjcRgR+NdrjyO5C\n"
851 "; 2OqM8qbDZmD/iuEmE6GPXnuMS0o6s13yzMj9YfDh3Df2jZnHESZcmG5Qpgg22T58\n"
852 "; 7t7bRx8Ha2EC3hb29AeYKwgEKDx8JH8ZBJ80AQP321HbyjXWshJLomzy5SJZo9nA\n"
853 "; bZOYlZPCQkomz92Zc9+kpLNQwDvtRLwkZ46B+b2JpKTFARbnvugONCEBuG6zNgoi\n"
854 "; EQIB\n"
855 "; Signature Information:\n"
856 "; Signature Type: Unknown Signature Type\n"
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800857 "\n";
858
859 BOOST_CHECK(testOutput.is_equal(expectedValue));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800860}
861
862BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
863{
864 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800865 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800866
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800867 m_tool.addRrSet(zoneName, "/label", name::Component("TXT"), NDNS_RESP, 100,
868 {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -0800869
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800870 std::string expectedValue =
871 "Bv0BdwchCAluZG5zLXRlc3QIBE5ETlMIBWxhYmVsCANUWFQIAv1kFAcZAgPotAEB\n"
872 "FRC/BlZhbHVlMb8GVmFsdWUyFjMbAQEcLgcsCAluZG5zLXRlc3QIA0tFWQgRZHNr\n"
873 "LTE0MTY5NzQwMDY2NTkIB0lELUNFUlQX/QEAqhzoQmGGeFcUlZ9pEp0ohVoTEw18\n"
874 "qfB1DN6sssOpjCu3V41SHmoybPPkdW84i/h5n6TmuuePWva+fk6l9Xppd3F4lCX0\n"
875 "Tb1lyxjnLaQjBKTYxNReM3h55XY7sCvb2RpFCSDZ/PFfzuCwdWAIqcau10H9IjNm\n"
876 "2NO+m+jW43tnou9TLrNzN80bYFQc4FEwUCJUA4jPf+1NwDUMp9LWN5IILwU+Ttx6\n"
877 "45u2rK6KXfgcHR3zifwi2IZ9mEjr5bhXxvL8zP+tgaPb0t6O9gujry0an2h+hUH5\n"
878 "Aldt49RIyipHXO7R2736ZqkUZ553DSIUhaYPuXRlByNbiW6Za+LMPgJaPg==\n";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800879
Jiewen Tan870b29b2014-11-17 19:09:49 -0800880 output_test_stream testOutput;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800881 m_tool.getRrSet(zoneName, "/label", name::Component("TXT"), testOutput);
882 BOOST_CHECK(testOutput.is_equal(expectedValue));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800883}
884
885BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
886{
887 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800888
Jiewen Tan870b29b2014-11-17 19:09:49 -0800889 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800890
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800891 BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/label", label::NS_RR_TYPE, NDNS_RESP));
892
Jiewen Tan870b29b2014-11-17 19:09:49 -0800893 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800894 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800895
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800896 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800897
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800898 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800899}
900
901BOOST_AUTO_TEST_SUITE_END()
902
903} // namespace tests
904} // namespace ndns
905} // namespace ndn