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 | /* |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 136 | face.receive(Interest(m_cert.getName())); |
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(); |
Davide Pesavento | 35e9354 | 2022-03-07 21:47:03 -0500 | [diff] [blame] | 152 | face.receive(Interest(wrongName)); |
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 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 180 | auto 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 | Name name; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 185 | name.append(ndn::tlv::GenericNameComponent, data->wireEncode()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 186 | |
| 187 | q.setRrLabel(name); |
| 188 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 189 | |
| 190 | bool hasDataBack = false; |
| 191 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 192 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 193 | hasDataBack = true; |
| 194 | NDNS_LOG_TRACE("get Data back"); |
| 195 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 196 | Response resp; |
| 197 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 198 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 199 | 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] | 200 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 201 | Block block = resp.getRrs()[0]; |
| 202 | block.parse(); |
| 203 | int ret = -1; |
| 204 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 205 | Block::element_const_iterator val = block.elements_begin(); |
| 206 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 207 | ret = readNonNegativeInteger(*val); |
| 208 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 209 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 210 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 211 | run(); |
| 212 | |
| 213 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 217 | { |
| 218 | Response re; |
| 219 | re.setZone(zone); |
| 220 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 221 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 222 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 223 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 224 | |
| 225 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 226 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 227 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 228 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 229 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 230 | auto data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 231 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 232 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 233 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 234 | Name name; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 235 | name.append(ndn::tlv::GenericNameComponent, data->wireEncode()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 248 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 249 | 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] | 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 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 269 | Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME); |
| 270 | Key dsk = m_keyChain.createKey(zoneIdentity); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 271 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 272 | Name dskCertName = dsk.getName(); |
| 273 | dskCertName |
| 274 | .append("CERT") |
| 275 | .appendVersion(); |
| 276 | Certificate dskCert; |
| 277 | dskCert.setName(dskCertName); |
| 278 | dskCert.setContentType(ndn::tlv::ContentType_Key); |
| 279 | dskCert.setFreshnessPeriod(time::hours(1)); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 280 | dskCert.setContent(dsk.getPublicKey()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 281 | SignatureInfo info; |
| 282 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 283 | time::system_clock::now() + time::days(365))); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 284 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 285 | m_keyChain.sign(dskCert, security::signingByCertificate(m_cert)); |
| 286 | m_keyChain.setDefaultCertificate(dsk, dskCert); |
| 287 | |
| 288 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: " |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 289 | << dskCert.getKeyLocator()->getName()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 290 | |
| 291 | Rrset rrset(&m_test); |
| 292 | Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 293 | rrset.setLabel(label); |
| 294 | rrset.setType(label::CERT_RR_TYPE); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 295 | rrset.setVersion(dskCert.getName().get(-1)); |
| 296 | rrset.setTtl(m_test.getTtl()); |
| 297 | rrset.setData(dskCert.wireEncode()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 298 | m_session.insert(rrset); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 299 | NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name=" |
| 300 | << dskCert.getName() << " rrLabel=" << label); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 301 | |
| 302 | Response re; |
| 303 | re.setZone(zone); |
| 304 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 305 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 306 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 307 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 308 | |
| 309 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 310 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 311 | str = "ns2.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 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 314 | auto data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 315 | m_keyChain.sign(*data, security::signingByCertificate(dskCert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 316 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 317 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 318 | Name name; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 319 | name.append(ndn::tlv::GenericNameComponent, data->wireEncode()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 320 | |
| 321 | q.setRrLabel(name); |
| 322 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 323 | |
| 324 | bool hasDataBack = false; |
| 325 | |
| 326 | // no data back, since the Update cannot pass verification |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame] | 327 | face.onSendData.connectSingleShot([&] (const Data&) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 328 | hasDataBack = true; |
| 329 | BOOST_FAIL("UNEXPECTED"); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 330 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 331 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 332 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 333 | run(); |
| 334 | |
| 335 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 336 | } |
| 337 | |
| 338 | class NameServerFixture2 : public DbTestData |
| 339 | { |
| 340 | public: |
| 341 | NameServerFixture2() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 342 | : face(io, m_keyChain, {false, true}) |
| 343 | , validatorFace(io, m_keyChain, {false, true}) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 344 | , zone(m_test.getName()) |
| 345 | , validator(NdnsValidatorBuilder::create(validatorFace)) // different face for validator |
| 346 | , server(zone, m_certName, face, m_session, m_keyChain, *validator) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 347 | { |
| 348 | // ensure prefix is registered |
| 349 | run(); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 350 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 351 | validatorFace.onSendInterest.connect([this] (const Interest& interest) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 352 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 353 | auto i = interest.shared_from_this(); |
| 354 | io.post([i, this] { face.receive(*i); }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 355 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void |
| 359 | run() |
| 360 | { |
| 361 | io.poll(); |
| 362 | io.reset(); |
| 363 | } |
| 364 | |
| 365 | public: |
| 366 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 367 | ndn::util::DummyClientFace face; |
| 368 | ndn::util::DummyClientFace validatorFace; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 369 | const Name& zone; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 370 | unique_ptr<security::Validator> validator; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 371 | ndns::NameServer server; |
| 372 | }; |
| 373 | |
| 374 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 375 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 376 | Response re; |
| 377 | re.setZone(zone); |
| 378 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 379 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 380 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 381 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 382 | |
| 383 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 384 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 385 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 386 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 387 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 388 | auto data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 389 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 390 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 391 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 392 | Name name; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 393 | name.append(ndn::tlv::GenericNameComponent, data->wireEncode()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 394 | |
| 395 | q.setRrLabel(name); |
| 396 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 397 | |
| 398 | bool hasDataBack = false; |
| 399 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 400 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 401 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 402 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 403 | shared_ptr<const Data> d = data.shared_from_this(); |
| 404 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 405 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 406 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 407 | } |
| 408 | else { |
| 409 | // cert is requested by validator |
| 410 | hasDataBack = true; |
| 411 | NDNS_LOG_TRACE("get Data back"); |
| 412 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 413 | Response resp; |
| 414 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 415 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 416 | 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] | 417 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 418 | Block block = resp.getRrs()[0]; |
| 419 | block.parse(); |
| 420 | int ret = -1; |
| 421 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 422 | auto val = block.elements_begin(); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 424 | ret = readNonNegativeInteger(*val); |
| 425 | BOOST_CHECK_EQUAL(ret, 0); |
| 426 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 427 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 428 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 429 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 430 | run(); |
| 431 | |
| 432 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 433 | } |
| 434 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 435 | BOOST_AUTO_TEST_SUITE_END() |
| 436 | |
| 437 | } // namespace tests |
| 438 | } // namespace ndns |
| 439 | } // namespace ndn |