Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
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 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | namespace tests { |
| 31 | |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 32 | NDNS_LOG_INIT("NameServerTest") |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 33 | |
| 34 | class NameServerFixture : public DbTestData |
| 35 | { |
| 36 | public: |
| 37 | NameServerFixture() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 38 | : face({false, true}) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 39 | , zone(m_root.getName()) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 40 | , validator(face) |
| 41 | , server(zone, m_certName, face, m_session, m_keyChain, validator) |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 42 | { |
| 43 | // ensure prefix is registered |
| 44 | run(); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | run() |
| 49 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 50 | face.getIoService().poll(); |
| 51 | face.getIoService().reset(); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public: |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 55 | ndn::util::DummyClientFace face; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 56 | const Name& zone; |
| 57 | Validator validator; |
| 58 | ndns::NameServer server; |
| 59 | }; |
| 60 | |
| 61 | BOOST_FIXTURE_TEST_SUITE(NameServer, NameServerFixture) |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(NdnsQuery) |
| 64 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 65 | Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 66 | q.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 67 | q.setRrType(ndns::label::NS_RR_TYPE); |
| 68 | |
| 69 | bool hasDataBack = false; |
| 70 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 71 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 72 | hasDataBack = true; |
| 73 | NDNS_LOG_TRACE("get Data back"); |
| 74 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 75 | |
| 76 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 77 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 78 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_LINK); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 79 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 80 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 81 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 82 | |
| 83 | run(); |
| 84 | |
| 85 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 86 | } |
| 87 | |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 88 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(KeyQuery, 2) |
| 89 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 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.setQueryType(ndns::label::NDNS_CERT_QUERY); |
| 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 | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 112 | // will ask for the existing record (will have type NDNS_BLOB, 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 | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 120 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_BLOB); |
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 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 123 | q.setRrLabel("dsk-1"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 124 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 125 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 126 | run(); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(nDataBack, 2); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 129 | |
| 130 | // explicit interest with correct version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 131 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FDd")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 132 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 133 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 134 | ++nDataBack; |
| 135 | |
| 136 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 137 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 138 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_BLOB); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 139 | }); |
| 140 | |
| 141 | run(); |
| 142 | BOOST_CHECK_EQUAL(nDataBack, 3); |
| 143 | |
| 144 | // explicit interest with wrong version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 145 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FD010101010")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 146 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 147 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 148 | ++nDataBack; |
| 149 | |
| 150 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 151 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 152 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 153 | }); |
| 154 | |
| 155 | run(); |
| 156 | BOOST_CHECK_EQUAL(nDataBack, 4); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(UpdateReplaceRr) |
| 160 | { |
| 161 | Response re; |
| 162 | re.setZone(zone); |
| 163 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 164 | re.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 165 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 166 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 167 | |
| 168 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 169 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 170 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 171 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 172 | |
| 173 | shared_ptr<Data> data = re.toData(); |
| 174 | m_keyChain.sign(*data, m_certName); |
| 175 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 176 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 177 | const Block& block = data->wireEncode(); |
| 178 | Name name; |
| 179 | name.append(block); |
| 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; |
| 188 | NDNS_LOG_TRACE("get Data back"); |
| 189 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 190 | Response resp; |
| 191 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 192 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 193 | 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] | 194 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 195 | Block block = resp.getRrs()[0]; |
| 196 | block.parse(); |
| 197 | int ret = -1; |
| 198 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 199 | Block::element_const_iterator val = block.elements_begin(); |
| 200 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 201 | ret = readNonNegativeInteger(*val); |
| 202 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 203 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 205 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 206 | run(); |
| 207 | |
| 208 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE(UpdateInsertNewRr) |
| 212 | { |
| 213 | Response re; |
| 214 | re.setZone(zone); |
| 215 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 216 | re.setRrLabel(Name("net-XYZ")); // insert new records |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 217 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 218 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 219 | |
| 220 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 221 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 222 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 223 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 224 | |
| 225 | shared_ptr<Data> data = re.toData(); |
| 226 | m_keyChain.sign(*data, m_certName); |
| 227 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 228 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 229 | const Block& block = data->wireEncode(); |
| 230 | Name name; |
| 231 | name.append(block); |
| 232 | |
| 233 | q.setRrLabel(name); |
| 234 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 235 | |
| 236 | bool hasDataBack = false; |
| 237 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 238 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 239 | hasDataBack = true; |
| 240 | NDNS_LOG_TRACE("get Data back"); |
| 241 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 242 | Response resp; |
| 243 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 244 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 245 | 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] | 246 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 247 | Block block = resp.getRrs()[0]; |
| 248 | block.parse(); |
| 249 | int ret = -1; |
| 250 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 251 | Block::element_const_iterator val = block.elements_begin(); |
| 252 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 253 | ret = readNonNegativeInteger(*val); |
| 254 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 255 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 256 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 257 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 258 | run(); |
| 259 | |
| 260 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 261 | } |
| 262 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 263 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 264 | { |
| 265 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 266 | std::vector<CertificateSubjectDescription> desc; |
| 267 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 268 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 269 | shared_ptr<IdentityCertificate> dskCert = |
| 270 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 271 | notBefore, notAfter, desc); |
| 272 | |
| 273 | m_keyChain.sign(*dskCert, m_certName); |
| 274 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 275 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 276 | << dskCert->getSignature().getKeyLocator().getName()); |
| 277 | |
| 278 | Rrset rrset(&m_root); |
| 279 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 280 | rrset.setLabel(label); |
| 281 | rrset.setType(label::CERT_RR_TYPE); |
| 282 | rrset.setVersion(dskCert->getName().get(-1)); |
| 283 | rrset.setTtl(m_root.getTtl()); |
| 284 | rrset.setData(dskCert->wireEncode()); |
| 285 | m_session.insert(rrset); |
| 286 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 287 | << dskCert->getName() << " rrLabel=" << label); |
| 288 | |
| 289 | Response re; |
| 290 | re.setZone(zone); |
| 291 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 292 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 293 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 294 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 295 | |
| 296 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 297 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 298 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 299 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 300 | |
| 301 | shared_ptr<Data> data = re.toData(); |
| 302 | m_keyChain.sign(*data, dskCert->getName()); |
| 303 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 304 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 305 | const Block& block = data->wireEncode(); |
| 306 | Name name; |
| 307 | name.append(block); |
| 308 | |
| 309 | q.setRrLabel(name); |
| 310 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 311 | |
| 312 | bool hasDataBack = false; |
| 313 | |
| 314 | // no data back, since the Update cannot pass verification |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 315 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 316 | hasDataBack = true; |
| 317 | BOOST_FAIL("UNEXPECTED"); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 318 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 319 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 320 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 321 | run(); |
| 322 | |
| 323 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 324 | } |
| 325 | |
| 326 | class NameServerFixture2 : public DbTestData |
| 327 | { |
| 328 | public: |
| 329 | NameServerFixture2() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 330 | : face(io, m_keyChain, {false, true}) |
| 331 | , validatorFace(io, m_keyChain, {false, true}) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 332 | , zone(m_root.getName()) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 333 | , validator(validatorFace) // different face for validator |
| 334 | , server(zone, m_certName, face, m_session, m_keyChain, validator) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 335 | { |
| 336 | // ensure prefix is registered |
| 337 | run(); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 338 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 339 | validatorFace.onSendInterest.connect([this] (const Interest& interest) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 340 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 341 | |
| 342 | shared_ptr<const Interest> i = interest.shared_from_this(); |
| 343 | io.post([i, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 344 | face.receive(*i); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 345 | }); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 346 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void |
| 350 | run() |
| 351 | { |
| 352 | io.poll(); |
| 353 | io.reset(); |
| 354 | } |
| 355 | |
| 356 | public: |
| 357 | boost::asio::io_service io; |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 358 | ndn::util::DummyClientFace face; |
| 359 | ndn::util::DummyClientFace validatorFace; |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 360 | const Name& zone; |
| 361 | Validator validator; |
| 362 | ndns::NameServer server; |
| 363 | }; |
| 364 | |
| 365 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 366 | { |
| 367 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 368 | std::vector<CertificateSubjectDescription> desc; |
| 369 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 370 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 371 | shared_ptr<IdentityCertificate> dskCert = |
| 372 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 373 | notBefore, notAfter, desc); |
| 374 | |
| 375 | m_keyChain.sign(*dskCert, m_certName); |
| 376 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 377 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 378 | << dskCert->getSignature().getKeyLocator().getName()); |
| 379 | |
| 380 | Rrset rrset(&m_root); |
| 381 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 382 | rrset.setLabel(label); |
| 383 | rrset.setType(label::CERT_RR_TYPE); |
| 384 | rrset.setVersion(dskCert->getName().get(-1)); |
| 385 | rrset.setTtl(m_root.getTtl()); |
| 386 | rrset.setData(dskCert->wireEncode()); |
| 387 | m_session.insert(rrset); |
| 388 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 389 | << dskCert->getName() << " rrLabel=" << label); |
| 390 | |
| 391 | Response re; |
| 392 | re.setZone(zone); |
| 393 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 394 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 395 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 396 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 397 | |
| 398 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 399 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 400 | str = "ns2.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 401 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 402 | |
| 403 | shared_ptr<Data> data = re.toData(); |
| 404 | m_keyChain.sign(*data, dskCert->getName()); |
| 405 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 406 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 407 | const Block& block = data->wireEncode(); |
| 408 | Name name; |
| 409 | name.append(block); |
| 410 | |
| 411 | q.setRrLabel(name); |
| 412 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 413 | |
| 414 | bool hasDataBack = false; |
| 415 | |
| 416 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 417 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 418 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 419 | shared_ptr<const Data> d = data.shared_from_this(); |
| 420 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 421 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 422 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 423 | } |
| 424 | else { |
| 425 | // cert is requested by validator |
| 426 | hasDataBack = true; |
| 427 | NDNS_LOG_TRACE("get Data back"); |
| 428 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 429 | Response resp; |
| 430 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 431 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame^] | 432 | 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] | 433 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 434 | Block block = resp.getRrs()[0]; |
| 435 | block.parse(); |
| 436 | int ret = -1; |
| 437 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 438 | Block::element_const_iterator val = block.elements_begin(); |
| 439 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 440 | ret = readNonNegativeInteger(*val); |
| 441 | BOOST_CHECK_EQUAL(ret, 0); |
| 442 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 443 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 444 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 445 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 446 | run(); |
| 447 | |
| 448 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 449 | } |
| 450 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 451 | BOOST_AUTO_TEST_SUITE_END() |
| 452 | |
| 453 | } // namespace tests |
| 454 | } // namespace ndns |
| 455 | } // namespace ndn |