blob: ef8c1b69005fe1ce71d26abaab560ecd00a91dea [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/*
3 * Copyright (c) 2014-2021, 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
27#include "../test-common.hpp"
28#include "../control-commands.hpp"
Nick Gordon4d2c6c02017-01-20 13:18:46 -060029
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070030#include <boost/lexical_cast.hpp>
31
Nick Gordon4d2c6c02017-01-20 13:18:46 -060032namespace nlsr {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060033namespace test {
34
35class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture
36{
37public:
38 NfdRibCommandProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050039 : face(m_ioService, 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 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060046 addIdentity(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 {
Junxiao Shi008c9b12019-01-13 23:15:56 +000057 ndn::Interest interest(prefix.append(parameters.wireEncode()));
58 interest.setCanBePrefix(false);
59 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:
91 ndn::util::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
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600101typedef boost::mpl::vector<update::NfdRibRegisterCommand,
102 update::NfdRibUnregisterCommand> Commands;
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;
117 parameters.setName("/test/prefixA").setCost(10);
118
119 bool wasValidated = true;
120 try {
121 processor.validateParameters<NfdRibCommand>(parameters);
122 }
123 catch (...) {
124 wasValidated = false;
125 }
126 BOOST_CHECK(!wasValidated);
127}
128
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600129BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
130{
131 ndn::Name name("/localhost/nlsr/rib/register");
132 ndn::Name prefixName("/test/prefixA");
133 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000134 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600135
Junxiao Shi008c9b12019-01-13 23:15:56 +0000136 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600137
Nick Gordonf14ec352017-07-24 16:09:58 -0500138 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
139 std::list<ndn::Name> names = namePrefixes.getNames();
140 auto itr = std::find(names.begin(), names.end(), prefixName);
141 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600142 BOOST_FAIL("Prefix was not inserted!");
143 }
144 BOOST_CHECK_EQUAL((*itr), prefixName);
145 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600146 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600147}
148
149BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
150{
151 ndn::Name name("/localhost/nlsr/rib/unregister");
152 ndn::Name prefixName("/test/prefixA");
153 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000154 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600155
Nick Gordonf14ec352017-07-24 16:09:58 -0500156 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600157
Junxiao Shi008c9b12019-01-13 23:15:56 +0000158 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600159
Nick Gordonf14ec352017-07-24 16:09:58 -0500160 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600161 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600162 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600163}
164
165BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
166{
167 ndn::Name name("/localhost/invalid/rib/register");
168 ndn::Name prefixName("/test/prefixA");
169 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000170 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600171
Junxiao Shi008c9b12019-01-13 23:15:56 +0000172 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600173
Nick Gordonf14ec352017-07-24 16:09:58 -0500174 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500175
176 // Cannot use routingUpdatePublish test now since in
177 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600178 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600179}
180
181BOOST_AUTO_TEST_SUITE_END()
182
183} // namespace test
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600184} // namespace nlsr