Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Eric Newberry | c196a16 | 2020-09-13 14:05:16 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, 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 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 20 | #include "daemon/name-server.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 21 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 22 | #include "clients/query.hpp" |
| 23 | #include "clients/response.hpp" |
| 24 | #include "daemon/db-mgr.hpp" |
| 25 | #include "logger.hpp" |
| 26 | |
| 27 | #include "boost-test.hpp" |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 28 | #include "unit/database-test-data.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 31 | #include <ndn-cxx/util/regex.hpp> |
| 32 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 33 | namespace ndn { |
| 34 | namespace ndns { |
| 35 | namespace tests { |
| 36 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [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}) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 44 | , zone(m_test.getName()) |
| 45 | , validator(NdnsValidatorBuilder::create(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(); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 50 | advanceClocks(time::milliseconds(10), 1); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void |
| 54 | run() |
| 55 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 56 | face.getIoService().poll(); |
| 57 | face.getIoService().reset(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public: |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 61 | ndn::util::DummyClientFace face; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 62 | const Name& zone; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 63 | unique_ptr<security::Validator> validator; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 64 | ndns::NameServer server; |
| 65 | }; |
| 66 | |
| 67 | BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture) |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(NdnsQuery) |
| 70 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 71 | Query q(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; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 83 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK); |
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 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 96 | Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 97 | q.setRrType(ndns::label::CERT_RR_TYPE); |
| 98 | |
| 99 | size_t nDataBack = 0; |
| 100 | |
| 101 | // will ask for non-existing record |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 102 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 103 | ++nDataBack; |
| 104 | NDNS_LOG_TRACE("get Data back"); |
| 105 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 106 | |
| 107 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 108 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 109 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 110 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 112 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 113 | run(); |
| 114 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 115 | // will ask for the existing record (will have type NDNS_KEY, as it is certificate) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 116 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 117 | ++nDataBack; |
| 118 | NDNS_LOG_TRACE("get Data back"); |
| 119 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 120 | |
| 121 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 122 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 124 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 125 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 126 | Response certResp; |
| 127 | certResp.fromData(zone, m_cert); |
| 128 | q.setRrLabel(certResp.getRrLabel()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 129 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 130 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 131 | run(); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(nDataBack, 2); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 134 | |
| 135 | // explicit interest with correct version |
Eric Newberry | c196a16 | 2020-09-13 14:05:16 -0700 | [diff] [blame^] | 136 | face.receive(Interest(m_cert.getName()).setCanBePrefix(false)); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 137 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 138 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 139 | ++nDataBack; |
| 140 | |
| 141 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 142 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 144 | }); |
| 145 | |
| 146 | run(); |
| 147 | BOOST_CHECK_EQUAL(nDataBack, 3); |
| 148 | |
| 149 | // explicit interest with wrong version |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 150 | Name wrongName = m_cert.getName().getPrefix(-1); |
| 151 | wrongName.appendVersion(); |
Eric Newberry | c196a16 | 2020-09-13 14:05:16 -0700 | [diff] [blame^] | 152 | face.receive(Interest(wrongName).setCanBePrefix(false)); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 153 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 154 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 155 | ++nDataBack; |
| 156 | |
| 157 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 158 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 160 | }); |
| 161 | |
| 162 | run(); |
| 163 | BOOST_CHECK_EQUAL(nDataBack, 4); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(UpdateReplaceRr) |
| 167 | { |
| 168 | Response re; |
| 169 | re.setZone(zone); |
| 170 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 171 | re.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 172 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 173 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 174 | |
| 175 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 176 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 177 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 178 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 179 | |
| 180 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 181 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 182 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 183 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 184 | const Block& block = data->wireEncode(); |
| 185 | Name name; |
| 186 | name.append(block); |
| 187 | |
| 188 | q.setRrLabel(name); |
| 189 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 190 | |
| 191 | bool hasDataBack = false; |
| 192 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 193 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 194 | hasDataBack = true; |
| 195 | NDNS_LOG_TRACE("get Data back"); |
| 196 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 197 | Response resp; |
| 198 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 199 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 201 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 202 | Block block = resp.getRrs()[0]; |
| 203 | block.parse(); |
| 204 | int ret = -1; |
| 205 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 206 | Block::element_const_iterator val = block.elements_begin(); |
| 207 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 208 | ret = readNonNegativeInteger(*val); |
| 209 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 210 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 211 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 212 | run(); |
| 213 | |
| 214 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 218 | { |
| 219 | Response re; |
| 220 | re.setZone(zone); |
| 221 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 222 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 223 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 224 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 225 | |
| 226 | std::string str = "ns1.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 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 229 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 230 | |
| 231 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 232 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 233 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 234 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 235 | const Block& block = data->wireEncode(); |
| 236 | Name name; |
| 237 | name.append(block); |
| 238 | |
| 239 | q.setRrLabel(name); |
| 240 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 241 | |
| 242 | bool hasDataBack = false; |
| 243 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 244 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 245 | hasDataBack = true; |
| 246 | NDNS_LOG_TRACE("get Data back"); |
| 247 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 248 | Response resp; |
| 249 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 250 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 251 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 252 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 253 | Block block = resp.getRrs()[0]; |
| 254 | block.parse(); |
| 255 | int ret = -1; |
| 256 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 257 | Block::element_const_iterator val = block.elements_begin(); |
| 258 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 259 | ret = readNonNegativeInteger(*val); |
| 260 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 261 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 263 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 264 | run(); |
| 265 | |
| 266 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 267 | } |
| 268 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 269 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 270 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 271 | Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME); |
| 272 | Key dsk = m_keyChain.createKey(zoneIdentity); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 273 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 274 | Name dskCertName = dsk.getName(); |
| 275 | dskCertName |
| 276 | .append("CERT") |
| 277 | .appendVersion(); |
| 278 | Certificate dskCert; |
| 279 | dskCert.setName(dskCertName); |
| 280 | dskCert.setContentType(ndn::tlv::ContentType_Key); |
| 281 | dskCert.setFreshnessPeriod(time::hours(1)); |
| 282 | dskCert.setContent(dsk.getPublicKey().data(), dsk.getPublicKey().size()); |
| 283 | SignatureInfo info; |
| 284 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 285 | time::system_clock::now() + time::days(365))); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 286 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 287 | m_keyChain.sign(dskCert, security::signingByCertificate(m_cert)); |
| 288 | m_keyChain.setDefaultCertificate(dsk, dskCert); |
| 289 | |
| 290 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: " |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 291 | << dskCert.getKeyLocator()->getName()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 292 | |
| 293 | Rrset rrset(&m_test); |
| 294 | Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 295 | rrset.setLabel(label); |
| 296 | rrset.setType(label::CERT_RR_TYPE); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 297 | rrset.setVersion(dskCert.getName().get(-1)); |
| 298 | rrset.setTtl(m_test.getTtl()); |
| 299 | rrset.setData(dskCert.wireEncode()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 300 | m_session.insert(rrset); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 301 | NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name=" |
| 302 | << dskCert.getName() << " rrLabel=" << label); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 303 | |
| 304 | Response re; |
| 305 | re.setZone(zone); |
| 306 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 307 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 308 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 309 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 310 | |
| 311 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 312 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 313 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 314 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 315 | |
| 316 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 317 | m_keyChain.sign(*data, security::signingByCertificate(dskCert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 318 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 319 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 320 | const Block& block = data->wireEncode(); |
| 321 | Name name; |
| 322 | name.append(block); |
| 323 | |
| 324 | q.setRrLabel(name); |
| 325 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 326 | |
| 327 | bool hasDataBack = false; |
| 328 | |
| 329 | // no data back, since the Update cannot pass verification |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 330 | face.onSendData.connectSingleShot([&] (const Data&) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 331 | hasDataBack = true; |
| 332 | BOOST_FAIL("UNEXPECTED"); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 333 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 334 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 335 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 336 | run(); |
| 337 | |
| 338 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 339 | } |
| 340 | |
| 341 | class NameServerFixture2 : public DbTestData |
| 342 | { |
| 343 | public: |
| 344 | NameServerFixture2() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 345 | : face(io, m_keyChain, {false, true}) |
| 346 | , validatorFace(io, m_keyChain, {false, true}) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 347 | , zone(m_test.getName()) |
| 348 | , validator(NdnsValidatorBuilder::create(validatorFace)) // different face for validator |
| 349 | , server(zone, m_certName, face, m_session, m_keyChain, *validator) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 350 | { |
| 351 | // ensure prefix is registered |
| 352 | run(); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 353 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 354 | validatorFace.onSendInterest.connect([this] (const Interest& interest) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 355 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 356 | |
| 357 | shared_ptr<const Interest> i = interest.shared_from_this(); |
| 358 | io.post([i, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 359 | face.receive(*i); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 360 | }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 361 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void |
| 365 | run() |
| 366 | { |
| 367 | io.poll(); |
| 368 | io.reset(); |
| 369 | } |
| 370 | |
| 371 | public: |
| 372 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 373 | ndn::util::DummyClientFace face; |
| 374 | ndn::util::DummyClientFace validatorFace; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 375 | const Name& zone; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 376 | unique_ptr<security::Validator> validator; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 377 | ndns::NameServer server; |
| 378 | }; |
| 379 | |
| 380 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 381 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 382 | Response re; |
| 383 | re.setZone(zone); |
| 384 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 385 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 386 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 387 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 388 | |
| 389 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 390 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 391 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 392 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 393 | |
| 394 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 395 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 396 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 397 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 398 | const Block& block = data->wireEncode(); |
| 399 | Name name; |
| 400 | name.append(block); |
| 401 | |
| 402 | q.setRrLabel(name); |
| 403 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 404 | |
| 405 | bool hasDataBack = false; |
| 406 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 407 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 408 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 409 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 410 | shared_ptr<const Data> d = data.shared_from_this(); |
| 411 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 412 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 413 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 414 | } |
| 415 | else { |
| 416 | // cert is requested by validator |
| 417 | hasDataBack = true; |
| 418 | NDNS_LOG_TRACE("get Data back"); |
| 419 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 420 | Response resp; |
| 421 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 422 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_RESP); // by default NDNS_BLOB is enough |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 424 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 425 | Block block = resp.getRrs()[0]; |
| 426 | block.parse(); |
| 427 | int ret = -1; |
| 428 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 429 | Block::element_const_iterator val = block.elements_begin(); |
| 430 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 431 | ret = readNonNegativeInteger(*val); |
| 432 | BOOST_CHECK_EQUAL(ret, 0); |
| 433 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 434 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 435 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 436 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 437 | run(); |
| 438 | |
| 439 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 440 | } |
| 441 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 442 | BOOST_AUTO_TEST_SUITE_END() |
| 443 | |
| 444 | } // namespace tests |
| 445 | } // namespace ndns |
| 446 | } // namespace ndn |