Junxiao Shi | 3b08154 | 2017-07-04 18:37:34 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 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 | |
| 28 | #include "tests/identity-management-fixture.hpp" |
| 29 | #include <ndn-cxx/encoding/buffer.hpp> |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace tools { |
| 34 | namespace autoconfig_server { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ::nfd::tests; |
| 38 | |
| 39 | class AutoconfigServerFixture : public IdentityManagementFixture |
| 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); |
| 51 | face.processEvents(time::milliseconds(1)); |
| 52 | face.sentInterests.clear(); |
| 53 | } |
| 54 | |
| 55 | public: |
| 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"); |
| 70 | face.receive(interest); |
| 71 | face.processEvents(time::milliseconds(1)); |
| 72 | |
| 73 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 74 | const Name& dataName = face.sentData.at(0).getName(); |
| 75 | BOOST_CHECK_EQUAL(dataName.size(), interest.getName().size() + 1); |
| 76 | BOOST_CHECK(interest.getName().isPrefixOf(dataName)); |
| 77 | BOOST_CHECK(dataName.at(-1).isVersion()); |
| 78 | |
| 79 | // interest2 asks for a different version, and should not be responded |
| 80 | Interest interest2(Name(interest.getName()).appendVersion(dataName.at(-1).toVersion() - 1)); |
| 81 | face.receive(interest2); |
| 82 | face.processEvents(time::milliseconds(1)); |
| 83 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset) |
| 87 | { |
| 88 | Options options; |
| 89 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 90 | const int nRoutablePrefixes = 2000; |
| 91 | for (int i = 0; i < nRoutablePrefixes; ++i) { |
| 92 | options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i)); |
| 93 | } |
| 94 | this->initialize(options); |
| 95 | |
| 96 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
| 97 | face.receive(interest); |
| 98 | face.processEvents(time::milliseconds(1)); |
| 99 | |
| 100 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 101 | const Name& dataName0 = face.sentData.at(0).getName(); |
| 102 | BOOST_CHECK_EQUAL(dataName0.size(), interest.getName().size() + 2); |
| 103 | BOOST_CHECK(interest.getName().isPrefixOf(dataName0)); |
| 104 | BOOST_CHECK(dataName0.at(-2).isVersion()); |
| 105 | BOOST_CHECK(dataName0.at(-1).isSegment()); |
| 106 | |
| 107 | while (face.sentData.back().getFinalBlockId() != face.sentData.back().getName().at(-1)) { |
| 108 | const Name& lastName = face.sentData.back().getName(); |
| 109 | Interest interest2(lastName.getPrefix(-1).appendSegment(lastName.at(-1).toSegment() + 1)); |
| 110 | face.receive(interest2); |
| 111 | face.processEvents(time::milliseconds(1)); |
| 112 | } |
| 113 | |
| 114 | auto buffer = make_shared<Buffer>(); |
| 115 | for (const Data& data : face.sentData) { |
| 116 | const Block& content = data.getContent(); |
| 117 | std::copy(content.value_begin(), content.value_end(), std::back_inserter(*buffer)); |
| 118 | } |
| 119 | |
| 120 | Block payload(tlv::Content, buffer); |
| 121 | payload.parse(); |
| 122 | BOOST_CHECK_EQUAL(payload.elements_size(), nRoutablePrefixes); |
| 123 | for (int i = 0; i < nRoutablePrefixes; ++i) { |
| 124 | BOOST_CHECK_EQUAL(Name(payload.elements().at(i)), options.routablePrefixes.at(i)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled) |
| 129 | { |
| 130 | Options options; |
| 131 | options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363"); |
| 132 | this->initialize(options); |
| 133 | |
| 134 | Interest interest("/localhop/nfd/rib/routable-prefixes"); |
| 135 | face.receive(interest); |
| 136 | face.processEvents(time::milliseconds(1)); |
| 137 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_SUITE_END() // TestProgram |
| 141 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer |
| 142 | |
| 143 | } // namespace tests |
| 144 | } // namespace autoconfig_server |
| 145 | } // namespace tools |
| 146 | } // namespace ndn |