blob: b8209b02e1556d2fc53db5a70d9a195b3769c306 [file] [log] [blame]
Junxiao Shi3b081542017-07-04 18:37:34 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento00335782018-02-10 22:31:33 -05002/*
Davide Pesavento1d12d2f2019-03-22 12:44:14 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi3b081542017-07-04 18:37:34 +00004 * 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 Pesavento1d12d2f2019-03-22 12:44:14 -040028#include "tests/key-chain-fixture.hpp"
Davide Pesavento00335782018-02-10 22:31:33 -050029
Junxiao Shi3b081542017-07-04 18:37:34 +000030#include <ndn-cxx/encoding/buffer.hpp>
31#include <ndn-cxx/util/dummy-client-face.hpp>
32
33namespace ndn {
34namespace tools {
35namespace autoconfig_server {
36namespace tests {
37
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040038class AutoconfigServerFixture : public ::nfd::tests::KeyChainFixture
Junxiao Shi3b081542017-07-04 18:37:34 +000039{
40public:
41 AutoconfigServerFixture()
42 : face({true, true})
43 {
44 }
45
46 void
47 initialize(const Options& options)
48 {
49 program = make_unique<Program>(options, face, m_keyChain);
Davide Pesavento00335782018-02-10 22:31:33 -050050 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +000051 face.sentInterests.clear();
52 }
53
Davide Pesavento00335782018-02-10 22:31:33 -050054protected:
Junxiao Shi3b081542017-07-04 18:37:34 +000055 util::DummyClientFace face;
56 unique_ptr<Program> program;
57};
58
59BOOST_AUTO_TEST_SUITE(NdnAutoconfigServer)
60BOOST_FIXTURE_TEST_SUITE(TestProgram, AutoconfigServerFixture)
61
62BOOST_AUTO_TEST_CASE(HubData)
63{
64 Options options;
65 options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
66 this->initialize(options);
67
68 Interest interest("/localhop/ndn-autoconf/hub");
69 face.receive(interest);
Davide Pesavento00335782018-02-10 22:31:33 -050070 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +000071
Davide Pesavento00335782018-02-10 22:31:33 -050072 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
73 const Name& dataName = face.sentData[0].getName();
Junxiao Shi3b081542017-07-04 18:37:34 +000074 BOOST_CHECK_EQUAL(dataName.size(), interest.getName().size() + 1);
75 BOOST_CHECK(interest.getName().isPrefixOf(dataName));
76 BOOST_CHECK(dataName.at(-1).isVersion());
77
78 // interest2 asks for a different version, and should not be responded
79 Interest interest2(Name(interest.getName()).appendVersion(dataName.at(-1).toVersion() - 1));
80 face.receive(interest2);
Davide Pesavento00335782018-02-10 22:31:33 -050081 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +000082 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
83}
84
85BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset)
86{
87 Options options;
88 options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
Davide Pesavento00335782018-02-10 22:31:33 -050089 const size_t nRoutablePrefixes = 2000;
90 for (size_t i = 0; i < nRoutablePrefixes; ++i) {
Junxiao Shi3b081542017-07-04 18:37:34 +000091 options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i));
92 }
93 this->initialize(options);
94
95 Interest interest("/localhop/nfd/rib/routable-prefixes");
96 face.receive(interest);
Davide Pesavento00335782018-02-10 22:31:33 -050097 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +000098
Davide Pesavento00335782018-02-10 22:31:33 -050099 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
100 const Name& dataName0 = face.sentData[0].getName();
Junxiao Shi3b081542017-07-04 18:37:34 +0000101 BOOST_CHECK_EQUAL(dataName0.size(), interest.getName().size() + 2);
102 BOOST_CHECK(interest.getName().isPrefixOf(dataName0));
103 BOOST_CHECK(dataName0.at(-2).isVersion());
104 BOOST_CHECK(dataName0.at(-1).isSegment());
105
Davide Pesavento17057442018-04-20 15:21:31 -0400106 while (face.sentData.back().getFinalBlock() != face.sentData.back().getName().at(-1)) {
Junxiao Shi3b081542017-07-04 18:37:34 +0000107 const Name& lastName = face.sentData.back().getName();
108 Interest interest2(lastName.getPrefix(-1).appendSegment(lastName.at(-1).toSegment() + 1));
109 face.receive(interest2);
Davide Pesavento00335782018-02-10 22:31:33 -0500110 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +0000111 }
112
113 auto buffer = make_shared<Buffer>();
114 for (const Data& data : face.sentData) {
115 const Block& content = data.getContent();
116 std::copy(content.value_begin(), content.value_end(), std::back_inserter(*buffer));
117 }
118
119 Block payload(tlv::Content, buffer);
120 payload.parse();
Davide Pesavento00335782018-02-10 22:31:33 -0500121 BOOST_REQUIRE_EQUAL(payload.elements_size(), nRoutablePrefixes);
122 for (size_t i = 0; i < nRoutablePrefixes; ++i) {
123 BOOST_CHECK_EQUAL(Name(payload.elements()[i]), options.routablePrefixes[i]);
Junxiao Shi3b081542017-07-04 18:37:34 +0000124 }
125}
126
127BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled)
128{
129 Options options;
130 options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
131 this->initialize(options);
132
133 Interest interest("/localhop/nfd/rib/routable-prefixes");
134 face.receive(interest);
Davide Pesavento00335782018-02-10 22:31:33 -0500135 face.processEvents(500_ms);
Junxiao Shi3b081542017-07-04 18:37:34 +0000136 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
137}
138
139BOOST_AUTO_TEST_SUITE_END() // TestProgram
140BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer
141
142} // namespace tests
143} // namespace autoconfig_server
144} // namespace tools
145} // namespace ndn