Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "ndn-autoconfig-server/program.hpp" |
| 27 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 29 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 30 | |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/encoding/buffer.hpp> |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace tools { |
| 36 | namespace autoconfig_server { |
| 37 | namespace tests { |
| 38 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 39 | class AutoconfigServerFixture : public ::nfd::tests::KeyChainFixture |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | AutoconfigServerFixture() |
| 43 | : face({true, true}) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | initialize(const Options& options) |
| 49 | { |
| 50 | program = make_unique<Program>(options, face, m_keyChain); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 51 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 52 | face.sentInterests.clear(); |
| 53 | } |
| 54 | |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 55 | protected: |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 56 | util::DummyClientFace face; |
| 57 | unique_ptr<Program> program; |
| 58 | }; |
| 59 | |
| 60 | BOOST_AUTO_TEST_SUITE(NdnAutoconfigServer) |
| 61 | BOOST_FIXTURE_TEST_SUITE(TestProgram, AutoconfigServerFixture) |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(HubData) |
| 64 | { |
| 65 | Options options; |
| 66 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 67 | this->initialize(options); |
| 68 | |
| 69 | Interest interest("/localhop/ndn-autoconf/hub"); |
Junxiao Shi | 73b4980 | 2019-05-25 07:52:26 +0000 | [diff] [blame^] | 70 | interest.setMustBeFresh(true); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 71 | face.receive(interest); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 72 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 73 | |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 74 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 75 | const Name& dataName = face.sentData[0].getName(); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(dataName.size(), interest.getName().size() + 1); |
| 77 | BOOST_CHECK(interest.getName().isPrefixOf(dataName)); |
| 78 | BOOST_CHECK(dataName.at(-1).isVersion()); |
| 79 | |
| 80 | // interest2 asks for a different version, and should not be responded |
| 81 | Interest interest2(Name(interest.getName()).appendVersion(dataName.at(-1).toVersion() - 1)); |
| 82 | face.receive(interest2); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 83 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset) |
| 88 | { |
| 89 | Options options; |
| 90 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 91 | const size_t nRoutablePrefixes = 2000; |
| 92 | for (size_t i = 0; i < nRoutablePrefixes; ++i) { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 93 | options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i)); |
| 94 | } |
| 95 | this->initialize(options); |
| 96 | |
| 97 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
| 98 | face.receive(interest); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 99 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 100 | |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 101 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 102 | const Name& dataName0 = face.sentData[0].getName(); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(dataName0.size(), interest.getName().size() + 2); |
| 104 | BOOST_CHECK(interest.getName().isPrefixOf(dataName0)); |
| 105 | BOOST_CHECK(dataName0.at(-2).isVersion()); |
| 106 | BOOST_CHECK(dataName0.at(-1).isSegment()); |
| 107 | |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 108 | while (face.sentData.back().getFinalBlock() != face.sentData.back().getName().at(-1)) { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 109 | const Name& lastName = face.sentData.back().getName(); |
| 110 | Interest interest2(lastName.getPrefix(-1).appendSegment(lastName.at(-1).toSegment() + 1)); |
| 111 | face.receive(interest2); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 112 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | auto buffer = make_shared<Buffer>(); |
| 116 | for (const Data& data : face.sentData) { |
| 117 | const Block& content = data.getContent(); |
| 118 | std::copy(content.value_begin(), content.value_end(), std::back_inserter(*buffer)); |
| 119 | } |
| 120 | |
| 121 | Block payload(tlv::Content, buffer); |
| 122 | payload.parse(); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 123 | BOOST_REQUIRE_EQUAL(payload.elements_size(), nRoutablePrefixes); |
| 124 | for (size_t i = 0; i < nRoutablePrefixes; ++i) { |
| 125 | BOOST_CHECK_EQUAL(Name(payload.elements()[i]), options.routablePrefixes[i]); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled) |
| 130 | { |
| 131 | Options options; |
| 132 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 133 | this->initialize(options); |
| 134 | |
| 135 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
| 136 | face.receive(interest); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 137 | face.processEvents(500_ms); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 138 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_SUITE_END() // TestProgram |
| 142 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer |
| 143 | |
| 144 | } // namespace tests |
| 145 | } // namespace autoconfig_server |
| 146 | } // namespace tools |
| 147 | } // namespace ndn |