blob: b0552759ac53a4252b11428da4d2f15076a3cdbb [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/*
Junxiao Shi43f37a02023-08-09 00:09:00 +00003 * Copyright (c) 2014-2023, 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
Nick Gordon4d2c6c02017-01-20 13:18:46 -060033namespace nlsr {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060034namespace test {
35
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040036class NfdRibCommandProcessorFixture : public IoKeyChainFixture
Nick Gordon4d2c6c02017-01-20 13:18:46 -060037{
38public:
39 NfdRibCommandProcessorFixture()
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040040 : face(m_io, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060041 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , confProcessor(conf)
43 , nlsr(face, m_keyChain, conf)
44 , namePrefixes(conf.getNamePrefixList())
45 , processor(nlsr.m_nfdRibCommandProcessor)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060046 {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040047 m_keyChain.createIdentity(conf.getRouterPrefix());
Nick Gordon4d2c6c02017-01-20 13:18:46 -060048
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050049 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060050 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050051
Ashlesh Gawande15052402018-12-12 20:20:00 -060052 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060053 }
54
Junxiao Shi008c9b12019-01-13 23:15:56 +000055 void
56 sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060057 {
Davide Pesaventoe28d8752022-03-19 03:55:25 -040058 ndn::Interest interest(prefix.append(ndn::tlv::GenericNameComponent, parameters.wireEncode()));
Junxiao Shi008c9b12019-01-13 23:15:56 +000059 face.receive(interest);
60 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060061 }
62
Ashlesh Gawande85998a12017-12-07 22:22:13 -060063 void sendInterestForPublishedData()
Junxiao Shi008c9b12019-01-13 23:15:56 +000064 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065 ndn::Name lsaInterestName = conf.getLsaPrefix();
66 lsaInterestName.append(conf.getSiteName());
67 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande15052402018-12-12 20:20:00 -060069 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050070
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 face.receive(ndn::Interest(lsaInterestName).setCanBePrefix(true));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050072 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060073 }
74
Nick Gordon4d2c6c02017-01-20 13:18:46 -060075 bool
76 wasRoutingUpdatePublished()
77 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060078 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079
Ashlesh Gawande85998a12017-12-07 22:22:13 -060080 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060081
82 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050083 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060084 return lsaPrefix.isPrefixOf(data.getName());
85 });
86
87 return (it != face.sentData.end());
88 }
89
90public:
Junxiao Shi43f37a02023-08-09 00:09:00 +000091 ndn::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060092 ConfParameter conf;
93 DummyConfFileProcessor confProcessor;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060094
95 Nlsr nlsr;
96 NamePrefixList& namePrefixes;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060097 update::NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050098 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060099};
100
Davide Pesavento5849ee72023-11-12 20:00:21 -0500101using Commands = boost::mp11::mp_list<
102 update::NfdRibRegisterCommand,
103 update::NfdRibUnregisterCommand
104>;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600105
106BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
107
108BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
109{
110 ndn::nfd::ControlParameters parameters;
111 parameters.setName("/test/prefixA");
112
113 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
114}
115
116BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
117{
118 ndn::nfd::ControlParameters parameters;
119 parameters.setName("/test/prefixA").setCost(10);
120
121 bool wasValidated = true;
122 try {
123 processor.validateParameters<NfdRibCommand>(parameters);
124 }
125 catch (...) {
126 wasValidated = false;
127 }
128 BOOST_CHECK(!wasValidated);
129}
130
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400131BOOST_AUTO_TEST_CASE(OnReceiveInterestRegisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600132{
133 ndn::Name name("/localhost/nlsr/rib/register");
134 ndn::Name prefixName("/test/prefixA");
135 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000136 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600137
Junxiao Shi008c9b12019-01-13 23:15:56 +0000138 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600139
Nick Gordonf14ec352017-07-24 16:09:58 -0500140 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
141 std::list<ndn::Name> names = namePrefixes.getNames();
142 auto itr = std::find(names.begin(), names.end(), prefixName);
143 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600144 BOOST_FAIL("Prefix was not inserted!");
145 }
146 BOOST_CHECK_EQUAL((*itr), prefixName);
147 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600148 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600149}
150
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400151BOOST_AUTO_TEST_CASE(OnReceiveInterestUnregisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600152{
153 ndn::Name name("/localhost/nlsr/rib/unregister");
154 ndn::Name prefixName("/test/prefixA");
155 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000156 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600157
Nick Gordonf14ec352017-07-24 16:09:58 -0500158 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600159
Junxiao Shi008c9b12019-01-13 23:15:56 +0000160 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600161
Nick Gordonf14ec352017-07-24 16:09:58 -0500162 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600163 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600164 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600165}
166
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400167BOOST_AUTO_TEST_CASE(OnReceiveInterestInvalidPrefix)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600168{
169 ndn::Name name("/localhost/invalid/rib/register");
170 ndn::Name prefixName("/test/prefixA");
171 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000172 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600173
Junxiao Shi008c9b12019-01-13 23:15:56 +0000174 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600175
Nick Gordonf14ec352017-07-24 16:09:58 -0500176 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500177
178 // Cannot use routingUpdatePublish test now since in
179 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600180 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600181}
182
183BOOST_AUTO_TEST_SUITE_END()
184
185} // namespace test
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600186} // namespace nlsr