blob: 47f02c5e3786fac034a6f067641d9a5cf0dce09d [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal427e0122019-11-28 11:58:02 -06003 * Copyright (c) 2014-2020, 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/>.
20 **/
21
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
46 // Initialize NLSR so a sync socket is created
47 nlsr.initialize();
Ashlesh Gawande415676b2016-12-22 00:26:23 -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 {
Junxiao Shi008c9b12019-01-13 23:15:56 +000058 ndn::Interest interest(prefix.append(parameters.wireEncode()));
59 interest.setCanBePrefix(false);
60 face.receive(interest);
61 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060062 }
63
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064 void sendInterestForPublishedData()
Junxiao Shi008c9b12019-01-13 23:15:56 +000065 {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060066 ndn::Name lsaInterestName = conf.getLsaPrefix();
67 lsaInterestName.append(conf.getSiteName());
68 lsaInterestName.append(conf.getRouterName());
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080069 lsaInterestName.append(boost::lexical_cast<std::string>(Lsa::Type::NAME));
Ashlesh Gawande15052402018-12-12 20:20:00 -060070 lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050071
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 face.receive(ndn::Interest(lsaInterestName).setCanBePrefix(true));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050073 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060074 }
75
Nick Gordon4d2c6c02017-01-20 13:18:46 -060076 bool
77 wasRoutingUpdatePublished()
78 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060079 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080
Ashlesh Gawande85998a12017-12-07 22:22:13 -060081 const ndn::Name& lsaPrefix = conf.getLsaPrefix();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060082
83 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050084 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060085 return lsaPrefix.isPrefixOf(data.getName());
86 });
87
88 return (it != face.sentData.end());
89 }
90
91public:
92 ndn::util::DummyClientFace face;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093 ConfParameter conf;
94 DummyConfFileProcessor confProcessor;
Nick Gordon4d2c6c02017-01-20 13:18:46 -060095
96 Nlsr nlsr;
97 NamePrefixList& namePrefixes;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 update::NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050099 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600100};
101
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102typedef boost::mpl::vector<update::NfdRibRegisterCommand,
103 update::NfdRibUnregisterCommand> Commands;
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
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600130BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
131{
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
150BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
151{
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
166BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
167{
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
184} // namespace test
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600185} // namespace nlsr