Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "../../src/mgmt/management-tool.hpp" |
| 21 | |
| 22 | #include "../../boost-test.hpp" |
| 23 | #include <boost/test/output_test_stream.hpp> |
| 24 | using boost::test_tools::output_test_stream; |
| 25 | |
| 26 | #include "ndns-enum.hpp" |
| 27 | #include "ndns-label.hpp" |
| 28 | #include "ndns-tlv.hpp" |
| 29 | |
| 30 | #include <vector> |
| 31 | #include <iostream> |
| 32 | #include <fstream> |
| 33 | #include <string> |
| 34 | |
| 35 | #include <boost/filesystem.hpp> |
| 36 | #include <boost/algorithm/string/replace.hpp> |
| 37 | |
| 38 | #include <ndn-cxx/security/key-chain.hpp> |
| 39 | #include <ndn-cxx/security/validator.hpp> |
| 40 | #include <ndn-cxx/util/io.hpp> |
| 41 | #include <ndn-cxx/util/regex.hpp> |
| 42 | |
| 43 | namespace ndn { |
| 44 | namespace ndns { |
| 45 | namespace tests { |
| 46 | |
| 47 | BOOST_AUTO_TEST_SUITE(ManagementTool) |
| 48 | |
| 49 | static const boost::filesystem::path TEST_DATABASE = TEST_CONFIG_PATH "/management_tool.db"; |
| 50 | static const boost::filesystem::path TEST_CERTDIR = TEST_CONFIG_PATH "/management_tool_certs"; |
| 51 | static const Name FAKE_ROOT("/fake-root/123456789"); |
| 52 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * @brief Recursive copy a directory using Boost Filesystem |
| 56 | * |
| 57 | * Based on from http://stackoverflow.com/q/8593608/2150331 |
| 58 | */ |
| 59 | void |
| 60 | copyDir(const boost::filesystem::path& source, const boost::filesystem::path& destination) |
| 61 | { |
| 62 | namespace fs = boost::filesystem; |
| 63 | |
| 64 | fs::create_directory(destination); |
| 65 | for (fs::directory_iterator file(source); file != fs::directory_iterator(); ++file) { |
| 66 | fs::path current(file->path()); |
| 67 | if (is_directory(current)) { |
| 68 | copyDir(current, destination / current.filename()); |
| 69 | } |
| 70 | else { |
| 71 | // cannot use fs::copy_file, see https://svn.boost.org/trac/boost/ticket/10038 |
| 72 | // fs::copy works, as it doesn't use problematic private API |
| 73 | copy(current, destination / current.filename()); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | class TestHome : boost::noncopyable |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 79 | { |
| 80 | public: |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 81 | TestHome() |
| 82 | { |
| 83 | if (std::getenv("HOME")) |
| 84 | m_origHome = std::getenv("HOME"); |
| 85 | |
| 86 | setenv("HOME", TEST_CONFIG_PATH "/tests/unit/mgmt/", 1); |
| 87 | boost::filesystem::remove_all(TEST_CONFIG_PATH "/tests/unit/mgmt/"); |
| 88 | boost::filesystem::create_directories(TEST_CONFIG_PATH "/tests/unit/mgmt"); |
| 89 | copyDir("tests/unit/mgmt/.ndn", TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn"); |
| 90 | } |
| 91 | |
| 92 | ~TestHome() |
| 93 | { |
| 94 | if (!m_origHome.empty()) |
| 95 | setenv("HOME", m_origHome.c_str(), 1); |
| 96 | else |
| 97 | unsetenv("HOME"); |
| 98 | } |
| 99 | |
| 100 | protected: |
| 101 | std::string m_origHome; |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | class ManagementToolFixture : public TestHome |
| 106 | { |
| 107 | public: |
| 108 | class Error : public std::runtime_error |
| 109 | { |
| 110 | public: |
| 111 | explicit |
| 112 | Error(const std::string& what) |
| 113 | : std::runtime_error(what) |
| 114 | { |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | class PreviousStateCleaner |
| 119 | { |
| 120 | public: |
| 121 | PreviousStateCleaner() |
| 122 | { |
| 123 | boost::filesystem::remove(TEST_DATABASE); |
| 124 | boost::filesystem::remove_all(TEST_CERTDIR); |
| 125 | } |
| 126 | }; |
| 127 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 128 | ManagementToolFixture() |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 129 | : m_keyChain("sqlite3", "file") |
| 130 | , m_tool(TEST_DATABASE.string().c_str(), m_keyChain) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 131 | , m_dbMgr(TEST_DATABASE.string().c_str()) |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 132 | |
| 133 | , rootKsk("/KEY/ksk-1416974006376/ID-CERT/%FD%00%00%01I%EA%3Bx%BD") |
| 134 | , rootDsk("/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28") |
| 135 | |
| 136 | , otherKsk("/ndns-test/KEY/ksk-1416974006577/ID-CERT/%FD%00%00%01I%EA%3By%7F") |
| 137 | , otherDsk("/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 138 | { |
| 139 | boost::filesystem::create_directory(TEST_CERTDIR); |
| 140 | } |
| 141 | |
| 142 | ~ManagementToolFixture() |
| 143 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 146 | std::vector<Name> |
| 147 | getKeys(const Name& identity) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 148 | { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 149 | std::vector<Name> keys; |
| 150 | m_keyChain.getAllKeyNamesOfIdentity(identity, keys, false); |
| 151 | m_keyChain.getAllKeyNamesOfIdentity(identity, keys, true); |
| 152 | return keys; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 155 | std::vector<Name> |
| 156 | getCerts(const Name& identity) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 157 | { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 158 | std::vector<Name> certs; |
| 159 | for (auto&& name : getKeys(identity)) { |
| 160 | m_keyChain.getAllCertificateNamesOfKey(name, certs, false); |
| 161 | m_keyChain.getAllCertificateNamesOfKey(name, certs, true); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 162 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 163 | return certs; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 166 | Rrset |
| 167 | findRrSet(Zone& zone, const Name& label, const name::Component& type) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 168 | { |
| 169 | Rrset rrset(&zone); |
| 170 | rrset.setLabel(label); |
| 171 | rrset.setType(type); |
| 172 | |
| 173 | if (!m_dbMgr.find(rrset)) |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 174 | throw Error("Record not found"); |
| 175 | else |
| 176 | return rrset; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 179 | Name |
| 180 | getLabel(const Zone& zone, const Name& fullName) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 181 | { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 182 | size_t zoneNameSize = zone.getName().size(); |
| 183 | return fullName.getSubName(zoneNameSize + 1, fullName.size() - zoneNameSize - 3); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 186 | IdentityCertificate |
| 187 | findIdCert(Zone& zone, const Name& fullName) |
| 188 | { |
| 189 | Rrset rrset = findRrSet(zone, getLabel(zone, fullName), label::CERT_RR_TYPE); |
| 190 | IdentityCertificate cert; |
| 191 | cert.wireDecode(rrset.getData()); |
| 192 | return cert; |
| 193 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 195 | Response |
| 196 | findResponse(Zone& zone, const Name& label, const name::Component& type) |
| 197 | { |
| 198 | Rrset rrset = findRrSet(zone, label, type); |
| 199 | Data data(rrset.getData()); |
| 200 | Response resp; |
| 201 | resp.fromData("", zone.getName(), data); |
| 202 | return resp; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | public: |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 206 | PreviousStateCleaner cleaner; // must be first variable |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 207 | ndn::KeyChain m_keyChain; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 208 | ndns::ManagementTool m_tool; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 209 | ndns::DbMgr m_dbMgr; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 210 | |
| 211 | // Names of pre-created certificates |
| 212 | // Uncomment and run InitPreconfiguredKeys test case and then update names in the |
| 213 | // constructor. |
| 214 | Name rootKsk; |
| 215 | Name rootDsk; |
| 216 | Name otherKsk; |
| 217 | Name otherDsk; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 220 | // BOOST_FIXTURE_TEST_CASE(InitPreconfiguredKeys, ManagementToolFixture) |
| 221 | // { |
| 222 | // using time::seconds; |
| 223 | |
| 224 | // auto generateCerts = [this] (const Name& zone, const Name& parentCert = Name()) -> Name { |
| 225 | // // to re-generate certificates, uncomment and then update rootKsk/rootDsk names |
| 226 | // Name kskName = m_keyChain.generateRsaKeyPair(zone, true); |
| 227 | // auto kskCert = m_keyChain |
| 228 | // .prepareUnsignedIdentityCertificate(kskName, zone, time::fromUnixTimestamp(seconds(0)), |
| 229 | // time::fromUnixTimestamp(seconds(2147483648)), {}); |
| 230 | // if (parentCert.empty()) { |
| 231 | // m_keyChain.selfSign(*kskCert); |
| 232 | // } |
| 233 | // else { |
| 234 | // m_keyChain.sign(*kskCert, parentCert); |
| 235 | // } |
| 236 | // m_keyChain.addCertificate(*kskCert); |
| 237 | |
| 238 | // Name dskName = m_keyChain.generateRsaKeyPair(zone, false); |
| 239 | // auto dskCert = m_keyChain |
| 240 | // .prepareUnsignedIdentityCertificate(dskName, zone, time::fromUnixTimestamp(seconds(0)), |
| 241 | // time::fromUnixTimestamp(seconds(2147483648)), {}); |
| 242 | // m_keyChain.sign(*dskCert, kskCert->getName()); |
| 243 | // m_keyChain.addCertificate(*dskCert); |
| 244 | |
| 245 | // return dskCert->getName(); |
| 246 | // }; |
| 247 | |
| 248 | // Name rootDsk = generateCerts(ROOT_ZONE); |
| 249 | // generateCerts("/ndns-test", rootDsk); |
| 250 | |
| 251 | // copyDir(TEST_CONFIG_PATH "/tests/unit/mgmt/.ndn", "/tmp/.ndn"); |
| 252 | // std::cout << "Manually copy contents of /tmp/.ndn into tests/unit/mgmt/.ndn" << std::endl; |
| 253 | // } |
| 254 | |
| 255 | BOOST_FIXTURE_TEST_CASE(CreateDeleteRootZone, ManagementToolFixture) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 256 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 257 | m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(4600), time::seconds(4600), |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 258 | rootKsk, rootDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 259 | |
| 260 | Zone zone(ROOT_ZONE); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 261 | BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true); |
| 262 | BOOST_REQUIRE_NO_THROW(findIdCert(zone, rootDsk)); |
| 263 | BOOST_CHECK_EQUAL(findIdCert(zone, rootDsk).getName(), rootDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 264 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 265 | BOOST_CHECK_NO_THROW(m_tool.deleteZone(ROOT_ZONE)); |
| 266 | BOOST_CHECK_EQUAL(m_dbMgr.find(zone), false); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 269 | BOOST_FIXTURE_TEST_CASE(CreateDeleteChildZone, ManagementToolFixture) |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 270 | { |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 271 | Name parentZoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 272 | Name zoneName = Name(parentZoneName).append("child-zone"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 273 | |
| 274 | BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), false); |
| 275 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 276 | // will generate keys automatically |
| 277 | m_tool.createZone(zoneName, parentZoneName); |
| 278 | BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 279 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 280 | std::vector<Name>&& certs = getCerts(zoneName); |
| 281 | BOOST_REQUIRE_EQUAL(certs.size(), 2); |
| 282 | std::sort(certs.begin(), certs.end()); |
| 283 | |
| 284 | // Name& ksk = certs[0]; |
| 285 | Name& dsk = certs[1]; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 286 | |
| 287 | Zone zone(zoneName); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 288 | BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true); |
| 289 | BOOST_REQUIRE_NO_THROW(findIdCert(zone, dsk)); |
| 290 | BOOST_CHECK_EQUAL(findIdCert(zone, dsk).getName(), dsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 291 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 292 | BOOST_CHECK_NO_THROW(m_tool.deleteZone(zoneName)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 293 | |
| 294 | BOOST_CHECK_THROW(m_tool.deleteZone(zoneName), ndns::ManagementTool::Error); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 295 | BOOST_CHECK_THROW(m_tool.deleteZone("/non/existing/zone"), ndns::ManagementTool::Error); |
| 296 | } |
| 297 | |
| 298 | BOOST_FIXTURE_TEST_CASE(CreateZoneWithTtl, ManagementToolFixture) |
| 299 | { |
| 300 | Name parentZoneName("/ndns-test"); |
| 301 | Name zoneName = Name(parentZoneName).append("child-zone"); |
| 302 | |
| 303 | m_tool.createZone(zoneName, parentZoneName, time::seconds(4200), time::days(30)); |
| 304 | BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(zoneName), true); |
| 305 | |
| 306 | std::vector<Name>&& certs = getCerts(zoneName); |
| 307 | BOOST_REQUIRE_EQUAL(certs.size(), 2); |
| 308 | std::sort(certs.begin(), certs.end()); |
| 309 | |
| 310 | // Name& ksk = certs[0]; |
| 311 | Name& dsk = certs[1]; |
| 312 | |
| 313 | // Check zone ttl |
| 314 | Zone zone(zoneName); |
| 315 | BOOST_REQUIRE_EQUAL(m_dbMgr.find(zone), true); |
| 316 | BOOST_CHECK_EQUAL(zone.getTtl(), time::seconds(4200)); |
| 317 | |
| 318 | // Check dsk rrset ttl |
| 319 | Rrset rrset; |
| 320 | BOOST_REQUIRE_NO_THROW(rrset = findRrSet(zone, getLabel(zone, dsk), label::CERT_RR_TYPE)); |
| 321 | BOOST_CHECK_EQUAL(rrset.getTtl(), time::seconds(4200)); |
| 322 | |
| 323 | // Check certificate freshnessPeriod and validity |
| 324 | IdentityCertificate cert; |
| 325 | BOOST_REQUIRE_NO_THROW(cert = findIdCert(zone, dsk)); |
| 326 | BOOST_CHECK_EQUAL(cert.getMetaInfo().getFreshnessPeriod(), time::seconds(4200)); |
| 327 | BOOST_CHECK_EQUAL(cert.getNotAfter() - cert.getNotBefore(), time::days(30)); |
| 328 | |
| 329 | m_tool.deleteZone(zoneName); |
| 330 | } |
| 331 | |
| 332 | BOOST_FIXTURE_TEST_CASE(ZoneCreatePreconditions, ManagementToolFixture) |
| 333 | { |
| 334 | BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net")); |
| 335 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net"), ndns::ManagementTool::Error); |
| 336 | |
| 337 | std::vector<Name>&& certs = getCerts("/net/ndnsim"); |
| 338 | BOOST_REQUIRE_EQUAL(certs.size(), 2); |
| 339 | std::sort(certs.begin(), certs.end()); |
| 340 | |
| 341 | Name& ksk = certs[0]; |
| 342 | Name& dsk = certs[1]; |
| 343 | |
| 344 | m_tool.deleteZone("/net/ndnsim"); |
| 345 | // identity will still exist after the zone is deleted |
| 346 | |
| 347 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net/ndnsim"), ndns::ManagementTool::Error); |
| 348 | |
| 349 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/com"), ndns::ManagementTool::Error); |
| 350 | |
| 351 | BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/", |
| 352 | time::seconds(1), time::days(1), ksk, dsk)); |
| 353 | BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 2); |
| 354 | m_tool.deleteZone("/net/ndnsim"); |
| 355 | |
| 356 | BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/", |
| 357 | time::seconds(1), time::days(1), Name(), dsk)); |
| 358 | BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 3); |
| 359 | m_tool.deleteZone("/net/ndnsim"); |
| 360 | |
| 361 | BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/", |
| 362 | time::seconds(1), time::days(1), ksk, Name())); |
| 363 | BOOST_CHECK_EQUAL(getCerts("/net/ndnsim").size(), 4); |
| 364 | m_tool.deleteZone("/net/ndnsim"); |
| 365 | |
| 366 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net", |
| 367 | time::seconds(1), time::days(1), "/com/ndnsim"), |
| 368 | ndns::ManagementTool::Error); |
| 369 | |
| 370 | m_keyChain.deleteIdentity("/net/ndnsim"); |
| 371 | Name cert = m_keyChain.createIdentity("/net/ndnsim"); |
| 372 | BOOST_CHECK_NO_THROW(m_tool.createZone("/net/ndnsim", "/net", |
| 373 | time::seconds(1), time::days(1), cert)); |
| 374 | |
| 375 | cert = m_keyChain.createIdentity("/com/ndnsim"); |
| 376 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net", |
| 377 | time::seconds(1), time::days(1), cert), |
| 378 | ndns::ManagementTool::Error); |
| 379 | |
| 380 | cert = m_keyChain.createIdentity("/net/ndnsim/www"); |
| 381 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net", |
| 382 | time::seconds(1), time::days(1), cert), |
| 383 | ndns::ManagementTool::Error); |
| 384 | |
| 385 | cert = m_keyChain.createIdentity("/net/ndnsim"); |
| 386 | m_keyChain.deleteKeyPairInTpm(m_keyChain.getCertificate(cert)->getPublicKeyName()); |
| 387 | BOOST_CHECK_THROW(m_tool.createZone("/net/ndnsim", "/net", |
| 388 | time::seconds(1), time::days(1), cert), |
| 389 | ndns::ManagementTool::Error); |
| 390 | |
| 391 | // for root zone special case (requires a valid KSK to be specified) |
| 392 | BOOST_CHECK_THROW(m_tool.createZone("/", "/"), ndns::ManagementTool::Error); |
| 393 | |
| 394 | BOOST_CHECK_NO_THROW(m_tool.createZone("/", "/", time::seconds(1), time::days(1), |
| 395 | rootKsk)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | class OutputTester |
| 399 | { |
| 400 | public: |
| 401 | OutputTester() |
| 402 | : savedBuf(std::clog.rdbuf()) |
| 403 | { |
| 404 | std::cout.rdbuf(buffer.rdbuf()); |
| 405 | } |
| 406 | |
| 407 | ~OutputTester() |
| 408 | { |
| 409 | std::cout.rdbuf(savedBuf); |
| 410 | } |
| 411 | |
| 412 | public: |
| 413 | std::stringstream buffer; |
| 414 | std::streambuf* savedBuf; |
| 415 | }; |
| 416 | |
| 417 | BOOST_FIXTURE_TEST_CASE(ExportCertificate, ManagementToolFixture) |
| 418 | { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 419 | std::string outputFile = TEST_CERTDIR.string() + "/ss.cert"; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 420 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 421 | BOOST_REQUIRE_THROW(m_tool.exportCertificate("/random/name", outputFile), |
| 422 | ndns::ManagementTool::Error); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 423 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 424 | BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), false); |
| 425 | // doesn't check the zone, export from KeyChain directly |
| 426 | BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile)); |
| 427 | BOOST_REQUIRE_EQUAL(boost::filesystem::exists(outputFile), true); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 428 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 429 | std::string dskValue = |
| 430 | "Bv0C3Ac3CAluZG5zLXRlc3QIA0tFWQgRZHNrLTE0MTY5NzQwMDY2NTkIB0lELUNF\n" |
| 431 | "UlQICf0AAAFJ6jt6DhQDGAECFf0BYTCCAV0wIhgPMTk3MDAxMDEwMDAwMDBaGA8y\n" |
| 432 | "MDM4MDExOTAzMTQwOFowEzARBgNVBCkTCi9uZG5zLXRlc3QwggEgMA0GCSqGSIb3\n" |
| 433 | "DQEBAQUAA4IBDQAwggEIAoIBAQDIFUL7Fz8mmxxIT8l3FtWm+CuH9+iQ0Uj/a30P\n" |
| 434 | "mKe4gWvtxzhb4vIngYbXGv2iUzHswdqYlTVeDdW6eOFKMvyY5p5eVtLqDFZ7EEK0\n" |
| 435 | "0rpTh648HjCSz+Awgp2nbiYAAVvhP6YF+NxGBH412uPI7kLY6ozypsNmYP+K4SYT\n" |
| 436 | "oY9ee4xLSjqzXfLMyP1h8OHcN/aNmccRJlyYblCmCDbZPnzu3ttHHwdrYQLeFvb0\n" |
| 437 | "B5grCAQoPHwkfxkEnzQBA/fbUdvKNdayEkuibPLlIlmj2cBtk5iVk8JCSibP3Zlz\n" |
| 438 | "36Sks1DAO+1EvCRnjoH5vYmkpMUBFue+6A40IQG4brM2CiIRAgERFjMbAQEcLgcs\n" |
| 439 | "CAluZG5zLXRlc3QIA0tFWQgRa3NrLTE0MTY5NzQwMDY1NzcIB0lELUNFUlQX/QEA\n" |
| 440 | "GP2bQqp/7rfb8tShwDbXihWrPojwEFqlfwLibK9aM1RxwpHVqbtRsPYmuWc87LaU\n" |
| 441 | "OztPOZinHGL80ypFC+wYadVGnE8MPdTkUYUik7mbHDEsYWADoyGMVhoZv+OTJ/5m\n" |
| 442 | "MUh/kR1FMiqtZcIQtLB3cdCeGlZBl9wm2SvhMKVUym3RsQO46RpnmsEQcCfWMBZg\n" |
| 443 | "u5U6mhYIpiQPZ/sYyZ9zXstwsIfaF1p0V+1dW5y99PZJXIegVKhkGGU0ibjYoJy7\n" |
| 444 | "6uUjqBBDX8KMdt6n/Zy1/pGG1eOchMyV0JZ8+MJxWuiTEh5PJeYMFHTV/BVp8aPy\n" |
| 445 | "8UNqhMpjAZwW6pdvOZADVg==\n"; |
| 446 | |
| 447 | { |
| 448 | std::ifstream ifs(outputFile.c_str()); |
| 449 | std::string actualValue((std::istreambuf_iterator<char>(ifs)), |
| 450 | std::istreambuf_iterator<char>()); |
| 451 | BOOST_CHECK_EQUAL(actualValue, dskValue); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 452 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 453 | boost::filesystem::remove(outputFile); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 454 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 455 | // doesn't check the zone, export from KeyChain directly |
| 456 | BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherKsk, outputFile)); |
| 457 | boost::filesystem::remove(outputFile); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 458 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 459 | Name zoneName("/ndns-test"); |
| 460 | m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(4200), time::days(30), |
| 461 | otherKsk, otherDsk); |
| 462 | |
| 463 | m_keyChain.deleteCertificate(otherKsk); |
| 464 | m_keyChain.deleteCertificate(otherDsk); |
| 465 | |
| 466 | // retrieve cert from the zone |
| 467 | BOOST_CHECK_NO_THROW(m_tool.exportCertificate(otherDsk, outputFile)); |
| 468 | { |
| 469 | std::ifstream ifs(outputFile.c_str()); |
| 470 | std::string actualValue((std::istreambuf_iterator<char>(ifs)), |
| 471 | std::istreambuf_iterator<char>()); |
| 472 | BOOST_CHECK_EQUAL(actualValue, dskValue); |
| 473 | } |
| 474 | boost::filesystem::remove(outputFile); |
| 475 | |
| 476 | BOOST_REQUIRE_THROW(m_tool.exportCertificate(otherKsk, outputFile), |
| 477 | ndns::ManagementTool::Error); |
| 478 | |
| 479 | // output to std::cout |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 480 | std::string acutalOutput; |
| 481 | { |
| 482 | OutputTester tester; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 483 | m_tool.exportCertificate(otherDsk, "-"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 484 | acutalOutput = tester.buffer.str(); |
| 485 | } |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 486 | BOOST_CHECK_EQUAL(acutalOutput, dskValue); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | BOOST_FIXTURE_TEST_CASE(AddRrSet1, ManagementToolFixture) |
| 490 | { |
| 491 | // check pre-condition |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 492 | BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_RESP), |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 493 | ndns::ManagementTool::Error); |
| 494 | |
| 495 | Name zoneName("/ndns-test"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 496 | Zone zone(zoneName); |
| 497 | m_dbMgr.insert(zone); |
| 498 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 499 | BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_UNKNOWN), |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 500 | ndns::ManagementTool::Error); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 501 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, "/test", label::CERT_RR_TYPE, NDNS_RAW), |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 502 | ndns::ManagementTool::Error); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 503 | BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::NS_RR_TYPE, NDNS_RAW), |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 504 | ndns::ManagementTool::Error); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 505 | BOOST_CHECK_THROW(m_tool.addRrSet(ROOT_ZONE, "/test", label::TXT_RR_TYPE, NDNS_RAW), |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 506 | ndns::ManagementTool::Error); |
| 507 | |
| 508 | m_dbMgr.remove(zone); |
| 509 | } |
| 510 | |
| 511 | BOOST_FIXTURE_TEST_CASE(AddRrSet2, ManagementToolFixture) |
| 512 | { |
| 513 | Name zoneName("/ndns-test"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 514 | Zone zone(zoneName); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 515 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 516 | uint64_t version = 1234; |
| 517 | time::seconds ttl1(4200); |
| 518 | time::seconds ttl2(4500); |
| 519 | m_tool.createZone(zoneName, ROOT_ZONE, ttl1); |
| 520 | |
| 521 | //add NS NDNS_AUTH and check user-defined ttl |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 522 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l1", label::NS_RR_TYPE, NDNS_AUTH, 7654, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 523 | {}, DEFAULT_CERT, ttl2)); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 524 | Response response; |
| 525 | BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l1", label::NS_RR_TYPE)); |
| 526 | BOOST_CHECK_EQUAL(response.getNdnsType(), NDNS_AUTH); |
| 527 | BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(7654)); |
| 528 | BOOST_CHECK_EQUAL(response.getFreshnessPeriod(), ttl2); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 529 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 530 | // add NS NDNS_RESP and check default ttl |
| 531 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l2", label::NS_RR_TYPE, NDNS_RESP, 15)); |
| 532 | BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", label::NS_RR_TYPE)); |
| 533 | BOOST_CHECK_EQUAL(response.getNdnsType(), NDNS_RESP); |
| 534 | BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(15)); |
| 535 | BOOST_CHECK_EQUAL(response.getFreshnessPeriod(), ttl1); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 536 | |
| 537 | //add TXT NDNS_RESP and check rr |
| 538 | std::string test = "oops"; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 539 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/l2", label::TXT_RR_TYPE, NDNS_RESP, version, |
| 540 | {"oops", "again"})); |
| 541 | BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", label::TXT_RR_TYPE)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 542 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 543 | BOOST_REQUIRE_EQUAL(response.getRrs().size(), 2); |
| 544 | { |
| 545 | const Block& block = response.getRrs()[0]; |
| 546 | std::string someString(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 547 | BOOST_CHECK_EQUAL("oops", someString); |
| 548 | } |
| 549 | |
| 550 | { |
| 551 | const Block& block = response.getRrs()[1]; |
| 552 | std::string someString(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 553 | BOOST_CHECK_EQUAL("again", someString); |
| 554 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 555 | |
| 556 | //add user defined type |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 557 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, "/l2", name::Component("USER-DEFINED"), NDNS_RAW, |
| 558 | 1112223, {"10.10.0.1", "SECOND_INVALID_DATA"}), |
| 559 | ndns::ManagementTool::Error); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 560 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 561 | m_tool.addRrSet(zoneName, "/l2", name::Component("USER-DEFINED"), NDNS_RAW, 1112223, |
| 562 | {"10.10.0.1"}); |
| 563 | BOOST_CHECK_NO_THROW(response = findResponse(zone, "/l2", name::Component("USER-DEFINED"))); |
| 564 | BOOST_CHECK_EQUAL(response.getVersion(), name::Component::fromVersion(1112223)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 565 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 566 | std::string actualValue(reinterpret_cast<const char*>(response.getAppContent().value()), |
| 567 | response.getAppContent().value_size()); |
| 568 | BOOST_CHECK_EQUAL(actualValue, "10.10.0.1"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | BOOST_FIXTURE_TEST_CASE(AddRrSet3, ManagementToolFixture) |
| 572 | { |
| 573 | // check pre-condition |
| 574 | Name zoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 575 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 576 | std::string certPath = TEST_CERTDIR.string(); |
| 577 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error); |
| 578 | |
| 579 | m_tool.createZone(zoneName, ROOT_ZONE); |
| 580 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, certPath), ndns::ManagementTool::Error); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | BOOST_FIXTURE_TEST_CASE(AddRrSet4, ManagementToolFixture) |
| 584 | { |
| 585 | Name parentZoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 586 | Name zoneName = Name(parentZoneName).append("/child-zone"); |
| 587 | |
| 588 | Zone parentZone(parentZoneName); |
| 589 | |
| 590 | m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 591 | m_tool.createZone(zoneName, parentZoneName); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 592 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 593 | std::vector<Name>&& certs = getCerts(zoneName); |
| 594 | BOOST_REQUIRE_EQUAL(certs.size(), 2); |
| 595 | std::sort(certs.begin(), certs.end()); |
| 596 | |
| 597 | Name& ksk = certs[0]; |
| 598 | // Name& dsk = certs[1]; |
| 599 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 600 | std::string output = TEST_CERTDIR.string() + "/ss.cert"; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 601 | m_tool.exportCertificate(ksk, output); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 602 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 603 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output)); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 604 | BOOST_CHECK_NO_THROW(findIdCert(parentZone, ksk)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 605 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 606 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, "/child-zone", |
| 607 | label::NS_RR_TYPE, NDNS_RESP)); |
| 608 | BOOST_CHECK_NO_THROW(findRrSet(parentZone, "/child-zone", label::NS_RR_TYPE)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 609 | |
| 610 | //add KSK ID-CERT with illegal name and convert it |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 611 | Name iZoneName = Name(parentZoneName).append("illegal"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 612 | Name illegalCertName = m_keyChain.createIdentity(iZoneName); |
| 613 | m_tool.exportCertificate(illegalCertName, output); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 614 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 615 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 616 | Name legalCertName = |
| 617 | Name(parentZoneName) |
| 618 | .append("KEY") |
| 619 | .append("illegal") |
| 620 | .append(illegalCertName.getSubName(3)); |
| 621 | BOOST_CHECK_NO_THROW(findIdCert(parentZone, legalCertName)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | BOOST_FIXTURE_TEST_CASE(AddRrSet5, ManagementToolFixture) |
| 625 | { |
| 626 | //check using user provided certificate |
| 627 | Name parentZoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 628 | Name zoneName = Name(parentZoneName).append("child-zone"); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 629 | |
| 630 | Name dskName = m_keyChain.generateRsaKeyPair(parentZoneName, false); |
| 631 | shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName); |
| 632 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 633 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 634 | // check addRrSet1 |
| 635 | m_tool.createZone(parentZoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 636 | m_tool.createZone(zoneName, parentZoneName); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 637 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 638 | std::vector<Name>&& certs = getCerts(zoneName); |
| 639 | BOOST_REQUIRE_EQUAL(certs.size(), 2); |
| 640 | std::sort(certs.begin(), certs.end()); |
| 641 | |
| 642 | Name& ksk = certs[0]; |
| 643 | // Name& dsk = certs[1]; |
| 644 | |
| 645 | std::string output = TEST_CERTDIR.string() + "/ss.cert"; |
| 646 | m_tool.exportCertificate(ksk, output); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 647 | |
| 648 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, output, time::seconds(4600), |
| 649 | dskCert->getName())); |
| 650 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 651 | // check addRrSet2 |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 652 | Name label1("/net/ndnsim1"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 653 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(parentZoneName, "/l1", label::NS_RR_TYPE, NDNS_AUTH, -1, {}, |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 654 | dskCert->getName())); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 657 | BOOST_FIXTURE_TEST_CASE(AddRrSet6, ManagementToolFixture) |
| 658 | { |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 659 | //check invalid output |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 660 | Name parentZoneName("/ndns-test"); |
| 661 | Name zoneName = Name(parentZoneName).append("child-zone"); |
| 662 | m_tool.createZone(zoneName, parentZoneName); |
| 663 | |
Jiewen Tan | d2d2182 | 2015-03-19 15:37:03 -0700 | [diff] [blame] | 664 | Name content = "invalid data packet"; |
| 665 | std::string output = TEST_CERTDIR.string() + "/ss.cert"; |
| 666 | ndn::io::save(content, output); |
| 667 | |
| 668 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, output), ndns::ManagementTool::Error); |
| 669 | } |
| 670 | |
Jiewen Tan | 8cd35ea | 2015-03-20 00:44:23 -0700 | [diff] [blame] | 671 | BOOST_FIXTURE_TEST_CASE(AddRrSet7, ManagementToolFixture) |
| 672 | { |
| 673 | //check version control |
| 674 | Name parentZoneName("/ndns-test"); |
| 675 | Name zoneName = Name(parentZoneName).append("child-zone"); |
| 676 | m_tool.createZone(zoneName, parentZoneName); |
| 677 | |
| 678 | Name label("/label"); |
| 679 | uint64_t version = 110; |
| 680 | |
| 681 | m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version); |
| 682 | // throw error when adding duplicated rrset with the same version |
| 683 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version), |
| 684 | ndns::ManagementTool::Error); |
| 685 | version--; |
| 686 | // throw error when adding duplicated rrset with older version |
| 687 | BOOST_CHECK_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version), |
| 688 | ndns::ManagementTool::Error); |
| 689 | |
| 690 | version++; |
| 691 | version++; |
| 692 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, label, label::NS_RR_TYPE, NDNS_RESP, version)); |
| 693 | |
| 694 | Zone zone(zoneName); |
| 695 | m_dbMgr.find(zone); |
| 696 | Rrset rrset; |
| 697 | rrset.setZone(&zone); |
| 698 | rrset.setLabel(label); |
| 699 | rrset.setType(label::NS_RR_TYPE); |
| 700 | m_dbMgr.find(rrset); |
| 701 | |
| 702 | BOOST_CHECK_EQUAL(rrset.getVersion(), name::Component::fromVersion(version)); |
| 703 | } |
| 704 | |
Jiewen Tan | 74d745c | 2015-03-20 01:40:41 -0700 | [diff] [blame] | 705 | BOOST_FIXTURE_TEST_CASE(AddRrSet8, ManagementToolFixture) |
| 706 | { |
| 707 | //check input with different formats |
| 708 | Name parentZoneName("/ndns-test"); |
| 709 | Name zoneName = Name(parentZoneName).append("child-zone"); |
| 710 | m_tool.createZone(zoneName, parentZoneName); |
| 711 | |
| 712 | std::string output = TEST_CERTDIR.string() + "/a.cert"; |
| 713 | |
| 714 | // base64 |
| 715 | Name dskName = m_keyChain.generateRsaKeyPair(zoneName, false); |
| 716 | shared_ptr<IdentityCertificate> dskCert = m_keyChain.selfSign(dskName); |
| 717 | |
| 718 | ndn::io::save(*dskCert, output, ndn::io::BASE_64); |
| 719 | BOOST_CHECK_NO_THROW( |
| 720 | m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::BASE_64)); |
| 721 | |
| 722 | // raw |
| 723 | dskName = m_keyChain.generateRsaKeyPair(zoneName, false); |
| 724 | dskCert = m_keyChain.selfSign(dskName); |
| 725 | |
| 726 | ndn::io::save(*dskCert, output, ndn::io::NO_ENCODING); |
| 727 | BOOST_CHECK_NO_THROW( |
| 728 | m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::NO_ENCODING)); |
| 729 | |
| 730 | // hex |
| 731 | dskName = m_keyChain.generateRsaKeyPair(zoneName, false); |
| 732 | dskCert = m_keyChain.selfSign(dskName); |
| 733 | |
| 734 | ndn::io::save(*dskCert, output, ndn::io::HEX); |
| 735 | BOOST_CHECK_NO_THROW( |
| 736 | m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, ndn::io::HEX)); |
| 737 | |
| 738 | // incorrect encoding input |
| 739 | dskName = m_keyChain.generateRsaKeyPair(zoneName, false); |
| 740 | dskCert = m_keyChain.selfSign(dskName); |
| 741 | |
| 742 | ndn::io::save(*dskCert, output, ndn::io::HEX); |
| 743 | BOOST_CHECK_THROW( |
| 744 | m_tool.addRrSet(zoneName, output, DEFAULT_CACHE_TTL, DEFAULT_CERT, |
| 745 | static_cast<ndn::io::IoEncoding>(127)), |
| 746 | ndns::ManagementTool::Error); |
| 747 | } |
| 748 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 749 | BOOST_FIXTURE_TEST_CASE(ListAllZones, ManagementToolFixture) |
| 750 | { |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 751 | m_tool.createZone(ROOT_ZONE, ROOT_ZONE, time::seconds(1), time::days(1), rootKsk, rootDsk); |
| 752 | m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 753 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 754 | std::string expectedValue = |
| 755 | "/ ; default-ttl=1 default-key=/dsk-1416974006466 " |
| 756 | "default-certificate=/KEY/dsk-1416974006466/ID-CERT/%FD%00%00%01I%EA%3By%28\n" |
| 757 | "/ndns-test ; default-ttl=10 default-key=/ndns-test/dsk-1416974006659 " |
| 758 | "default-certificate=/ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E\n"; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 759 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 760 | output_test_stream testOutput; |
| 761 | m_tool.listAllZones(testOutput); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 762 | BOOST_CHECK(testOutput.is_equal(expectedValue)); |
| 763 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 764 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 765 | BOOST_FIXTURE_TEST_CASE(ListZone, ManagementToolFixture) |
| 766 | { |
| 767 | m_tool.createZone("/ndns-test", ROOT_ZONE, time::seconds(10), time::days(1), otherKsk, otherDsk); |
| 768 | |
| 769 | // Add NS with NDNS_RESP |
| 770 | m_tool.addRrSet("/ndns-test", "/label1", label::NS_RR_TYPE, NDNS_RESP, 100); |
| 771 | |
| 772 | // Add NS with NDNS_AUTH |
| 773 | m_tool.addRrSet("/ndns-test", "/label2", label::NS_RR_TYPE, NDNS_AUTH, 100000); |
| 774 | |
| 775 | // Add TXT from file |
| 776 | std::string output = TEST_CERTDIR.string() + "/a.rrset"; |
| 777 | Response re1; |
| 778 | re1.setZone("/ndns-test"); |
| 779 | re1.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 780 | re1.setRrLabel("/label2"); |
| 781 | re1.setRrType(label::TXT_RR_TYPE); |
| 782 | re1.setNdnsType(NDNS_RESP); |
| 783 | re1.setVersion(name::Component::fromVersion(654321)); |
| 784 | re1.addRr("First RR"); |
| 785 | re1.addRr("Second RR"); |
| 786 | re1.addRr("Last RR"); |
| 787 | shared_ptr<Data> data1 = re1.toData(); |
| 788 | m_keyChain.sign(*data1, otherDsk); |
| 789 | ndn::io::save(*data1, output); |
| 790 | m_tool.addRrSet("/ndns-test", output); |
| 791 | |
| 792 | // Add TXT in normal way |
| 793 | m_tool.addRrSet("/ndns-test", "/label3", label::TXT_RR_TYPE, NDNS_RESP, 3333, |
| 794 | {"Hello", "World"}, otherDsk); |
| 795 | |
| 796 | // Add User-Defined |
| 797 | Response re2; |
| 798 | re2.setZone("/ndns-test"); |
| 799 | re2.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 800 | re2.setRrLabel("/label4"); |
| 801 | re2.setRrType(name::Component("USER-DEFINED")); |
| 802 | re2.setNdnsType(NDNS_RAW); |
| 803 | re2.setVersion(name::Component::fromVersion(1234567)); |
| 804 | re2.setAppContent(dataBlock(ndn::tlv::Content, "Hello", sizeof("Hello"))); |
| 805 | shared_ptr<Data> data2 = re2.toData(); |
| 806 | m_keyChain.sign(*data2, otherDsk); |
| 807 | ndn::io::save(*data2, output); |
| 808 | m_tool.addRrSet("/ndns-test", output); |
| 809 | |
| 810 | output_test_stream testOutput; |
| 811 | m_tool.listZone("/ndns-test", testOutput, true); |
| 812 | |
| 813 | std::string expectedValue = |
| 814 | "; Zone /ndns-test\n" |
| 815 | "\n" |
| 816 | "; rrset=/label1 type=NS version=%FDd signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n" |
| 817 | "/label1 10 NS \n" |
| 818 | "\n" |
| 819 | "/label2 10 NS ; content-type=NDNS-Auth version=%FD%00%01%86%A0 " |
| 820 | "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n" |
| 821 | "\n" |
| 822 | "; rrset=/label2 type=TXT version=%FD%00%09%FB%F1 " |
| 823 | "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n" |
| 824 | "/label2 10 TXT First RR\n" |
| 825 | "/label2 10 TXT Second RR\n" |
| 826 | "/label2 10 TXT Last RR\n" |
| 827 | "\n" |
| 828 | "; rrset=/label3 type=TXT version=%FD%0D%05 " |
| 829 | "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n" |
| 830 | "/label3 10 TXT Hello\n" |
| 831 | "/label3 10 TXT World\n" |
| 832 | "\n" |
| 833 | "/label4 10 USER-DEFINED ; content-type=NDNS-Raw version=%FD%00%12%D6%87 " |
| 834 | "signed-by=/ndns-test/KEY/dsk-1416974006659/ID-CERT\n" |
| 835 | "; FQZIZWxsbwA=\n" |
| 836 | "\n" |
| 837 | "/dsk-1416974006659 10 ID-CERT ; content-type=NDNS-Raw version=%FD%00%00%01I%EA%3Bz%0E " |
| 838 | "signed-by=/ndns-test/KEY/ksk-1416974006577/ID-CERT\n" |
Jiewen Tan | d1dd86d | 2015-03-20 10:26:28 -0700 | [diff] [blame^] | 839 | "; Certificate name:\n" |
| 840 | "; /ndns-test/KEY/dsk-1416974006659/ID-CERT/%FD%00%00%01I%EA%3Bz%0E\n" |
| 841 | "; Validity:\n" |
| 842 | "; NotBefore: 19700101T000000\n" |
| 843 | "; NotAfter: 20380119T031408\n" |
| 844 | "; Subject Description:\n" |
| 845 | "; 2.5.4.41: /ndns-test\n" |
| 846 | "; Public key bits: (RSA)\n" |
| 847 | "; MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAyBVC+xc/JpscSE/JdxbV\n" |
| 848 | "; pvgrh/fokNFI/2t9D5inuIFr7cc4W+LyJ4GG1xr9olMx7MHamJU1Xg3VunjhSjL8\n" |
| 849 | "; mOaeXlbS6gxWexBCtNK6U4euPB4wks/gMIKdp24mAAFb4T+mBfjcRgR+NdrjyO5C\n" |
| 850 | "; 2OqM8qbDZmD/iuEmE6GPXnuMS0o6s13yzMj9YfDh3Df2jZnHESZcmG5Qpgg22T58\n" |
| 851 | "; 7t7bRx8Ha2EC3hb29AeYKwgEKDx8JH8ZBJ80AQP321HbyjXWshJLomzy5SJZo9nA\n" |
| 852 | "; bZOYlZPCQkomz92Zc9+kpLNQwDvtRLwkZ46B+b2JpKTFARbnvugONCEBuG6zNgoi\n" |
| 853 | "; EQIB\n" |
| 854 | "; Signature Information:\n" |
| 855 | "; Signature Type: Unknown Signature Type\n" |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 856 | "\n"; |
| 857 | |
| 858 | BOOST_CHECK(testOutput.is_equal(expectedValue)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | BOOST_FIXTURE_TEST_CASE(GetRrSet, ManagementToolFixture) |
| 862 | { |
| 863 | Name zoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 864 | m_tool.createZone(zoneName, ROOT_ZONE, time::seconds(1), time::days(1), otherKsk, otherDsk); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 865 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 866 | m_tool.addRrSet(zoneName, "/label", name::Component("TXT"), NDNS_RESP, 100, |
| 867 | {"Value1", "Value2"}); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 868 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 869 | std::string expectedValue = |
| 870 | "Bv0BdwchCAluZG5zLXRlc3QIBE5ETlMIBWxhYmVsCANUWFQIAv1kFAcZAgPotAEB\n" |
| 871 | "FRC/BlZhbHVlMb8GVmFsdWUyFjMbAQEcLgcsCAluZG5zLXRlc3QIA0tFWQgRZHNr\n" |
| 872 | "LTE0MTY5NzQwMDY2NTkIB0lELUNFUlQX/QEAqhzoQmGGeFcUlZ9pEp0ohVoTEw18\n" |
| 873 | "qfB1DN6sssOpjCu3V41SHmoybPPkdW84i/h5n6TmuuePWva+fk6l9Xppd3F4lCX0\n" |
| 874 | "Tb1lyxjnLaQjBKTYxNReM3h55XY7sCvb2RpFCSDZ/PFfzuCwdWAIqcau10H9IjNm\n" |
| 875 | "2NO+m+jW43tnou9TLrNzN80bYFQc4FEwUCJUA4jPf+1NwDUMp9LWN5IILwU+Ttx6\n" |
| 876 | "45u2rK6KXfgcHR3zifwi2IZ9mEjr5bhXxvL8zP+tgaPb0t6O9gujry0an2h+hUH5\n" |
| 877 | "Aldt49RIyipHXO7R2736ZqkUZ553DSIUhaYPuXRlByNbiW6Za+LMPgJaPg==\n"; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 878 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 879 | output_test_stream testOutput; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 880 | m_tool.getRrSet(zoneName, "/label", name::Component("TXT"), testOutput); |
| 881 | BOOST_CHECK(testOutput.is_equal(expectedValue)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | BOOST_FIXTURE_TEST_CASE(RemoveRrSet, ManagementToolFixture) |
| 885 | { |
| 886 | Name zoneName("/ndns-test"); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 887 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 888 | m_tool.createZone(zoneName, ROOT_ZONE); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 889 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 890 | BOOST_CHECK_NO_THROW(m_tool.addRrSet(zoneName, "/label", label::NS_RR_TYPE, NDNS_RESP)); |
| 891 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 892 | Zone zone(zoneName); |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 893 | BOOST_CHECK_NO_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 894 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 895 | BOOST_CHECK_NO_THROW(m_tool.removeRrSet(zoneName, "/label", label::NS_RR_TYPE)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 896 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 897 | BOOST_CHECK_THROW(findRrSet(zone, "/label", label::NS_RR_TYPE), Error); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | BOOST_AUTO_TEST_SUITE_END() |
| 901 | |
| 902 | } // namespace tests |
| 903 | } // namespace ndns |
| 904 | } // namespace ndn |