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