blob: 35555dd34181c7f1a7fe1c45535f7a65b8d79aa7 [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"
Davide Pesaventobdd88c12020-11-26 00:35:08 -050025
26#include "boost-test.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080027#include "unit/database-test-data.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070028
Davide Pesaventobdd88c12020-11-26 00:35:08 -050029#include <ndn-cxx/util/dummy-client-face.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080030#include <ndn-cxx/util/regex.hpp>
31
Shock Jiangcde28712014-10-19 21:17:20 -070032namespace ndn {
33namespace ndns {
34namespace tests {
35
Shock Jiangcde28712014-10-19 21:17:20 -070036class NameServerFixture : public DbTestData
37{
38public:
39 NameServerFixture()
Junxiao Shibbf7ac82016-07-14 14:45:15 +000040 : face({false, true})
Yumin Xia2c509c22017-02-09 14:37:36 -080041 , zone(m_test.getName())
42 , validator(NdnsValidatorBuilder::create(face))
43 , server(zone, m_certName, face, m_session, m_keyChain, *validator)
Shock Jiangcde28712014-10-19 21:17:20 -070044 {
45 // ensure prefix is registered
46 run();
Yumin Xiafa2bce72017-04-09 16:20:25 -070047 advanceClocks(time::milliseconds(10), 1);
Shock Jiangcde28712014-10-19 21:17:20 -070048 }
49
50 void
51 run()
52 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000053 face.getIoService().poll();
54 face.getIoService().reset();
Shock Jiangcde28712014-10-19 21:17:20 -070055 }
56
57public:
Junxiao Shibbf7ac82016-07-14 14:45:15 +000058 ndn::util::DummyClientFace face;
Shock Jiangcde28712014-10-19 21:17:20 -070059 const Name& zone;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040060 unique_ptr<security::Validator> validator;
Shock Jiangcde28712014-10-19 21:17:20 -070061 ndns::NameServer server;
62};
63
64BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture)
65
66BOOST_AUTO_TEST_CASE(NdnsQuery)
67{
Yumin Xia6343c5b2016-10-20 15:45:50 -070068 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -080069 q.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -070070 q.setRrType(ndns::label::NS_RR_TYPE);
71
72 bool hasDataBack = false;
73
Junxiao Shibbf7ac82016-07-14 14:45:15 +000074 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -070075 hasDataBack = true;
Shock Jiangcde28712014-10-19 21:17:20 -070076 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
77
78 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -070079 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -080080 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070081 });
Shock Jiangcde28712014-10-19 21:17:20 -070082
Junxiao Shibbf7ac82016-07-14 14:45:15 +000083 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -070084
85 run();
86
87 BOOST_CHECK_EQUAL(hasDataBack, true);
88}
89
90BOOST_AUTO_TEST_CASE(KeyQuery)
91{
Yumin Xia6343c5b2016-10-20 15:45:50 -070092 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070093 q.setRrType(ndns::label::CERT_RR_TYPE);
94
95 size_t nDataBack = 0;
96
97 // will ask for non-existing record
Junxiao Shibbf7ac82016-07-14 14:45:15 +000098 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -070099 ++nDataBack;
Shock Jiangcde28712014-10-19 21:17:20 -0700100 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
101
102 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700103 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800104 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700105 });
Shock Jiangcde28712014-10-19 21:17:20 -0700106
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000107 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700108 run();
109
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800110 // will ask for the existing record (will have type NDNS_KEY, as it is certificate)
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000111 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700112 ++nDataBack;
Shock Jiangcde28712014-10-19 21:17:20 -0700113 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
114
115 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700116 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800117 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700118 });
Shock Jiangcde28712014-10-19 21:17:20 -0700119
Yumin Xia2c509c22017-02-09 14:37:36 -0800120 Response certResp;
121 certResp.fromData(zone, m_cert);
122 q.setRrLabel(certResp.getRrLabel());
Shock Jiangcde28712014-10-19 21:17:20 -0700123
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000124 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700125 run();
126
127 BOOST_CHECK_EQUAL(nDataBack, 2);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800128
129 // explicit interest with correct version
Davide Pesavento35e93542022-03-07 21:47:03 -0500130 face.receive(Interest(m_cert.getName()));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800131
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000132 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800133 ++nDataBack;
134
135 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700136 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800137 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800138 });
139
140 run();
141 BOOST_CHECK_EQUAL(nDataBack, 3);
142
143 // explicit interest with wrong version
Yumin Xia2c509c22017-02-09 14:37:36 -0800144 Name wrongName = m_cert.getName().getPrefix(-1);
145 wrongName.appendVersion();
Davide Pesavento35e93542022-03-07 21:47:03 -0500146 face.receive(Interest(wrongName));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800147
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000148 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800149 ++nDataBack;
150
151 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700152 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800153 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800154 });
155
156 run();
157 BOOST_CHECK_EQUAL(nDataBack, 4);
Shock Jiangcde28712014-10-19 21:17:20 -0700158}
159
160BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
161{
162 Response re;
163 re.setZone(zone);
164 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800165 re.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -0700166 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800167 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700168
169 std::string str = "ns1.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400170 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiangcde28712014-10-19 21:17:20 -0700171 str = "ns2.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400172 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiangcde28712014-10-19 21:17:20 -0700173
Davide Pesavento98026122022-03-14 22:00:03 -0400174 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800175 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700176
Yumin Xia6343c5b2016-10-20 15:45:50 -0700177 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700178 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400179 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiangcde28712014-10-19 21:17:20 -0700180
181 q.setRrLabel(name);
182 q.setRrType(label::NDNS_UPDATE_LABEL);
183
184 bool hasDataBack = false;
185
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000186 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700187 hasDataBack = true;
Shock Jiangcde28712014-10-19 21:17:20 -0700188 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
189 Response resp;
190
Yumin Xia6343c5b2016-10-20 15:45:50 -0700191 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800192 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Davide Pesavento38fd3982022-04-18 22:22:02 -0400193 BOOST_TEST_REQUIRE(resp.getRrs().size() > 0);
Shock Jiangcde28712014-10-19 21:17:20 -0700194 Block block = resp.getRrs()[0];
195 block.parse();
196 int ret = -1;
197 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
Davide Pesavento38fd3982022-04-18 22:22:02 -0400198 auto val = block.elements_begin();
Shock Jiangcde28712014-10-19 21:17:20 -0700199 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
200 ret = readNonNegativeInteger(*val);
201 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700202 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000203 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700204 run();
205
206 BOOST_CHECK_EQUAL(hasDataBack, true);
207}
208
209BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
210{
211 Response re;
212 re.setZone(zone);
213 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800214 re.setRrLabel(Name("net-XYZ")); // insert new records
Shock Jiangcde28712014-10-19 21:17:20 -0700215 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800216 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700217
218 std::string str = "ns1.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400219 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiangcde28712014-10-19 21:17:20 -0700220 str = "ns2.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400221 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiangcde28712014-10-19 21:17:20 -0700222
Davide Pesavento98026122022-03-14 22:00:03 -0400223 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800224 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700225
Yumin Xia6343c5b2016-10-20 15:45:50 -0700226 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700227 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400228 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiangcde28712014-10-19 21:17:20 -0700229
230 q.setRrLabel(name);
231 q.setRrType(label::NDNS_UPDATE_LABEL);
232
233 bool hasDataBack = false;
234
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000235 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700236 hasDataBack = true;
Shock Jiangcde28712014-10-19 21:17:20 -0700237 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
238 Response resp;
239
Yumin Xia6343c5b2016-10-20 15:45:50 -0700240 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800241 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Davide Pesavento38fd3982022-04-18 22:22:02 -0400242 BOOST_TEST_REQUIRE(resp.getRrs().size() > 0);
Shock Jiangcde28712014-10-19 21:17:20 -0700243 Block block = resp.getRrs()[0];
244 block.parse();
245 int ret = -1;
246 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
Davide Pesavento38fd3982022-04-18 22:22:02 -0400247 auto val = block.elements_begin();
Shock Jiangcde28712014-10-19 21:17:20 -0700248 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
249 ret = readNonNegativeInteger(*val);
250 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700251 });
Shock Jiangcde28712014-10-19 21:17:20 -0700252
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000253 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700254 run();
255
256 BOOST_CHECK_EQUAL(hasDataBack, true);
257}
258
Shock Jiange1a81fd2014-11-20 20:25:49 -0800259BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
260{
Yumin Xia2c509c22017-02-09 14:37:36 -0800261 Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
262 Key dsk = m_keyChain.createKey(zoneIdentity);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800263
Yumin Xia2c509c22017-02-09 14:37:36 -0800264 Name dskCertName = dsk.getName();
265 dskCertName
266 .append("CERT")
267 .appendVersion();
268 Certificate dskCert;
269 dskCert.setName(dskCertName);
270 dskCert.setContentType(ndn::tlv::ContentType_Key);
271 dskCert.setFreshnessPeriod(time::hours(1));
Davide Pesavento98026122022-03-14 22:00:03 -0400272 dskCert.setContent(dsk.getPublicKey());
Yumin Xia2c509c22017-02-09 14:37:36 -0800273 SignatureInfo info;
274 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
275 time::system_clock::now() + time::days(365)));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800276
Yumin Xia2c509c22017-02-09 14:37:36 -0800277 m_keyChain.sign(dskCert, security::signingByCertificate(m_cert));
278 m_keyChain.setDefaultCertificate(dsk, dskCert);
Davide Pesavento38fd3982022-04-18 22:22:02 -0400279 BOOST_TEST_MESSAGE("Added Certificate=" << dskCert.getName()
280 << " KeyLocator=" << dskCert.getKeyLocator()->getName());
Yumin Xia2c509c22017-02-09 14:37:36 -0800281
282 Rrset rrset(&m_test);
283 Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800284 rrset.setLabel(label);
285 rrset.setType(label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800286 rrset.setVersion(dskCert.getName().get(-1));
287 rrset.setTtl(m_test.getTtl());
288 rrset.setData(dskCert.wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800289 m_session.insert(rrset);
Davide Pesavento38fd3982022-04-18 22:22:02 -0400290 BOOST_TEST_MESSAGE(m_test << " added CERT RR with name=" << dskCert.getName() << " label=" << label);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800291
292 Response re;
293 re.setZone(zone);
294 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
295 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
296 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800297 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800298
299 std::string str = "ns1.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400300 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800301 str = "ns2.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400302 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800303
Davide Pesavento98026122022-03-14 22:00:03 -0400304 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800305 m_keyChain.sign(*data, security::signingByCertificate(dskCert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800306
Yumin Xia6343c5b2016-10-20 15:45:50 -0700307 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800308 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400309 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800310
311 q.setRrLabel(name);
312 q.setRrType(label::NDNS_UPDATE_LABEL);
313
314 bool hasDataBack = false;
315
316 // no data back, since the Update cannot pass verification
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400317 face.onSendData.connectSingleShot([&] (const Data&) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800318 hasDataBack = true;
Davide Pesavento38fd3982022-04-18 22:22:02 -0400319 BOOST_ERROR("unexpected onSendData");
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700320 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800321
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000322 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800323 run();
324
325 BOOST_CHECK_EQUAL(hasDataBack, false);
326}
327
328class NameServerFixture2 : public DbTestData
329{
330public:
331 NameServerFixture2()
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000332 : face(io, m_keyChain, {false, true})
333 , validatorFace(io, m_keyChain, {false, true})
Yumin Xia2c509c22017-02-09 14:37:36 -0800334 , zone(m_test.getName())
335 , validator(NdnsValidatorBuilder::create(validatorFace)) // different face for validator
336 , server(zone, m_certName, face, m_session, m_keyChain, *validator)
Shock Jiange1a81fd2014-11-20 20:25:49 -0800337 {
338 // ensure prefix is registered
339 run();
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700340
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000341 validatorFace.onSendInterest.connect([this] (const Interest& interest) {
Davide Pesavento38fd3982022-04-18 22:22:02 -0400342 BOOST_TEST_MESSAGE("Interest on validatorFace: " << interest);
343 io.post([this, i = interest.shared_from_this()] { face.receive(*i); });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700344 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800345 }
346
347 void
348 run()
349 {
350 io.poll();
351 io.reset();
352 }
353
354public:
355 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000356 ndn::util::DummyClientFace face;
357 ndn::util::DummyClientFace validatorFace;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800358 const Name& zone;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400359 unique_ptr<security::Validator> validator;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800360 ndns::NameServer server;
361};
362
363BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
364{
Shock Jiange1a81fd2014-11-20 20:25:49 -0800365 Response re;
366 re.setZone(zone);
367 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
368 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
369 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800370 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800371
372 std::string str = "ns1.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400373 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800374 str = "ns2.ndnsim.net";
Davide Pesavento38fd3982022-04-18 22:22:02 -0400375 re.addRr(makeStringBlock(ndns::tlv::RrData, str));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800376
Davide Pesavento98026122022-03-14 22:00:03 -0400377 auto data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800378 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800379
Yumin Xia6343c5b2016-10-20 15:45:50 -0700380 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800381 Name name;
Davide Pesavento98026122022-03-14 22:00:03 -0400382 name.append(ndn::tlv::GenericNameComponent, data->wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800383
384 q.setRrLabel(name);
385 q.setRrType(label::NDNS_UPDATE_LABEL);
386
387 bool hasDataBack = false;
Davide Pesavento38fd3982022-04-18 22:22:02 -0400388 auto regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>");
Shock Jiange1a81fd2014-11-20 20:25:49 -0800389
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000390 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800391 if (regex->match(data.getName())) {
Davide Pesavento38fd3982022-04-18 22:22:02 -0400392 io.post([this, d = data.shared_from_this()] {
393 validatorFace.receive(*d); // It's data requested by validator
394 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800395 }
396 else {
397 // cert is requested by validator
398 hasDataBack = true;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800399 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
400 Response resp;
401
Yumin Xia6343c5b2016-10-20 15:45:50 -0700402 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800403 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Davide Pesavento38fd3982022-04-18 22:22:02 -0400404 BOOST_TEST_REQUIRE(resp.getRrs().size() > 0);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800405 Block block = resp.getRrs()[0];
406 block.parse();
407 int ret = -1;
408 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
Davide Pesavento98026122022-03-14 22:00:03 -0400409 auto val = block.elements_begin();
Shock Jiange1a81fd2014-11-20 20:25:49 -0800410 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
411 ret = readNonNegativeInteger(*val);
412 BOOST_CHECK_EQUAL(ret, 0);
413 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700414 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800415
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000416 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800417 run();
418
419 BOOST_CHECK_EQUAL(hasDataBack, true);
420}
421
Shock Jiangcde28712014-10-19 21:17:20 -0700422BOOST_AUTO_TEST_SUITE_END()
423
424} // namespace tests
425} // namespace ndns
426} // namespace ndn