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 | 1b22a8c | 2022-03-07 21:23:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/security/validator-null.hpp> |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 35 | namespace ndn::autoconfig_server::tests { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 37 | class AutoconfigServerFixture : public ::nfd::tests::KeyChainFixture |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 38 | { |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 39 | protected: |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 40 | void |
| 41 | initialize(const Options& options) |
| 42 | { |
| 43 | program = make_unique<Program>(options, face, m_keyChain); |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 44 | face.processEvents(1_s); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 45 | face.sentInterests.clear(); |
| 46 | } |
| 47 | |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 48 | protected: |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 49 | util::DummyClientFace face{{true, true}}; |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 50 | unique_ptr<Program> program; |
| 51 | }; |
| 52 | |
| 53 | BOOST_AUTO_TEST_SUITE(NdnAutoconfigServer) |
| 54 | BOOST_FIXTURE_TEST_SUITE(TestProgram, AutoconfigServerFixture) |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(HubData) |
| 57 | { |
| 58 | Options options; |
| 59 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 60 | this->initialize(options); |
| 61 | |
| 62 | Interest interest("/localhop/ndn-autoconf/hub"); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 63 | interest.setCanBePrefix(true); |
Junxiao Shi | 73b4980 | 2019-05-25 07:52:26 +0000 | [diff] [blame] | 64 | interest.setMustBeFresh(true); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 65 | face.receive(interest); |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 66 | face.processEvents(1_s); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 67 | |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 68 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 69 | const Name& dataName = face.sentData[0].getName(); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(dataName.size(), interest.getName().size() + 1); |
| 71 | BOOST_CHECK(interest.getName().isPrefixOf(dataName)); |
| 72 | BOOST_CHECK(dataName.at(-1).isVersion()); |
| 73 | |
| 74 | // interest2 asks for a different version, and should not be responded |
| 75 | Interest interest2(Name(interest.getName()).appendVersion(dataName.at(-1).toVersion() - 1)); |
| 76 | face.receive(interest2); |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 77 | face.processEvents(1_s); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset) |
| 82 | { |
| 83 | Options options; |
| 84 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 85 | const size_t nRoutablePrefixes = 2000; |
| 86 | for (size_t i = 0; i < nRoutablePrefixes; ++i) { |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 87 | options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i)); |
| 88 | } |
| 89 | this->initialize(options); |
| 90 | |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 91 | util::DummyClientFace clientFace(face.getIoService()); |
| 92 | clientFace.linkTo(face); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 93 | |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 94 | Name baseName("/localhop/nfd/rib/routable-prefixes"); |
| 95 | auto fetcher = util::SegmentFetcher::start(clientFace, Interest(baseName), |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 96 | security::getAcceptAllValidator()); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 97 | fetcher->afterSegmentReceived.connect([baseName] (const Data& data) { |
| 98 | const Name& dataName = data.getName(); |
| 99 | BOOST_CHECK_EQUAL(dataName.size(), baseName.size() + 2); |
| 100 | BOOST_CHECK(dataName.at(-2).isVersion()); |
| 101 | BOOST_CHECK(dataName.at(-1).isSegment()); |
| 102 | }); |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 103 | fetcher->onError.connect([] (auto, const auto& msg) { BOOST_FAIL(msg); }); |
| 104 | |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 105 | bool isComplete = false; |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 106 | fetcher->onComplete.connect([&] (auto buffer) { |
| 107 | Block payload(tlv::Content, std::move(buffer)); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 108 | payload.parse(); |
| 109 | BOOST_REQUIRE_EQUAL(payload.elements_size(), nRoutablePrefixes); |
| 110 | for (size_t i = 0; i < nRoutablePrefixes; ++i) { |
| 111 | BOOST_CHECK_EQUAL(Name(payload.elements()[i]), options.routablePrefixes[i]); |
| 112 | } |
| 113 | isComplete = true; |
| 114 | }); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 115 | |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 116 | face.processEvents(1_s); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 117 | BOOST_CHECK(isComplete); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled) |
| 121 | { |
| 122 | Options options; |
| 123 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 124 | this->initialize(options); |
| 125 | |
| 126 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 127 | interest.setCanBePrefix(true); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 128 | face.receive(interest); |
Davide Pesavento | b93fb6c | 2020-04-12 14:10:45 -0400 | [diff] [blame] | 129 | face.processEvents(1_s); |
Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() // TestProgram |
| 134 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer |
| 135 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 136 | } // namespace ndn::autoconfig_server::tests |