blob: 6378f43b22086dbde43c5bb65f9164f84f5eb4dc [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Eric Newberryc196a162020-09-13 14:05:16 -07003 * Copyright (c) 2014-2021, Regents of the University of California.
Shock Jiangcde28712014-10-19 21:17:20 -07004 *
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
Shock Jiangcde28712014-10-19 21:17:20 -070020#include "daemon/name-server.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070021
Davide Pesaventobdd88c12020-11-26 00:35:08 -050022#include "clients/query.hpp"
23#include "clients/response.hpp"
24#include "daemon/db-mgr.hpp"
25#include "logger.hpp"
26
27#include "boost-test.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080028#include "unit/database-test-data.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070029
Davide Pesaventobdd88c12020-11-26 00:35:08 -050030#include <ndn-cxx/util/dummy-client-face.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080031#include <ndn-cxx/util/regex.hpp>
32
Shock Jiangcde28712014-10-19 21:17:20 -070033namespace ndn {
34namespace ndns {
35namespace tests {
36
Alexander Afanasyev08d18742018-03-15 16:31:28 -040037NDNS_LOG_INIT(NameServerTest);
Shock Jiangcde28712014-10-19 21:17:20 -070038
39class NameServerFixture : public DbTestData
40{
41public:
42 NameServerFixture()
Junxiao Shibbf7ac82016-07-14 14:45:15 +000043 : face({false, true})
Yumin Xia2c509c22017-02-09 14:37:36 -080044 , zone(m_test.getName())
45 , validator(NdnsValidatorBuilder::create(face))
46 , server(zone, m_certName, face, m_session, m_keyChain, *validator)
Shock Jiangcde28712014-10-19 21:17:20 -070047 {
48 // ensure prefix is registered
49 run();
Yumin Xiafa2bce72017-04-09 16:20:25 -070050 advanceClocks(time::milliseconds(10), 1);
Shock Jiangcde28712014-10-19 21:17:20 -070051 }
52
53 void
54 run()
55 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000056 face.getIoService().poll();
57 face.getIoService().reset();
Shock Jiangcde28712014-10-19 21:17:20 -070058 }
59
60public:
Junxiao Shibbf7ac82016-07-14 14:45:15 +000061 ndn::util::DummyClientFace face;
Shock Jiangcde28712014-10-19 21:17:20 -070062 const Name& zone;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040063 unique_ptr<security::Validator> validator;
Shock Jiangcde28712014-10-19 21:17:20 -070064 ndns::NameServer server;
65};
66
67BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture)
68
69BOOST_AUTO_TEST_CASE(NdnsQuery)
70{
Yumin Xia6343c5b2016-10-20 15:45:50 -070071 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -080072 q.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -070073 q.setRrType(ndns::label::NS_RR_TYPE);
74
75 bool hasDataBack = false;
76
Junxiao Shibbf7ac82016-07-14 14:45:15 +000077 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -070078 hasDataBack = true;
79 NDNS_LOG_TRACE("get Data back");
80 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
81
82 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -070083 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -080084 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070085 });
Shock Jiangcde28712014-10-19 21:17:20 -070086
Junxiao Shibbf7ac82016-07-14 14:45:15 +000087 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -070088
89 run();
90
91 BOOST_CHECK_EQUAL(hasDataBack, true);
92}
93
94BOOST_AUTO_TEST_CASE(KeyQuery)
95{
Yumin Xia6343c5b2016-10-20 15:45:50 -070096 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070097 q.setRrType(ndns::label::CERT_RR_TYPE);
98
99 size_t nDataBack = 0;
100
101 // will ask for non-existing record
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000102 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700103 ++nDataBack;
104 NDNS_LOG_TRACE("get Data back");
105 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
106
107 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700108 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800109 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700110 });
Shock Jiangcde28712014-10-19 21:17:20 -0700111
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000112 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700113 run();
114
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800115 // will ask for the existing record (will have type NDNS_KEY, as it is certificate)
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000116 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700117 ++nDataBack;
118 NDNS_LOG_TRACE("get Data back");
119 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
120
121 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700122 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800123 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700124 });
Shock Jiangcde28712014-10-19 21:17:20 -0700125
Yumin Xia2c509c22017-02-09 14:37:36 -0800126 Response certResp;
127 certResp.fromData(zone, m_cert);
128 q.setRrLabel(certResp.getRrLabel());
Shock Jiangcde28712014-10-19 21:17:20 -0700129
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000130 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700131 run();
132
133 BOOST_CHECK_EQUAL(nDataBack, 2);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800134
135 // explicit interest with correct version
Eric Newberryc196a162020-09-13 14:05:16 -0700136 face.receive(Interest(m_cert.getName()).setCanBePrefix(false));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800137
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000138 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800139 ++nDataBack;
140
141 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700142 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800143 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800144 });
145
146 run();
147 BOOST_CHECK_EQUAL(nDataBack, 3);
148
149 // explicit interest with wrong version
Yumin Xia2c509c22017-02-09 14:37:36 -0800150 Name wrongName = m_cert.getName().getPrefix(-1);
151 wrongName.appendVersion();
Eric Newberryc196a162020-09-13 14:05:16 -0700152 face.receive(Interest(wrongName).setCanBePrefix(false));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800153
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000154 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800155 ++nDataBack;
156
157 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700158 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800159 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800160 });
161
162 run();
163 BOOST_CHECK_EQUAL(nDataBack, 4);
Shock Jiangcde28712014-10-19 21:17:20 -0700164}
165
166BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
167{
168 Response re;
169 re.setZone(zone);
170 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800171 re.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -0700172 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800173 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700174
175 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000176 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700177 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000178 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700179
180 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800181 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700182
Yumin Xia6343c5b2016-10-20 15:45:50 -0700183 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700184 const Block& block = data->wireEncode();
185 Name name;
186 name.append(block);
187
188 q.setRrLabel(name);
189 q.setRrType(label::NDNS_UPDATE_LABEL);
190
191 bool hasDataBack = false;
192
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000193 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700194 hasDataBack = true;
195 NDNS_LOG_TRACE("get Data back");
196 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
197 Response resp;
198
Yumin Xia6343c5b2016-10-20 15:45:50 -0700199 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800200 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700201 BOOST_CHECK_GT(resp.getRrs().size(), 0);
202 Block block = resp.getRrs()[0];
203 block.parse();
204 int ret = -1;
205 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
206 Block::element_const_iterator val = block.elements_begin();
207 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
208 ret = readNonNegativeInteger(*val);
209 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700210 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000211 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700212 run();
213
214 BOOST_CHECK_EQUAL(hasDataBack, true);
215}
216
217BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
218{
219 Response re;
220 re.setZone(zone);
221 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800222 re.setRrLabel(Name("net-XYZ")); // insert new records
Shock Jiangcde28712014-10-19 21:17:20 -0700223 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800224 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700225
226 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000227 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700228 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000229 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700230
231 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800232 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700233
Yumin Xia6343c5b2016-10-20 15:45:50 -0700234 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700235 const Block& block = data->wireEncode();
236 Name name;
237 name.append(block);
238
239 q.setRrLabel(name);
240 q.setRrType(label::NDNS_UPDATE_LABEL);
241
242 bool hasDataBack = false;
243
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000244 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700245 hasDataBack = true;
246 NDNS_LOG_TRACE("get Data back");
247 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
248 Response resp;
249
Yumin Xia6343c5b2016-10-20 15:45:50 -0700250 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800251 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700252 BOOST_CHECK_GT(resp.getRrs().size(), 0);
253 Block block = resp.getRrs()[0];
254 block.parse();
255 int ret = -1;
256 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
257 Block::element_const_iterator val = block.elements_begin();
258 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
259 ret = readNonNegativeInteger(*val);
260 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700261 });
Shock Jiangcde28712014-10-19 21:17:20 -0700262
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000263 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700264 run();
265
266 BOOST_CHECK_EQUAL(hasDataBack, true);
267}
268
Shock Jiange1a81fd2014-11-20 20:25:49 -0800269BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
270{
Yumin Xia2c509c22017-02-09 14:37:36 -0800271 Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
272 Key dsk = m_keyChain.createKey(zoneIdentity);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800273
Yumin Xia2c509c22017-02-09 14:37:36 -0800274 Name dskCertName = dsk.getName();
275 dskCertName
276 .append("CERT")
277 .appendVersion();
278 Certificate dskCert;
279 dskCert.setName(dskCertName);
280 dskCert.setContentType(ndn::tlv::ContentType_Key);
281 dskCert.setFreshnessPeriod(time::hours(1));
282 dskCert.setContent(dsk.getPublicKey().data(), dsk.getPublicKey().size());
283 SignatureInfo info;
284 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
285 time::system_clock::now() + time::days(365)));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800286
Yumin Xia2c509c22017-02-09 14:37:36 -0800287 m_keyChain.sign(dskCert, security::signingByCertificate(m_cert));
288 m_keyChain.setDefaultCertificate(dsk, dskCert);
289
290 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: "
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400291 << dskCert.getKeyLocator()->getName());
Yumin Xia2c509c22017-02-09 14:37:36 -0800292
293 Rrset rrset(&m_test);
294 Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800295 rrset.setLabel(label);
296 rrset.setType(label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800297 rrset.setVersion(dskCert.getName().get(-1));
298 rrset.setTtl(m_test.getTtl());
299 rrset.setData(dskCert.wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800300 m_session.insert(rrset);
Yumin Xia2c509c22017-02-09 14:37:36 -0800301 NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name="
302 << dskCert.getName() << " rrLabel=" << label);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800303
304 Response re;
305 re.setZone(zone);
306 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
307 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
308 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800309 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800310
311 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000312 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800313 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000314 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800315
316 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800317 m_keyChain.sign(*data, security::signingByCertificate(dskCert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800318
Yumin Xia6343c5b2016-10-20 15:45:50 -0700319 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800320 const Block& block = data->wireEncode();
321 Name name;
322 name.append(block);
323
324 q.setRrLabel(name);
325 q.setRrType(label::NDNS_UPDATE_LABEL);
326
327 bool hasDataBack = false;
328
329 // no data back, since the Update cannot pass verification
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400330 face.onSendData.connectSingleShot([&] (const Data&) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800331 hasDataBack = true;
332 BOOST_FAIL("UNEXPECTED");
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700333 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800334
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000335 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800336 run();
337
338 BOOST_CHECK_EQUAL(hasDataBack, false);
339}
340
341class NameServerFixture2 : public DbTestData
342{
343public:
344 NameServerFixture2()
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000345 : face(io, m_keyChain, {false, true})
346 , validatorFace(io, m_keyChain, {false, true})
Yumin Xia2c509c22017-02-09 14:37:36 -0800347 , zone(m_test.getName())
348 , validator(NdnsValidatorBuilder::create(validatorFace)) // different face for validator
349 , server(zone, m_certName, face, m_session, m_keyChain, *validator)
Shock Jiange1a81fd2014-11-20 20:25:49 -0800350 {
351 // ensure prefix is registered
352 run();
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700353
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000354 validatorFace.onSendInterest.connect([this] (const Interest& interest) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800355 NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName());
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700356
357 shared_ptr<const Interest> i = interest.shared_from_this();
358 io.post([i, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000359 face.receive(*i);
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700360 });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700361 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800362 }
363
364 void
365 run()
366 {
367 io.poll();
368 io.reset();
369 }
370
371public:
372 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000373 ndn::util::DummyClientFace face;
374 ndn::util::DummyClientFace validatorFace;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800375 const Name& zone;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400376 unique_ptr<security::Validator> validator;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800377 ndns::NameServer server;
378};
379
380BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
381{
Shock Jiange1a81fd2014-11-20 20:25:49 -0800382 Response re;
383 re.setZone(zone);
384 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
385 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
386 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800387 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800388
389 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000390 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800391 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000392 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800393
394 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800395 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800396
Yumin Xia6343c5b2016-10-20 15:45:50 -0700397 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800398 const Block& block = data->wireEncode();
399 Name name;
400 name.append(block);
401
402 q.setRrLabel(name);
403 q.setRrType(label::NDNS_UPDATE_LABEL);
404
405 bool hasDataBack = false;
406
Yumin Xia2c509c22017-02-09 14:37:36 -0800407 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>");
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000408 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800409 if (regex->match(data.getName())) {
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700410 shared_ptr<const Data> d = data.shared_from_this();
411 io.post([d, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000412 validatorFace.receive(*d); // It's data requested by validator
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700413 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800414 }
415 else {
416 // cert is requested by validator
417 hasDataBack = true;
418 NDNS_LOG_TRACE("get Data back");
419 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
420 Response resp;
421
Yumin Xia6343c5b2016-10-20 15:45:50 -0700422 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800423 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiange1a81fd2014-11-20 20:25:49 -0800424 BOOST_CHECK_GT(resp.getRrs().size(), 0);
425 Block block = resp.getRrs()[0];
426 block.parse();
427 int ret = -1;
428 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
429 Block::element_const_iterator val = block.elements_begin();
430 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
431 ret = readNonNegativeInteger(*val);
432 BOOST_CHECK_EQUAL(ret, 0);
433 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700434 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800435
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000436 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800437 run();
438
439 BOOST_CHECK_EQUAL(hasDataBack, true);
440}
441
Shock Jiangcde28712014-10-19 21:17:20 -0700442BOOST_AUTO_TEST_SUITE_END()
443
444} // namespace tests
445} // namespace ndns
446} // namespace ndn