blob: 39c92861693fa5d6ad6a254dd368a3a014a12c80 [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/*
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003 * Copyright (c) 2014-2018, 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"
21
22#include "test-common.hpp"
23#include "mgmt/management-tool.hpp"
24
25#include <boost/lexical_cast.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080026#include <ndn-cxx/security/verification-helpers.hpp>
Yumin Xiab87b9d92016-11-14 23:41:25 -080027
28namespace ndn {
29namespace ndns {
30namespace tests {
31
Alexander Afanasyev08d18742018-03-15 16:31:28 -040032NDNS_LOG_INIT(RrsetFactoryTest);
Yumin Xiab87b9d92016-11-14 23:41:25 -080033
34class RrsetFactoryFixture : public IdentityManagementFixture
35{
36public:
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 Xia2c509c22017-02-09 14:37:36 -080049 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 Xiab87b9d92016-11-14 23:41:25 -080055
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
69public:
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 Xia2c509c22017-02-09 14:37:36 -080076 Identity m_identity;
77 Certificate m_cert;
Yumin Xiab87b9d92016-11-14 23:41:25 -080078};
79
80BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture)
81
82BOOST_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 Xia2c509c22017-02-09 14:37:36 -080090 BOOST_CHECK_THROW(rf2.checkZoneKey(), std::runtime_error);
Yumin Xiab87b9d92016-11-14 23:41:25 -080091
92 RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
93 BOOST_CHECK_NO_THROW(rf3.checkZoneKey());
94}
95
96BOOST_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 Xia2c509c22017-02-09 14:37:36 -0800108 ndn::DelegationList delegations;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700109 BOOST_CHECK_THROW(rf.generateNsRrset(label, version, ttl, delegations),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800110 ndns::RrsetFactory::Error);
111 rf.checkZoneKey();
112
113 for (int i = 1; i <= 4; i++) {
114 Name name("/delegation/" + std::to_string(i));
Yumin Xia2c509c22017-02-09 14:37:36 -0800115 delegations.insert(i, name);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800116 }
117
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700118 Rrset rrset = rf.generateNsRrset(label, version, ttl, delegations);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800119
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 Xia2c509c22017-02-09 14:37:36 -0800134 BOOST_CHECK(link.getDelegationList() == delegations);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800135
Yumin Xia2c509c22017-02-09 14:37:36 -0800136 // BOOST_CHECK_EQUAL(Validator::verifySignature(link, m_cert.getPublicKeyInfo()), true);
137 security::verifySignature(link, m_cert);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800138}
139
140BOOST_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 Xiad4e8ce52017-03-17 19:56:52 -0700153 BOOST_CHECK_THROW(rf.generateTxtRrset(label, version, ttl, txts),
Yumin Xiab87b9d92016-11-14 23:41:25 -0800154 ndns::RrsetFactory::Error);
155 rf.checkZoneKey();
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700156 BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, version, ttl, txts));
Yumin Xiab87b9d92016-11-14 23:41:25 -0800157 rf.checkZoneKey();
158
159 for (int i = 1; i <= 4; i++) {
160 txts.push_back(std::to_string(i));
161 }
162
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700163 Rrset rrset = rf.generateTxtRrset(label, version, ttl, txts);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800164
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 Xia2c509c22017-02-09 14:37:36 -0800185 // shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
186 // BOOST_CHECK(Validator::verifySignature(data, cert->getPublicKeyInfo()));
187 security::verifySignature(data, m_cert);
Yumin Xiab87b9d92016-11-14 23:41:25 -0800188}
189
190BOOST_AUTO_TEST_SUITE_END()
191
192} // namespace tests
193} // namespace ndns
194} // namespace ndn