blob: 040bf380dc244893774f00d5cffa6ad95f3937b4 [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 {
64 // cannot use fs::copy_file, see https://svn.boost.org/trac/boost/ticket/10038
65 // fs::copy works, as it doesn't use problematic private API
66 copy(current, destination / current.filename());
67 }
68 }
69}
70
71class TestHome : boost::noncopyable
Jiewen Tan870b29b2014-11-17 19:09:49 -080072{
73public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080074 TestHome()
75 {
76 if (std::getenv("HOME"))
77 m_origHome = std::getenv("HOME");
78
79 setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1);
80 boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/");
81 boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt");
82 copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn");
83 }
84
85 ~TestHome()
86 {
87 if (!m_origHome.empty())
88 setenv("HOME", m_origHome.c_str(), 1);
89 else
90 unsetenv("HOME");
91 }
92
93protected:
94 std::string m_origHome;
95};
96
97
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080098class ManagementToolFixture : public TestHome, public IdentityManagementFixture
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080099{
100public:
101 class Error : public std::runtime_error
102 {
103 public:
104 explicit
105 Error(const std::string& what)
106 : std::runtime_error(what)
107 {
108 }
109 };
110
111 class PreviousStateCleaner
112 {
113 public:
114 PreviousStateCleaner()
115 {
116 boost::filesystem::remove(TEST_DATABASE);
117 boost::filesystem::remove_all(TEST_CERTDIR);
118 }
119 };
120
Jiewen Tan870b29b2014-11-17 19:09:49 -0800121 ManagementToolFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800122 : m_tool(TEST_DATABASE.string().c_str(), m_keyChain)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800123 , m_dbMgr(TEST_DATABASE.string().c_str())
124 {
125 boost::filesystem::create_directory(TEST_CERTDIR);
Yumin Xia2c509c22017-02-09 14:37:36 -0800126 Identity root = addIdentity("NDNS");
127 Key ksk = root.getDefaultKey();
128 m_keyChain.deleteCertificate(ksk, ksk.getDefaultCertificate().getName());
129 Certificate kskCert = CertHelper::createCertificate(m_keyChain, ksk, ksk, "CERT");
130 m_keyChain.addCertificate(ksk, kskCert);
131 rootKsk = kskCert.getName();
132
133 Key dsk = m_keyChain.createKey(root);
134 // replace rootDsk's default cert with ksk-signing cert
135 m_keyChain.deleteCertificate(dsk, dsk.getDefaultCertificate().getName());
136 Certificate dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT");
137 m_keyChain.addCertificate(dsk, dskCert);
138 rootDsk = dskCert.getName();
139
140 Identity other = addIdentity("/ndns-test/NDNS");
141 Key otherKskKey = other.getDefaultKey();
142 m_keyChain.deleteCertificate(otherKskKey, otherKskKey.getDefaultCertificate().getName());
143 Certificate otherKskCert = CertHelper::createCertificate(m_keyChain, otherKskKey, otherKskKey, "CERT");
144 m_keyChain.addCertificate(otherKskKey, otherKskCert);
145 otherKsk = otherKskCert.getName();
146
147 // replace rootDsk's default cert with ksk-signing cert
148 Key otherDskKey = m_keyChain.createKey(other);
149 m_keyChain.deleteCertificate(otherDskKey, otherDskKey.getDefaultCertificate().getName());
150 Certificate otherDskCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
151 m_keyChain.addCertificate(otherDskKey, otherDskCert);
152 otherDsk = otherDskCert.getName();
153
154 Certificate rootDkeyCert = CertHelper::createCertificate(m_keyChain, otherDskKey, otherKskKey, "CERT");
155 m_keyChain.addCertificate(otherDskKey, rootDkeyCert);
156 rootDkey = rootDkeyCert.getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800157 }
158
159 ~ManagementToolFixture()
160 {
Jiewen Tan870b29b2014-11-17 19:09:49 -0800161 }
162
Yumin Xia2c509c22017-02-09 14:37:36 -0800163 std::vector<Certificate>
164 getCerts(const Name& zoneName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800165 {
Yumin Xia2c509c22017-02-09 14:37:36 -0800166 Zone zone(zoneName);
167 std::vector<Certificate> certs;
168 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
169 // ksk are always the first key
170 certs.push_back(Certificate(zoneInfo["ksk"]));
171 certs.push_back(Certificate(zoneInfo["dsk"]));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800172 return certs;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800173 }
174
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800175 Rrset
176 findRrSet(Zone& zone, const Name& label, const name::Component& type)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800177 {
178 Rrset rrset(&zone);
179 rrset.setLabel(label);
180 rrset.setType(type);
181
182 if (!m_dbMgr.find(rrset))
Yumin Xia2c509c22017-02-09 14:37:36 -0800183 BOOST_THROW_EXCEPTION(Error("Record not found"));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800184 else
185 return rrset;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800186 }
187
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800188 Name
189 getLabel(const Zone& zone, const Name& fullName)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800190 {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800191 size_t zoneNameSize = zone.getName().size();
192 return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800193 }
194
Yumin Xia2c509c22017-02-09 14:37:36 -0800195 Certificate
196 findCertFromIdentity(const Name& identityName, const Name& certName)
197 {
198 Certificate rtn;
199 Identity identity = CertHelper::getIdentity(m_keyChain, identityName);
200 for (const auto& key : identity.getKeys()) {
201 for (const auto& cert : key.getCertificates()) {
202 if (cert.getName() == certName) {
203 rtn = cert;
204 return rtn;
205 }
206 }
207 }
208 BOOST_THROW_EXCEPTION(Error("Certificate not found in keyChain"));
209 return rtn;
210 }
211
212 Certificate
213 findCertFromDb(Zone& zone, const Name& fullName)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800214 {
215 Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800216 Certificate cert;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800217 cert.wireDecode(rrset.getData());
218 return cert;
219 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800220
Yumin Xia2c509c22017-02-09 14:37:36 -0800221 Certificate
222 findDkeyFromDb(const Name& zoneName)
223 {
224 Zone zone(zoneName);
225 std::map<std::string, Block> zoneInfo = m_dbMgr.getZoneInfo(zone);
226 return Certificate(zoneInfo["dkey"]);
227 }
228
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800229 Response
230 findResponse(Zone& zone, const Name& label, const name::Component& type)
231 {
232 Rrset rrset = findRrSet(zone, label, type);
233 Data data(rrset.getData());
234 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700235 resp.fromData(zone.getName(), data);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800236 return resp;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800237 }
238
239public:
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800240 PreviousStateCleaner cleaner; // must be first variable
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800241 ndns::ManagementTool m_tool;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800242 ndns::DbMgr m_dbMgr;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800243
244 // Names of pre-created certificates
245 // Uncomment and run InitPreconfiguredKeys test case and then update names in the
246 // constructor.
247 Name rootKsk;
248 Name rootDsk;
249 Name otherKsk;
250 Name otherDsk;
Yumin Xia2c509c22017-02-09 14:37:36 -0800251 Name rootDkey;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800252};
253
Yumin Xia9d110ce2016-11-15 14:03:14 -0800254BOOST_FIXTURE_TEST_SUITE(ManagementTool, ManagementToolFixture)
255
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800256// BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture)
257// {
258// using time::seconds;
259
260// auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name {
261// // to re-generate certificates, uncomment and then update rootKsk/rootDsk names
262// Name kskName = m_keyChain.generateRsaKeyPair(zone, true);
263// auto kskCert = m_keyChain
264// .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)),
265// time::fromUnixTimestamp(seconds(2147483648)), {});
266// if (parentCert.empty()) {
267// m_keyChain.selfSign(*kskCert);
268// }
269// else {
270// m_keyChain.sign(*kskCert, parentCert);
271// }
272// m_keyChain.addCertificate(*kskCert);
273
274// Name dskName = m_keyChain.generateRsaKeyPair(zone, false);
275// auto dskCert = m_keyChain
276// .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)),
277// time::fromUnixTimestamp(seconds(2147483648)), {});
278// m_keyChain.sign(*dskCert, kskCert->getName());
279// m_keyChain.addCertificate(*dskCert);
280
281// return dskCert->getName();
282// };
283
284// Name rootDsk = generateCerts(ROOT_ZONE);
285// generateCerts("/ndns-test", rootDsk);
286
287// copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn");
288// std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl;
289// }
290
Yumin Xia55a7cc42017-05-14 18:43:34 -0700291BOOST_AUTO_TEST_CASE(SqliteLabelOrder)
292{
293 // the correctness of our DoE design rely on the ordering of SQLite
294 // this unit test make sure that our label::isSmallerInLabelOrder
295 // is the same as the ordering of BLOB in SQLite
296
297 std::random_device seed;
298 std::mt19937 gen(seed());
299
300 auto genRandomString = [&] (int length) -> std::string {
301 std::string charset =
302 "0123456789"
303 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
304 "abcdefghijklmnopqrstuvwxyz";
305 std::uniform_int_distribution<size_t> dist(0, charset.size() - 1);
306
307 std::string str(length, 0);
308 std::generate_n(str.begin(), length, [&] { return charset[dist(gen)];} );
309 return str;
310 };
311
312 auto genRandomLabel = [&]() -> Name {
313 std::uniform_int_distribution<size_t> numberOfLabelsDist(1, 5);
314 std::uniform_int_distribution<size_t> labelSizeDist(1, 10);
315 Name nm;
316 size_t length = numberOfLabelsDist(gen);
317 for (size_t i = 0; i < length; i++) {
318 nm.append(genRandomString(labelSizeDist(gen)));
319 }
320 return nm;
321 };
322
323 const size_t NGENERATED_LABELS = 10;
324
325 Zone zone = m_tool.createZone("/net/ndnsim", "/net");
326 RrsetFactory rrsetFactory(TEST_DATABASE.string(), zone.getName(),
327 m_keyChain, DEFAULT_CERT);
328 rrsetFactory.checkZoneKey();
329 std::vector<Rrset> rrsets;
330 std::vector<Name> names;
331 for (size_t i = 0; i < NGENERATED_LABELS; i++) {
332 Name randomLabel = genRandomLabel();
333 Rrset randomTxt = rrsetFactory.generateTxtRrset(randomLabel,
334 VERSION_USE_UNIX_TIMESTAMP,
335 DEFAULT_CACHE_TTL,
336 {});
337
338 rrsets.push_back(randomTxt);
339 m_dbMgr.insert(randomTxt);
340 names.push_back(randomLabel);
341 }
342
343 std::sort(rrsets.begin(), rrsets.end());
344
345 using boost::adaptors::filtered;
346 using boost::adaptors::transformed;
347
348 std::vector<Rrset> rrsetsFromDb = m_dbMgr.findRrsets(zone);
349 auto fromDbFiltered = rrsetsFromDb | filtered([] (const Rrset& rrset) {
350 return rrset.getType() == label::TXT_RR_TYPE;
351 });
352
353 auto extractLabel = [] (const Rrset& rr) {
354 return rr.getLabel();
355 };
356 auto expected = rrsets | transformed(extractLabel);
357 auto actual = fromDbFiltered | transformed(extractLabel);
358
359 BOOST_CHECK_EQUAL_COLLECTIONS(expected.begin(), expected.end(),
360 actual.begin(), actual.end());
361}
362
Yumin Xia9d110ce2016-11-15 14:03:14 -0800363BOOST_AUTO_TEST_CASE(CreateDeleteRootFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800364{
Yumin Xia2c509c22017-02-09 14:37:36 -0800365 // creating root_zone need a rootDkey
366 BOOST_CHECK_THROW(m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
367 time::seconds(4600),
368 time::seconds(4600),
369 rootKsk, rootDsk), ndns::ManagementTool::Error);
370
371 m_tool.createZone(ROOT_ZONE, ROOT_ZONE,
372 time::seconds(4600),
373 time::seconds(4600),
374 rootKsk, rootDsk, rootDkey);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800375
376 Zone zone(ROOT_ZONE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800377 Name zoneIdentityName = Name(ROOT_ZONE).append("NDNS");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800378 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800379 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, rootDsk));
380 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootDsk).getName(), rootDsk);
381 BOOST_CHECK_EQUAL(findCertFromDb(zone, rootKsk).getName(), rootKsk);
382 BOOST_CHECK_EQUAL(findDkeyFromDb(ROOT_ZONE).getName(), rootDkey);
383
384 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootDsk).getName(), rootDsk);
385 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, rootKsk).getName(), rootKsk);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800386
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800387 BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE));
388 BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800389}
390
Yumin Xia9d110ce2016-11-15 14:03:14 -0800391BOOST_AUTO_TEST_CASE(CreateDeleteChildFixture)
Jiewen Tan870b29b2014-11-17 19:09:49 -0800392{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800393 Name parentZoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800394 Name zoneName = Name(parentZoneName).append("child-zone");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800395
Yumin Xia2c509c22017-02-09 14:37:36 -0800396 Zone zone1(zoneName);
Yumin Xia918343d2017-03-17 19:04:55 -0700397 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia2c509c22017-02-09 14:37:36 -0800398 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone1), false);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800399
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800400 // will generate keys automatically
401 m_tool.createZone(zoneName, parentZoneName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800402 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800403
Yumin Xia2c509c22017-02-09 14:37:36 -0800404 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800405 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800406
Yumin Xia2c509c22017-02-09 14:37:36 -0800407 const Name& ksk = certs[0].getName();
408 const Name& dsk = certs[1].getName();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800409
410 Zone zone(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800411 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800412 BOOST_REQUIRE_NO_THROW(findCertFromDb(zone, dsk));
413 BOOST_CHECK_EQUAL(findCertFromDb(zone, dsk).getName(), dsk);
414 BOOST_CHECK_EQUAL(findCertFromDb(zone, ksk).getName(), ksk);
415
416 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, dsk), findCertFromDb(zone, dsk));
417 BOOST_CHECK_EQUAL(findCertFromIdentity(zoneIdentityName, ksk), findCertFromDb(zone, ksk));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800418
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800419 BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800420
421 BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800422 BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error);
423}
424
Yumin Xia9d110ce2016-11-15 14:03:14 -0800425BOOST_AUTO_TEST_CASE(CreateZoneWithFixture)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800426{
427 Name parentZoneName("/ndns-test");
428 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700429 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800430
431 m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30));
Yumin Xia2c509c22017-02-09 14:37:36 -0800432 BOOST_CHECK_EQUAL(CertHelper::doesIdentityExist(m_keyChain, zoneIdentityName), true);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800433
Yumin Xia2c509c22017-02-09 14:37:36 -0800434 std::vector<Certificate>&& certs = getCerts(zoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800435 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800436
Yumin Xia2c509c22017-02-09 14:37:36 -0800437 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800438
439 // Check zone ttl
440 Zone zone(zoneName);
441 BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true);
442 BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200));
443
Yumin Xia2c509c22017-02-09 14:37:36 -0800444 // check dkey name
445 Name dkeyName = Name(parentZoneName).append("NDNS").append(zoneName.getSubName(parentZoneName.size()));
446 Certificate dkey = findDkeyFromDb(zoneName);
447 BOOST_CHECK(dkeyName.isPrefixOf(dkey.getName()));
448
449 // TODO: check signing hierarchy
450
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800451 // Check dsk rrset ttl
452 Rrset rrset;
453 BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE));
454 BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200));
455
456 // Check certificate freshnessPeriod and validity
Yumin Xia2c509c22017-02-09 14:37:36 -0800457 Certificate cert = CertHelper::getCertificate(m_keyChain, zoneIdentityName, dsk);
458 time::system_clock::TimePoint beg,end;
459 std::tie(beg, end) = cert.getValidityPeriod().getPeriod();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800460
Yumin Xia2c509c22017-02-09 14:37:36 -0800461 BOOST_REQUIRE_NO_THROW(cert = findCertFromDb(zone, dsk));
462 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), time::seconds(4200));
463 BOOST_CHECK_EQUAL(end - beg, time::days(30));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800464 m_tool.deleteZone(zoneName);
465}
466
Yumin Xia9d110ce2016-11-15 14:03:14 -0800467BOOST_AUTO_TEST_CASE(ZoneCreatePreconditions)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800468{
469 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net"));
470 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error);
471
Yumin Xia2c509c22017-02-09 14:37:36 -0800472 std::vector<Certificate>&& certs = getCerts("/net/ndnsim");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800473 BOOST_REQUIRE_EQUAL(certs.size(), 2);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800474
Yumin Xia2c509c22017-02-09 14:37:36 -0800475 const Name& ksk = certs[0].getName();
476 const Name& dsk = certs[1].getName();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800477
478 m_tool.deleteZone("/net/ndnsim");
479 // identity will still exist after the zone is deleted
480
481 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error);
482
483 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error);
484
485 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
486 time::seconds(1), time::days(1), ksk, dsk));
487 BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2);
488 m_tool.deleteZone("/net/ndnsim");
489
490 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
491 time::seconds(1), time::days(1), Name(), dsk));
Yumin Xia2c509c22017-02-09 14:37:36 -0800492
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800493 m_tool.deleteZone("/net/ndnsim");
494
495 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/",
496 time::seconds(1), time::days(1), ksk, Name()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800497 m_tool.deleteZone("/net/ndnsim");
498
499 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
500 time::seconds(1), time::days(1), "/com/ndnsim"),
Yumin Xia2c509c22017-02-09 14:37:36 -0800501 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800502
Yumin Xia2c509c22017-02-09 14:37:36 -0800503 Identity id = addIdentity("/net/ndnsim/NDNS");
504 Certificate cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800505 BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800506 time::seconds(1), time::days(1), cert.getName()));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800507
Yumin Xia2c509c22017-02-09 14:37:36 -0800508 id = addIdentity("/com/ndnsim/NDNS");
509 cert = id.getDefaultKey().getDefaultCertificate();
510
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800511 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800512 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800513 ndns::ManagementTool::Error);
514
Yumin Xia2c509c22017-02-09 14:37:36 -0800515 id = addIdentity("/net/ndnsim/www/NDNS");
516 cert = id.getDefaultKey().getDefaultCertificate();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800517 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800518 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800519 ndns::ManagementTool::Error);
520
Yumin Xia2c509c22017-02-09 14:37:36 -0800521 id = addIdentity("/net/ndnsim/NDNS");
522 cert = id.getDefaultKey().getDefaultCertificate();
523 m_keyChain.deleteCertificate(id.getDefaultKey(), cert.getName());
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800524 BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net",
Yumin Xia2c509c22017-02-09 14:37:36 -0800525 time::seconds(1), time::days(1), cert.getName()),
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800526 ndns::ManagementTool::Error);
527
Yumin Xia2c509c22017-02-09 14:37:36 -0800528 // for root zone special case (requires a valid DKEY to be specified)
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800529 BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error);
530
531 BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1),
Yumin Xia2c509c22017-02-09 14:37:36 -0800532 DEFAULT_CERT, DEFAULT_CERT, rootDkey));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800533}
534
535class OutputTester
536{
537public:
538 OutputTester()
539 : savedBuf(std::clog.rdbuf())
540 {
541 std::cout.rdbuf(buffer.rdbuf());
542 }
543
544 ~OutputTester()
545 {
546 std::cout.rdbuf(savedBuf);
547 }
548
549public:
550 std::stringstream buffer;
551 std::streambuf* savedBuf;
552};
553
Yumin Xia2c509c22017-02-09 14:37:36 -0800554// BOOST_AUTO_TEST_CASE(ExportCertificate)
555// {
556// std::string outputFile = TEST_CERTDIR.string() + "/ss.cert";
Jiewen Tan870b29b2014-11-17 19:09:49 -0800557
Yumin Xia2c509c22017-02-09 14:37:36 -0800558// BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile),
559// ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800560
Yumin Xia2c509c22017-02-09 14:37:36 -0800561// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false);
562// // doesn't check the zone, export from KeyChain directly
563// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
564// BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800565
Yumin Xia2c509c22017-02-09 14:37:36 -0800566// std::string dskValue =
567// "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n"
568// "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n"
569// "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n"
570// "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n"
571// "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n"
572// "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n"
573// "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n"
574// "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n"
575// "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n"
576// "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n"
577// "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n"
578// "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n"
579// "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n"
580// "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n"
581// "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n"
582// "8UNqhMpjAZwW6pdvOZADVg==\n";
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800583
Yumin Xia2c509c22017-02-09 14:37:36 -0800584// {
585// std::ifstream ifs(outputFile.c_str());
586// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
587// std::istreambuf_iterator<char>());
588// BOOST_CHECK_EQUAL(actualValue, dskValue);
589// }
590// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800591
Yumin Xia2c509c22017-02-09 14:37:36 -0800592// // doesn't check the zone, export from KeyChain directly
593// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile));
594// boost::filesystem::remove(outputFile);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800595
Yumin Xia2c509c22017-02-09 14:37:36 -0800596// Name zoneName("/ndns-test");
597// m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30),
598// otherKsk, otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800599
Yumin Xia2c509c22017-02-09 14:37:36 -0800600// m_keyChain.deleteCertificate(otherKsk);
601// m_keyChain.deleteCertificate(otherDsk);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800602
Yumin Xia2c509c22017-02-09 14:37:36 -0800603// // retrieve cert from the zone
604// BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile));
605// {
606// std::ifstream ifs(outputFile.c_str());
607// std::string actualValue((std::istreambuf_iterator<char>(ifs)),
608// std::istreambuf_iterator<char>());
609// BOOST_CHECK_EQUAL(actualValue, dskValue);
610// }
611// boost::filesystem::remove(outputFile);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800612
Yumin Xia2c509c22017-02-09 14:37:36 -0800613// BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile),
614// ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800615
Yumin Xia2c509c22017-02-09 14:37:36 -0800616// // output to std::cout
617// std::string acutalOutput;
618// {
619// OutputTester tester;
620// m_tool.exportCertificate(otherDsk, "-");
621// acutalOutput = tester.buffer.str();
622// }
623// BOOST_CHECK_EQUAL(acutalOutput, dskValue);
624// }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800625
Yumin Xia9d110ce2016-11-15 14:03:14 -0800626BOOST_AUTO_TEST_CASE(AddRrset)
627{
628 Name zoneName("/ndns-test");
629 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800630
Yumin Xia9d110ce2016-11-15 14:03:14 -0800631 time::seconds ttl1(4200);
632 time::seconds ttl2(4500);
633 m_tool.createZone(zoneName, ROOT_ZONE, ttl1);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800634
Yumin Xia9d110ce2016-11-15 14:03:14 -0800635 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
636 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700637 Rrset rrset1 = rf.generateNsRrset("/l1", 7654, ttl2, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800638
Yumin Xia9d110ce2016-11-15 14:03:14 -0800639 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
640 Rrset rrset2 = findRrSet(zone, "/l1", label::NS_RR_TYPE);
641 BOOST_CHECK_EQUAL(rrset1, rrset2);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800642}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800643
Yumin Xia9d110ce2016-11-15 14:03:14 -0800644BOOST_AUTO_TEST_CASE(AddMultiLevelLabelRrset)
645{
Yumin Xia9d110ce2016-11-15 14:03:14 -0800646 Name zoneName("/ndns-test");
647 Zone zone(zoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800648
Yumin Xia9d110ce2016-11-15 14:03:14 -0800649 time::seconds ttl(4200);
650 m_tool.createZone(zoneName, ROOT_ZONE, ttl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800651
Yumin Xia9d110ce2016-11-15 14:03:14 -0800652 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
653 rf.checkZoneKey();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800654
Yumin Xia9d110ce2016-11-15 14:03:14 -0800655 auto checkRrset = [&zone, &zoneName, this](Name label,
656 name::Component type,
657 NdnsContentType contentType) -> void {
658 Rrset rr1 = findRrSet(zone, label, type);
659 BOOST_CHECK_EQUAL(Data(rr1.getData()).getContentType(), contentType);
660 Response response1;
661 response1.fromData(zoneName, Data(rr1.getData()));
662 BOOST_CHECK_EQUAL(response1.getRrLabel(), label);
663 };
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800664
Yumin Xia9d110ce2016-11-15 14:03:14 -0800665 Name labelName("/l1/l2/l3");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800666
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700667 Rrset rrset1 = rf.generateNsRrset(labelName, 7654, ttl, DelegationList());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800668
Yumin Xia9d110ce2016-11-15 14:03:14 -0800669 //add NS NDNS_AUTH and check user-defined ttl
670 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(rrset1, rf, ttl));
671 Rrset rrset2 = findRrSet(zone, labelName, label::NS_RR_TYPE);
672 BOOST_CHECK_EQUAL(rrset1, rrset2);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800673
Yumin Xia9d110ce2016-11-15 14:03:14 -0800674 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
675 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800676
Yumin Xia9d110ce2016-11-15 14:03:14 -0800677 // insert a same-name rrset with TXT type
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700678 Rrset txtRr = rf.generateTxtRrset("/l1/l2/l3", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800679 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(txtRr, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800680
Yumin Xia9d110ce2016-11-15 14:03:14 -0800681 checkRrset("/l1", label::NS_RR_TYPE, ndns::NDNS_AUTH);
682 checkRrset("/l1/l2", label::NS_RR_TYPE, ndns::NDNS_AUTH);
683 checkRrset("/l1/l2/l3", label::TXT_RR_TYPE, ndns::NDNS_RESP);
684 // check that there is no confliction
685 checkRrset("/l1/l2/l3", label::NS_RR_TYPE, ndns::NDNS_LINK);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800686
Yumin Xia9d110ce2016-11-15 14:03:14 -0800687 // insert a shorter NS, when there are longer NS or TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700688 Rrset shorterNs = rf.generateNsRrset("/l1/l2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800689 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(shorterNs, rf, ttl),
690 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800691
Yumin Xia9d110ce2016-11-15 14:03:14 -0800692 // insert a longer NS, when there is already a shorter NS
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700693 Rrset longerNs = rf.generateNsRrset("/l1/l2/l3/l4", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800694 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(longerNs, rf, ttl),
695 ndns::ManagementTool::Error);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800696
Yumin Xia9d110ce2016-11-15 14:03:14 -0800697 // insert a smaller TXT, when there are longer NS and TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700698 Rrset shorterTxt = rf.generateTxtRrset("/l1/l2", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800699 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(shorterTxt, rf, ttl));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800700
Yumin Xia9d110ce2016-11-15 14:03:14 -0800701 // insert a smaller NS, when there is long TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700702 Rrset longTxt = rf.generateTxtRrset("/k1/k2/k3", 7654, ttl, std::vector<std::string>());
703 Rrset smallerNs = rf.generateNsRrset("/k1/k2", 7654, ttl, DelegationList());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800704 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longTxt, rf, ttl));
705 BOOST_CHECK_THROW(m_tool.addMultiLevelLabelRrset(smallerNs, rf, ttl),
706 ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800707
Yumin Xia9d110ce2016-11-15 14:03:14 -0800708 // inserting a longer TXT, when there is shoter TXT
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700709 Rrset longerTxt = rf.generateTxtRrset("/k1/k2/k3/k4", 7654, ttl, std::vector<std::string>());
Yumin Xia9d110ce2016-11-15 14:03:14 -0800710 BOOST_CHECK_NO_THROW(m_tool.addMultiLevelLabelRrset(longerTxt, rf, ttl));
711}
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800712
Yumin Xia9d110ce2016-11-15 14:03:14 -0800713BOOST_AUTO_TEST_CASE(AddRrSetDskCertPreConditon)
714{
715 // check pre-condition
716 Name zoneName("/ndns-test");
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800717
Yumin Xia9d110ce2016-11-15 14:03:14 -0800718 // Check: throw if zone not exist
719 std::string certPath = TEST_CERTDIR.string();
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800720 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800721
Yumin Xia9d110ce2016-11-15 14:03:14 -0800722 m_tool.createZone(zoneName, ROOT_ZONE);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800723
Yumin Xia9d110ce2016-11-15 14:03:14 -0800724 // Check: throw if certificate does not match
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800725 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, certPath), ndns::ManagementTool::Error);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800726
Yumin Xia9d110ce2016-11-15 14:03:14 -0800727 std::string rightCertPath = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800728 std::vector<Certificate>&& certs = getCerts(zoneName);
729 const Name& ksk = certs[0].getName();
730 m_tool.exportCertificate(ksk, rightCertPath);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800731
Yumin Xia2c509c22017-02-09 14:37:36 -0800732 // Check: throw if it's a duplicated certificate
733 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, rightCertPath), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800734}
Jiewen Tan870b29b2014-11-17 19:09:49 -0800735
Yumin Xia9d110ce2016-11-15 14:03:14 -0800736BOOST_AUTO_TEST_CASE(AddRrSetDskCert)
737{
738 Name parentZoneName("/ndns-test");
739 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700740 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800741
Yumin Xia9d110ce2016-11-15 14:03:14 -0800742 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
743 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800744
Yumin Xia2c509c22017-02-09 14:37:36 -0800745 Zone zone(zoneName);
746 Zone parentZone(parentZoneName);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800747
Yumin Xia2c509c22017-02-09 14:37:36 -0800748 Certificate dkey(findDkeyFromDb(zone.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800749 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800750 ndn::io::save(dkey, output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800751
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800752 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output));
Yumin Xia2c509c22017-02-09 14:37:36 -0800753 // Check if child zone's d-key could be inserted correctly
754 BOOST_CHECK_NO_THROW(findRrSet(parentZone, getLabel(parentZone, dkey.getName()), label::CERT_RR_TYPE));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800755}
Jiewen Tand2d21822015-03-19 15:37:03 -0700756
Yumin Xia9d110ce2016-11-15 14:03:14 -0800757BOOST_AUTO_TEST_CASE(AddRrSetDskCertUserProvidedCert)
758{
759 //check using user provided certificate
760 Name parentZoneName("/ndns-test");
Yumin Xia918343d2017-03-17 19:04:55 -0700761 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800762 Name zoneName("/ndns-test/child-zone");
Yumin Xia918343d2017-03-17 19:04:55 -0700763 Name zoneIdentityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800764
Yumin Xia2c509c22017-02-09 14:37:36 -0800765 // Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false);
766 Identity id = CertHelper::getIdentity(m_keyChain, parentZoneIdentityName);
767 Key dsk = m_keyChain.createKey(id);
768 Certificate dskCert = dsk.getDefaultCertificate();
Yumin Xia9d110ce2016-11-15 14:03:14 -0800769
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800770 // check addRrsetFromFile1
Yumin Xia9d110ce2016-11-15 14:03:14 -0800771 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
772 m_tool.createZone(zoneName, parentZoneName);
773
Yumin Xia2c509c22017-02-09 14:37:36 -0800774 Certificate dkey(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800775 std::string output = TEST_CERTDIR.string() + "/ss.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -0800776 ndn::io::save(dkey, output);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800777
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800778 BOOST_CHECK_NO_THROW(m_tool.addRrsetFromFile(parentZoneName, output, time::seconds(4600),
Yumin Xia2c509c22017-02-09 14:37:36 -0800779 dskCert.getName()));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800780}
781
782BOOST_AUTO_TEST_CASE(AddRrSetDskCertInvalidOutput)
783{
784 //check invalid output
785 Name parentZoneName("/ndns-test");
786 Name zoneName = Name(parentZoneName).append("child-zone");
787 m_tool.createZone(zoneName, parentZoneName);
788
789 Name content = "invalid data packet";
790 std::string output = TEST_CERTDIR.string() + "/ss.cert";
791 ndn::io::save(content, output);
792
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800793 BOOST_CHECK_THROW(m_tool.addRrsetFromFile(zoneName, output), ndns::ManagementTool::Error);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800794}
795
796BOOST_AUTO_TEST_CASE(AddRrSetVersionControl)
797{
798 //check version control
799 time::seconds ttl(4200);
800 Name parentZoneName("/ndns-test");
801 Name zoneName = Name(parentZoneName).append("child-zone");
802 m_tool.createZone(zoneName, parentZoneName);
803
804 Name label("/label");
805 uint64_t version = 110;
806
807 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
808 rf.checkZoneKey();
809
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700810 Rrset rrset1 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800811
812 m_tool.addRrset(rrset1);
813 // throw error when adding duplicated rrset with the same version
814 BOOST_CHECK_THROW(m_tool.addRrset(rrset1),
815 ndns::ManagementTool::Error);
816 version--;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700817 Rrset rrset2 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800818 // throw error when adding duplicated rrset with older version
819 BOOST_CHECK_THROW(m_tool.addRrset(rrset2),
820 ndns::ManagementTool::Error);
821
822 version++;
823 version++;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700824 Rrset rrset3 = rf.generateTxtRrset(label, version, ttl, {});
Yumin Xia9d110ce2016-11-15 14:03:14 -0800825 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset3));
826
827 Zone zone(zoneName);
828 m_dbMgr.find(zone);
829 Rrset rrset;
830 rrset.setZone(&zone);
831 rrset.setLabel(label);
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700832 rrset.setType(label::TXT_RR_TYPE);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800833 m_dbMgr.find(rrset);
834
835 BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version));
836}
837
838BOOST_AUTO_TEST_CASE(AddRrSetDskCertFormat)
839{
840 //check input with different formats
841 Name parentZoneName("/ndns-test");
842 Name zoneName = Name(parentZoneName).append("child-zone");
Yumin Xia2c509c22017-02-09 14:37:36 -0800843 Zone parentZone(parentZoneName);
844
845 m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800846 m_tool.createZone(zoneName, parentZoneName);
Jiewen Tand2d21822015-03-19 15:37:03 -0700847
Yumin Xia2c509c22017-02-09 14:37:36 -0800848 Certificate cert(findDkeyFromDb(zoneName));
Yumin Xia9d110ce2016-11-15 14:03:14 -0800849 std::string output = TEST_CERTDIR.string() + "/a.cert";
Jiewen Tand2d21822015-03-19 15:37:03 -0700850
Yumin Xia918343d2017-03-17 19:04:55 -0700851 Name parentZoneIdentityName = Name(parentZoneName).append(label::NDNS_ITERATIVE_QUERY);
852
Yumin Xia9d110ce2016-11-15 14:03:14 -0800853 // base64
Yumin Xia2c509c22017-02-09 14:37:36 -0800854 ndn::io::save(cert, output, ndn::io::BASE64);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800855 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800856 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE64));
857 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700858
Yumin Xia9d110ce2016-11-15 14:03:14 -0800859 // raw
Yumin Xia2c509c22017-02-09 14:37:36 -0800860 ndn::io::save(cert, output, ndn::io::NO_ENCODING);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800861 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800862 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING));
863 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700864
Yumin Xia9d110ce2016-11-15 14:03:14 -0800865 // hex
Yumin Xia2c509c22017-02-09 14:37:36 -0800866 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800867 BOOST_CHECK_NO_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800868 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX));
869 m_tool.removeRrSet(parentZoneName, getLabel(parentZone, cert.getName()), label::CERT_RR_TYPE);
Jiewen Tan8cd35ea2015-03-20 00:44:23 -0700870
Yumin Xia9d110ce2016-11-15 14:03:14 -0800871 // incorrect encoding input
Yumin Xia2c509c22017-02-09 14:37:36 -0800872 ndn::io::save(cert, output, ndn::io::HEX);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800873 BOOST_CHECK_THROW(
Yumin Xia2c509c22017-02-09 14:37:36 -0800874 m_tool.addRrsetFromFile(parentZoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT,
875 static_cast<ndn::io::IoEncoding>(127)),
Yumin Xia9d110ce2016-11-15 14:03:14 -0800876 ndns::ManagementTool::Error);
877}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700878
Yumin Xia9d110ce2016-11-15 14:03:14 -0800879BOOST_AUTO_TEST_CASE(ListAllZones)
880{
Yumin Xia2c509c22017-02-09 14:37:36 -0800881 m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk, rootDkey);
Yumin Xia9d110ce2016-11-15 14:03:14 -0800882 m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700883
Yumin Xia2c509c22017-02-09 14:37:36 -0800884 Name rootDskName = CertHelper::getCertificate(m_keyChain, "/NDNS/", rootDsk).getKeyName();
885 Name otherDskName = CertHelper::getCertificate(m_keyChain, "/ndns-test/NDNS/", otherDsk).getKeyName();
886
Yumin Xia9d110ce2016-11-15 14:03:14 -0800887 std::string expectedValue =
Yumin Xia2c509c22017-02-09 14:37:36 -0800888 "/ ; default-ttl=1 default-key=" + rootDskName.toUri() + " "
889 "default-certificate=" + rootDsk.toUri() + "\n"
890 "/ndns-test ; default-ttl=10 default-key=" + otherDskName.toUri() + " "
891 "default-certificate=" + otherDsk.toUri() + "\n";
Jiewen Tan74d745c2015-03-20 01:40:41 -0700892
Yumin Xia9d110ce2016-11-15 14:03:14 -0800893 output_test_stream testOutput;
894 m_tool.listAllZones(testOutput);
895 BOOST_CHECK(testOutput.is_equal(expectedValue));
896}
Jiewen Tan74d745c2015-03-20 01:40:41 -0700897
Yumin Xia55a7cc42017-05-14 18:43:34 -0700898// Test need to fix values of keys, otherwise it produces different values every time
Yumin Xia2c509c22017-02-09 14:37:36 -0800899
Yumin Xia55a7cc42017-05-14 18:43:34 -0700900// BOOST_AUTO_TEST_CASE(ListZone)
901// {
902// m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700903
Yumin Xia55a7cc42017-05-14 18:43:34 -0700904// RrsetFactory rf(TEST_DATABASE, "/ndns-test", m_keyChain, DEFAULT_CERT);
905// rf.checkZoneKey();
Jiewen Tan74d745c2015-03-20 01:40:41 -0700906
Yumin Xia55a7cc42017-05-14 18:43:34 -0700907// // Add NS with NDNS_RESP
908// Delegation del;
909// del.preference = 10;
910// del.name = Name("/get/link");
911// DelegationList ds = {del};
912// Rrset rrset1 = rf.generateNsRrset("/label1", 100, DEFAULT_RR_TTL, ds);
913// m_tool.addRrset(rrset1);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700914
Yumin Xia55a7cc42017-05-14 18:43:34 -0700915// // Add NS with NDNS_AUTH
916// Rrset rrset2 = rf.generateAuthRrset("/label2", 100000, DEFAULT_RR_TTL);
917// m_tool.addRrset(rrset2);
Jiewen Tan74d745c2015-03-20 01:40:41 -0700918
Yumin Xia55a7cc42017-05-14 18:43:34 -0700919// // Add TXT from file
920// std::string output = TEST_CERTDIR.string() + "/a.rrset";
921// Response re1;
922// re1.setZone("/ndns-test");
923// re1.setQueryType(label::NDNS_ITERATIVE_QUERY);
924// re1.setRrLabel("/label2");
925// re1.setRrType(label::TXT_RR_TYPE);
926// re1.setContentType(NDNS_RESP);
927// re1.setVersion(name::Component::fromVersion(654321));
928// re1.addRr("First RR");
929// re1.addRr("Second RR");
930// re1.addRr("Last RR");
931// shared_ptr<Data> data1 = re1.toData();
932// m_keyChain.sign(*data1, security::signingByCertificate(otherDsk));
933// ndn::io::save(*data1, output);
934// m_tool.addRrsetFromFile("/ndns-test", output);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800935
Yumin Xia55a7cc42017-05-14 18:43:34 -0700936// // Add TXT in normal way
937// Rrset rrset3 = rf.generateTxtRrset("/label3", 3333, DEFAULT_RR_TTL, {"Hello", "World"});
938// m_tool.addRrset(rrset3);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800939
Yumin Xia55a7cc42017-05-14 18:43:34 -0700940// m_tool.listZone("/ndns-test", std::cout, true);
Yumin Xia2c509c22017-02-09 14:37:36 -0800941
Yumin Xia55a7cc42017-05-14 18:43:34 -0700942// output_test_stream testOutput;
943// m_tool.listZone("/ndns-test", testOutput, true);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800944
Yumin Xia55a7cc42017-05-14 18:43:34 -0700945// std::string expectedValue =
946// R"VALUE(; Zone /ndns-test
Yumin Xia2c509c22017-02-09 14:37:36 -0800947
Yumin Xia55a7cc42017-05-14 18:43:34 -0700948// ; rrset=/ type=DOE version=%FD%00%00%01b%26e%AE%99 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
949// / 3600 DOE Bw0IBmxhYmVsMwgDVFhU
950// / 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800951
Yumin Xia55a7cc42017-05-14 18:43:34 -0700952// /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
953// Certificate name:
954// /ndns-test/NDNS/KEY/%12%05t%90%3AjvA/CERT/%FD%00%00%01b%26e%AEY
955// Validity:
956// NotBefore: 20180314T212340
957// NotAfter: 20180324T212340
958// Public key bits:
959// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
960// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
961// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
962// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
963// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
964// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABHU62fbCa6KR7G1iyMr6/NtF
965// 5oHrAdzttIgh5pk1VS1YcFO1zhpUnpJS43FlduYHVBLrXwYS6tZ15Ge/D3uy1f4=
966// Signature Information:
967// Signature Type: SignatureSha256WithEcdsa
968// Key Locator: Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800969
Yumin Xia55a7cc42017-05-14 18:43:34 -0700970// ; 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
971// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
972// /KEY/%12%05t%90%3AjvA/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800973
Yumin Xia55a7cc42017-05-14 18:43:34 -0700974// /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
975// Certificate name:
976// /ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT/%FD%00%00%01b%26e%AEX
977// Validity:
978// NotBefore: 20180314T212340
979// NotAfter: 20180324T212340
980// Public key bits:
981// MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
982// AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
983// ///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
984// NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
985// RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
986// //////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABMRVD/FUfCQVvjcwQLe9k1aS
987// 5pZ/xmFndOHn1+a0OYVzxCV1JcxL1eojcij42tCP5mtocrj9DjYyFBv4Atg1RZE=
988// Signature Information:
989// Signature Type: SignatureSha256WithEcdsa
990// Key Locator: Self-Signed Name=/ndns-test/NDNS/KEY/%8D%1Dj%1E%BE%B0%2A%E4
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800991
Yumin Xia55a7cc42017-05-14 18:43:34 -0700992// ; 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
993// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BxUIA0tFWQgIjR1qHr6wKuQIBENFUlQ=
994// /KEY/%8D%1Dj%1E%BE%B0%2A%E4/CERT 3600 DOE BwwIBmxhYmVsMQgCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800995
Yumin Xia55a7cc42017-05-14 18:43:34 -0700996// ; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
997// /label1 10 NS 10
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800998
Yumin Xia55a7cc42017-05-14 18:43:34 -0700999// ; rrset=/label1/NS type=DOE version=%FD%00%00%01b%26e%AE%94 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1000// /label1/NS 3600 DOE BwwIBmxhYmVsMQgCTlM=
1001// /label1/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001002
Yumin Xia55a7cc42017-05-14 18:43:34 -07001003// ; rrset=/label2 type=NS version=%FD%00%01%86%A0 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1004// /label2 10 NS NDNS-Auth
1005
1006// ; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1007// /label2 10 TXT First RR
1008// /label2 10 TXT Second RR
1009// /label2 10 TXT Last RR
1010
1011// ; rrset=/label2/NS type=DOE version=%FD%00%00%01b%26e%AE%96 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1012// /label2/NS 3600 DOE BwwIBmxhYmVsMggCTlM=
1013// /label2/NS 3600 DOE Bw0IBmxhYmVsMggDVFhU
1014
1015// ; rrset=/label2/TXT type=DOE version=%FD%00%00%01b%26e%AE%97 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1016// /label2/TXT 3600 DOE Bw0IBmxhYmVsMggDVFhU
1017// /label2/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1018
1019// ; rrset=/label3 type=TXT version=%FD%0D%05 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1020// /label3 10 TXT Hello
1021// /label3 10 TXT World
1022
1023// ; rrset=/label3/TXT type=DOE version=%FD%00%00%01b%26e%AE%98 signed-by=/ndns-test/NDNS/KEY/%12%05t%90%3AjvA
1024// /label3/TXT 3600 DOE Bw0IBmxhYmVsMwgDVFhU
1025// /label3/TXT 3600 DOE BxUIA0tFWQgIEgV0kDpqdkEIBENFUlQ=
1026
1027// )VALUE";
1028
1029// BOOST_CHECK(testOutput.is_equal(expectedValue));
1030// }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001031
Yumin Xia9d110ce2016-11-15 14:03:14 -08001032BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture)
1033{
1034 Name zoneName("/ndns-test");
1035 m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk);
1036 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1037 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001038 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {"Value1", "Value2"});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001039
Yumin Xia9d110ce2016-11-15 14:03:14 -08001040 m_tool.addRrset(rrset1);
Jiewen Tan870b29b2014-11-17 19:09:49 -08001041
Yumin Xia2c509c22017-02-09 14:37:36 -08001042 std::stringstream os;
1043
1044 using security::transform::base64Encode;
1045 using security::transform::streamSink;
1046 using security::transform::bufferSource;
1047
1048 bufferSource(rrset1.getData().wire(), rrset1.getData().size()) >> base64Encode() >> streamSink(os);
1049
1050 std::string expectedValue = os.str();
Jiewen Tan870b29b2014-11-17 19:09:49 -08001051
Yumin Xia9d110ce2016-11-15 14:03:14 -08001052 output_test_stream testOutput;
1053 m_tool.getRrSet(zoneName, "/label",label::TXT_RR_TYPE, testOutput);
Yumin Xia9d110ce2016-11-15 14:03:14 -08001054 BOOST_CHECK(testOutput.is_equal(expectedValue));
1055}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001056
Yumin Xia9d110ce2016-11-15 14:03:14 -08001057BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture)
1058{
1059 Name zoneName("/ndns-test");
Jiewen Tan870b29b2014-11-17 19:09:49 -08001060
Yumin Xia9d110ce2016-11-15 14:03:14 -08001061 m_tool.createZone(zoneName, ROOT_ZONE);
1062 RrsetFactory rf(TEST_DATABASE, zoneName, m_keyChain, DEFAULT_CERT);
1063 rf.checkZoneKey();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001064
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001065 Rrset rrset1 = rf.generateTxtRrset("/label", 100, DEFAULT_RR_TTL, {});
Jiewen Tan870b29b2014-11-17 19:09:49 -08001066
Yumin Xia9d110ce2016-11-15 14:03:14 -08001067 BOOST_CHECK_NO_THROW(m_tool.addRrset(rrset1));
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -08001068
Yumin Xia9d110ce2016-11-15 14:03:14 -08001069 Zone zone(zoneName);
Yumin Xiad4e8ce52017-03-17 19:56:52 -07001070 BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::TXT_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001071
Yumin Xia9d110ce2016-11-15 14:03:14 -08001072 BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE));
Jiewen Tan870b29b2014-11-17 19:09:49 -08001073
Yumin Xia9d110ce2016-11-15 14:03:14 -08001074 BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error);
1075}
Jiewen Tan870b29b2014-11-17 19:09:49 -08001076
1077BOOST_AUTO_TEST_SUITE_END()
1078
1079} // namespace tests
1080} // namespace ndns
1081} // namespace ndn