blob: 66c65dd79f2a6826014003cd6fec45022333c55b [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/*
Junxiao Shi81e98762022-01-11 18:17:24 +00003 * Copyright (c) 2014-2022, 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
Davide Pesaventobdd88c12020-11-26 00:35:08 -050034const auto TEST_DATABASE2 = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-ndns.db";
35const auto TEST_CERT = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "anchors" / "root.cert";
36
37class RrsetFactoryFixture : public KeyChainFixture
Yumin Xiab87b9d92016-11-14 23:41:25 -080038{
39public:
40 RrsetFactoryFixture()
Davide Pesaventobdd88c12020-11-26 00:35:08 -050041 : TEST_IDENTITY_NAME("/rrest/factory")
42 , m_session(TEST_DATABASE2.string())
43 , m_zoneName(TEST_IDENTITY_NAME)
Yumin Xiab87b9d92016-11-14 23:41:25 -080044 {
45 Zone zone1;
46 zone1.setName(m_zoneName);
47 zone1.setTtl(time::seconds(4600));
Davide Pesavento38fd3982022-04-18 22:22:02 -040048 m_session.insert(zone1);
Yumin Xiab87b9d92016-11-14 23:41:25 -080049
Yumin Xia2c509c22017-02-09 14:37:36 -080050 Name identityName = Name(TEST_IDENTITY_NAME).append("NDNS");
Davide Pesaventobdd88c12020-11-26 00:35:08 -050051 m_identity = m_keyChain.createIdentity(identityName);
Yumin Xia2c509c22017-02-09 14:37:36 -080052 m_cert = m_identity.getDefaultKey().getDefaultCertificate();
53 m_certName = m_cert.getName();
Davide Pesaventobdd88c12020-11-26 00:35:08 -050054 saveIdentityCert(m_identity, TEST_CERT.string());
Yumin Xiab87b9d92016-11-14 23:41:25 -080055 }
56
57 ~RrsetFactoryFixture()
58 {
59 m_session.close();
Davide Pesaventobdd88c12020-11-26 00:35:08 -050060 boost::filesystem::remove(TEST_DATABASE2);
Yumin Xiab87b9d92016-11-14 23:41:25 -080061 boost::filesystem::remove(TEST_CERT);
62 }
63
64public:
Yumin Xiab87b9d92016-11-14 23:41:25 -080065 const Name TEST_IDENTITY_NAME;
Yumin Xiab87b9d92016-11-14 23:41:25 -080066 ndns::DbMgr m_session;
67 Name m_zoneName;
68 Name m_certName;
Yumin Xia2c509c22017-02-09 14:37:36 -080069 Identity m_identity;
70 Certificate m_cert;
Yumin Xiab87b9d92016-11-14 23:41:25 -080071};
72
73BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture)
74
75BOOST_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 Xia2c509c22017-02-09 14:37:36 -080083 BOOST_CHECK_THROW(rf2.checkZoneKey(), std::runtime_error);
Yumin Xiab87b9d92016-11-14 23:41:25 -080084
85 RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
86 BOOST_CHECK_NO_THROW(rf3.checkZoneKey());
87}
88
89BOOST_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 Shi81e98762022-01-11 18:17:24 +0000101 std::vector<Name> delegations;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700102 BOOST_CHECK_THROW(rf.generateNsRrset(label, version, ttl, delegations),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800103 ndns::RrsetFactory::Error);
104 rf.checkZoneKey();
105
106 for (int i = 1; i <= 4; i++) {
Junxiao Shi81e98762022-01-11 18:17:24 +0000107 delegations.emplace_back("/delegation/" + std::to_string(i));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800108 }
109
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700110 Rrset rrset = rf.generateNsRrset(label, version, ttl, delegations);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800111
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 Newberryb8adcdf2021-03-25 18:35:50 -0700117 BOOST_CHECK_EQUAL(rrset.getVersion(), Name::Component::fromVersion(version));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800118 BOOST_CHECK_EQUAL(rrset.getTtl(), ttl);
119
Eric Newberryb8adcdf2021-03-25 18:35:50 -0700120 const auto linkName = Name("/rrest/factory/NDNS/nstest/NS").appendVersion(version);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800121 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 Shi81e98762022-01-11 18:17:24 +0000126 BOOST_CHECK_EQUAL_COLLECTIONS(
127 link.getDelegationList().begin(), link.getDelegationList().end(),
128 delegations.begin(), delegations.end());
Yumin Xiab87b9d92016-11-14 23:41:25 -0800129
Eric Newberryb8adcdf2021-03-25 18:35:50 -0700130 BOOST_CHECK(security::verifySignature(link, m_cert));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800131}
132
133BOOST_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 Xiad4e8ce52017-03-17 19:56:52 -0700146 BOOST_CHECK_THROW(rf.generateTxtRrset(label, version, ttl, txts),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800147 ndns::RrsetFactory::Error);
148 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700149 BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, version, ttl, txts));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800150 rf.checkZoneKey();
151
152 for (int i = 1; i <= 4; i++) {
153 txts.push_back(std::to_string(i));
154 }
155
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700156 Rrset rrset = rf.generateTxtRrset(label, version, ttl, txts);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800157
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 Newberryb8adcdf2021-03-25 18:35:50 -0700162 BOOST_CHECK_EQUAL(rrset.getVersion(), Name::Component::fromVersion(version));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800163 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 Newberryb8adcdf2021-03-25 18:35:50 -0700178 BOOST_CHECK(security::verifySignature(data, m_cert));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800179}
180
181BOOST_AUTO_TEST_SUITE_END()
182
183} // namespace tests
184} // namespace ndns
185} // namespace ndn