Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 33 | namespace ndn { |
| 34 | namespace ndns { |
| 35 | namespace tests { |
| 36 | |
| 37 | NDNS_LOG_INIT("NameServerTest"); |
| 38 | |
| 39 | class NameServerFixture : public DbTestData |
| 40 | { |
| 41 | public: |
| 42 | NameServerFixture() |
| 43 | : face(ndn::util::makeDummyClientFace({ false, true })) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 44 | , zone(m_root.getName()) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
| 59 | public: |
| 60 | shared_ptr<ndn::util::DummyClientFace> face; |
| 61 | Name hint; |
| 62 | const Name& zone; |
| 63 | Validator validator; |
| 64 | ndns::NameServer server; |
| 65 | }; |
| 66 | |
| 67 | BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture) |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(NdnsQuery) |
| 70 | { |
| 71 | Query q(hint, zone, ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 72 | q.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
| 94 | BOOST_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 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 128 | q.setRrLabel("dsk-1"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 129 | |
| 130 | face->receive(q.toInterest()); |
| 131 | run(); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(nDataBack, 2); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(UpdateReplaceRr) |
| 137 | { |
| 138 | Response re; |
| 139 | re.setZone(zone); |
| 140 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 141 | re.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
| 189 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 190 | { |
| 191 | Response re; |
| 192 | re.setZone(zone); |
| 193 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 194 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame^] | 242 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 243 | { |
| 244 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 245 | std::vector<CertificateSubjectDescription> desc; |
| 246 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 247 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 248 | shared_ptr<IdentityCertificate> dskCert = |
| 249 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 250 | notBefore, notAfter, desc); |
| 251 | |
| 252 | m_keyChain.sign(*dskCert, m_certName); |
| 253 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 254 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 255 | << dskCert->getSignature().getKeyLocator().getName()); |
| 256 | |
| 257 | Rrset rrset(&m_root); |
| 258 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 259 | rrset.setLabel(label); |
| 260 | rrset.setType(label::CERT_RR_TYPE); |
| 261 | rrset.setVersion(dskCert->getName().get(-1)); |
| 262 | rrset.setTtl(m_root.getTtl()); |
| 263 | rrset.setData(dskCert->wireEncode()); |
| 264 | m_session.insert(rrset); |
| 265 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 266 | << dskCert->getName() << " rrLabel=" << label); |
| 267 | |
| 268 | Response re; |
| 269 | re.setZone(zone); |
| 270 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 271 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 272 | re.setRrType(label::NS_RR_TYPE); |
| 273 | re.setNdnsType(NDNS_RESP); |
| 274 | |
| 275 | std::string str = "ns1.ndnsim.net"; |
| 276 | re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
| 277 | str = "ns2.ndnsim.net"; |
| 278 | re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
| 279 | |
| 280 | shared_ptr<Data> data = re.toData(); |
| 281 | m_keyChain.sign(*data, dskCert->getName()); |
| 282 | |
| 283 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 284 | const Block& block = data->wireEncode(); |
| 285 | Name name; |
| 286 | name.append(block); |
| 287 | |
| 288 | q.setRrLabel(name); |
| 289 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 290 | |
| 291 | bool hasDataBack = false; |
| 292 | |
| 293 | // no data back, since the Update cannot pass verification |
| 294 | face->onData += [&] (const Data& data) { |
| 295 | hasDataBack = true; |
| 296 | BOOST_FAIL("UNEXPECTED"); |
| 297 | }; |
| 298 | |
| 299 | face->receive(q.toInterest()); |
| 300 | run(); |
| 301 | |
| 302 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 303 | } |
| 304 | |
| 305 | class NameServerFixture2 : public DbTestData |
| 306 | { |
| 307 | public: |
| 308 | NameServerFixture2() |
| 309 | : face(ndn::util::makeDummyClientFace(io, { false, true })) |
| 310 | , validatorFace(ndn::util::makeDummyClientFace(io, { false, true })) |
| 311 | , zone(m_root.getName()) |
| 312 | , validator(*validatorFace) // different face for validator |
| 313 | , server(zone, m_certName, *face, m_session, m_keyChain, validator) |
| 314 | { |
| 315 | // ensure prefix is registered |
| 316 | run(); |
| 317 | validatorFace->onInterest += [&] (const Interest& interest) { |
| 318 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
| 319 | face->receive(interest); |
| 320 | }; |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | run() |
| 325 | { |
| 326 | io.poll(); |
| 327 | io.reset(); |
| 328 | } |
| 329 | |
| 330 | public: |
| 331 | boost::asio::io_service io; |
| 332 | shared_ptr<ndn::util::DummyClientFace> face; |
| 333 | shared_ptr<ndn::util::DummyClientFace> validatorFace; |
| 334 | Name hint; |
| 335 | const Name& zone; |
| 336 | Validator validator; |
| 337 | ndns::NameServer server; |
| 338 | }; |
| 339 | |
| 340 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 341 | { |
| 342 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 343 | std::vector<CertificateSubjectDescription> desc; |
| 344 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 345 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 346 | shared_ptr<IdentityCertificate> dskCert = |
| 347 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 348 | notBefore, notAfter, desc); |
| 349 | |
| 350 | m_keyChain.sign(*dskCert, m_certName); |
| 351 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 352 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 353 | << dskCert->getSignature().getKeyLocator().getName()); |
| 354 | |
| 355 | Rrset rrset(&m_root); |
| 356 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 357 | rrset.setLabel(label); |
| 358 | rrset.setType(label::CERT_RR_TYPE); |
| 359 | rrset.setVersion(dskCert->getName().get(-1)); |
| 360 | rrset.setTtl(m_root.getTtl()); |
| 361 | rrset.setData(dskCert->wireEncode()); |
| 362 | m_session.insert(rrset); |
| 363 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 364 | << dskCert->getName() << " rrLabel=" << label); |
| 365 | |
| 366 | Response re; |
| 367 | re.setZone(zone); |
| 368 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 369 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 370 | re.setRrType(label::NS_RR_TYPE); |
| 371 | re.setNdnsType(NDNS_RESP); |
| 372 | |
| 373 | std::string str = "ns1.ndnsim.net"; |
| 374 | re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
| 375 | str = "ns2.ndnsim.net"; |
| 376 | re.addRr(dataBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
| 377 | |
| 378 | shared_ptr<Data> data = re.toData(); |
| 379 | m_keyChain.sign(*data, dskCert->getName()); |
| 380 | |
| 381 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 382 | const Block& block = data->wireEncode(); |
| 383 | Name name; |
| 384 | name.append(block); |
| 385 | |
| 386 | q.setRrLabel(name); |
| 387 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 388 | |
| 389 | bool hasDataBack = false; |
| 390 | |
| 391 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
| 392 | face->onData += [&] (const Data& data) { |
| 393 | if (regex->match(data.getName())) { |
| 394 | validatorFace->receive(data); // It's data requested by validator |
| 395 | } |
| 396 | else { |
| 397 | // cert is requested by validator |
| 398 | hasDataBack = true; |
| 399 | NDNS_LOG_TRACE("get Data back"); |
| 400 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 401 | Response resp; |
| 402 | |
| 403 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
| 404 | std::cout << resp << std::endl; |
| 405 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough |
| 406 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 407 | Block block = resp.getRrs()[0]; |
| 408 | block.parse(); |
| 409 | int ret = -1; |
| 410 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 411 | Block::element_const_iterator val = block.elements_begin(); |
| 412 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 413 | ret = readNonNegativeInteger(*val); |
| 414 | BOOST_CHECK_EQUAL(ret, 0); |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | face->receive(q.toInterest()); |
| 419 | run(); |
| 420 | |
| 421 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 422 | } |
| 423 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 424 | BOOST_AUTO_TEST_SUITE_END() |
| 425 | |
| 426 | } // namespace tests |
| 427 | } // namespace ndns |
| 428 | } // namespace ndn |