blob: c081103a8d63e6ca442d0ad3fc212b4ccdf7525a [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shibbf7ac82016-07-14 14:45:15 +00003 * Copyright (c) 2014-2016, 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
28namespace ndn {
29namespace ndns {
30namespace tests {
31
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070032NDNS_LOG_INIT("NameServerTest")
Shock Jiangcde28712014-10-19 21:17:20 -070033
34class NameServerFixture : public DbTestData
35{
36public:
37 NameServerFixture()
Junxiao Shibbf7ac82016-07-14 14:45:15 +000038 : face({false, true})
Shock Jiange1a81fd2014-11-20 20:25:49 -080039 , zone(m_root.getName())
Junxiao Shibbf7ac82016-07-14 14:45:15 +000040 , validator(face)
41 , server(zone, m_certName, face, m_session, m_keyChain, validator)
Shock Jiangcde28712014-10-19 21:17:20 -070042 {
43 // ensure prefix is registered
44 run();
45 }
46
47 void
48 run()
49 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000050 face.getIoService().poll();
51 face.getIoService().reset();
Shock Jiangcde28712014-10-19 21:17:20 -070052 }
53
54public:
Junxiao Shibbf7ac82016-07-14 14:45:15 +000055 ndn::util::DummyClientFace face;
Shock Jiangcde28712014-10-19 21:17:20 -070056 const Name& zone;
57 Validator validator;
58 ndns::NameServer server;
59};
60
61BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture)
62
63BOOST_AUTO_TEST_CASE(NdnsQuery)
64{
Yumin Xia6343c5b2016-10-20 15:45:50 -070065 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -080066 q.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -070067 q.setRrType(ndns::label::NS_RR_TYPE);
68
69 bool hasDataBack = false;
70
Junxiao Shibbf7ac82016-07-14 14:45:15 +000071 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -070072 hasDataBack = true;
73 NDNS_LOG_TRACE("get Data back");
74 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
75
76 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -070077 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Shock Jiangcde28712014-10-19 21:17:20 -070078 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -070079 });
Shock Jiangcde28712014-10-19 21:17:20 -070080
Junxiao Shibbf7ac82016-07-14 14:45:15 +000081 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -070082
83 run();
84
85 BOOST_CHECK_EQUAL(hasDataBack, true);
86}
87
88BOOST_AUTO_TEST_CASE(KeyQuery)
89{
Yumin Xia6343c5b2016-10-20 15:45:50 -070090 Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -070091 q.setQueryType(ndns::label::NDNS_CERT_QUERY);
92 q.setRrType(ndns::label::CERT_RR_TYPE);
93
94 size_t nDataBack = 0;
95
96 // will ask for non-existing record
Junxiao Shibbf7ac82016-07-14 14:45:15 +000097 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -070098 ++nDataBack;
99 NDNS_LOG_TRACE("get Data back");
100 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));
Shock Jiangcde28712014-10-19 21:17:20 -0700104 BOOST_CHECK_EQUAL(resp.getNdnsType(), 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
110 // will ask for the existing record (will have type NDNS_RAW, 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;
113 NDNS_LOG_TRACE("get Data back");
114 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
115
116 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700117 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Shock Jiangcde28712014-10-19 21:17:20 -0700118 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RAW);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700119 });
Shock Jiangcde28712014-10-19 21:17:20 -0700120
Shock Jiange1a81fd2014-11-20 20:25:49 -0800121 q.setRrLabel("dsk-1");
Shock Jiangcde28712014-10-19 21:17:20 -0700122
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000123 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700124 run();
125
126 BOOST_CHECK_EQUAL(nDataBack, 2);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800127
128 // explicit interest with correct version
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000129 face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FDd"));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800130
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000131 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800132 ++nDataBack;
133
134 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700135 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800136 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RAW);
137 });
138
139 run();
140 BOOST_CHECK_EQUAL(nDataBack, 3);
141
142 // explicit interest with wrong version
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000143 face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FD010101010"));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800144
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000145 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800146 ++nDataBack;
147
148 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700149 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800150 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_NACK);
151 });
152
153 run();
154 BOOST_CHECK_EQUAL(nDataBack, 4);
Shock Jiangcde28712014-10-19 21:17:20 -0700155}
156
157BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
158{
159 Response re;
160 re.setZone(zone);
161 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800162 re.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -0700163 re.setRrType(label::NS_RR_TYPE);
164 re.setNdnsType(NDNS_RESP);
165
166 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000167 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700168 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000169 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700170
171 shared_ptr<Data> data = re.toData();
172 m_keyChain.sign(*data, m_certName);
173
Yumin Xia6343c5b2016-10-20 15:45:50 -0700174 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700175 const Block& block = data->wireEncode();
176 Name name;
177 name.append(block);
178
179 q.setRrLabel(name);
180 q.setRrType(label::NDNS_UPDATE_LABEL);
181
182 bool hasDataBack = false;
183
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000184 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700185 hasDataBack = true;
186 NDNS_LOG_TRACE("get Data back");
187 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
188 Response resp;
189
Yumin Xia6343c5b2016-10-20 15:45:50 -0700190 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Shock Jiangcde28712014-10-19 21:17:20 -0700191 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough
192 BOOST_CHECK_GT(resp.getRrs().size(), 0);
193 Block block = resp.getRrs()[0];
194 block.parse();
195 int ret = -1;
196 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
197 Block::element_const_iterator val = block.elements_begin();
198 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
199 ret = readNonNegativeInteger(*val);
200 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700201 });
Shock Jiangcde28712014-10-19 21:17:20 -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);
216 re.setNdnsType(NDNS_RESP);
217
218 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000219 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700220 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000221 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700222
223 shared_ptr<Data> data = re.toData();
224 m_keyChain.sign(*data, m_certName);
225
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 const Block& block = data->wireEncode();
228 Name name;
229 name.append(block);
230
231 q.setRrLabel(name);
232 q.setRrType(label::NDNS_UPDATE_LABEL);
233
234 bool hasDataBack = false;
235
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000236 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700237 hasDataBack = true;
238 NDNS_LOG_TRACE("get Data back");
239 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
240 Response resp;
241
Yumin Xia6343c5b2016-10-20 15:45:50 -0700242 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Shock Jiangcde28712014-10-19 21:17:20 -0700243 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough
244 BOOST_CHECK_GT(resp.getRrs().size(), 0);
245 Block block = resp.getRrs()[0];
246 block.parse();
247 int ret = -1;
248 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
249 Block::element_const_iterator val = block.elements_begin();
250 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
251 ret = readNonNegativeInteger(*val);
252 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700253 });
Shock Jiangcde28712014-10-19 21:17:20 -0700254
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000255 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700256 run();
257
258 BOOST_CHECK_EQUAL(hasDataBack, true);
259}
260
Shock Jiange1a81fd2014-11-20 20:25:49 -0800261BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
262{
263 Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
264 std::vector<CertificateSubjectDescription> desc;
265 time::system_clock::TimePoint notBefore = time::system_clock::now();
266 time::system_clock::TimePoint notAfter = notBefore + time::days(365);
267 shared_ptr<IdentityCertificate> dskCert =
268 m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName,
269 notBefore, notAfter, desc);
270
271 m_keyChain.sign(*dskCert, m_certName);
272 m_keyChain.addCertificateAsKeyDefault(*dskCert);
273 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: "
274 << dskCert->getSignature().getKeyLocator().getName());
275
276 Rrset rrset(&m_root);
277 Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1);
278 rrset.setLabel(label);
279 rrset.setType(label::CERT_RR_TYPE);
280 rrset.setVersion(dskCert->getName().get(-1));
281 rrset.setTtl(m_root.getTtl());
282 rrset.setData(dskCert->wireEncode());
283 m_session.insert(rrset);
284 NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name="
285 << dskCert->getName() << " rrLabel=" << label);
286
287 Response re;
288 re.setZone(zone);
289 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
290 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
291 re.setRrType(label::NS_RR_TYPE);
292 re.setNdnsType(NDNS_RESP);
293
294 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000295 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800296 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000297 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800298
299 shared_ptr<Data> data = re.toData();
300 m_keyChain.sign(*data, dskCert->getName());
301
Yumin Xia6343c5b2016-10-20 15:45:50 -0700302 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800303 const Block& block = data->wireEncode();
304 Name name;
305 name.append(block);
306
307 q.setRrLabel(name);
308 q.setRrType(label::NDNS_UPDATE_LABEL);
309
310 bool hasDataBack = false;
311
312 // no data back, since the Update cannot pass verification
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000313 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800314 hasDataBack = true;
315 BOOST_FAIL("UNEXPECTED");
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700316 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800317
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000318 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800319 run();
320
321 BOOST_CHECK_EQUAL(hasDataBack, false);
322}
323
324class NameServerFixture2 : public DbTestData
325{
326public:
327 NameServerFixture2()
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000328 : face(io, m_keyChain, {false, true})
329 , validatorFace(io, m_keyChain, {false, true})
Shock Jiange1a81fd2014-11-20 20:25:49 -0800330 , zone(m_root.getName())
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000331 , validator(validatorFace) // different face for validator
332 , server(zone, m_certName, face, m_session, m_keyChain, validator)
Shock Jiange1a81fd2014-11-20 20:25:49 -0800333 {
334 // ensure prefix is registered
335 run();
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700336
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000337 validatorFace.onSendInterest.connect([this] (const Interest& interest) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800338 NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName());
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700339
340 shared_ptr<const Interest> i = interest.shared_from_this();
341 io.post([i, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000342 face.receive(*i);
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700343 });
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;
359 Validator validator;
360 ndns::NameServer server;
361};
362
363BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
364{
365 Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
366 std::vector<CertificateSubjectDescription> desc;
367 time::system_clock::TimePoint notBefore = time::system_clock::now();
368 time::system_clock::TimePoint notAfter = notBefore + time::days(365);
369 shared_ptr<IdentityCertificate> dskCert =
370 m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName,
371 notBefore, notAfter, desc);
372
373 m_keyChain.sign(*dskCert, m_certName);
374 m_keyChain.addCertificateAsKeyDefault(*dskCert);
375 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: "
376 << dskCert->getSignature().getKeyLocator().getName());
377
378 Rrset rrset(&m_root);
379 Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1);
380 rrset.setLabel(label);
381 rrset.setType(label::CERT_RR_TYPE);
382 rrset.setVersion(dskCert->getName().get(-1));
383 rrset.setTtl(m_root.getTtl());
384 rrset.setData(dskCert->wireEncode());
385 m_session.insert(rrset);
386 NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name="
387 << dskCert->getName() << " rrLabel=" << label);
388
389 Response re;
390 re.setZone(zone);
391 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
392 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
393 re.setRrType(label::NS_RR_TYPE);
394 re.setNdnsType(NDNS_RESP);
395
396 std::string str = "ns1.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000397 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800398 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000399 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800400
401 shared_ptr<Data> data = re.toData();
402 m_keyChain.sign(*data, dskCert->getName());
403
Yumin Xia6343c5b2016-10-20 15:45:50 -0700404 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800405 const Block& block = data->wireEncode();
406 Name name;
407 name.append(block);
408
409 q.setRrLabel(name);
410 q.setRrType(label::NDNS_UPDATE_LABEL);
411
412 bool hasDataBack = false;
413
414 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000415 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800416 if (regex->match(data.getName())) {
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700417 shared_ptr<const Data> d = data.shared_from_this();
418 io.post([d, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000419 validatorFace.receive(*d); // It's data requested by validator
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700420 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800421 }
422 else {
423 // cert is requested by validator
424 hasDataBack = true;
425 NDNS_LOG_TRACE("get Data back");
426 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
427 Response resp;
428
Yumin Xia6343c5b2016-10-20 15:45:50 -0700429 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800430 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough
431 BOOST_CHECK_GT(resp.getRrs().size(), 0);
432 Block block = resp.getRrs()[0];
433 block.parse();
434 int ret = -1;
435 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
436 Block::element_const_iterator val = block.elements_begin();
437 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
438 ret = readNonNegativeInteger(*val);
439 BOOST_CHECK_EQUAL(ret, 0);
440 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700441 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800442
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000443 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800444 run();
445
446 BOOST_CHECK_EQUAL(hasDataBack, true);
447}
448
Shock Jiangcde28712014-10-19 21:17:20 -0700449BOOST_AUTO_TEST_SUITE_END()
450
451} // namespace tests
452} // namespace ndns
453} // namespace ndn