blob: ee99740a84beb2b1690b5fa86339ce8559ab704e [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 Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, 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 Pesaventocf7db2f2019-03-24 23:17:28 -040028#include "tests/test-common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029#include "tests/key-chain-fixture.hpp"
Davide Pesavento00335782018-02-10 22:31:33 -050030
Junxiao Shi47c343b2019-05-25 07:53:01 +000031#include <ndn-cxx/security/validator-null.hpp>
Junxiao Shi3b081542017-07-04 18:37:34 +000032#include <ndn-cxx/util/dummy-client-face.hpp>
Junxiao Shi47c343b2019-05-25 07:53:01 +000033#include <ndn-cxx/util/segment-fetcher.hpp>
Junxiao Shi3b081542017-07-04 18:37:34 +000034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace ndn::autoconfig_server::tests {
Junxiao Shi3b081542017-07-04 18:37:34 +000036
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040037class AutoconfigServerFixture : public ::nfd::tests::KeyChainFixture
Junxiao Shi3b081542017-07-04 18:37:34 +000038{
Davide Pesaventob93fb6c2020-04-12 14:10:45 -040039protected:
Junxiao Shi3b081542017-07-04 18:37:34 +000040 void
41 initialize(const Options& options)
42 {
43 program = make_unique<Program>(options, face, m_keyChain);
Davide Pesaventob93fb6c2020-04-12 14:10:45 -040044 face.processEvents(1_s);
Junxiao Shi3b081542017-07-04 18:37:34 +000045 face.sentInterests.clear();
46 }
47
Davide Pesavento00335782018-02-10 22:31:33 -050048protected:
Davide Pesaventoae430302023-05-11 01:42:46 -040049 DummyClientFace face{{true, true}};
Junxiao Shi3b081542017-07-04 18:37:34 +000050 unique_ptr<Program> program;
51};
52
53BOOST_AUTO_TEST_SUITE(NdnAutoconfigServer)
54BOOST_FIXTURE_TEST_SUITE(TestProgram, AutoconfigServerFixture)
55
56BOOST_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 Shi47c343b2019-05-25 07:53:01 +000063 interest.setCanBePrefix(true);
Junxiao Shi73b49802019-05-25 07:52:26 +000064 interest.setMustBeFresh(true);
Junxiao Shi3b081542017-07-04 18:37:34 +000065 face.receive(interest);
Davide Pesaventob93fb6c2020-04-12 14:10:45 -040066 face.processEvents(1_s);
Junxiao Shi3b081542017-07-04 18:37:34 +000067
Davide Pesavento00335782018-02-10 22:31:33 -050068 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
69 const Name& dataName = face.sentData[0].getName();
Junxiao Shi3b081542017-07-04 18:37:34 +000070 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 Pesaventob93fb6c2020-04-12 14:10:45 -040077 face.processEvents(1_s);
Junxiao Shi3b081542017-07-04 18:37:34 +000078 BOOST_CHECK_EQUAL(face.sentData.size(), 1);
79}
80
81BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset)
82{
83 Options options;
84 options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
Davide Pesavento00335782018-02-10 22:31:33 -050085 const size_t nRoutablePrefixes = 2000;
86 for (size_t i = 0; i < nRoutablePrefixes; ++i) {
Junxiao Shi3b081542017-07-04 18:37:34 +000087 options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i));
88 }
89 this->initialize(options);
90
Davide Pesaventoae430302023-05-11 01:42:46 -040091 DummyClientFace clientFace(face.getIoService());
Junxiao Shi47c343b2019-05-25 07:53:01 +000092 clientFace.linkTo(face);
Junxiao Shi3b081542017-07-04 18:37:34 +000093
Junxiao Shi47c343b2019-05-25 07:53:01 +000094 Name baseName("/localhop/nfd/rib/routable-prefixes");
Davide Pesaventoae430302023-05-11 01:42:46 -040095 auto fetcher = SegmentFetcher::start(clientFace, Interest(baseName), security::getAcceptAllValidator());
Junxiao Shi47c343b2019-05-25 07:53:01 +000096 fetcher->afterSegmentReceived.connect([baseName] (const Data& data) {
97 const Name& dataName = data.getName();
98 BOOST_CHECK_EQUAL(dataName.size(), baseName.size() + 2);
99 BOOST_CHECK(dataName.at(-2).isVersion());
100 BOOST_CHECK(dataName.at(-1).isSegment());
101 });
Davide Pesaventob93fb6c2020-04-12 14:10:45 -0400102 fetcher->onError.connect([] (auto, const auto& msg) { BOOST_FAIL(msg); });
103
Junxiao Shi47c343b2019-05-25 07:53:01 +0000104 bool isComplete = false;
Davide Pesaventob93fb6c2020-04-12 14:10:45 -0400105 fetcher->onComplete.connect([&] (auto buffer) {
106 Block payload(tlv::Content, std::move(buffer));
Junxiao Shi47c343b2019-05-25 07:53:01 +0000107 payload.parse();
108 BOOST_REQUIRE_EQUAL(payload.elements_size(), nRoutablePrefixes);
109 for (size_t i = 0; i < nRoutablePrefixes; ++i) {
110 BOOST_CHECK_EQUAL(Name(payload.elements()[i]), options.routablePrefixes[i]);
111 }
112 isComplete = true;
113 });
Junxiao Shi3b081542017-07-04 18:37:34 +0000114
Davide Pesaventob93fb6c2020-04-12 14:10:45 -0400115 face.processEvents(1_s);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000116 BOOST_CHECK(isComplete);
Junxiao Shi3b081542017-07-04 18:37:34 +0000117}
118
119BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled)
120{
121 Options options;
122 options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
123 this->initialize(options);
124
125 Interest interest("/localhop/nfd/rib/routable-prefixes");
Junxiao Shi47c343b2019-05-25 07:53:01 +0000126 interest.setCanBePrefix(true);
Junxiao Shi3b081542017-07-04 18:37:34 +0000127 face.receive(interest);
Davide Pesaventob93fb6c2020-04-12 14:10:45 -0400128 face.processEvents(1_s);
Junxiao Shi3b081542017-07-04 18:37:34 +0000129 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
130}
131
132BOOST_AUTO_TEST_SUITE_END() // TestProgram
133BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer
134
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400135} // namespace ndn::autoconfig_server::tests