blob: a0ec0f521f4c5e0453efa8275fa876b3be06d5eb [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/*
Davide Pesavento35e93542022-03-07 21:47:03 -05003 * Copyright (c) 2014-2022, 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
Davide Pesavento35e93542022-03-07 21:47:03 -0500136 face.receive(Interest(m_cert.getName()));
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();
Davide Pesavento35e93542022-03-07 21:47:03 -0500152 face.receive(Interest(wrongName));
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
Davide Pesavento98026122022-03-14 22:00:03 -0400180 auto 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 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400185 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiangcde28712014-10-19 21:17:20 -0700186
187 q.setRrLabel(name);
188 q.setRrType(label::NDNS_UPDATE_LABEL);
189
190 bool hasDataBack = false;
191
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000192 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700193 hasDataBack = true;
194 NDNS_LOG_TRACE("get Data back");
195 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
196 Response resp;
197
Yumin Xia6343c5b2016-10-20 15:45:50 -0700198 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800199 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700200 BOOST_CHECK_GT(resp.getRrs().size(), 0);
201 Block block = resp.getRrs()[0];
202 block.parse();
203 int ret = -1;
204 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
205 Block::element_const_iterator val = block.elements_begin();
206 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
207 ret = readNonNegativeInteger(*val);
208 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700209 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000210 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700211 run();
212
213 BOOST_CHECK_EQUAL(hasDataBack, true);
214}
215
216BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
217{
218 Response re;
219 re.setZone(zone);
220 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800221 re.setRrLabel(Name("net-XYZ")); // insert new records
Shock Jiangcde28712014-10-19 21:17:20 -0700222 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800223 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700224
225 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000226 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700227 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000228 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700229
Davide Pesavento98026122022-03-14 22:00:03 -0400230 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800231 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700232
Yumin Xia6343c5b2016-10-20 15:45:50 -0700233 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700234 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400235 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiangcde28712014-10-19 21:17:20 -0700236
237 q.setRrLabel(name);
238 q.setRrType(label::NDNS_UPDATE_LABEL);
239
240 bool hasDataBack = false;
241
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000242 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700243 hasDataBack = true;
244 NDNS_LOG_TRACE("get Data back");
245 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
246 Response resp;
247
Yumin Xia6343c5b2016-10-20 15:45:50 -0700248 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800249 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700250 BOOST_CHECK_GT(resp.getRrs().size(), 0);
251 Block block = resp.getRrs()[0];
252 block.parse();
253 int ret = -1;
254 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
255 Block::element_const_iterator val = block.elements_begin();
256 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
257 ret = readNonNegativeInteger(*val);
258 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700259 });
Shock Jiangcde28712014-10-19 21:17:20 -0700260
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000261 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700262 run();
263
264 BOOST_CHECK_EQUAL(hasDataBack, true);
265}
266
Shock Jiange1a81fd2014-11-20 20:25:49 -0800267BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
268{
Yumin Xia2c509c22017-02-09 14:37:36 -0800269 Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
270 Key dsk = m_keyChain.createKey(zoneIdentity);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800271
Yumin Xia2c509c22017-02-09 14:37:36 -0800272 Name dskCertName = dsk.getName();
273 dskCertName
274 .append("CERT")
275 .appendVersion();
276 Certificate dskCert;
277 dskCert.setName(dskCertName);
278 dskCert.setContentType(ndn::tlv::ContentType_Key);
279 dskCert.setFreshnessPeriod(time::hours(1));
Davide Pesavento98026122022-03-14 22:00:03 -0400280 dskCert.setContent(dsk.getPublicKey());
Yumin Xia2c509c22017-02-09 14:37:36 -0800281 SignatureInfo info;
282 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
283 time::system_clock::now() + time::days(365)));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800284
Yumin Xia2c509c22017-02-09 14:37:36 -0800285 m_keyChain.sign(dskCert, security::signingByCertificate(m_cert));
286 m_keyChain.setDefaultCertificate(dsk, dskCert);
287
288 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: "
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400289 << dskCert.getKeyLocator()->getName());
Yumin Xia2c509c22017-02-09 14:37:36 -0800290
291 Rrset rrset(&m_test);
292 Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800293 rrset.setLabel(label);
294 rrset.setType(label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800295 rrset.setVersion(dskCert.getName().get(-1));
296 rrset.setTtl(m_test.getTtl());
297 rrset.setData(dskCert.wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800298 m_session.insert(rrset);
Yumin Xia2c509c22017-02-09 14:37:36 -0800299 NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name="
300 << dskCert.getName() << " rrLabel=" << label);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800301
302 Response re;
303 re.setZone(zone);
304 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
305 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
306 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800307 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800308
309 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000310 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800311 str = "ns2.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
Davide Pesavento98026122022-03-14 22:00:03 -0400314 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800315 m_keyChain.sign(*data, security::signingByCertificate(dskCert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800316
Yumin Xia6343c5b2016-10-20 15:45:50 -0700317 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800318 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400319 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800320
321 q.setRrLabel(name);
322 q.setRrType(label::NDNS_UPDATE_LABEL);
323
324 bool hasDataBack = false;
325
326 // no data back, since the Update cannot pass verification
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400327 face.onSendData.connectSingleShot([&] (const Data&) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800328 hasDataBack = true;
329 BOOST_FAIL("UNEXPECTED");
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700330 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800331
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000332 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800333 run();
334
335 BOOST_CHECK_EQUAL(hasDataBack, false);
336}
337
338class NameServerFixture2 : public DbTestData
339{
340public:
341 NameServerFixture2()
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000342 : face(io, m_keyChain, {false, true})
343 , validatorFace(io, m_keyChain, {false, true})
Yumin Xia2c509c22017-02-09 14:37:36 -0800344 , zone(m_test.getName())
345 , validator(NdnsValidatorBuilder::create(validatorFace)) // different face for validator
346 , server(zone, m_certName, face, m_session, m_keyChain, *validator)
Shock Jiange1a81fd2014-11-20 20:25:49 -0800347 {
348 // ensure prefix is registered
349 run();
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700350
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000351 validatorFace.onSendInterest.connect([this] (const Interest& interest) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800352 NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName());
Davide Pesavento98026122022-03-14 22:00:03 -0400353 auto i = interest.shared_from_this();
354 io.post([i, this] { face.receive(*i); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700355 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800356 }
357
358 void
359 run()
360 {
361 io.poll();
362 io.reset();
363 }
364
365public:
366 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000367 ndn::util::DummyClientFace face;
368 ndn::util::DummyClientFace validatorFace;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800369 const Name& zone;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400370 unique_ptr<security::Validator> validator;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800371 ndns::NameServer server;
372};
373
374BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
375{
Shock Jiange1a81fd2014-11-20 20:25:49 -0800376 Response re;
377 re.setZone(zone);
378 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
379 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
380 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800381 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800382
383 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000384 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800385 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000386 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800387
Davide Pesavento98026122022-03-14 22:00:03 -0400388 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800389 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800390
Yumin Xia6343c5b2016-10-20 15:45:50 -0700391 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800392 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400393 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800394
395 q.setRrLabel(name);
396 q.setRrType(label::NDNS_UPDATE_LABEL);
397
398 bool hasDataBack = false;
399
Yumin Xia2c509c22017-02-09 14:37:36 -0800400 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>");
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000401 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800402 if (regex->match(data.getName())) {
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700403 shared_ptr<const Data> d = data.shared_from_this();
404 io.post([d, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000405 validatorFace.receive(*d); // It's data requested by validator
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700406 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800407 }
408 else {
409 // cert is requested by validator
410 hasDataBack = true;
411 NDNS_LOG_TRACE("get Data back");
412 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
413 Response resp;
414
Yumin Xia6343c5b2016-10-20 15:45:50 -0700415 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800416 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiange1a81fd2014-11-20 20:25:49 -0800417 BOOST_CHECK_GT(resp.getRrs().size(), 0);
418 Block block = resp.getRrs()[0];
419 block.parse();
420 int ret = -1;
421 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
Davide Pesavento98026122022-03-14 22:00:03 -0400422 auto val = block.elements_begin();
Shock Jiange1a81fd2014-11-20 20:25:49 -0800423 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
424 ret = readNonNegativeInteger(*val);
425 BOOST_CHECK_EQUAL(ret, 0);
426 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700427 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800428
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000429 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800430 run();
431
432 BOOST_CHECK_EQUAL(hasDataBack, true);
433}
434
Shock Jiangcde28712014-10-19 21:17:20 -0700435BOOST_AUTO_TEST_SUITE_END()
436
437} // namespace tests
438} // namespace ndns
439} // namespace ndn