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