blob: 4b6af1274e98c6d1331769dea156a182c13da5f1 [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));
Yumin Xiaa484ba72016-11-10 20:40:12 -080078 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK);
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
Yumin Xia4e561892016-10-21 10:48:01 -070088BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(KeyQuery, 2)
89
Shock Jiangcde28712014-10-19 21:17:20 -070090BOOST_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.setQueryType(ndns::label::NDNS_CERT_QUERY);
94 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 Xiaa484ba72016-11-10 20:40:12 -0800112 // will ask for the existing record (will have type NDNS_BLOB, 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 Xiaa484ba72016-11-10 20:40:12 -0800120 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_BLOB);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700121 });
Shock Jiangcde28712014-10-19 21:17:20 -0700122
Shock Jiange1a81fd2014-11-20 20:25:49 -0800123 q.setRrLabel("dsk-1");
Shock Jiangcde28712014-10-19 21:17:20 -0700124
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000125 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700126 run();
127
128 BOOST_CHECK_EQUAL(nDataBack, 2);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800129
130 // explicit interest with correct version
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000131 face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FDd"));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800132
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000133 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800134 ++nDataBack;
135
136 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700137 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800138 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_BLOB);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800139 });
140
141 run();
142 BOOST_CHECK_EQUAL(nDataBack, 3);
143
144 // explicit interest with wrong version
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000145 face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FD010101010"));
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800146
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000147 face.onSendData.connectSingleShot([&] (const Data& data) {
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800148 ++nDataBack;
149
150 Response resp;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700151 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800152 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK);
Alexander Afanasyevaa46c272016-03-09 12:54:12 -0800153 });
154
155 run();
156 BOOST_CHECK_EQUAL(nDataBack, 4);
Shock Jiangcde28712014-10-19 21:17:20 -0700157}
158
159BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
160{
161 Response re;
162 re.setZone(zone);
163 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800164 re.setRrLabel(Name("net"));
Shock Jiangcde28712014-10-19 21:17:20 -0700165 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800166 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700167
168 std::string str = "ns1.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 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000171 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700172
173 shared_ptr<Data> data = re.toData();
174 m_keyChain.sign(*data, m_certName);
175
Yumin Xia6343c5b2016-10-20 15:45:50 -0700176 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700177 const Block& block = data->wireEncode();
178 Name name;
179 name.append(block);
180
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;
188 NDNS_LOG_TRACE("get Data back");
189 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
190 Response resp;
191
Yumin Xia6343c5b2016-10-20 15:45:50 -0700192 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800193 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700194 BOOST_CHECK_GT(resp.getRrs().size(), 0);
195 Block block = resp.getRrs()[0];
196 block.parse();
197 int ret = -1;
198 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
199 Block::element_const_iterator val = block.elements_begin();
200 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
201 ret = readNonNegativeInteger(*val);
202 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700203 });
Shock Jiangcde28712014-10-19 21:17:20 -0700204
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000205 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700206 run();
207
208 BOOST_CHECK_EQUAL(hasDataBack, true);
209}
210
211BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
212{
213 Response re;
214 re.setZone(zone);
215 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800216 re.setRrLabel(Name("net-XYZ")); // insert new records
Shock Jiangcde28712014-10-19 21:17:20 -0700217 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800218 re.setContentType(NDNS_RESP);
Shock Jiangcde28712014-10-19 21:17:20 -0700219
220 std::string str = "ns1.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 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000223 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiangcde28712014-10-19 21:17:20 -0700224
225 shared_ptr<Data> data = re.toData();
226 m_keyChain.sign(*data, m_certName);
227
Yumin Xia6343c5b2016-10-20 15:45:50 -0700228 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiangcde28712014-10-19 21:17:20 -0700229 const Block& block = data->wireEncode();
230 Name name;
231 name.append(block);
232
233 q.setRrLabel(name);
234 q.setRrType(label::NDNS_UPDATE_LABEL);
235
236 bool hasDataBack = false;
237
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000238 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiangcde28712014-10-19 21:17:20 -0700239 hasDataBack = true;
240 NDNS_LOG_TRACE("get Data back");
241 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
242 Response resp;
243
Yumin Xia6343c5b2016-10-20 15:45:50 -0700244 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800245 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiangcde28712014-10-19 21:17:20 -0700246 BOOST_CHECK_GT(resp.getRrs().size(), 0);
247 Block block = resp.getRrs()[0];
248 block.parse();
249 int ret = -1;
250 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
251 Block::element_const_iterator val = block.elements_begin();
252 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
253 ret = readNonNegativeInteger(*val);
254 BOOST_CHECK_EQUAL(ret, 0);
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700255 });
Shock Jiangcde28712014-10-19 21:17:20 -0700256
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000257 face.receive(q.toInterest());
Shock Jiangcde28712014-10-19 21:17:20 -0700258 run();
259
260 BOOST_CHECK_EQUAL(hasDataBack, true);
261}
262
Shock Jiange1a81fd2014-11-20 20:25:49 -0800263BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert)
264{
265 Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
266 std::vector<CertificateSubjectDescription> desc;
267 time::system_clock::TimePoint notBefore = time::system_clock::now();
268 time::system_clock::TimePoint notAfter = notBefore + time::days(365);
269 shared_ptr<IdentityCertificate> dskCert =
270 m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName,
271 notBefore, notAfter, desc);
272
273 m_keyChain.sign(*dskCert, m_certName);
274 m_keyChain.addCertificateAsKeyDefault(*dskCert);
275 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: "
276 << dskCert->getSignature().getKeyLocator().getName());
277
278 Rrset rrset(&m_root);
279 Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1);
280 rrset.setLabel(label);
281 rrset.setType(label::CERT_RR_TYPE);
282 rrset.setVersion(dskCert->getName().get(-1));
283 rrset.setTtl(m_root.getTtl());
284 rrset.setData(dskCert->wireEncode());
285 m_session.insert(rrset);
286 NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name="
287 << dskCert->getName() << " rrLabel=" << label);
288
289 Response re;
290 re.setZone(zone);
291 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
292 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
293 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800294 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800295
296 std::string str = "ns1.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 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000299 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800300
301 shared_ptr<Data> data = re.toData();
302 m_keyChain.sign(*data, dskCert->getName());
303
Yumin Xia6343c5b2016-10-20 15:45:50 -0700304 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800305 const Block& block = data->wireEncode();
306 Name name;
307 name.append(block);
308
309 q.setRrLabel(name);
310 q.setRrType(label::NDNS_UPDATE_LABEL);
311
312 bool hasDataBack = false;
313
314 // no data back, since the Update cannot pass verification
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000315 face.onSendData.connectSingleShot([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800316 hasDataBack = true;
317 BOOST_FAIL("UNEXPECTED");
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700318 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800319
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000320 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800321 run();
322
323 BOOST_CHECK_EQUAL(hasDataBack, false);
324}
325
326class NameServerFixture2 : public DbTestData
327{
328public:
329 NameServerFixture2()
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000330 : face(io, m_keyChain, {false, true})
331 , validatorFace(io, m_keyChain, {false, true})
Shock Jiange1a81fd2014-11-20 20:25:49 -0800332 , zone(m_root.getName())
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000333 , validator(validatorFace) // different face for validator
334 , server(zone, m_certName, face, m_session, m_keyChain, validator)
Shock Jiange1a81fd2014-11-20 20:25:49 -0800335 {
336 // ensure prefix is registered
337 run();
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700338
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000339 validatorFace.onSendInterest.connect([this] (const Interest& interest) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800340 NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName());
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700341
342 shared_ptr<const Interest> i = interest.shared_from_this();
343 io.post([i, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000344 face.receive(*i);
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700345 });
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700346 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800347 }
348
349 void
350 run()
351 {
352 io.poll();
353 io.reset();
354 }
355
356public:
357 boost::asio::io_service io;
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000358 ndn::util::DummyClientFace face;
359 ndn::util::DummyClientFace validatorFace;
Shock Jiange1a81fd2014-11-20 20:25:49 -0800360 const Name& zone;
361 Validator validator;
362 ndns::NameServer server;
363};
364
365BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2)
366{
367 Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
368 std::vector<CertificateSubjectDescription> desc;
369 time::system_clock::TimePoint notBefore = time::system_clock::now();
370 time::system_clock::TimePoint notAfter = notBefore + time::days(365);
371 shared_ptr<IdentityCertificate> dskCert =
372 m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName,
373 notBefore, notAfter, desc);
374
375 m_keyChain.sign(*dskCert, m_certName);
376 m_keyChain.addCertificateAsKeyDefault(*dskCert);
377 NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: "
378 << dskCert->getSignature().getKeyLocator().getName());
379
380 Rrset rrset(&m_root);
381 Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1);
382 rrset.setLabel(label);
383 rrset.setType(label::CERT_RR_TYPE);
384 rrset.setVersion(dskCert->getName().get(-1));
385 rrset.setTtl(m_root.getTtl());
386 rrset.setData(dskCert->wireEncode());
387 m_session.insert(rrset);
388 NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name="
389 << dskCert->getName() << " rrLabel=" << label);
390
391 Response re;
392 re.setZone(zone);
393 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
394 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
395 re.setRrType(label::NS_RR_TYPE);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800396 re.setContentType(NDNS_RESP);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800397
398 std::string str = "ns1.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 str = "ns2.ndnsim.net";
Junxiao Shi767f35c2016-07-23 01:54:42 +0000401 re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size()));
Shock Jiange1a81fd2014-11-20 20:25:49 -0800402
403 shared_ptr<Data> data = re.toData();
404 m_keyChain.sign(*data, dskCert->getName());
405
Yumin Xia6343c5b2016-10-20 15:45:50 -0700406 Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
Shock Jiange1a81fd2014-11-20 20:25:49 -0800407 const Block& block = data->wireEncode();
408 Name name;
409 name.append(block);
410
411 q.setRrLabel(name);
412 q.setRrType(label::NDNS_UPDATE_LABEL);
413
414 bool hasDataBack = false;
415
416 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000417 face.onSendData.connect([&] (const Data& data) {
Shock Jiange1a81fd2014-11-20 20:25:49 -0800418 if (regex->match(data.getName())) {
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700419 shared_ptr<const Data> d = data.shared_from_this();
420 io.post([d, this] {
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000421 validatorFace.receive(*d); // It's data requested by validator
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700422 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800423 }
424 else {
425 // cert is requested by validator
426 hasDataBack = true;
427 NDNS_LOG_TRACE("get Data back");
428 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
429 Response resp;
430
Yumin Xia6343c5b2016-10-20 15:45:50 -0700431 BOOST_CHECK_NO_THROW(resp.fromData(zone, data));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800432 BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough
Shock Jiange1a81fd2014-11-20 20:25:49 -0800433 BOOST_CHECK_GT(resp.getRrs().size(), 0);
434 Block block = resp.getRrs()[0];
435 block.parse();
436 int ret = -1;
437 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
438 Block::element_const_iterator val = block.elements_begin();
439 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
440 ret = readNonNegativeInteger(*val);
441 BOOST_CHECK_EQUAL(ret, 0);
442 }
Junxiao Shi8f5be2a2015-01-06 10:06:43 -0700443 });
Shock Jiange1a81fd2014-11-20 20:25:49 -0800444
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000445 face.receive(q.toInterest());
Shock Jiange1a81fd2014-11-20 20:25:49 -0800446 run();
447
448 BOOST_CHECK_EQUAL(hasDataBack, true);
449}
450
Shock Jiangcde28712014-10-19 21:17:20 -0700451BOOST_AUTO_TEST_SUITE_END()
452
453} // namespace tests
454} // namespace ndns
455} // namespace ndn