blob: 578533d3ae744757f3db8ad08ec5b60e17226d6d [file] [log] [blame]
Shock Jiangcde28712014-10-19 21:17:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
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
20#include "daemon/db-mgr.hpp"
21#include "daemon/name-server.hpp"
22#include "clients/response.hpp"
23#include "clients/query.hpp"
24#include "logger.hpp"
25
26#include "../database-test-data.hpp"
27#include "../../boost-test.hpp"
28
29#include <boost/filesystem.hpp>
30#include <ndn-cxx/face.hpp>
31#include <ndn-cxx/util/dummy-client-face.hpp>
32
33namespace ndn {
34namespace ndns {
35namespace tests {
36
37NDNS_LOG_INIT("NameServerTest");
38
39class NameServerFixture : public DbTestData
40{
41public:
42 NameServerFixture()
43 : face(ndn::util::makeDummyClientFace({ false, true }))
44 , zone(m_net.getName())
45 , validator(*face)
46 , server(zone, m_certName, *face, m_session, m_keyChain, validator)
47 {
48 // ensure prefix is registered
49 run();
50 }
51
52 void
53 run()
54 {
55 face->getIoService().poll();
56 face->getIoService().reset();
57 }
58
59public:
60 shared_ptr<ndn::util::DummyClientFace> face;
61 Name hint;
62 const Name& zone;
63 Validator validator;
64 ndns::NameServer server;
65};
66
67BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture)
68
69BOOST_AUTO_TEST_CASE(NdnsQuery)
70{
71 Query q(hint, zone, ndns::label::NDNS_ITERATIVE_QUERY);
72 q.setRrLabel(Name("ndnsim"));
73 q.setRrType(ndns::label::NS_RR_TYPE);
74
75 bool hasDataBack = false;
76
77 face->onData += [&] (const Data& data) {
78 hasDataBack = true;
79 NDNS_LOG_TRACE("get Data back");
80 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
81
82 Response resp;
83 BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data));
84 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP);
85 };
86
87 face->receive(q.toInterest());
88
89 run();
90
91 BOOST_CHECK_EQUAL(hasDataBack, true);
92}
93
94BOOST_AUTO_TEST_CASE(KeyQuery)
95{
96 Query q(hint, zone, ndns::label::NDNS_ITERATIVE_QUERY);
97 q.setQueryType(ndns::label::NDNS_CERT_QUERY);
98 q.setRrType(ndns::label::CERT_RR_TYPE);
99
100 size_t nDataBack = 0;
101
102 // will ask for non-existing record
103 face->onData += [&] (const Data& data) {
104 ++nDataBack;
105 NDNS_LOG_TRACE("get Data back");
106 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
107
108 Response resp;
109 BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data));
110 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_NACK);
111 };
112
113 face->receive(q.toInterest());
114 run();
115
116 // will ask for the existing record (will have type NDNS_RAW, as it is certificate)
117 face->onData.clear();
118 face->onData += [&] (const Data& data) {
119 ++nDataBack;
120 NDNS_LOG_TRACE("get Data back");
121 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
122
123 Response resp;
124 BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data));
125 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RAW);
126 };
127
128 q.setRrLabel("dsk-3");
129
130 face->receive(q.toInterest());
131 run();
132
133 BOOST_CHECK_EQUAL(nDataBack, 2);
134}
135
136BOOST_AUTO_TEST_CASE(UpdateReplaceRr)
137{
138 Response re;
139 re.setZone(zone);
140 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
141 re.setRrLabel(Name("ndnsim"));
142 re.setRrType(label::NS_RR_TYPE);
143 re.setNdnsType(NDNS_RESP);
144
145 std::string str = "ns1.ndnsim.net";
146 re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size()));
147 str = "ns2.ndnsim.net";
148 re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size()));
149
150 shared_ptr<Data> data = re.toData();
151 m_keyChain.sign(*data, m_certName);
152
153 Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
154 const Block& block = data->wireEncode();
155 Name name;
156 name.append(block);
157
158 q.setRrLabel(name);
159 q.setRrType(label::NDNS_UPDATE_LABEL);
160
161 bool hasDataBack = false;
162
163 face->onData += [&] (const Data& data) {
164 hasDataBack = true;
165 NDNS_LOG_TRACE("get Data back");
166 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
167 Response resp;
168
169 BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data));
170 std::cout << resp << std::endl;
171 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough
172 BOOST_CHECK_GT(resp.getRrs().size(), 0);
173 Block block = resp.getRrs()[0];
174 block.parse();
175 int ret = -1;
176 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
177 Block::element_const_iterator val = block.elements_begin();
178 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
179 ret = readNonNegativeInteger(*val);
180 BOOST_CHECK_EQUAL(ret, 0);
181 };
182
183 face->receive(q.toInterest());
184 run();
185
186 BOOST_CHECK_EQUAL(hasDataBack, true);
187}
188
189BOOST_AUTO_TEST_CASE(UpdateInsertNewRr)
190{
191 Response re;
192 re.setZone(zone);
193 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
194 re.setRrLabel(Name("ndnsim-XYZ")); // insert new records
195 re.setRrType(label::NS_RR_TYPE);
196 re.setNdnsType(NDNS_RESP);
197
198 std::string str = "ns1.ndnsim.net";
199 re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size()));
200 str = "ns2.ndnsim.net";
201 re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size()));
202
203 shared_ptr<Data> data = re.toData();
204 m_keyChain.sign(*data, m_certName);
205
206 Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY);
207 const Block& block = data->wireEncode();
208 Name name;
209 name.append(block);
210
211 q.setRrLabel(name);
212 q.setRrType(label::NDNS_UPDATE_LABEL);
213
214 bool hasDataBack = false;
215
216 face->onData += [&] (const Data& data) {
217 hasDataBack = true;
218 NDNS_LOG_TRACE("get Data back");
219 BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName());
220 Response resp;
221
222 BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data));
223 std::cout << resp << std::endl;
224 BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough
225 BOOST_CHECK_GT(resp.getRrs().size(), 0);
226 Block block = resp.getRrs()[0];
227 block.parse();
228 int ret = -1;
229 BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData);
230 Block::element_const_iterator val = block.elements_begin();
231 BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code
232 ret = readNonNegativeInteger(*val);
233 BOOST_CHECK_EQUAL(ret, 0);
234 };
235
236 face->receive(q.toInterest());
237 run();
238
239 BOOST_CHECK_EQUAL(hasDataBack, true);
240}
241
242BOOST_AUTO_TEST_SUITE_END()
243
244} // namespace tests
245} // namespace ndns
246} // namespace ndn