blob: 938579063f5a55c1513ef6776feb01489044dd92 [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 Pesaventod1f1df82022-03-12 16:40:37 -05003 * Copyright (c) 2014-2022, 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>
31
Nick Gordon4d2c6c02017-01-20 13:18:46 -060032namespace nlsr {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060033namespace test {
34
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:
90 ndn::util::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
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600100typedef boost::mpl::vector<update::NfdRibRegisterCommand,
101 update::NfdRibUnregisterCommand> Commands;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600102
103BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
104
105BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
106{
107 ndn::nfd::ControlParameters parameters;
108 parameters.setName("/test/prefixA");
109
110 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
111}
112
113BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
114{
115 ndn::nfd::ControlParameters parameters;
116 parameters.setName("/test/prefixA").setCost(10);
117
118 bool wasValidated = true;
119 try {
120 processor.validateParameters<NfdRibCommand>(parameters);
121 }
122 catch (...) {
123 wasValidated = false;
124 }
125 BOOST_CHECK(!wasValidated);
126}
127
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400128BOOST_AUTO_TEST_CASE(OnReceiveInterestRegisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600129{
130 ndn::Name name("/localhost/nlsr/rib/register");
131 ndn::Name prefixName("/test/prefixA");
132 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000133 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600134
Junxiao Shi008c9b12019-01-13 23:15:56 +0000135 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600136
Nick Gordonf14ec352017-07-24 16:09:58 -0500137 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
138 std::list<ndn::Name> names = namePrefixes.getNames();
139 auto itr = std::find(names.begin(), names.end(), prefixName);
140 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600141 BOOST_FAIL("Prefix was not inserted!");
142 }
143 BOOST_CHECK_EQUAL((*itr), prefixName);
144 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600145 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600146}
147
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400148BOOST_AUTO_TEST_CASE(OnReceiveInterestUnregisterCommand)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600149{
150 ndn::Name name("/localhost/nlsr/rib/unregister");
151 ndn::Name prefixName("/test/prefixA");
152 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000153 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600154
Nick Gordonf14ec352017-07-24 16:09:58 -0500155 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600156
Junxiao Shi008c9b12019-01-13 23:15:56 +0000157 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600158
Nick Gordonf14ec352017-07-24 16:09:58 -0500159 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600160 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600161 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600162}
163
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400164BOOST_AUTO_TEST_CASE(OnReceiveInterestInvalidPrefix)
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600165{
166 ndn::Name name("/localhost/invalid/rib/register");
167 ndn::Name prefixName("/test/prefixA");
168 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000169 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600170
Junxiao Shi008c9b12019-01-13 23:15:56 +0000171 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600172
Nick Gordonf14ec352017-07-24 16:09:58 -0500173 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500174
175 // Cannot use routingUpdatePublish test now since in
176 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600177 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600178}
179
180BOOST_AUTO_TEST_SUITE_END()
181
182} // namespace test
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600183} // namespace nlsr