blob: 6f2eb6985d768b2fec6d3a47c9cd4e8063c19649 [file] [log] [blame]
Yumin Xiab87b9d92016-11-14 23:41:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesaventobdd88c12020-11-26 00:35:08 -05003 * Copyright (c) 2014-2020, Regents of the University of California.
Yumin Xiab87b9d92016-11-14 23:41:25 -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
20#include "daemon/rrset-factory.hpp"
Yumin Xiab87b9d92016-11-14 23:41:25 -080021#include "mgmt/management-tool.hpp"
22
Davide Pesaventobdd88c12020-11-26 00:35:08 -050023#include "boost-test.hpp"
24#include "key-chain-fixture.hpp"
25
Yumin Xiab87b9d92016-11-14 23:41:25 -080026#include <boost/lexical_cast.hpp>
Davide Pesaventobdd88c12020-11-26 00:35:08 -050027
Yumin Xia2c509c22017-02-09 14:37:36 -080028#include <ndn-cxx/security/verification-helpers.hpp>
Yumin Xiab87b9d92016-11-14 23:41:25 -080029
30namespace ndn {
31namespace ndns {
32namespace tests {
33
Alexander Afanasyev08d18742018-03-15 16:31:28 -040034NDNS_LOG_INIT(RrsetFactoryTest);
Yumin Xiab87b9d92016-11-14 23:41:25 -080035
Davide Pesaventobdd88c12020-11-26 00:35:08 -050036const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
37const auto TEST_CERT = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert";
38
39class RrsetFactoryFixture : public KeyChainFixture
Yumin Xiab87b9d92016-11-14 23:41:25 -080040{
41public:
42 RrsetFactoryFixture()
Davide Pesaventobdd88c12020-11-26 00:35:08 -050043 : TEST_IDENTITY_NAME("/rrest/factory")
44 , m_session(TEST_DATABASE2.string())
45 , m_zoneName(TEST_IDENTITY_NAME)
Yumin Xiab87b9d92016-11-14 23:41:25 -080046 {
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 Xia2c509c22017-02-09 14:37:36 -080052 Name identityName = Name(TEST_IDENTITY_NAME).append("NDNS");
53
Davide Pesaventobdd88c12020-11-26 00:35:08 -050054 m_identity = m_keyChain.createIdentity(identityName);
Yumin Xia2c509c22017-02-09 14:37:36 -080055 m_cert = m_identity.getDefaultKey().getDefaultCertificate();
56 m_certName = m_cert.getName();
Davide Pesaventobdd88c12020-11-26 00:35:08 -050057 saveIdentityCert(m_identity, TEST_CERT.string());
Yumin Xiab87b9d92016-11-14 23:41:25 -080058
Davide Pesaventobdd88c12020-11-26 00:35:08 -050059 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT);
Yumin Xiab87b9d92016-11-14 23:41:25 -080060 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 Xiab87b9d92016-11-14 23:41:25 -080067 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
Davide Pesaventobdd88c12020-11-26 00:35:08 -050068 boost::filesystem::remove(TEST_DATABASE2);
Yumin Xiab87b9d92016-11-14 23:41:25 -080069 boost::filesystem::remove(TEST_CERT);
70 }
71
72public:
Yumin Xiab87b9d92016-11-14 23:41:25 -080073 const Name TEST_IDENTITY_NAME;
Yumin Xiab87b9d92016-11-14 23:41:25 -080074 ndns::DbMgr m_session;
75 Name m_zoneName;
76 Name m_certName;
Yumin Xia2c509c22017-02-09 14:37:36 -080077 Identity m_identity;
78 Certificate m_cert;
Yumin Xiab87b9d92016-11-14 23:41:25 -080079};
80
81BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture)
82
83BOOST_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 Xia2c509c22017-02-09 14:37:36 -080091 BOOST_CHECK_THROW(rf2.checkZoneKey(), std::runtime_error);
Yumin Xiab87b9d92016-11-14 23:41:25 -080092
93 RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
94 BOOST_CHECK_NO_THROW(rf3.checkZoneKey());
95}
96
97BOOST_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 Xia2c509c22017-02-09 14:37:36 -0800109 ndn::DelegationList delegations;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700110 BOOST_CHECK_THROW(rf.generateNsRrset(label, version, ttl, delegations),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800111 ndns::RrsetFactory::Error);
112 rf.checkZoneKey();
113
114 for (int i = 1; i <= 4; i++) {
115 Name name("/delegation/" + std::to_string(i));
Yumin Xia2c509c22017-02-09 14:37:36 -0800116 delegations.insert(i, name);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800117 }
118
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700119 Rrset rrset = rf.generateNsRrset(label, version, ttl, delegations);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800120
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 Xia2c509c22017-02-09 14:37:36 -0800135 BOOST_CHECK(link.getDelegationList() == delegations);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800136
Yumin Xia2c509c22017-02-09 14:37:36 -0800137 // BOOST_CHECK_EQUAL(Validator::verifySignature(link, m_cert.getPublicKeyInfo()), true);
138 security::verifySignature(link, m_cert);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800139}
140
141BOOST_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 Xiad4e8ce52017-03-17 19:56:52 -0700154 BOOST_CHECK_THROW(rf.generateTxtRrset(label, version, ttl, txts),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800155 ndns::RrsetFactory::Error);
156 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700157 BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, version, ttl, txts));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800158 rf.checkZoneKey();
159
160 for (int i = 1; i <= 4; i++) {
161 txts.push_back(std::to_string(i));
162 }
163
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700164 Rrset rrset = rf.generateTxtRrset(label, version, ttl, txts);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800165
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 Xia2c509c22017-02-09 14:37:36 -0800186 // shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
187 // BOOST_CHECK(Validator::verifySignature(data, cert->getPublicKeyInfo()));
188 security::verifySignature(data, m_cert);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800189}
190
191BOOST_AUTO_TEST_SUITE_END()
192
193} // namespace tests
194} // namespace ndns
195} // namespace ndn