Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 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 "daemon/rrset-factory.hpp" |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 21 | #include "mgmt/management-tool.hpp" |
| 22 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 23 | #include "boost-test.hpp" |
| 24 | #include "key-chain-fixture.hpp" |
| 25 | |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 27 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/security/verification-helpers.hpp> |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | namespace tests { |
| 33 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 34 | const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db"; |
| 35 | const auto TEST_CERT = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert"; |
| 36 | |
| 37 | class RrsetFactoryFixture : public KeyChainFixture |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 38 | { |
| 39 | public: |
| 40 | RrsetFactoryFixture() |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 41 | : TEST_IDENTITY_NAME("/rrest/factory") |
| 42 | , m_session(TEST_DATABASE2.string()) |
| 43 | , m_zoneName(TEST_IDENTITY_NAME) |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 44 | { |
| 45 | Zone zone1; |
| 46 | zone1.setName(m_zoneName); |
| 47 | zone1.setTtl(time::seconds(4600)); |
Davide Pesavento | 38fd398 | 2022-04-18 22:22:02 -0400 | [diff] [blame] | 48 | m_session.insert(zone1); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 49 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 50 | Name identityName = Name(TEST_IDENTITY_NAME).append("NDNS"); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 51 | m_identity = m_keyChain.createIdentity(identityName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 52 | m_cert = m_identity.getDefaultKey().getDefaultCertificate(); |
| 53 | m_certName = m_cert.getName(); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 54 | saveIdentityCert(m_identity, TEST_CERT.string()); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | ~RrsetFactoryFixture() |
| 58 | { |
| 59 | m_session.close(); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 60 | boost::filesystem::remove(TEST_DATABASE2); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 61 | boost::filesystem::remove(TEST_CERT); |
| 62 | } |
| 63 | |
| 64 | public: |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 65 | const Name TEST_IDENTITY_NAME; |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 66 | ndns::DbMgr m_session; |
| 67 | Name m_zoneName; |
| 68 | Name m_certName; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 69 | Identity m_identity; |
| 70 | Certificate m_cert; |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture) |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(CheckZoneKey) |
| 76 | { |
| 77 | // zone throws check: zone not exists |
| 78 | RrsetFactory rf1(TEST_DATABASE2, "/not/exist/zone", m_keyChain, m_certName); |
| 79 | BOOST_CHECK_THROW(rf1.checkZoneKey(), ndns::RrsetFactory::Error); |
| 80 | |
| 81 | // cert throws check: !matchCertificate |
| 82 | RrsetFactory rf2(TEST_DATABASE2, m_zoneName, m_keyChain, "wrongCert"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 83 | BOOST_CHECK_THROW(rf2.checkZoneKey(), std::runtime_error); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 84 | |
| 85 | RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 86 | BOOST_CHECK_NO_THROW(rf3.checkZoneKey()); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(GenerateNsRrset) |
| 90 | { |
| 91 | Name label("/nstest"); |
| 92 | name::Component type = label::NS_RR_TYPE; |
| 93 | uint64_t version = 1234; |
| 94 | time::seconds ttl(2000); |
| 95 | Zone zone(m_zoneName); |
| 96 | m_session.find(zone); |
| 97 | |
| 98 | RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 99 | |
| 100 | // rf without checkZoneKey: throw. |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 101 | std::vector<Name> delegations; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 102 | BOOST_CHECK_THROW(rf.generateNsRrset(label, version, ttl, delegations), |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 103 | ndns::RrsetFactory::Error); |
| 104 | rf.checkZoneKey(); |
| 105 | |
| 106 | for (int i = 1; i <= 4; i++) { |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 107 | delegations.emplace_back("/delegation/" + std::to_string(i)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 110 | Rrset rrset = rf.generateNsRrset(label, version, ttl, delegations); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 111 | |
| 112 | BOOST_CHECK_EQUAL(rrset.getId(), 0); |
| 113 | BOOST_REQUIRE(rrset.getZone() != nullptr); |
| 114 | BOOST_CHECK_EQUAL(*rrset.getZone(), zone); |
| 115 | BOOST_CHECK_EQUAL(rrset.getLabel(), label); |
| 116 | BOOST_CHECK_EQUAL(rrset.getType(), type); |
Eric Newberry | b8adcdf | 2021-03-25 18:35:50 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(rrset.getVersion(), Name::Component::fromVersion(version)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(rrset.getTtl(), ttl); |
| 119 | |
Eric Newberry | b8adcdf | 2021-03-25 18:35:50 -0700 | [diff] [blame] | 120 | const auto linkName = Name("/rrest/factory/NDNS/nstest/NS").appendVersion(version); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 121 | Link link; |
| 122 | BOOST_CHECK_NO_THROW(link.wireDecode(rrset.getData())); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(link.getName(), linkName); |
| 125 | BOOST_CHECK_EQUAL(link.getContentType(), NDNS_LINK); |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL_COLLECTIONS( |
| 127 | link.getDelegationList().begin(), link.getDelegationList().end(), |
| 128 | delegations.begin(), delegations.end()); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 129 | |
Eric Newberry | b8adcdf | 2021-03-25 18:35:50 -0700 | [diff] [blame] | 130 | BOOST_CHECK(security::verifySignature(link, m_cert)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(GenerateTxtRrset) |
| 134 | { |
| 135 | Name label("/txttest"); |
| 136 | name::Component type = label::TXT_RR_TYPE; |
| 137 | uint64_t version = 1234; |
| 138 | time::seconds ttl(2000); |
| 139 | std::vector<std::string> txts; |
| 140 | Zone zone(m_zoneName); |
| 141 | m_session.find(zone); |
| 142 | |
| 143 | RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 144 | |
| 145 | // rf without checkZoneKey: throw. |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 146 | BOOST_CHECK_THROW(rf.generateTxtRrset(label, version, ttl, txts), |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 147 | ndns::RrsetFactory::Error); |
| 148 | rf.checkZoneKey(); |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 149 | BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, version, ttl, txts)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 150 | rf.checkZoneKey(); |
| 151 | |
| 152 | for (int i = 1; i <= 4; i++) { |
| 153 | txts.push_back(std::to_string(i)); |
| 154 | } |
| 155 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 156 | Rrset rrset = rf.generateTxtRrset(label, version, ttl, txts); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 157 | |
| 158 | BOOST_CHECK_EQUAL(rrset.getId(), 0); |
| 159 | BOOST_CHECK_EQUAL(*rrset.getZone(), zone); |
| 160 | BOOST_CHECK_EQUAL(rrset.getLabel(), label); |
| 161 | BOOST_CHECK_EQUAL(rrset.getType(), type); |
Eric Newberry | b8adcdf | 2021-03-25 18:35:50 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(rrset.getVersion(), Name::Component::fromVersion(version)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(rrset.getTtl(), ttl); |
| 164 | |
| 165 | Name dataName = m_zoneName.append(label::NDNS_ITERATIVE_QUERY) |
| 166 | .append(label) |
| 167 | .append(type) |
| 168 | .append(rrset.getVersion()); |
| 169 | |
| 170 | Data data; |
| 171 | BOOST_CHECK_NO_THROW(data.wireDecode(rrset.getData())); |
| 172 | |
| 173 | BOOST_CHECK_EQUAL(data.getName(), dataName); |
| 174 | BOOST_CHECK_EQUAL(data.getContentType(), NDNS_RESP); |
| 175 | |
| 176 | BOOST_CHECK(txts == RrsetFactory::wireDecodeTxt(data.getContent())); |
| 177 | |
Eric Newberry | b8adcdf | 2021-03-25 18:35:50 -0700 | [diff] [blame] | 178 | BOOST_CHECK(security::verifySignature(data, m_cert)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | BOOST_AUTO_TEST_SUITE_END() |
| 182 | |
| 183 | } // namespace tests |
| 184 | } // namespace ndns |
| 185 | } // namespace ndn |