blob: d5fa0c8dfe772daf3f93d7a9a40c537e380d0e9f [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
30namespace nlsr {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060031namespace test {
32
33class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture
34{
35public:
36 NfdRibCommandProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050037 : face(m_ioService, m_keyChain, {true, true})
Saurab Dulal427e0122019-11-28 11:58:02 -060038 , conf(face, m_keyChain)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060039 , confProcessor(conf)
40 , nlsr(face, m_keyChain, conf)
41 , namePrefixes(conf.getNamePrefixList())
42 , processor(nlsr.m_nfdRibCommandProcessor)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060043 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060044 addIdentity(conf.getRouterPrefix());
Nick Gordon4d2c6c02017-01-20 13:18:46 -060045
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050046 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060047 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050048
Ashlesh Gawande15052402018-12-12 20:20:00 -060049 nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060050 }
51
Junxiao Shi008c9b12019-01-13 23:15:56 +000052 void
53 sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060054 {
Junxiao Shi008c9b12019-01-13 23:15:56 +000055 ndn::Interest interest(prefix.append(parameters.wireEncode()));
56 interest.setCanBePrefix(false);
57 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:
89 ndn::util::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
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099typedef boost::mpl::vector<update::NfdRibRegisterCommand,
100 update::NfdRibUnregisterCommand> Commands;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600101
102BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
103
104BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
105{
106 ndn::nfd::ControlParameters parameters;
107 parameters.setName("/test/prefixA");
108
109 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
110}
111
112BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
113{
114 ndn::nfd::ControlParameters parameters;
115 parameters.setName("/test/prefixA").setCost(10);
116
117 bool wasValidated = true;
118 try {
119 processor.validateParameters<NfdRibCommand>(parameters);
120 }
121 catch (...) {
122 wasValidated = false;
123 }
124 BOOST_CHECK(!wasValidated);
125}
126
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600127BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
128{
129 ndn::Name name("/localhost/nlsr/rib/register");
130 ndn::Name prefixName("/test/prefixA");
131 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000132 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600133
Junxiao Shi008c9b12019-01-13 23:15:56 +0000134 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600135
Nick Gordonf14ec352017-07-24 16:09:58 -0500136 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
137 std::list<ndn::Name> names = namePrefixes.getNames();
138 auto itr = std::find(names.begin(), names.end(), prefixName);
139 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600140 BOOST_FAIL("Prefix was not inserted!");
141 }
142 BOOST_CHECK_EQUAL((*itr), prefixName);
143 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600144 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600145}
146
147BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
148{
149 ndn::Name name("/localhost/nlsr/rib/unregister");
150 ndn::Name prefixName("/test/prefixA");
151 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000152 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600153
Nick Gordonf14ec352017-07-24 16:09:58 -0500154 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600155
Junxiao Shi008c9b12019-01-13 23:15:56 +0000156 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600157
Nick Gordonf14ec352017-07-24 16:09:58 -0500158 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600159 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande15052402018-12-12 20:20:00 -0600160 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600161}
162
163BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
164{
165 ndn::Name name("/localhost/invalid/rib/register");
166 ndn::Name prefixName("/test/prefixA");
167 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000168 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600169
Junxiao Shi008c9b12019-01-13 23:15:56 +0000170 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600171
Nick Gordonf14ec352017-07-24 16:09:58 -0500172 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500173
174 // Cannot use routingUpdatePublish test now since in
175 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
Ashlesh Gawande15052402018-12-12 20:20:00 -0600176 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600177}
178
179BOOST_AUTO_TEST_SUITE_END()
180
181} // namespace test
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600182} // namespace nlsr