blob: 371610f2beb6eb7c1f703d5224edc3d5893e6611 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -07002/*
Davide Pesavento384327d2025-01-02 01:40:23 -05003 * Copyright (c) 2014-2025, The University of Memphis,
Nick Gordon4d2c6c02017-01-20 13:18:46 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070020 */
Nick Gordon4d2c6c02017-01-20 13:18:46 -060021
22#include "update/nfd-rib-command-processor.hpp"
Junxiao Shi008c9b12019-01-13 23:15:56 +000023#include "conf-parameter.hpp"
24#include "nlsr.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060025
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040026#include "tests/io-key-chain-fixture.hpp"
27#include "tests/test-common.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060028
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070029#include <boost/lexical_cast.hpp>
Davide Pesavento5849ee72023-11-12 20:00:21 -050030#include <boost/mp11/list.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070031
Davide Pesavento288141a2024-02-13 17:30:35 -050032namespace nlsr::tests {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060033
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040034class NfdRibCommandProcessorFixture : public IoKeyChainFixture
Nick Gordon4d2c6c02017-01-20 13:18:46 -060035{
36public:
37 NfdRibCommandProcessorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040038 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060039 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , confProcessor(conf)
41 , nlsr(face, m_keyChain, conf)
42 , namePrefixes(conf.getNamePrefixList())
43 , processor(nlsr.m_nfdRibCommandProcessor)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060044 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040045 m_keyChain.createIdentity(conf.getRouterPrefix());
Nick Gordon4d2c6c02017-01-20 13:18:46 -060046
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050047 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060048 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050049
Ashlesh Gawande15052402018-12-12 20:20:00 -060050 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060051 }
52
Junxiao Shi008c9b12019-01-13 23:15:56 +000053 void
54 sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060055 {
Davide Pesaventoe28d8752022-03-19 03:55:25 -040056 ndn::Interest interest(prefix.append(ndn::tlv::GenericNameComponent, parameters.wireEncode()));
Junxiao Shi008c9b12019-01-13 23:15:56 +000057 face.receive(interest);
58 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060059 }
60
Ashlesh Gawande85998a12017-12-07 22:22:13 -060061 void sendInterestForPublishedData()
Junxiao Shi008c9b12019-01-13 23:15:56 +000062 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060063 ndn::Name lsaInterestName = conf.getLsaPrefix();
64 lsaInterestName.append(conf.getSiteName());
65 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080066 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande15052402018-12-12 20:20:00 -060067 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050068
Ashlesh Gawande85998a12017-12-07 22:22:13 -060069 face.receive(ndn::Interest(lsaInterestName).setCanBePrefix(true));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050070 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060071 }
72
Nick Gordon4d2c6c02017-01-20 13:18:46 -060073 bool
74 wasRoutingUpdatePublished()
75 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060076 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050077
Ashlesh Gawande85998a12017-12-07 22:22:13 -060078 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060079
80 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050081 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060082 return lsaPrefix.isPrefixOf(data.getName());
83 });
84
85 return (it != face.sentData.end());
86 }
87
88public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000089 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060090 ConfParameter conf;
91 DummyConfFileProcessor confProcessor;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060092
93 Nlsr nlsr;
94 NamePrefixList& namePrefixes;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060095 update::NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050096 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060097};
98
Davide Pesavento5849ee72023-11-12 20:00:21 -050099using Commands = boost::mp11::mp_list<
Davide Pesavento384327d2025-01-02 01:40:23 -0500100 ndn::nfd::RibRegisterCommand,
101 ndn::nfd::RibUnregisterCommand
Davide Pesavento5849ee72023-11-12 20:00:21 -0500102>;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600103
104BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
105
106BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
107{
108 ndn::nfd::ControlParameters parameters;
109 parameters.setName("/test/prefixA");
110
111 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
112}
113
114BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
115{
116 ndn::nfd::ControlParameters parameters;
Davide Pesavento384327d2025-01-02 01:40:23 -0500117 parameters.setName("/test/prefixA").setMtu(500);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600118
Davide Pesavento384327d2025-01-02 01:40:23 -0500119 BOOST_CHECK_THROW(processor.validateParameters<NfdRibCommand>(parameters), std::invalid_argument);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600120}
121
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400122BOOST_AUTO_TEST_CASE(OnReceiveInterestRegisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600123{
124 ndn::Name name("/localhost/nlsr/rib/register");
125 ndn::Name prefixName("/test/prefixA");
126 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000127 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600128
Junxiao Shi008c9b12019-01-13 23:15:56 +0000129 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600130
Nick Gordonf14ec352017-07-24 16:09:58 -0500131 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
132 std::list<ndn::Name> names = namePrefixes.getNames();
133 auto itr = std::find(names.begin(), names.end(), prefixName);
134 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600135 BOOST_FAIL("Prefix was not inserted!");
136 }
137 BOOST_CHECK_EQUAL((*itr), prefixName);
138 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600139 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600140}
141
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400142BOOST_AUTO_TEST_CASE(OnReceiveInterestUnregisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600143{
144 ndn::Name name("/localhost/nlsr/rib/unregister");
145 ndn::Name prefixName("/test/prefixA");
146 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000147 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600148
Nick Gordonf14ec352017-07-24 16:09:58 -0500149 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600150
Junxiao Shi008c9b12019-01-13 23:15:56 +0000151 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600152
Nick Gordonf14ec352017-07-24 16:09:58 -0500153 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600154 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600155 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600156}
157
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400158BOOST_AUTO_TEST_CASE(OnReceiveInterestInvalidPrefix)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600159{
160 ndn::Name name("/localhost/invalid/rib/register");
161 ndn::Name prefixName("/test/prefixA");
162 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000163 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600164
Junxiao Shi008c9b12019-01-13 23:15:56 +0000165 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600166
Nick Gordonf14ec352017-07-24 16:09:58 -0500167 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500168
169 // Cannot use routingUpdatePublish test now since in
170 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600171 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600172}
173
174BOOST_AUTO_TEST_SUITE_END()
175
Davide Pesavento288141a2024-02-13 17:30:35 -0500176} // namespace nlsr::tests