blob: 76daa2f96ff59d89d8d2a74fa3cb47f33887b140 [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 Pesavento1bff1b22020-06-08 18:46:05 -04003 * Copyright (c) 2014-2020, 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"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080021#include "daemon/db-mgr.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070022#include "clients/response.hpp"
23#include "clients/query.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070024
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080025#include "test-common.hpp"
26#include "unit/database-test-data.hpp"
Shock Jiangcde28712014-10-19 21:17:20 -070027
Yumin Xia2c509c22017-02-09 14:37:36 -080028#include <ndn-cxx/util/regex.hpp>
29
Shock Jiangcde28712014-10-19 21:17:20 -070030namespace ndn {
31namespace ndns {
32namespace tests {
33
Alexander Afanasyev08d18742018-03-15 16:31:28 -040034NDNS_LOG_INIT(NameServerTest);
Shock Jiangcde28712014-10-19 21:17:20 -070035
36class 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;
Yumin Xia2c509c22017-02-09 14:37:36 -080060 unique_ptr<security::v2::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;
76 NDNS_LOG_TRACE("get Data back");
77 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
78
79 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -070080 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -080081 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070082 });
Shock Jiangcde28712014-10-19 21:17:20 -070083
Junxiao Shibbf7ac82016-07-14 14:45:15 +000084 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -070085
86 run();
87
88 BOOST_CHECK_EQUAL(hasDataBack, true);
89}
90
91BOOST_AUTO_TEST_CASE(KeyQuery)
92{
Yumin Xia6343c5b2016-10-20 15:45:50 -070093 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070094 q.setRrType(ndns::label::CERT_RR_TYPE);
95
96 size_t nDataBack = 0;
97
98 // will ask for non-existing record
Junxiao Shibbf7ac82016-07-14 14:45:15 +000099 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700100 ++nDataBack;
101 NDNS_LOG_TRACE("get Data back");
102 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
103
104 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700105 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800106 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700107 });
Shock Jiangcde28712014-10-19 21:17:20 -0700108
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000109 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700110 run();
111
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800112 // will ask for the existing record (will have type NDNS_KEY, as it is certificate)
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000113 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700114 ++nDataBack;
115 NDNS_LOG_TRACE("get Data back");
116 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
117
118 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700119 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800120 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700121 });
Shock Jiangcde28712014-10-19 21:17:20 -0700122
Yumin Xia2c509c22017-02-09 14:37:36 -0800123 Response certResp;
124 certResp.fromData(zone, m_cert);
125 q.setRrLabel(certResp.getRrLabel());
Shock Jiangcde28712014-10-19 21:17:20 -0700126
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000127 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700128 run();
129
130 BOOST_CHECK_EQUAL(nDataBack, 2);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800131
132 // explicit interest with correct version
Yumin Xia2c509c22017-02-09 14:37:36 -0800133 face.receive(Interest(m_cert.getName()));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800134
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000135 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800136 ++nDataBack;
137
138 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700139 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800140 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800141 });
142
143 run();
144 BOOST_CHECK_EQUAL(nDataBack, 3);
145
146 // explicit interest with wrong version
Yumin Xia2c509c22017-02-09 14:37:36 -0800147 Name wrongName = m_cert.getName().getPrefix(-1);
148 wrongName.appendVersion();
149 face.receive(Interest(wrongName));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800150
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000151 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800152 ++nDataBack;
153
154 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700155 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800156 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800157 });
158
159 run();
160 BOOST_CHECK_EQUAL(nDataBack, 4);
Shock Jiangcde28712014-10-19 21:17:20 -0700161}
162
163BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
164{
165 Response re;
166 re.setZone(zone);
167 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800168 re.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -0700169 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800170 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700171
172 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000173 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700174 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000175 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700176
177 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800178 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700179
Yumin Xia6343c5b2016-10-20 15:45:50 -0700180 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700181 const Block& block = data->wireEncode();
182 Name name;
183 name.append(block);
184
185 q.setRrLabel(name);
186 q.setRrType(label::NDNS_UPDATE_LABEL);
187
188 bool hasDataBack = false;
189
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000190 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700191 hasDataBack = true;
192 NDNS_LOG_TRACE("get Data back");
193 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
194 Response resp;
195
Yumin Xia6343c5b2016-10-20 15:45:50 -0700196 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800197 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700198 BOOST_CHECK_GT(resp.getRrs().size(), 0);
199 Block block = resp.getRrs()[0];
200 block.parse();
201 int ret = -1;
202 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
203 Block::element_const_iterator val = block.elements_begin();
204 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
205 ret = readNonNegativeInteger(*val);
206 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700207 });
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000208 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700209 run();
210
211 BOOST_CHECK_EQUAL(hasDataBack, true);
212}
213
214BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
215{
216 Response re;
217 re.setZone(zone);
218 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800219 re.setRrLabel(Name("net-XYZ")); // insert new records
Shock Jiangcde28712014-10-19 21:17:20 -0700220 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800221 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700222
223 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000224 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700225 str = "ns2.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
228 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800229 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiangcde28712014-10-19 21:17:20 -0700230
Yumin Xia6343c5b2016-10-20 15:45:50 -0700231 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700232 const Block& block = data->wireEncode();
233 Name name;
234 name.append(block);
235
236 q.setRrLabel(name);
237 q.setRrType(label::NDNS_UPDATE_LABEL);
238
239 bool hasDataBack = false;
240
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000241 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700242 hasDataBack = true;
243 NDNS_LOG_TRACE("get Data back");
244 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
245 Response resp;
246
Yumin Xia6343c5b2016-10-20 15:45:50 -0700247 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800248 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700249 BOOST_CHECK_GT(resp.getRrs().size(), 0);
250 Block block = resp.getRrs()[0];
251 block.parse();
252 int ret = -1;
253 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
254 Block::element_const_iterator val = block.elements_begin();
255 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
256 ret = readNonNegativeInteger(*val);
257 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700258 });
Shock Jiangcde28712014-10-19 21:17:20 -0700259
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000260 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700261 run();
262
263 BOOST_CHECK_EQUAL(hasDataBack, true);
264}
265
Shock Jiange1a81fd2014-11-20 20:25:49 -0800266BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
267{
Yumin Xia2c509c22017-02-09 14:37:36 -0800268 Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
269 Key dsk = m_keyChain.createKey(zoneIdentity);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800270
Yumin Xia2c509c22017-02-09 14:37:36 -0800271 Name dskCertName = dsk.getName();
272 dskCertName
273 .append("CERT")
274 .appendVersion();
275 Certificate dskCert;
276 dskCert.setName(dskCertName);
277 dskCert.setContentType(ndn::tlv::ContentType_Key);
278 dskCert.setFreshnessPeriod(time::hours(1));
279 dskCert.setContent(dsk.getPublicKey().data(), dsk.getPublicKey().size());
280 SignatureInfo info;
281 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
282 time::system_clock::now() + time::days(365)));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800283
Yumin Xia2c509c22017-02-09 14:37:36 -0800284 m_keyChain.sign(dskCert, security::signingByCertificate(m_cert));
285 m_keyChain.setDefaultCertificate(dsk, dskCert);
286
287 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: "
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400288 << dskCert.getKeyLocator()->getName());
Yumin Xia2c509c22017-02-09 14:37:36 -0800289
290 Rrset rrset(&m_test);
291 Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800292 rrset.setLabel(label);
293 rrset.setType(label::CERT_RR_TYPE);
Yumin Xia2c509c22017-02-09 14:37:36 -0800294 rrset.setVersion(dskCert.getName().get(-1));
295 rrset.setTtl(m_test.getTtl());
296 rrset.setData(dskCert.wireEncode());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800297 m_session.insert(rrset);
Yumin Xia2c509c22017-02-09 14:37:36 -0800298 NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name="
299 << dskCert.getName() << " rrLabel=" << label);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800300
301 Response re;
302 re.setZone(zone);
303 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
304 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
305 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800306 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800307
308 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000309 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800310 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000311 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800312
313 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800314 m_keyChain.sign(*data, security::signingByCertificate(dskCert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800315
Yumin Xia6343c5b2016-10-20 15:45:50 -0700316 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800317 const Block& block = data->wireEncode();
318 Name name;
319 name.append(block);
320
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());
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700353
354 shared_ptr<const Interest> i = interest.shared_from_this();
355 io.post([i, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000356 face.receive(*i);
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700357 });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700358 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800359 }
360
361 void
362 run()
363 {
364 io.poll();
365 io.reset();
366 }
367
368public:
369 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000370 ndn::util::DummyClientFace face;
371 ndn::util::DummyClientFace validatorFace;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800372 const Name& zone;
Yumin Xia2c509c22017-02-09 14:37:36 -0800373 unique_ptr<security::v2::Validator> validator;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800374 ndns::NameServer server;
375};
376
377BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
378{
Shock Jiange1a81fd2014-11-20 20:25:49 -0800379 Response re;
380 re.setZone(zone);
381 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
382 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
383 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800384 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800385
386 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000387 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800388 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000389 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800390
391 shared_ptr<Data> data = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800392 m_keyChain.sign(*data, security::signingByCertificate(m_cert));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800393
Yumin Xia6343c5b2016-10-20 15:45:50 -0700394 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800395 const Block& block = data->wireEncode();
396 Name name;
397 name.append(block);
398
399 q.setRrLabel(name);
400 q.setRrType(label::NDNS_UPDATE_LABEL);
401
402 bool hasDataBack = false;
403
Yumin Xia2c509c22017-02-09 14:37:36 -0800404 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>");
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000405 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800406 if (regex->match(data.getName())) {
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700407 shared_ptr<const Data> d = data.shared_from_this();
408 io.post([d, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000409 validatorFace.receive(*d); // It's data requested by validator
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700410 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800411 }
412 else {
413 // cert is requested by validator
414 hasDataBack = true;
415 NDNS_LOG_TRACE("get Data back");
416 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
417 Response resp;
418
Yumin Xia6343c5b2016-10-20 15:45:50 -0700419 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800420 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiange1a81fd2014-11-20 20:25:49 -0800421 BOOST_CHECK_GT(resp.getRrs().size(), 0);
422 Block block = resp.getRrs()[0];
423 block.parse();
424 int ret = -1;
425 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
426 Block::element_const_iterator val = block.elements_begin();
427 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
428 ret = readNonNegativeInteger(*val);
429 BOOST_CHECK_EQUAL(ret, 0);
430 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700431 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800432
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000433 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800434 run();
435
436 BOOST_CHECK_EQUAL(hasDataBack, true);
437}
438
Shock Jiangcde28712014-10-19 21:17:20 -0700439BOOST_AUTO_TEST_SUITE_END()
440
441} // namespace tests
442} // namespace ndns
443} // namespace ndn