blob: 93a5f4dd01cd993c73d894a3a952dfb384203bd3 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi008c9b12019-01-13 23:15:56 +00003 * Copyright (c) 2014-2019, 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 {
31namespace update {
32namespace test {
33
34class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture
35{
36public:
37 NfdRibCommandProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050038 : face(m_ioService, m_keyChain, {true, true})
39 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060040 , namePrefixes(nlsr.getNamePrefixList())
41 , processor(nlsr.getNfdRibCommandProcessor())
42 {
43 // Set the network so the LSA prefix is constructed
44 nlsr.getConfParameter().setNetwork("/ndn");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050045 nlsr.getConfParameter().setRouterName(ndn::Name("/This/router"));
46
47 addIdentity(ndn::Name("/ndn/This/router"));
Nick Gordon4d2c6c02017-01-20 13:18:46 -060048
49 // Initialize NLSR so a sync socket is created
50 nlsr.initialize();
Ashlesh Gawande415676b2016-12-22 00:26:23 -060051
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050052 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060053 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050054
55 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060056 }
57
Junxiao Shi008c9b12019-01-13 23:15:56 +000058 void
59 sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060060 {
Junxiao Shi008c9b12019-01-13 23:15:56 +000061 ndn::Interest interest(prefix.append(parameters.wireEncode()));
62 interest.setCanBePrefix(false);
63 face.receive(interest);
64 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060065 }
66
Junxiao Shi008c9b12019-01-13 23:15:56 +000067 void
68 sendInterestForPublishedData()
69 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060070 // Need to send an interest now since ChronoSync
71 // no longer does face->put(*data) in publishData.
72 // Instead it does it in onInterest
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050073 ndn::Name lsaInterestName("/localhop/ndn/nlsr/LSA/This/router");
Nick Gordon727d4832017-10-13 18:04:25 -050074 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050076 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Junxiao Shi008c9b12019-01-13 23:15:56 +000077 auto lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
78 lsaInterest->setCanBePrefix(true);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060079 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060081 }
82
Nick Gordon4d2c6c02017-01-20 13:18:46 -060083 bool
84 wasRoutingUpdatePublished()
85 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060086 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050087
Nick Gordon4d2c6c02017-01-20 13:18:46 -060088 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
89
90 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050091 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060092 return lsaPrefix.isPrefixOf(data.getName());
93 });
94
95 return (it != face.sentData.end());
96 }
97
98public:
99 ndn::util::DummyClientFace face;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600100
101 Nlsr nlsr;
102 NamePrefixList& namePrefixes;
103 NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500104 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600105};
106
107typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands;
108
109BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
110
111BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
112{
113 ndn::nfd::ControlParameters parameters;
114 parameters.setName("/test/prefixA");
115
116 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
117}
118
119BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
120{
121 ndn::nfd::ControlParameters parameters;
122 parameters.setName("/test/prefixA").setCost(10);
123
124 bool wasValidated = true;
125 try {
126 processor.validateParameters<NfdRibCommand>(parameters);
127 }
128 catch (...) {
129 wasValidated = false;
130 }
131 BOOST_CHECK(!wasValidated);
132}
133
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600134BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
135{
136 ndn::Name name("/localhost/nlsr/rib/register");
137 ndn::Name prefixName("/test/prefixA");
138 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000139 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600140
Junxiao Shi008c9b12019-01-13 23:15:56 +0000141 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600142
Nick Gordonf14ec352017-07-24 16:09:58 -0500143 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
144 std::list<ndn::Name> names = namePrefixes.getNames();
145 auto itr = std::find(names.begin(), names.end(), prefixName);
146 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600147 BOOST_FAIL("Prefix was not inserted!");
148 }
149 BOOST_CHECK_EQUAL((*itr), prefixName);
150 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500151 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600152}
153
154BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
155{
156 ndn::Name name("/localhost/nlsr/rib/unregister");
157 ndn::Name prefixName("/test/prefixA");
158 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000159 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600160
Nick Gordonf14ec352017-07-24 16:09:58 -0500161 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600162
Junxiao Shi008c9b12019-01-13 23:15:56 +0000163 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600164
Nick Gordonf14ec352017-07-24 16:09:58 -0500165 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600166 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500167 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600168}
169
170BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
171{
172 ndn::Name name("/localhost/invalid/rib/register");
173 ndn::Name prefixName("/test/prefixA");
174 ndn::nfd::ControlParameters parameters;
Junxiao Shi008c9b12019-01-13 23:15:56 +0000175 parameters.setName(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600176
Junxiao Shi008c9b12019-01-13 23:15:56 +0000177 sendCommand(name, parameters);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600178
Nick Gordonf14ec352017-07-24 16:09:58 -0500179 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500180
181 // Cannot use routingUpdatePublish test now since in
182 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
183 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600184}
185
186BOOST_AUTO_TEST_SUITE_END()
187
188} // namespace test
189} // namespace update
190} // namespace nlsr