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 | /* |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 34 | NDNS_LOG_INIT(RrsetFactoryTest); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 36 | const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db"; |
| 37 | const auto TEST_CERT = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert"; |
| 38 | |
| 39 | class RrsetFactoryFixture : public KeyChainFixture |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | RrsetFactoryFixture() |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 43 | : TEST_IDENTITY_NAME("/rrest/factory") |
| 44 | , m_session(TEST_DATABASE2.string()) |
| 45 | , m_zoneName(TEST_IDENTITY_NAME) |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 46 | { |
| 47 | Zone zone1; |
| 48 | zone1.setName(m_zoneName); |
| 49 | zone1.setTtl(time::seconds(4600)); |
| 50 | BOOST_CHECK_NO_THROW(m_session.insert(zone1)); |
| 51 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 52 | Name identityName = Name(TEST_IDENTITY_NAME).append("NDNS"); |
| 53 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 54 | m_identity = m_keyChain.createIdentity(identityName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 55 | m_cert = m_identity.getDefaultKey().getDefaultCertificate(); |
| 56 | m_certName = m_cert.getName(); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 57 | saveIdentityCert(m_identity, TEST_CERT.string()); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 58 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 59 | NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 60 | BOOST_CHECK_GT(m_certName.size(), 0); |
| 61 | NDNS_LOG_TRACE("test certName: " << m_certName); |
| 62 | } |
| 63 | |
| 64 | ~RrsetFactoryFixture() |
| 65 | { |
| 66 | m_session.close(); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 67 | NDNS_LOG_INFO("remove database " << TEST_DATABASE2); |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 68 | boost::filesystem::remove(TEST_DATABASE2); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 69 | boost::filesystem::remove(TEST_CERT); |
| 70 | } |
| 71 | |
| 72 | public: |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 73 | const Name TEST_IDENTITY_NAME; |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 74 | ndns::DbMgr m_session; |
| 75 | Name m_zoneName; |
| 76 | Name m_certName; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 77 | Identity m_identity; |
| 78 | Certificate m_cert; |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture) |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(CheckZoneKey) |
| 84 | { |
| 85 | // zone throws check: zone not exists |
| 86 | RrsetFactory rf1(TEST_DATABASE2, "/not/exist/zone", m_keyChain, m_certName); |
| 87 | BOOST_CHECK_THROW(rf1.checkZoneKey(), ndns::RrsetFactory::Error); |
| 88 | |
| 89 | // cert throws check: !matchCertificate |
| 90 | RrsetFactory rf2(TEST_DATABASE2, m_zoneName, m_keyChain, "wrongCert"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 91 | BOOST_CHECK_THROW(rf2.checkZoneKey(), std::runtime_error); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 92 | |
| 93 | RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 94 | BOOST_CHECK_NO_THROW(rf3.checkZoneKey()); |
| 95 | } |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(GenerateNsRrset) |
| 98 | { |
| 99 | Name label("/nstest"); |
| 100 | name::Component type = label::NS_RR_TYPE; |
| 101 | uint64_t version = 1234; |
| 102 | time::seconds ttl(2000); |
| 103 | Zone zone(m_zoneName); |
| 104 | m_session.find(zone); |
| 105 | |
| 106 | RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 107 | |
| 108 | // rf without checkZoneKey: throw. |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 109 | ndn::DelegationList delegations; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 110 | BOOST_CHECK_THROW(rf.generateNsRrset(label, version, ttl, delegations), |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 111 | ndns::RrsetFactory::Error); |
| 112 | rf.checkZoneKey(); |
| 113 | |
| 114 | for (int i = 1; i <= 4; i++) { |
| 115 | Name name("/delegation/" + std::to_string(i)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 116 | delegations.insert(i, name); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 119 | Rrset rrset = rf.generateNsRrset(label, version, ttl, delegations); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 120 | |
| 121 | BOOST_CHECK_EQUAL(rrset.getId(), 0); |
| 122 | BOOST_REQUIRE(rrset.getZone() != nullptr); |
| 123 | BOOST_CHECK_EQUAL(*rrset.getZone(), zone); |
| 124 | BOOST_CHECK_EQUAL(rrset.getLabel(), label); |
| 125 | BOOST_CHECK_EQUAL(rrset.getType(), type); |
| 126 | BOOST_CHECK_EQUAL(rrset.getVersion().toVersion(), version); |
| 127 | BOOST_CHECK_EQUAL(rrset.getTtl(), ttl); |
| 128 | |
| 129 | const Name linkName("/rrest/factory/NDNS/nstest/NS/%FD%04%D2"); |
| 130 | Link link; |
| 131 | BOOST_CHECK_NO_THROW(link.wireDecode(rrset.getData())); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(link.getName(), linkName); |
| 134 | BOOST_CHECK_EQUAL(link.getContentType(), NDNS_LINK); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 135 | BOOST_CHECK(link.getDelegationList() == delegations); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 136 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 137 | // BOOST_CHECK_EQUAL(Validator::verifySignature(link, m_cert.getPublicKeyInfo()), true); |
| 138 | security::verifySignature(link, m_cert); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(GenerateTxtRrset) |
| 142 | { |
| 143 | Name label("/txttest"); |
| 144 | name::Component type = label::TXT_RR_TYPE; |
| 145 | uint64_t version = 1234; |
| 146 | time::seconds ttl(2000); |
| 147 | std::vector<std::string> txts; |
| 148 | Zone zone(m_zoneName); |
| 149 | m_session.find(zone); |
| 150 | |
| 151 | RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName); |
| 152 | |
| 153 | // rf without checkZoneKey: throw. |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 154 | BOOST_CHECK_THROW(rf.generateTxtRrset(label, version, ttl, txts), |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 155 | ndns::RrsetFactory::Error); |
| 156 | rf.checkZoneKey(); |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 157 | BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, version, ttl, txts)); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 158 | rf.checkZoneKey(); |
| 159 | |
| 160 | for (int i = 1; i <= 4; i++) { |
| 161 | txts.push_back(std::to_string(i)); |
| 162 | } |
| 163 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 164 | Rrset rrset = rf.generateTxtRrset(label, version, ttl, txts); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 165 | |
| 166 | BOOST_CHECK_EQUAL(rrset.getId(), 0); |
| 167 | BOOST_CHECK_EQUAL(*rrset.getZone(), zone); |
| 168 | BOOST_CHECK_EQUAL(rrset.getLabel(), label); |
| 169 | BOOST_CHECK_EQUAL(rrset.getType(), type); |
| 170 | BOOST_CHECK_EQUAL(rrset.getVersion().toVersion(), version); |
| 171 | BOOST_CHECK_EQUAL(rrset.getTtl(), ttl); |
| 172 | |
| 173 | Name dataName = m_zoneName.append(label::NDNS_ITERATIVE_QUERY) |
| 174 | .append(label) |
| 175 | .append(type) |
| 176 | .append(rrset.getVersion()); |
| 177 | |
| 178 | Data data; |
| 179 | BOOST_CHECK_NO_THROW(data.wireDecode(rrset.getData())); |
| 180 | |
| 181 | BOOST_CHECK_EQUAL(data.getName(), dataName); |
| 182 | BOOST_CHECK_EQUAL(data.getContentType(), NDNS_RESP); |
| 183 | |
| 184 | BOOST_CHECK(txts == RrsetFactory::wireDecodeTxt(data.getContent())); |
| 185 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 186 | // shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName); |
| 187 | // BOOST_CHECK(Validator::verifySignature(data, cert->getPublicKeyInfo())); |
| 188 | security::verifySignature(data, m_cert); |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | BOOST_AUTO_TEST_SUITE_END() |
| 192 | |
| 193 | } // namespace tests |
| 194 | } // namespace ndns |
| 195 | } // namespace ndn |