blob: 17106dc0d86f5ff8884dcd4d7123246ed86f7d5d [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 Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, 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 "adjacency-list.hpp"
24#include "conf-parameter.hpp"
25#include "nlsr.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060026
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040027#include "tests/io-key-chain-fixture.hpp"
28#include "tests/test-common.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060029
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030#include <boost/lexical_cast.hpp>
Davide Pesavento5849ee72023-11-12 20:00:21 -050031#include <boost/mp11/list.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070032
Davide Pesavento288141a2024-02-13 17:30:35 -050033namespace nlsr::tests {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060034
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040035class NfdRibCommandProcessorFixture : public IoKeyChainFixture
Nick Gordon4d2c6c02017-01-20 13:18:46 -060036{
37public:
38 NfdRibCommandProcessorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040039 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060040 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060041 , confProcessor(conf)
42 , nlsr(face, m_keyChain, conf)
43 , namePrefixes(conf.getNamePrefixList())
44 , processor(nlsr.m_nfdRibCommandProcessor)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060045 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040046 m_keyChain.createIdentity(conf.getRouterPrefix());
Nick Gordon4d2c6c02017-01-20 13:18:46 -060047
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050048 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060049 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050050
Ashlesh Gawande15052402018-12-12 20:20:00 -060051 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060052 }
53
Junxiao Shi008c9b12019-01-13 23:15:56 +000054 void
55 sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060056 {
Davide Pesaventoe28d8752022-03-19 03:55:25 -040057 ndn::Interest interest(prefix.append(ndn::tlv::GenericNameComponent, parameters.wireEncode()));
Junxiao Shi008c9b12019-01-13 23:15:56 +000058 face.receive(interest);
59 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060060 }
61
Ashlesh Gawande85998a12017-12-07 22:22:13 -060062 void sendInterestForPublishedData()
Junxiao Shi008c9b12019-01-13 23:15:56 +000063 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 ndn::Name lsaInterestName = conf.getLsaPrefix();
65 lsaInterestName.append(conf.getSiteName());
66 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080067 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande15052402018-12-12 20:20:00 -060068 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050069
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 face.receive(ndn::Interest(lsaInterestName).setCanBePrefix(true));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050071 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060072 }
73
Nick Gordon4d2c6c02017-01-20 13:18:46 -060074 bool
75 wasRoutingUpdatePublished()
76 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060077 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050078
Ashlesh Gawande85998a12017-12-07 22:22:13 -060079 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060080
81 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050082 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060083 return lsaPrefix.isPrefixOf(data.getName());
84 });
85
86 return (it != face.sentData.end());
87 }
88
89public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000090 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060091 ConfParameter conf;
92 DummyConfFileProcessor confProcessor;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060093
94 Nlsr nlsr;
95 NamePrefixList& namePrefixes;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096 update::NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050097 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060098};
99
Davide Pesavento5849ee72023-11-12 20:00:21 -0500100using Commands = boost::mp11::mp_list<
101 update::NfdRibRegisterCommand,
102 update::NfdRibUnregisterCommand
103>;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600104
105BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
106
107BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
108{
109 ndn::nfd::ControlParameters parameters;
110 parameters.setName("/test/prefixA");
111
112 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
113}
114
115BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
116{
117 ndn::nfd::ControlParameters parameters;
118 parameters.setName("/test/prefixA").setCost(10);
119
120 bool wasValidated = true;
121 try {
122 processor.validateParameters<NfdRibCommand>(parameters);
123 }
124 catch (...) {
125 wasValidated = false;
126 }
127 BOOST_CHECK(!wasValidated);
128}
129
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400130BOOST_AUTO_TEST_CASE(OnReceiveInterestRegisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600131{
132 ndn::Name name("/localhost/nlsr/rib/register");
133 ndn::Name prefixName("/test/prefixA");
134 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000135 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600136
Junxiao Shi008c9b12019-01-13 23:15:56 +0000137 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600138
Nick Gordonf14ec352017-07-24 16:09:58 -0500139 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
140 std::list<ndn::Name> names = namePrefixes.getNames();
141 auto itr = std::find(names.begin(), names.end(), prefixName);
142 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600143 BOOST_FAIL("Prefix was not inserted!");
144 }
145 BOOST_CHECK_EQUAL((*itr), prefixName);
146 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600147 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600148}
149
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400150BOOST_AUTO_TEST_CASE(OnReceiveInterestUnregisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600151{
152 ndn::Name name("/localhost/nlsr/rib/unregister");
153 ndn::Name prefixName("/test/prefixA");
154 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000155 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600156
Nick Gordonf14ec352017-07-24 16:09:58 -0500157 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600158
Junxiao Shi008c9b12019-01-13 23:15:56 +0000159 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600160
Nick Gordonf14ec352017-07-24 16:09:58 -0500161 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600162 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600163 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600164}
165
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400166BOOST_AUTO_TEST_CASE(OnReceiveInterestInvalidPrefix)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600167{
168 ndn::Name name("/localhost/invalid/rib/register");
169 ndn::Name prefixName("/test/prefixA");
170 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000171 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600172
Junxiao Shi008c9b12019-01-13 23:15:56 +0000173 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600174
Nick Gordonf14ec352017-07-24 16:09:58 -0500175 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500176
177 // Cannot use routingUpdatePublish test now since in
178 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600179 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600180}
181
182BOOST_AUTO_TEST_SUITE_END()
183
Davide Pesavento288141a2024-02-13 17:30:35 -0500184} // namespace nlsr::tests