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 | |
| 88 | BOOST_AUTO_TEST_CASE(KeyQuery) |
| 89 | { |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 90 | Query q(zone, ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 91 | q.setQueryType(ndns::label::NDNS_CERT_QUERY); |
| 92 | q.setRrType(ndns::label::CERT_RR_TYPE); |
| 93 | |
| 94 | size_t nDataBack = 0; |
| 95 | |
| 96 | // will ask for non-existing record |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 97 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 98 | ++nDataBack; |
| 99 | NDNS_LOG_TRACE("get Data back"); |
| 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; |
| 113 | NDNS_LOG_TRACE("get Data back"); |
| 114 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 115 | |
| 116 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 117 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame^] | 118 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 119 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 120 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 121 | q.setRrLabel("dsk-1"); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 123 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 124 | run(); |
| 125 | |
| 126 | BOOST_CHECK_EQUAL(nDataBack, 2); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 127 | |
| 128 | // explicit interest with correct version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 129 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FDd")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 130 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 131 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 132 | ++nDataBack; |
| 133 | |
| 134 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 135 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame^] | 136 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_KEY); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 137 | }); |
| 138 | |
| 139 | run(); |
| 140 | BOOST_CHECK_EQUAL(nDataBack, 3); |
| 141 | |
| 142 | // explicit interest with wrong version |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 143 | face.receive(Interest("/test19/KEY/dsk-1/ID-CERT/%FD010101010")); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 144 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 145 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 146 | ++nDataBack; |
| 147 | |
| 148 | Response resp; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 149 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(resp.getContentType(), NDNS_NACK); |
Alexander Afanasyev | aa46c27 | 2016-03-09 12:54:12 -0800 | [diff] [blame] | 151 | }); |
| 152 | |
| 153 | run(); |
| 154 | BOOST_CHECK_EQUAL(nDataBack, 4); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(UpdateReplaceRr) |
| 158 | { |
| 159 | Response re; |
| 160 | re.setZone(zone); |
| 161 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 162 | re.setRrLabel(Name("net")); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 163 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 164 | re.setContentType(NDNS_RESP); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 165 | |
| 166 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 167 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 168 | str = "ns2.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 | |
| 171 | shared_ptr<Data> data = re.toData(); |
| 172 | m_keyChain.sign(*data, m_certName); |
| 173 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 174 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 175 | const Block& block = data->wireEncode(); |
| 176 | Name name; |
| 177 | name.append(block); |
| 178 | |
| 179 | q.setRrLabel(name); |
| 180 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 181 | |
| 182 | bool hasDataBack = false; |
| 183 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 184 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 185 | hasDataBack = true; |
| 186 | NDNS_LOG_TRACE("get Data back"); |
| 187 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 188 | Response resp; |
| 189 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 190 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 191 | 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] | 192 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 193 | Block block = resp.getRrs()[0]; |
| 194 | block.parse(); |
| 195 | int ret = -1; |
| 196 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 197 | Block::element_const_iterator val = block.elements_begin(); |
| 198 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 199 | ret = readNonNegativeInteger(*val); |
| 200 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 201 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -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"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 219 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 220 | str = "ns2.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 | |
| 223 | shared_ptr<Data> data = re.toData(); |
| 224 | m_keyChain.sign(*data, m_certName); |
| 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 | const Block& block = data->wireEncode(); |
| 228 | Name name; |
| 229 | name.append(block); |
| 230 | |
| 231 | q.setRrLabel(name); |
| 232 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 233 | |
| 234 | bool hasDataBack = false; |
| 235 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 236 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 237 | hasDataBack = true; |
| 238 | NDNS_LOG_TRACE("get Data back"); |
| 239 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 240 | Response resp; |
| 241 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 242 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 243 | 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] | 244 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 245 | Block block = resp.getRrs()[0]; |
| 246 | block.parse(); |
| 247 | int ret = -1; |
| 248 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 249 | Block::element_const_iterator val = block.elements_begin(); |
| 250 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 251 | ret = readNonNegativeInteger(*val); |
| 252 | BOOST_CHECK_EQUAL(ret, 0); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 253 | }); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 254 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 255 | face.receive(q.toInterest()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 256 | run(); |
| 257 | |
| 258 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 259 | } |
| 260 | |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 261 | BOOST_AUTO_TEST_CASE(UpdateValidatorCannotFetchCert) |
| 262 | { |
| 263 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 264 | std::vector<CertificateSubjectDescription> desc; |
| 265 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 266 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 267 | shared_ptr<IdentityCertificate> dskCert = |
| 268 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 269 | notBefore, notAfter, desc); |
| 270 | |
| 271 | m_keyChain.sign(*dskCert, m_certName); |
| 272 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 273 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 274 | << dskCert->getSignature().getKeyLocator().getName()); |
| 275 | |
| 276 | Rrset rrset(&m_root); |
| 277 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 278 | rrset.setLabel(label); |
| 279 | rrset.setType(label::CERT_RR_TYPE); |
| 280 | rrset.setVersion(dskCert->getName().get(-1)); |
| 281 | rrset.setTtl(m_root.getTtl()); |
| 282 | rrset.setData(dskCert->wireEncode()); |
| 283 | m_session.insert(rrset); |
| 284 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 285 | << dskCert->getName() << " rrLabel=" << label); |
| 286 | |
| 287 | Response re; |
| 288 | re.setZone(zone); |
| 289 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 290 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 291 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 292 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 293 | |
| 294 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 295 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 296 | str = "ns2.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 | |
| 299 | shared_ptr<Data> data = re.toData(); |
| 300 | m_keyChain.sign(*data, dskCert->getName()); |
| 301 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 302 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 303 | const Block& block = data->wireEncode(); |
| 304 | Name name; |
| 305 | name.append(block); |
| 306 | |
| 307 | q.setRrLabel(name); |
| 308 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 309 | |
| 310 | bool hasDataBack = false; |
| 311 | |
| 312 | // no data back, since the Update cannot pass verification |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 313 | face.onSendData.connectSingleShot([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 314 | hasDataBack = true; |
| 315 | BOOST_FAIL("UNEXPECTED"); |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 316 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 317 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 318 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 319 | run(); |
| 320 | |
| 321 | BOOST_CHECK_EQUAL(hasDataBack, false); |
| 322 | } |
| 323 | |
| 324 | class NameServerFixture2 : public DbTestData |
| 325 | { |
| 326 | public: |
| 327 | NameServerFixture2() |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 328 | : face(io, m_keyChain, {false, true}) |
| 329 | , validatorFace(io, m_keyChain, {false, true}) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 330 | , zone(m_root.getName()) |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 331 | , validator(validatorFace) // different face for validator |
| 332 | , server(zone, m_certName, face, m_session, m_keyChain, validator) |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 333 | { |
| 334 | // ensure prefix is registered |
| 335 | run(); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 336 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 337 | validatorFace.onSendInterest.connect([this] (const Interest& interest) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 338 | NDNS_LOG_TRACE("validatorFace get Interest: " << interest.getName()); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 339 | |
| 340 | shared_ptr<const Interest> i = interest.shared_from_this(); |
| 341 | io.post([i, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 342 | face.receive(*i); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 343 | }); |
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; |
| 359 | Validator validator; |
| 360 | ndns::NameServer server; |
| 361 | }; |
| 362 | |
| 363 | BOOST_FIXTURE_TEST_CASE(UpdateValidatorFetchCert, NameServerFixture2) |
| 364 | { |
| 365 | Name dskName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false); |
| 366 | std::vector<CertificateSubjectDescription> desc; |
| 367 | time::system_clock::TimePoint notBefore = time::system_clock::now(); |
| 368 | time::system_clock::TimePoint notAfter = notBefore + time::days(365); |
| 369 | shared_ptr<IdentityCertificate> dskCert = |
| 370 | m_keyChain.prepareUnsignedIdentityCertificate(dskName, m_certName, |
| 371 | notBefore, notAfter, desc); |
| 372 | |
| 373 | m_keyChain.sign(*dskCert, m_certName); |
| 374 | m_keyChain.addCertificateAsKeyDefault(*dskCert); |
| 375 | NDNS_LOG_TRACE("KeyChain: add cert: " << dskCert->getName() << ". KeyLocator: " |
| 376 | << dskCert->getSignature().getKeyLocator().getName()); |
| 377 | |
| 378 | Rrset rrset(&m_root); |
| 379 | Name label = dskCert->getName().getPrefix(-2).getSubName(m_root.getName().size() + 1); |
| 380 | rrset.setLabel(label); |
| 381 | rrset.setType(label::CERT_RR_TYPE); |
| 382 | rrset.setVersion(dskCert->getName().get(-1)); |
| 383 | rrset.setTtl(m_root.getTtl()); |
| 384 | rrset.setData(dskCert->wireEncode()); |
| 385 | m_session.insert(rrset); |
| 386 | NDNS_LOG_TRACE("DB: zone " << m_root << " add a ID-CERT RR with name=" |
| 387 | << dskCert->getName() << " rrLabel=" << label); |
| 388 | |
| 389 | Response re; |
| 390 | re.setZone(zone); |
| 391 | re.setQueryType(label::NDNS_ITERATIVE_QUERY); |
| 392 | re.setRrLabel(Name("ndnsim-XYZ")); // insert new records |
| 393 | re.setRrType(label::NS_RR_TYPE); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 394 | re.setContentType(NDNS_RESP); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 395 | |
| 396 | std::string str = "ns1.ndnsim.net"; |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 397 | re.addRr(makeBinaryBlock(ndns::tlv::RrData, str.c_str(), str.size())); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 398 | str = "ns2.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 | |
| 401 | shared_ptr<Data> data = re.toData(); |
| 402 | m_keyChain.sign(*data, dskCert->getName()); |
| 403 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 404 | Query q(Name(zone), ndns::label::NDNS_ITERATIVE_QUERY); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 405 | const Block& block = data->wireEncode(); |
| 406 | Name name; |
| 407 | name.append(block); |
| 408 | |
| 409 | q.setRrLabel(name); |
| 410 | q.setRrType(label::NDNS_UPDATE_LABEL); |
| 411 | |
| 412 | bool hasDataBack = false; |
| 413 | |
| 414 | shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>"); |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 415 | face.onSendData.connect([&] (const Data& data) { |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 416 | if (regex->match(data.getName())) { |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 417 | shared_ptr<const Data> d = data.shared_from_this(); |
| 418 | io.post([d, this] { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 419 | validatorFace.receive(*d); // It's data requested by validator |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 420 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 421 | } |
| 422 | else { |
| 423 | // cert is requested by validator |
| 424 | hasDataBack = true; |
| 425 | NDNS_LOG_TRACE("get Data back"); |
| 426 | BOOST_CHECK_EQUAL(data.getName().getPrefix(-1), q.toInterest().getName()); |
| 427 | Response resp; |
| 428 | |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 429 | BOOST_CHECK_NO_THROW(resp.fromData(zone, data)); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 430 | 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] | 431 | BOOST_CHECK_GT(resp.getRrs().size(), 0); |
| 432 | Block block = resp.getRrs()[0]; |
| 433 | block.parse(); |
| 434 | int ret = -1; |
| 435 | BOOST_CHECK_EQUAL(block.type(), ndns::tlv::RrData); |
| 436 | Block::element_const_iterator val = block.elements_begin(); |
| 437 | BOOST_CHECK_EQUAL(val->type(), ndns::tlv::UpdateReturnCode); // the first must be return code |
| 438 | ret = readNonNegativeInteger(*val); |
| 439 | BOOST_CHECK_EQUAL(ret, 0); |
| 440 | } |
Junxiao Shi | 8f5be2a | 2015-01-06 10:06:43 -0700 | [diff] [blame] | 441 | }); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 442 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 443 | face.receive(q.toInterest()); |
Shock Jiang | e1a81fd | 2014-11-20 20:25:49 -0800 | [diff] [blame] | 444 | run(); |
| 445 | |
| 446 | BOOST_CHECK_EQUAL(hasDataBack, true); |
| 447 | } |
| 448 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 449 | BOOST_AUTO_TEST_SUITE_END() |
| 450 | |
| 451 | } // namespace tests |
| 452 | } // namespace ndns |
| 453 | } // namespace ndn |