blob: e4ce0c5f86545ab164de46ce529160c0428e1a93 [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/*
Yumin Xia55a7cc42017-05-14 18:43:34 -07003 * Copyright (c) 2014-2018, 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 Xia2c509c22017-02-09 14:37:36 -080021#include "test-common.hpp"
Yumin Xia9d110ce2016-11-15 14:03:14 -080022#include "daemon/rrset-factory.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include "util/cert-helper.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080024#include "ndns-enum.hpp"
25#include "ndns-label.hpp"
26#include "ndns-tlv.hpp"
27
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>
Jiewen Tan870b29b2014-11-17 19:09:49 -080032
Jiewen Tan870b29b2014-11-17 19:09:49 -080033#include <ndn-cxx/util/io.hpp>
34#include <ndn-cxx/util/regex.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080035#include <ndn-cxx/security/transform.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080036
37using boost::test_tools::output_test_stream;
38
Jiewen Tan870b29b2014-11-17 19:09:49 -080039namespace ndn {
40namespace ndns {
41namespace tests {
42
Jiewen Tan870b29b2014-11-17 19:09:49 -080043static const boost::filesystem::path TEST_DATABASE = TEST_CONFIG_PATH "/management_tool.db";
44static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs";
45static const Name FAKE_ROOT("/fake-root/123456789");
46
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080047/**
48 * @brief Recursive copy a directory using Boost Filesystem
49 *
50 * Based on from http://stackoverflow.com/q/8593608/2150331
51 */
52void
53copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination)
54{
55 namespace fs = boost::filesystem;
56
57 fs::create_directory(destination);
58 for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) {
59 fs::path current(file->path());
60 if (is_directory(current)) {
61 copyDir(current, destination / current.filename());
62 }
63 else {
Eric Newberry9edaf262018-06-07 23:44:57 -070064 copy_file(current, destination / current.filename());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080065 }
66 }
67}
68
69class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080070{
71public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080072 TestHome()
73 {
74 if (std::getenv("HOME"))
75 m_origHome = std::getenv("HOME");
76
77 setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1);
78 boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/");
79 boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt");
80 copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn");
81 }
82
83 ~TestHome()
84 {
85 if (!m_origHome.empty())
86 setenv("HOME", m_origHome.c_str(), 1);
87 else
88 unsetenv("HOME");
89 }
90
91protected:
92 std::string m_origHome;
93};
94
95
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080096class ManagementToolFixture : public TestHome, public IdentityManagementFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080097{
98public:
99 class Error : public std::runtime_error
100 {
101 public:
102 explicit
103 Error(const std::string& what)
104 : std::runtime_error(what)
105 {
106 }
107 };
108
109 class PreviousStateCleaner
110 {
111 public:
112 PreviousStateCleaner()
113 {
114 boost::filesystem::remove(TEST_DATABASE);
115 boost::filesystem::remove_all(TEST_CERTDIR);
116 }
117 };
118
Jiewen Tan870b29b2014-11-17 19:09:49 -0800119 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800120 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800121 , m_dbMgr(TEST_DATABASE.string().c_str())
122 {
123 boost::filesystem::create_directory(TEST_CERTDIR);
Yumin Xia2c509c22017-02-09 14:37:36 -0800124 Identity root = addIdentity("NDNS");
125 Key ksk = root.getDefaultKey();
126 m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName());
127 Certificate kskCert = CertHelper::createCertificate(m_keyChain, ksk, ksk, "CERT");
128 m_keyChain.addCertificate(ksk, kskCert);
129 rootKsk = kskCert.getName();
130
131 Key dsk = m_keyChain.createKey(root);
132 // replace rootDsk's default cert with ksk-signing cert
133 m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName());
134 Certificate dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT");
135 m_keyChain.addCertificate(dsk, dskCert);
136 rootDsk = dskCert.getName();
137
138 Identity other = addIdentity("/ndns-test/NDNS");
139 Key otherKskKey = other.getDefaultKey();
140 m_keyChain.deleteCertificate(otherKskKey, otherKskKey.getDefaultCertificate().getName());
141 Certificate otherKskCert = CertHelper::createCertificate(m_keyChain, otherKskKey, otherKskKey, "CERT");
142 m_keyChain.addCertificate(otherKskKey, otherKskCert);
143 otherKsk = otherKskCert.getName();
144
145 // replace rootDsk's default cert with ksk-signing cert
146 Key otherDskKey = m_keyChain.createKey(other);
147 m_keyChain.deleteCertificate(otherDskKey, otherDskKey.getDefaultCertificate().getName());
148 Certificate otherDskCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
149 m_keyChain.addCertificate(otherDskKey, otherDskCert);
150 otherDsk = otherDskCert.getName();
151
152 Certificate rootDkeyCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
153 m_keyChain.addCertificate(otherDskKey, rootDkeyCert);
154 rootDkey = rootDkeyCert.getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800155 }
156
157 ~ManagementToolFixture()
158 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800159 }
160
Yumin Xia2c509c22017-02-09 14:37:36 -0800161 std::vector<Certificate>
162 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800163 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800164 Zone zone(zoneName);
165 std::vector<Certificate> certs;
166 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
167 // ksk are always the first key
168 certs.push_back(Certificate(zoneInfo["ksk"]));
169 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800170 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800171 }
172
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800173 Rrset
174 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800175 {
176 Rrset rrset(&zone);
177 rrset.setLabel(label);
178 rrset.setType(type);
179
180 if (!m_dbMgr.find(rrset))
Yumin Xia2c509c22017-02-09 14:37:36 -0800181 BOOST_THROW_EXCEPTION(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800182 else
183 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800184 }
185
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800186 Name
187 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800188 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800189 size_t zoneNameSize = zone.getName().size();
190 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800191 }
192
Yumin Xia2c509c22017-02-09 14:37:36 -0800193 Certificate
194 findCertFromIdentity(const Name& identityName, const Name& certName)
195 {
196 Certificate rtn;
197 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
198 for (const auto& key : identity.getKeys()) {
199 for (const auto& cert : key.getCertificates()) {
200 if (cert.getName() == certName) {
201 rtn = cert;
202 return rtn;
203 }
204 }
205 }
206 BOOST_THROW_EXCEPTION(Error("Certificate not found in keyChain"));
207 return rtn;
208 }
209
210 Certificate
211 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800212 {
213 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800214 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800215 cert.wireDecode(rrset.getData());
216 return cert;
217 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800218
Yumin Xia2c509c22017-02-09 14:37:36 -0800219 Certificate
220 findDkeyFromDb(const Name& zoneName)
221 {
222 Zone zone(zoneName);
223 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
224 return Certificate(zoneInfo["dkey"]);
225 }
226
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800227 Response
228 findResponse(Zone& zone, const Name& label, const name::Component& type)
229 {
230 Rrset rrset = findRrSet(zone, label, type);
231 Data data(rrset.getData());
232 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700233 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800234 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800235 }
236
237public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800238 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800239 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800240 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800241
242 // Names of pre-created certificates
243 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
244 // constructor.
245 Name rootKsk;
246 Name rootDsk;
247 Name otherKsk;
248 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800249 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800250};
251
Yumin Xia9d110ce2016-11-15 14:03:14 -0800252BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
253
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800254// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
255// {
256// using time::seconds;
257
258// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
259// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
260// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
261// auto kskCert = m_keyChain
262// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
263// time::fromUnixTimestamp(seconds(2147483648)), {});
264// if (parentCert.empty()) {
265// m_keyChain.selfSign(*kskCert);
266// }
267// else {
268// m_keyChain.sign(*kskCert, parentCert);
269// }
270// m_keyChain.addCertificate(*kskCert);
271
272// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
273// auto dskCert = m_keyChain
274// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
275// time::fromUnixTimestamp(seconds(2147483648)), {});
276// m_keyChain.sign(*dskCert, kskCert->getName());
277// m_keyChain.addCertificate(*dskCert);
278
279// return dskCert->getName();
280// };
281
282// Name rootDsk = generateCerts(ROOT_ZONE);
283// generateCerts("/ndns-test", rootDsk);
284
285// copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
286// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
287// }
288
Yumin Xia55a7cc42017-05-14 18:43:34 -0700289BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
290{
291 // the correctness of our DoE design rely on the ordering of SQLite
292 // this unit test make sure that our label::isSmallerInLabelOrder
293 // is the same as the ordering of BLOB in SQLite
294
295 std::random_device seed;
296 std::mt19937 gen(seed());
297
298 auto genRandomString = [&] (int length) -> std::string {
299 std::string charset =
300 "0123456789"
301 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
302 "abcdefghijklmnopqrstuvwxyz";
303 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
304
305 std::string str(length, 0);
306 std::generate_n(str.begin(), length, [&] { return charset[dist(gen)];} );
307 return str;
308 };
309
310 auto genRandomLabel = [&]() -> Name {
311 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
312 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
313 Name nm;
314 size_t length = numberOfLabelsDist(gen);
315 for (size_t i = 0; i < length; i++) {
316 nm.append(genRandomString(labelSizeDist(gen)));
317 }
318 return nm;
319 };
320
321 const size_t NGENERATED_LABELS = 10;
322
323 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
324 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
325 m_keyChain, DEFAULT_CERT);
326 rrsetFactory.checkZoneKey();
327 std::vector<Rrset> rrsets;
328 std::vector<Name> names;
329 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
330 Name randomLabel = genRandomLabel();
331 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
332 VERSION_USE_UNIX_TIMESTAMP,
333 DEFAULT_CACHE_TTL,
334 {});
335
336 rrsets.push_back(randomTxt);
337 m_dbMgr.insert(randomTxt);
338 names.push_back(randomLabel);
339 }
340
341 std::sort(rrsets.begin(), rrsets.end());
342
343 using boost::adaptors::filtered;
344 using boost::adaptors::transformed;
345
346 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
347 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
348 return rrset.getType() == label::TXT_RR_TYPE;
349 });
350
351 auto extractLabel = [] (const Rrset& rr) {
352 return rr.getLabel();
353 };
354 auto expected = rrsets | transformed(extractLabel);
355 auto actual = fromDbFiltered | transformed(extractLabel);
356
357 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
358 actual.begin(), actual.end());
359}
360
Yumin Xia9d110ce2016-11-15 14:03:14 -0800361BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800362{
Yumin Xia2c509c22017-02-09 14:37:36 -0800363 // creating root_zone need a rootDkey
364 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
365 time::seconds(4600),
366 time::seconds(4600),
367 rootKsk, rootDsk), ndns::ManagementTool::Error);
368
369 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
370 time::seconds(4600),
371 time::seconds(4600),
372 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800373
374 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800375 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800376 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800377 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
378 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
379 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
380 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
381
382 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
383 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800384
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800385 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
386 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800387}
388
Yumin Xia9d110ce2016-11-15 14:03:14 -0800389BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800390{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800391 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800392 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800393
Yumin Xia2c509c22017-02-09 14:37:36 -0800394 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700395 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800396 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800397
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800398 // will generate keys automatically
399 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800400 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800401
Yumin Xia2c509c22017-02-09 14:37:36 -0800402 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800403 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800404
Yumin Xia2c509c22017-02-09 14:37:36 -0800405 const Name& ksk = certs[0].getName();
406 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800407
408 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800409 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800410 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
411 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
412 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
413
414 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
415 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800416
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800417 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800418
419 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800420 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
421}
422
Yumin Xia9d110ce2016-11-15 14:03:14 -0800423BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800424{
425 Name parentZoneName("/ndns-test");
426 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700427 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800428
429 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800430 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800431
Yumin Xia2c509c22017-02-09 14:37:36 -0800432 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800433 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800434
Yumin Xia2c509c22017-02-09 14:37:36 -0800435 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800436
437 // Check zone ttl
438 Zone zone(zoneName);
439 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
440 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
441
Yumin Xia2c509c22017-02-09 14:37:36 -0800442 // check dkey name
443 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
444 Certificate dkey = findDkeyFromDb(zoneName);
445 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
446
447 // TODO: check signing hierarchy
448
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800449 // Check dsk rrset ttl
450 Rrset rrset;
451 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
452 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
453
454 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800455 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
456 time::system_clock::TimePoint beg,end;
457 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800458
Yumin Xia2c509c22017-02-09 14:37:36 -0800459 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
460 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
461 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800462 m_tool.deleteZone(zoneName);
463}
464
Yumin Xia9d110ce2016-11-15 14:03:14 -0800465BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800466{
467 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
468 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
469
Yumin Xia2c509c22017-02-09 14:37:36 -0800470 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800471 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800472
Yumin Xia2c509c22017-02-09 14:37:36 -0800473 const Name& ksk = certs[0].getName();
474 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800475
476 m_tool.deleteZone("/net/ndnsim");
477 // identity will still exist after the zone is deleted
478
479 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
480
481 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
482
483 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
484 time::seconds(1), time::days(1), ksk, dsk));
485 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
486 m_tool.deleteZone("/net/ndnsim");
487
488 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
489 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800490
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800491 m_tool.deleteZone("/net/ndnsim");
492
493 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
494 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800495 m_tool.deleteZone("/net/ndnsim");
496
497 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
498 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800499 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800500
Yumin Xia2c509c22017-02-09 14:37:36 -0800501 Identity id = addIdentity("/net/ndnsim/NDNS");
502 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800503 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800504 time::seconds(1), time::days(1), cert.getName()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800505
Yumin Xia2c509c22017-02-09 14:37:36 -0800506 id = addIdentity("/com/ndnsim/NDNS");
507 cert = id.getDefaultKey().getDefaultCertificate();
508
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800509 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800510 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800511 ndns::ManagementTool::Error);
512
Yumin Xia2c509c22017-02-09 14:37:36 -0800513 id = addIdentity("/net/ndnsim/www/NDNS");
514 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800515 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800516 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800517 ndns::ManagementTool::Error);
518
Yumin Xia2c509c22017-02-09 14:37:36 -0800519 id = addIdentity("/net/ndnsim/NDNS");
520 cert = id.getDefaultKey().getDefaultCertificate();
521 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800522 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800523 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800524 ndns::ManagementTool::Error);
525
Yumin Xia2c509c22017-02-09 14:37:36 -0800526 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800527 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
528
529 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800530 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800531}
532
533class OutputTester
534{
535public:
536 OutputTester()
537 : savedBuf(std::clog.rdbuf())
538 {
539 std::cout.rdbuf(buffer.rdbuf());
540 }
541
542 ~OutputTester()
543 {
544 std::cout.rdbuf(savedBuf);
545 }
546
547public:
548 std::stringstream buffer;
549 std::streambuf* savedBuf;
550};
551
Yumin Xia2c509c22017-02-09 14:37:36 -0800552// BOOST_AUTO_TEST_CASE(ExportCertificate)
553// {
554// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800555
Yumin Xia2c509c22017-02-09 14:37:36 -0800556// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
557// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800558
Yumin Xia2c509c22017-02-09 14:37:36 -0800559// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
560// // doesn't check the zone, export from KeyChain directly
561// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
562// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800563
Yumin Xia2c509c22017-02-09 14:37:36 -0800564// std::string dskValue =
565// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
566// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
567// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
568// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
569// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
570// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
571// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
572// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
573// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
574// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
575// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
576// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
577// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
578// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
579// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
580// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800581
Yumin Xia2c509c22017-02-09 14:37:36 -0800582// {
583// std::ifstream ifs(outputFile.c_str());
584// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
585// std::istreambuf_iterator<char>());
586// BOOST_CHECK_EQUAL(actualValue, dskValue);
587// }
588// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800589
Yumin Xia2c509c22017-02-09 14:37:36 -0800590// // doesn't check the zone, export from KeyChain directly
591// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
592// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800593
Yumin Xia2c509c22017-02-09 14:37:36 -0800594// Name zoneName("/ndns-test");
595// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
596// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800597
Yumin Xia2c509c22017-02-09 14:37:36 -0800598// m_keyChain.deleteCertificate(otherKsk);
599// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800600
Yumin Xia2c509c22017-02-09 14:37:36 -0800601// // retrieve cert from the zone
602// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
603// {
604// std::ifstream ifs(outputFile.c_str());
605// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
606// std::istreambuf_iterator<char>());
607// BOOST_CHECK_EQUAL(actualValue, dskValue);
608// }
609// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800610
Yumin Xia2c509c22017-02-09 14:37:36 -0800611// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
612// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800613
Yumin Xia2c509c22017-02-09 14:37:36 -0800614// // output to std::cout
615// std::string acutalOutput;
616// {
617// OutputTester tester;
618// m_tool.exportCertificate(otherDsk, "-");
619// acutalOutput = tester.buffer.str();
620// }
621// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
622// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800623
Yumin Xia9d110ce2016-11-15 14:03:14 -0800624BOOST_AUTO_TEST_CASE(AddRrset)
625{
626 Name zoneName("/ndns-test");
627 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800628
Yumin Xia9d110ce2016-11-15 14:03:14 -0800629 time::seconds ttl1(4200);
630 time::seconds ttl2(4500);
631 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800632
Yumin Xia9d110ce2016-11-15 14:03:14 -0800633 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
634 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700635 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800636
Yumin Xia9d110ce2016-11-15 14:03:14 -0800637 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
638 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
639 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800640}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800641
Yumin Xia9d110ce2016-11-15 14:03:14 -0800642BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
643{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800644 Name zoneName("/ndns-test");
645 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800646
Yumin Xia9d110ce2016-11-15 14:03:14 -0800647 time::seconds ttl(4200);
648 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800649
Yumin Xia9d110ce2016-11-15 14:03:14 -0800650 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
651 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800652
Yumin Xia9d110ce2016-11-15 14:03:14 -0800653 auto checkRrset = [&zone, &zoneName, this](Name label,
654 name::Component type,
655 NdnsContentType contentType) -> void {
656 Rrset rr1 = findRrSet(zone, label, type);
657 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
658 Response response1;
659 response1.fromData(zoneName, Data(rr1.getData()));
660 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
661 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800662
Yumin Xia9d110ce2016-11-15 14:03:14 -0800663 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800664
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700665 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800666
Yumin Xia9d110ce2016-11-15 14:03:14 -0800667 //add NS NDNS_AUTH and check user-defined ttl
668 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
669 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
670 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800671
Yumin Xia9d110ce2016-11-15 14:03:14 -0800672 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
673 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800674
Yumin Xia9d110ce2016-11-15 14:03:14 -0800675 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700676 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800677 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800678
Yumin Xia9d110ce2016-11-15 14:03:14 -0800679 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
680 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
681 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
682 // check that there is no confliction
683 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800684
Yumin Xia9d110ce2016-11-15 14:03:14 -0800685 // insert a shorter NS, when there are longer NS or TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700686 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800687 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
688 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800689
Yumin Xia9d110ce2016-11-15 14:03:14 -0800690 // insert a longer NS, when there is already a shorter NS
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700691 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800692 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
693 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800694
Yumin Xia9d110ce2016-11-15 14:03:14 -0800695 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700696 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800697 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800698
Yumin Xia9d110ce2016-11-15 14:03:14 -0800699 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700700 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
701 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800702 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
703 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
704 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800705
Yumin Xia9d110ce2016-11-15 14:03:14 -0800706 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700707 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800708 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
709}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800710
Yumin Xia9d110ce2016-11-15 14:03:14 -0800711BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
712{
713 // check pre-condition
714 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800715
Yumin Xia9d110ce2016-11-15 14:03:14 -0800716 // Check: throw if zone not exist
717 std::string certPath = TEST_CERTDIR.string();
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800718 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800719
Yumin Xia9d110ce2016-11-15 14:03:14 -0800720 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800721
Yumin Xia9d110ce2016-11-15 14:03:14 -0800722 // Check: throw if certificate does not match
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 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800726 std::vector<Certificate>&& certs = getCerts(zoneName);
727 const Name& ksk = certs[0].getName();
728 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800729
Yumin Xia2c509c22017-02-09 14:37:36 -0800730 // Check: throw if it's a duplicated certificate
731 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800732}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800733
Yumin Xia9d110ce2016-11-15 14:03:14 -0800734BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
735{
736 Name parentZoneName("/ndns-test");
737 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700738 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800739
Yumin Xia9d110ce2016-11-15 14:03:14 -0800740 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
741 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800742
Yumin Xia2c509c22017-02-09 14:37:36 -0800743 Zone zone(zoneName);
744 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800745
Yumin Xia2c509c22017-02-09 14:37:36 -0800746 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800747 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800748 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800749
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800750 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800751 // Check if child zone's d-key could be inserted correctly
752 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800753}
Jiewen Tand2d21822015-03-19 15:37:03 -0700754
Yumin Xia9d110ce2016-11-15 14:03:14 -0800755BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
756{
757 //check using user provided certificate
758 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700759 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800760 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700761 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800762
Yumin Xia2c509c22017-02-09 14:37:36 -0800763 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
764 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
765 Key dsk = m_keyChain.createKey(id);
766 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800767
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800768 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800769 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
770 m_tool.createZone(zoneName, parentZoneName);
771
Yumin Xia2c509c22017-02-09 14:37:36 -0800772 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800773 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800774 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800775
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800776 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800777 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800778}
779
780BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
781{
782 //check invalid output
783 Name parentZoneName("/ndns-test");
784 Name zoneName = Name(parentZoneName).append("child-zone");
785 m_tool.createZone(zoneName, parentZoneName);
786
787 Name content = "invalid data packet";
788 std::string output = TEST_CERTDIR.string() + "/ss.cert";
789 ndn::io::save(content, output);
790
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800791 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800792}
793
794BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
795{
796 //check version control
797 time::seconds ttl(4200);
798 Name parentZoneName("/ndns-test");
799 Name zoneName = Name(parentZoneName).append("child-zone");
800 m_tool.createZone(zoneName, parentZoneName);
801
802 Name label("/label");
803 uint64_t version = 110;
804
805 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
806 rf.checkZoneKey();
807
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700808 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800809
810 m_tool.addRrset(rrset1);
811 // throw error when adding duplicated rrset with the same version
812 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
813 ndns::ManagementTool::Error);
814 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700815 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800816 // throw error when adding duplicated rrset with older version
817 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
818 ndns::ManagementTool::Error);
819
820 version++;
821 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700822 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800823 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
824
825 Zone zone(zoneName);
826 m_dbMgr.find(zone);
827 Rrset rrset;
828 rrset.setZone(&zone);
829 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700830 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800831 m_dbMgr.find(rrset);
832
833 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
834}
835
836BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
837{
838 //check input with different formats
839 Name parentZoneName("/ndns-test");
840 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800841 Zone parentZone(parentZoneName);
842
843 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800844 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700845
Yumin Xia2c509c22017-02-09 14:37:36 -0800846 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800847 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700848
Yumin Xia918343d2017-03-17 19:04:55 -0700849 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
850
Yumin Xia9d110ce2016-11-15 14:03:14 -0800851 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800852 ndn::io::save(cert, output, ndn::io::BASE64);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800853 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800854 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
855 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700856
Yumin Xia9d110ce2016-11-15 14:03:14 -0800857 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800858 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800859 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800860 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
861 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700862
Yumin Xia9d110ce2016-11-15 14:03:14 -0800863 // hex
Yumin Xia2c509c22017-02-09 14:37:36 -0800864 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800865 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800866 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
867 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700868
Yumin Xia9d110ce2016-11-15 14:03:14 -0800869 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800870 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800871 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800872 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
873 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800874 ndns::ManagementTool::Error);
875}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700876
Yumin Xia9d110ce2016-11-15 14:03:14 -0800877BOOST_AUTO_TEST_CASE(ListAllZones)
878{
Yumin Xia2c509c22017-02-09 14:37:36 -0800879 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800880 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700881
Yumin Xia2c509c22017-02-09 14:37:36 -0800882 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
883 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
884
Yumin Xia9d110ce2016-11-15 14:03:14 -0800885 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800886 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
887 "default-certificate=" + rootDsk.toUri() + "\n"
888 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
889 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700890
Yumin Xia9d110ce2016-11-15 14:03:14 -0800891 output_test_stream testOutput;
892 m_tool.listAllZones(testOutput);
893 BOOST_CHECK(testOutput.is_equal(expectedValue));
894}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700895
Yumin Xia55a7cc42017-05-14 18:43:34 -0700896// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800897
Yumin Xia55a7cc42017-05-14 18:43:34 -0700898// BOOST_AUTO_TEST_CASE(ListZone)
899// {
900// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700901
Yumin Xia55a7cc42017-05-14 18:43:34 -0700902// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
903// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700904
Yumin Xia55a7cc42017-05-14 18:43:34 -0700905// // Add NS with NDNS_RESP
906// Delegation del;
907// del.preference = 10;
908// del.name = Name("/get/link");
909// DelegationList ds = {del};
910// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
911// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700912
Yumin Xia55a7cc42017-05-14 18:43:34 -0700913// // Add NS with NDNS_AUTH
914// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
915// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700916
Yumin Xia55a7cc42017-05-14 18:43:34 -0700917// // Add TXT from file
918// std::string output = TEST_CERTDIR.string() + "/a.rrset";
919// Response re1;
920// re1.setZone("/ndns-test");
921// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
922// re1.setRrLabel("/label2");
923// re1.setRrType(label::TXT_RR_TYPE);
924// re1.setContentType(NDNS_RESP);
925// re1.setVersion(name::Component::fromVersion(654321));
926// re1.addRr("First RR");
927// re1.addRr("Second RR");
928// re1.addRr("Last RR");
929// shared_ptr<Data> data1 = re1.toData();
930// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
931// ndn::io::save(*data1, output);
932// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800933
Yumin Xia55a7cc42017-05-14 18:43:34 -0700934// // Add TXT in normal way
935// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
936// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800937
Yumin Xia55a7cc42017-05-14 18:43:34 -0700938// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800939
Yumin Xia55a7cc42017-05-14 18:43:34 -0700940// output_test_stream testOutput;
941// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800942
Yumin Xia55a7cc42017-05-14 18:43:34 -0700943// std::string expectedValue =
944// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800945
Yumin Xia55a7cc42017-05-14 18:43:34 -0700946// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
947// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
948// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800949
Yumin Xia55a7cc42017-05-14 18:43:34 -0700950// /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
951// Certificate name:
952// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
953// Validity:
954// NotBefore: 20180314T212340
955// NotAfter: 20180324T212340
956// Public key bits:
957// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
958// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
959// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
960// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
961// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
962// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
963// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
964// Signature Information:
965// Signature Type: SignatureSha256WithEcdsa
966// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800967
Yumin Xia55a7cc42017-05-14 18:43:34 -0700968// ; 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
969// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
970// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800971
Yumin Xia55a7cc42017-05-14 18:43:34 -0700972// /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
973// Certificate name:
974// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
975// Validity:
976// NotBefore: 20180314T212340
977// NotAfter: 20180324T212340
978// Public key bits:
979// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
980// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
981// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
982// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
983// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
984// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
985// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
986// Signature Information:
987// Signature Type: SignatureSha256WithEcdsa
988// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800989
Yumin Xia55a7cc42017-05-14 18:43:34 -0700990// ; 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
991// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
992// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800993
Yumin Xia55a7cc42017-05-14 18:43:34 -0700994// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
995// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800996
Yumin Xia55a7cc42017-05-14 18:43:34 -0700997// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
998// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
999// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001000
Yumin Xia55a7cc42017-05-14 18:43:34 -07001001// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1002// /label2 10 NS NDNS-Auth
1003
1004// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1005// /label2 10 TXT First RR
1006// /label2 10 TXT Second RR
1007// /label2 10 TXT Last RR
1008
1009// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1010// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1011// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1012
1013// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1014// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1015// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1016
1017// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1018// /label3 10 TXT Hello
1019// /label3 10 TXT World
1020
1021// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1022// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1023// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1024
1025// )VALUE";
1026
1027// BOOST_CHECK(testOutput.is_equal(expectedValue));
1028// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001029
Yumin Xia9d110ce2016-11-15 14:03:14 -08001030BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1031{
1032 Name zoneName("/ndns-test");
1033 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1034 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1035 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001036 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001037
Yumin Xia9d110ce2016-11-15 14:03:14 -08001038 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001039
Yumin Xia2c509c22017-02-09 14:37:36 -08001040 std::stringstream os;
1041
1042 using security::transform::base64Encode;
1043 using security::transform::streamSink;
1044 using security::transform::bufferSource;
1045
1046 bufferSource(rrset1.getData().wire(), rrset1.getData().size()) >> base64Encode() >> streamSink(os);
1047
1048 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001049
Yumin Xia9d110ce2016-11-15 14:03:14 -08001050 output_test_stream testOutput;
1051 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001052 BOOST_CHECK(testOutput.is_equal(expectedValue));
1053}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001054
Yumin Xia9d110ce2016-11-15 14:03:14 -08001055BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1056{
1057 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001058
Yumin Xia9d110ce2016-11-15 14:03:14 -08001059 m_tool.createZone(zoneName, ROOT_ZONE);
1060 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1061 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001062
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001063 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001064
Yumin Xia9d110ce2016-11-15 14:03:14 -08001065 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001066
Yumin Xia9d110ce2016-11-15 14:03:14 -08001067 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001068 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001069
Yumin Xia9d110ce2016-11-15 14:03:14 -08001070 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001071
Yumin Xia9d110ce2016-11-15 14:03:14 -08001072 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1073}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001074
1075BOOST_AUTO_TEST_SUITE_END()
1076
1077} // namespace tests
1078} // namespace ndns
1079} // namespace ndn