blob: 39476ecb419d5b96878c8c916f68fb62d9786d06 [file] [log] [blame]
Yumin Xiab87b9d92016-11-14 23:41:25 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California.
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>
26#include <ndn-cxx/security/validator.hpp>
27
28namespace ndn {
29namespace ndns {
30namespace tests {
31
32NDNS_LOG_INIT("RrsetFactoryTest")
33
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
49 this->addIdentity(TEST_IDENTITY_NAME);
50 m_certName = m_keyChain.getDefaultCertificateNameForIdentity(TEST_IDENTITY_NAME);
51 ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string());
52
53 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
54 BOOST_CHECK_GT(m_certName.size(), 0);
55 NDNS_LOG_TRACE("test certName: " << m_certName);
56 }
57
58 ~RrsetFactoryFixture()
59 {
60 m_session.close();
61 boost::filesystem::remove(TEST_DATABASE2);
62 NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
63 boost::filesystem::remove(TEST_CERT);
64 }
65
66public:
67 const boost::filesystem::path TEST_DATABASE2;
68 const Name TEST_IDENTITY_NAME;
69 const boost::filesystem::path TEST_CERT;
70 ndns::DbMgr m_session;
71 Name m_zoneName;
72 Name m_certName;
73};
74
75BOOST_FIXTURE_TEST_SUITE(RrsetFactoryTest, RrsetFactoryFixture)
76
77BOOST_AUTO_TEST_CASE(CheckZoneKey)
78{
79 // zone throws check: zone not exists
80 RrsetFactory rf1(TEST_DATABASE2, "/not/exist/zone", m_keyChain, m_certName);
81 BOOST_CHECK_THROW(rf1.checkZoneKey(), ndns::RrsetFactory::Error);
82
83 // cert throws check: !matchCertificate
84 RrsetFactory rf2(TEST_DATABASE2, m_zoneName, m_keyChain, "wrongCert");
85 BOOST_CHECK_THROW(rf2.checkZoneKey(), ndns::RrsetFactory::Error);
86
87 RrsetFactory rf3(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
88 BOOST_CHECK_NO_THROW(rf3.checkZoneKey());
89}
90
91BOOST_AUTO_TEST_CASE(GenerateNsRrset)
92{
93 Name label("/nstest");
94 name::Component type = label::NS_RR_TYPE;
95 uint64_t version = 1234;
96 time::seconds ttl(2000);
97 Zone zone(m_zoneName);
98 m_session.find(zone);
99
100 RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
101
102 // rf without checkZoneKey: throw.
103 ndn::Link::DelegationSet delegations;
104 BOOST_CHECK_THROW(rf.generateNsRrset(label, type, version, ttl, delegations),
105 ndns::RrsetFactory::Error);
106 rf.checkZoneKey();
107
108 for (int i = 1; i <= 4; i++) {
109 Name name("/delegation/" + std::to_string(i));
110 delegations.insert(std::pair<uint32_t, Name>(i, name));
111 }
112
113 Rrset rrset = rf.generateNsRrset(label, type, version, ttl, delegations);
114
115 BOOST_CHECK_EQUAL(rrset.getId(), 0);
116 BOOST_REQUIRE(rrset.getZone() != nullptr);
117 BOOST_CHECK_EQUAL(*rrset.getZone(), zone);
118 BOOST_CHECK_EQUAL(rrset.getLabel(), label);
119 BOOST_CHECK_EQUAL(rrset.getType(), type);
120 BOOST_CHECK_EQUAL(rrset.getVersion().toVersion(), version);
121 BOOST_CHECK_EQUAL(rrset.getTtl(), ttl);
122
123 const Name linkName("/rrest/factory/NDNS/nstest/NS/%FD%04%D2");
124 Link link;
125 BOOST_CHECK_NO_THROW(link.wireDecode(rrset.getData()));
126
127 BOOST_CHECK_EQUAL(link.getName(), linkName);
128 BOOST_CHECK_EQUAL(link.getContentType(), NDNS_LINK);
129 BOOST_CHECK(link.getDelegations() == delegations);
130
131 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
132 BOOST_CHECK_EQUAL(Validator::verifySignature(link, cert->getPublicKeyInfo()), true);
133}
134
135BOOST_AUTO_TEST_CASE(GenerateTxtRrset)
136{
137 Name label("/txttest");
138 name::Component type = label::TXT_RR_TYPE;
139 uint64_t version = 1234;
140 time::seconds ttl(2000);
141 std::vector<std::string> txts;
142 Zone zone(m_zoneName);
143 m_session.find(zone);
144
145 RrsetFactory rf(TEST_DATABASE2, m_zoneName, m_keyChain, m_certName);
146
147 // rf without checkZoneKey: throw.
148 BOOST_CHECK_THROW(rf.generateTxtRrset(label, type, version, ttl, txts),
149 ndns::RrsetFactory::Error);
150 rf.checkZoneKey();
151 BOOST_CHECK_NO_THROW(rf.generateTxtRrset(label, type, version, ttl, txts));
152 rf.checkZoneKey();
153
154 for (int i = 1; i <= 4; i++) {
155 txts.push_back(std::to_string(i));
156 }
157
158 Rrset rrset = rf.generateTxtRrset(label, type, version, ttl, txts);
159
160 BOOST_CHECK_EQUAL(rrset.getId(), 0);
161 BOOST_CHECK_EQUAL(*rrset.getZone(), zone);
162 BOOST_CHECK_EQUAL(rrset.getLabel(), label);
163 BOOST_CHECK_EQUAL(rrset.getType(), type);
164 BOOST_CHECK_EQUAL(rrset.getVersion().toVersion(), version);
165 BOOST_CHECK_EQUAL(rrset.getTtl(), ttl);
166
167 Name dataName = m_zoneName.append(label::NDNS_ITERATIVE_QUERY)
168 .append(label)
169 .append(type)
170 .append(rrset.getVersion());
171
172 Data data;
173 BOOST_CHECK_NO_THROW(data.wireDecode(rrset.getData()));
174
175 BOOST_CHECK_EQUAL(data.getName(), dataName);
176 BOOST_CHECK_EQUAL(data.getContentType(), NDNS_RESP);
177
178 BOOST_CHECK(txts == RrsetFactory::wireDecodeTxt(data.getContent()));
179
180 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
181 BOOST_CHECK(Validator::verifySignature(data, cert->getPublicKeyInfo()));
182}
183
184BOOST_AUTO_TEST_SUITE_END()
185
186} // namespace tests
187} // namespace ndns
188} // namespace ndn