Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 37 | NDNS_LOG_INIT("NameServerTest") |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 38 | |
| 39 | class NameServerFixture : public DbTestData |
| 40 | { |
| 41 | public: |
| 42 | NameServerFixture() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 43 | : face({false, true}) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 44 | , zone(m_root.getName()) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 45 | , validator(face) |
| 46 | , server(zone, m_certName, face, m_session, m_keyChain, validator) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 47 | { |
| 48 | // ensure prefix is registered |
| 49 | run(); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | run() |
| 54 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 55 | face.getIoService().poll(); |
| 56 | face.getIoService().reset(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public: |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 60 | ndn::util::DummyClientFace face; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 77 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 85 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 87 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 103 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 111 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 113 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 114 | run(); |
| 115 | |
| 116 | // will ask for the existing record (will have type NDNS_RAW, as it is certificate) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 117 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 118 | ++nDataBack; |
| 119 | NDNS_LOG_TRACE("get Data back"); |
| 120 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 121 | |
| 122 | Response resp; |
| 123 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
| 124 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RAW); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 125 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 126 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 127 | q.setRrLabel("dsk-1"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 129 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 130 | run(); |
| 131 | |
| 132 | BOOST_CHECK_EQUAL(nDataBack, 2); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 133 | |
| 134 | // explicit interest with correct version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 135 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FDd")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 136 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 137 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 138 | ++nDataBack; |
| 139 | |
| 140 | Response resp; |
| 141 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
| 142 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RAW); |
| 143 | }); |
| 144 | |
| 145 | run(); |
| 146 | BOOST_CHECK_EQUAL(nDataBack, 3); |
| 147 | |
| 148 | // explicit interest with wrong version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 149 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FD010101010")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 150 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 151 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 152 | ++nDataBack; |
| 153 | |
| 154 | Response resp; |
| 155 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
| 156 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_NACK); |
| 157 | }); |
| 158 | |
| 159 | run(); |
| 160 | BOOST_CHECK_EQUAL(nDataBack, 4); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(UpdateReplaceRr) |
| 164 | { |
| 165 | Response re; |
| 166 | re.setZone(zone); |
| 167 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 168 | re.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 169 | re.setRrType(label::NS_RR_TYPE); |
| 170 | re.setNdnsType(NDNS_RESP); |
| 171 | |
| 172 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 173 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 174 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 175 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 176 | |
| 177 | shared_ptr<Data> data = re.toData(); |
| 178 | m_keyChain.sign(*data, m_certName); |
| 179 | |
| 180 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 181 | 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 Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 190 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 191 | hasDataBack = true; |
| 192 | NDNS_LOG_TRACE("get Data back"); |
| 193 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 194 | Response resp; |
| 195 | |
| 196 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough |
| 198 | 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 Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 207 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 208 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 209 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 210 | run(); |
| 211 | |
| 212 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 216 | { |
| 217 | Response re; |
| 218 | re.setZone(zone); |
| 219 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 220 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 221 | re.setRrType(label::NS_RR_TYPE); |
| 222 | re.setNdnsType(NDNS_RESP); |
| 223 | |
| 224 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 225 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 226 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 227 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 228 | |
| 229 | shared_ptr<Data> data = re.toData(); |
| 230 | m_keyChain.sign(*data, m_certName); |
| 231 | |
| 232 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 233 | const Block& block = data->wireEncode(); |
| 234 | Name name; |
| 235 | name.append(block); |
| 236 | |
| 237 | q.setRrLabel(name); |
| 238 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 239 | |
| 240 | bool hasDataBack = false; |
| 241 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 242 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 243 | hasDataBack = true; |
| 244 | NDNS_LOG_TRACE("get Data back"); |
| 245 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 246 | Response resp; |
| 247 | |
| 248 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough |
| 250 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 251 | Block block = resp.getRrs()[0]; |
| 252 | block.parse(); |
| 253 | int ret = -1; |
| 254 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 255 | Block::element_const_iterator val = block.elements_begin(); |
| 256 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 257 | ret = readNonNegativeInteger(*val); |
| 258 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 259 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 260 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 261 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 262 | run(); |
| 263 | |
| 264 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 265 | } |
| 266 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 267 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 268 | { |
| 269 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 270 | std::vector<CertificateSubjectDescription> desc; |
| 271 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 272 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 273 | shared_ptr<IdentityCertificate> dskCert = |
| 274 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 275 | notBefore, notAfter, desc); |
| 276 | |
| 277 | m_keyChain.sign(*dskCert, m_certName); |
| 278 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 279 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 280 | << dskCert->getSignature().getKeyLocator().getName()); |
| 281 | |
| 282 | Rrset rrset(&m_root); |
| 283 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 284 | rrset.setLabel(label); |
| 285 | rrset.setType(label::CERT_RR_TYPE); |
| 286 | rrset.setVersion(dskCert->getName().get(-1)); |
| 287 | rrset.setTtl(m_root.getTtl()); |
| 288 | rrset.setData(dskCert->wireEncode()); |
| 289 | m_session.insert(rrset); |
| 290 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 291 | << dskCert->getName() << " rrLabel=" << label); |
| 292 | |
| 293 | Response re; |
| 294 | re.setZone(zone); |
| 295 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 296 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 297 | re.setRrType(label::NS_RR_TYPE); |
| 298 | re.setNdnsType(NDNS_RESP); |
| 299 | |
| 300 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 301 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 302 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 303 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 304 | |
| 305 | shared_ptr<Data> data = re.toData(); |
| 306 | m_keyChain.sign(*data, dskCert->getName()); |
| 307 | |
| 308 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 309 | const Block& block = data->wireEncode(); |
| 310 | Name name; |
| 311 | name.append(block); |
| 312 | |
| 313 | q.setRrLabel(name); |
| 314 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 315 | |
| 316 | bool hasDataBack = false; |
| 317 | |
| 318 | // no data back, since the Update cannot pass verification |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 319 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 320 | hasDataBack = true; |
| 321 | BOOST_FAIL("UNEXPECTED"); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 322 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 323 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 324 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 325 | run(); |
| 326 | |
| 327 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 328 | } |
| 329 | |
| 330 | class NameServerFixture2 : public DbTestData |
| 331 | { |
| 332 | public: |
| 333 | NameServerFixture2() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 334 | : face(io, m_keyChain, {false, true}) |
| 335 | , validatorFace(io, m_keyChain, {false, true}) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 336 | , zone(m_root.getName()) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 337 | , validator(validatorFace) // different face for validator |
| 338 | , server(zone, m_certName, face, m_session, m_keyChain, validator) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 339 | { |
| 340 | // ensure prefix is registered |
| 341 | run(); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 342 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 343 | validatorFace.onSendInterest.connect([this] (const Interest& interest) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 344 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 345 | |
| 346 | shared_ptr<const Interest> i = interest.shared_from_this(); |
| 347 | io.post([i, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 348 | face.receive(*i); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 349 | }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 350 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
| 354 | run() |
| 355 | { |
| 356 | io.poll(); |
| 357 | io.reset(); |
| 358 | } |
| 359 | |
| 360 | public: |
| 361 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 362 | ndn::util::DummyClientFace face; |
| 363 | ndn::util::DummyClientFace validatorFace; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 364 | Name hint; |
| 365 | const Name& zone; |
| 366 | Validator validator; |
| 367 | ndns::NameServer server; |
| 368 | }; |
| 369 | |
| 370 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 371 | { |
| 372 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 373 | std::vector<CertificateSubjectDescription> desc; |
| 374 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 375 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 376 | shared_ptr<IdentityCertificate> dskCert = |
| 377 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 378 | notBefore, notAfter, desc); |
| 379 | |
| 380 | m_keyChain.sign(*dskCert, m_certName); |
| 381 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 382 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 383 | << dskCert->getSignature().getKeyLocator().getName()); |
| 384 | |
| 385 | Rrset rrset(&m_root); |
| 386 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 387 | rrset.setLabel(label); |
| 388 | rrset.setType(label::CERT_RR_TYPE); |
| 389 | rrset.setVersion(dskCert->getName().get(-1)); |
| 390 | rrset.setTtl(m_root.getTtl()); |
| 391 | rrset.setData(dskCert->wireEncode()); |
| 392 | m_session.insert(rrset); |
| 393 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 394 | << dskCert->getName() << " rrLabel=" << label); |
| 395 | |
| 396 | Response re; |
| 397 | re.setZone(zone); |
| 398 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 399 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 400 | re.setRrType(label::NS_RR_TYPE); |
| 401 | re.setNdnsType(NDNS_RESP); |
| 402 | |
| 403 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 404 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 405 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 406 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 407 | |
| 408 | shared_ptr<Data> data = re.toData(); |
| 409 | m_keyChain.sign(*data, dskCert->getName()); |
| 410 | |
| 411 | Query q(Name(hint), Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
| 412 | const Block& block = data->wireEncode(); |
| 413 | Name name; |
| 414 | name.append(block); |
| 415 | |
| 416 | q.setRrLabel(name); |
| 417 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 418 | |
| 419 | bool hasDataBack = false; |
| 420 | |
| 421 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 422 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 423 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 424 | shared_ptr<const Data> d = data.shared_from_this(); |
| 425 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 426 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 427 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 428 | } |
| 429 | else { |
| 430 | // cert is requested by validator |
| 431 | hasDataBack = true; |
| 432 | NDNS_LOG_TRACE("get Data back"); |
| 433 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 434 | Response resp; |
| 435 | |
| 436 | BOOST_CHECK_NO_THROW(resp.fromData(hint, zone, data)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(resp.getNdnsType(), NDNS_RESP); // by default NDNS_RAW is enough |
| 438 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 439 | Block block = resp.getRrs()[0]; |
| 440 | block.parse(); |
| 441 | int ret = -1; |
| 442 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 443 | Block::element_const_iterator val = block.elements_begin(); |
| 444 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 445 | ret = readNonNegativeInteger(*val); |
| 446 | BOOST_CHECK_EQUAL(ret, 0); |
| 447 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 448 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 449 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 450 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 451 | run(); |
| 452 | |
| 453 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 454 | } |
| 455 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 456 | BOOST_AUTO_TEST_SUITE_END() |
| 457 | |
| 458 | } // namespace tests |
| 459 | } // namespace ndns |
| 460 | } // namespace ndn |