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 | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, 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" |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 21 | #include "daemon/db-mgr.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 22 | #include "clients/response.hpp" |
| 23 | #include "clients/query.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 25 | #include "test-common.hpp" |
| 26 | #include "unit/database-test-data.hpp" |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 27 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/util/regex.hpp> |
| 29 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | namespace tests { |
| 33 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 34 | NDNS_LOG_INIT(NameServerTest); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 35 | |
| 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; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 60 | unique_ptr<security::v2::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; |
| 76 | NDNS_LOG_TRACE("get Data back"); |
| 77 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 78 | |
| 79 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 80 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 82 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 84 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 85 | |
| 86 | run(); |
| 87 | |
| 88 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_CASE(KeyQuery) |
| 92 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 93 | Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 94 | q.setRrType(ndns::label::CERT_RR_TYPE); |
| 95 | |
| 96 | size_t nDataBack = 0; |
| 97 | |
| 98 | // will ask for non-existing record |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 99 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 100 | ++nDataBack; |
| 101 | NDNS_LOG_TRACE("get Data back"); |
| 102 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 103 | |
| 104 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 105 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 107 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 109 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 110 | run(); |
| 111 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 112 | // 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] | 113 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 114 | ++nDataBack; |
| 115 | NDNS_LOG_TRACE("get Data back"); |
| 116 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 117 | |
| 118 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 119 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 121 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 122 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 123 | Response certResp; |
| 124 | certResp.fromData(zone, m_cert); |
| 125 | q.setRrLabel(certResp.getRrLabel()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 126 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 127 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 128 | run(); |
| 129 | |
| 130 | BOOST_CHECK_EQUAL(nDataBack, 2); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 131 | |
| 132 | // explicit interest with correct version |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 133 | face.receive(Interest(m_cert.getName())); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 134 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 135 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 136 | ++nDataBack; |
| 137 | |
| 138 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 139 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 141 | }); |
| 142 | |
| 143 | run(); |
| 144 | BOOST_CHECK_EQUAL(nDataBack, 3); |
| 145 | |
| 146 | // explicit interest with wrong version |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 147 | Name wrongName = m_cert.getName().getPrefix(-1); |
| 148 | wrongName.appendVersion(); |
| 149 | face.receive(Interest(wrongName)); |
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; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 155 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 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); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 170 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 178 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 179 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 180 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 196 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 197 | 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] | 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 | }); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 208 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 209 | run(); |
| 210 | |
| 211 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 215 | { |
| 216 | Response re; |
| 217 | re.setZone(zone); |
| 218 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 219 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 220 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 221 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 222 | |
| 223 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 224 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 225 | str = "ns2.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 | |
| 228 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 229 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 230 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 231 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 232 | const Block& block = data->wireEncode(); |
| 233 | Name name; |
| 234 | name.append(block); |
| 235 | |
| 236 | q.setRrLabel(name); |
| 237 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 238 | |
| 239 | bool hasDataBack = false; |
| 240 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 241 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 242 | hasDataBack = true; |
| 243 | NDNS_LOG_TRACE("get Data back"); |
| 244 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 245 | Response resp; |
| 246 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 247 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 248 | 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] | 249 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 250 | Block block = resp.getRrs()[0]; |
| 251 | block.parse(); |
| 252 | int ret = -1; |
| 253 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 254 | Block::element_const_iterator val = block.elements_begin(); |
| 255 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 256 | ret = readNonNegativeInteger(*val); |
| 257 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 258 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 259 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 260 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 261 | run(); |
| 262 | |
| 263 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 264 | } |
| 265 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 266 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 267 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 268 | Identity zoneIdentity = m_keyChain.createIdentity(TEST_IDENTITY_NAME); |
| 269 | Key dsk = m_keyChain.createKey(zoneIdentity); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 270 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 271 | Name dskCertName = dsk.getName(); |
| 272 | dskCertName |
| 273 | .append("CERT") |
| 274 | .appendVersion(); |
| 275 | Certificate dskCert; |
| 276 | dskCert.setName(dskCertName); |
| 277 | dskCert.setContentType(ndn::tlv::ContentType_Key); |
| 278 | dskCert.setFreshnessPeriod(time::hours(1)); |
| 279 | dskCert.setContent(dsk.getPublicKey().data(), dsk.getPublicKey().size()); |
| 280 | SignatureInfo info; |
| 281 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 282 | time::system_clock::now() + time::days(365))); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 283 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 284 | m_keyChain.sign(dskCert, security::signingByCertificate(m_cert)); |
| 285 | m_keyChain.setDefaultCertificate(dsk, dskCert); |
| 286 | |
| 287 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert.getName() << ". KeyLocator: " |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 288 | << dskCert.getKeyLocator()->getName()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 289 | |
| 290 | Rrset rrset(&m_test); |
| 291 | Name label = dskCert.getName().getPrefix(-2).getSubName(m_test.getName().size() + 1); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 292 | rrset.setLabel(label); |
| 293 | rrset.setType(label::CERT_RR_TYPE); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 294 | rrset.setVersion(dskCert.getName().get(-1)); |
| 295 | rrset.setTtl(m_test.getTtl()); |
| 296 | rrset.setData(dskCert.wireEncode()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 297 | m_session.insert(rrset); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 298 | NDNS_LOG_TRACE("DB: zone " << m_test << " add a CERT RR with name=" |
| 299 | << dskCert.getName() << " rrLabel=" << label); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 300 | |
| 301 | Response re; |
| 302 | re.setZone(zone); |
| 303 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 304 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 305 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 306 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 307 | |
| 308 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 309 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 310 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 311 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 312 | |
| 313 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 314 | m_keyChain.sign(*data, security::signingByCertificate(dskCert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 315 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 316 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 317 | const Block& block = data->wireEncode(); |
| 318 | Name name; |
| 319 | name.append(block); |
| 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()); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 353 | |
| 354 | shared_ptr<const Interest> i = interest.shared_from_this(); |
| 355 | io.post([i, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 356 | face.receive(*i); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 357 | }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 358 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
| 362 | run() |
| 363 | { |
| 364 | io.poll(); |
| 365 | io.reset(); |
| 366 | } |
| 367 | |
| 368 | public: |
| 369 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 370 | ndn::util::DummyClientFace face; |
| 371 | ndn::util::DummyClientFace validatorFace; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 372 | const Name& zone; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 373 | unique_ptr<security::v2::Validator> validator; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 374 | ndns::NameServer server; |
| 375 | }; |
| 376 | |
| 377 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 378 | { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 379 | Response re; |
| 380 | re.setZone(zone); |
| 381 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 382 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 383 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 384 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 385 | |
| 386 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 387 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 388 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 389 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 390 | |
| 391 | shared_ptr<Data> data = re.toData(); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 392 | m_keyChain.sign(*data, security::signingByCertificate(m_cert)); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 393 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 394 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 395 | const Block& block = data->wireEncode(); |
| 396 | Name name; |
| 397 | name.append(block); |
| 398 | |
| 399 | q.setRrLabel(name); |
| 400 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 401 | |
| 402 | bool hasDataBack = false; |
| 403 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 404 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS><KEY>(<>+)<CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 405 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 406 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 407 | shared_ptr<const Data> d = data.shared_from_this(); |
| 408 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 409 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 410 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 411 | } |
| 412 | else { |
| 413 | // cert is requested by validator |
| 414 | hasDataBack = true; |
| 415 | NDNS_LOG_TRACE("get Data back"); |
| 416 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 417 | Response resp; |
| 418 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 419 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 420 | 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] | 421 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 422 | Block block = resp.getRrs()[0]; |
| 423 | block.parse(); |
| 424 | int ret = -1; |
| 425 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 426 | Block::element_const_iterator val = block.elements_begin(); |
| 427 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 428 | ret = readNonNegativeInteger(*val); |
| 429 | BOOST_CHECK_EQUAL(ret, 0); |
| 430 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 431 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 432 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 433 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 434 | run(); |
| 435 | |
| 436 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 437 | } |
| 438 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 439 | BOOST_AUTO_TEST_SUITE_END() |
| 440 | |
| 441 | } // namespace tests |
| 442 | } // namespace ndns |
| 443 | } // namespace ndn |