blob: 0709e43dfab217b6bcab63d4381566bc14ba96e5 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevb0bf8df2018-02-19 12:25:44 -05003 * Copyright (c) 2014-2018, 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"
23
24#include "../test-common.hpp"
25#include "../control-commands.hpp"
26#include "conf-parameter.hpp"
27#include "adjacency-list.hpp"
28#include "nlsr.hpp"
29
Ashlesh Gawande415676b2016-12-22 00:26:23 -060030#include <ndn-cxx/interest.hpp>
Nick Gordon4d2c6c02017-01-20 13:18:46 -060031#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon4d2c6c02017-01-20 13:18:46 -060032#include <ndn-cxx/security/key-chain.hpp>
33
34namespace nlsr {
35namespace update {
36namespace test {
37
38class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture
39{
40public:
41 NfdRibCommandProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050042 : face(m_ioService, m_keyChain, {true, true})
43 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Nick Gordon4d2c6c02017-01-20 13:18:46 -060044 , namePrefixes(nlsr.getNamePrefixList())
45 , processor(nlsr.getNfdRibCommandProcessor())
46 {
47 // Set the network so the LSA prefix is constructed
48 nlsr.getConfParameter().setNetwork("/ndn");
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050049 nlsr.getConfParameter().setRouterName(ndn::Name("/This/router"));
50
51 addIdentity(ndn::Name("/ndn/This/router"));
Nick Gordon4d2c6c02017-01-20 13:18:46 -060052
53 // Initialize NLSR so a sync socket is created
54 nlsr.initialize();
Ashlesh Gawande415676b2016-12-22 00:26:23 -060055
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050056 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060057 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050058
59 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060060 }
61
62 std::shared_ptr<ndn::Interest>
63 makeInterest(const ndn::Name& name, uint32_t nonce)
64 {
65 auto interest = std::make_shared<ndn::Interest>(name);
66 if (nonce != 0) {
67 interest->setNonce(nonce);
68 }
69 return interest;
70 }
71
Ashlesh Gawande415676b2016-12-22 00:26:23 -060072 void sendInterestForPublishedData() {
73 // Need to send an interest now since ChronoSync
74 // no longer does face->put(*data) in publishData.
75 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050076 ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA/This/router");
Nick Gordon727d4832017-10-13 18:04:25 -050077 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050078
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050079 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -060080 shared_ptr<ndn::Interest> lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
81
82 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050083 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060084 }
85
Nick Gordon4d2c6c02017-01-20 13:18:46 -060086 bool
87 wasRoutingUpdatePublished()
88 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060089 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050090
Nick Gordon4d2c6c02017-01-20 13:18:46 -060091 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
92
93 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050094 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060095 return lsaPrefix.isPrefixOf(data.getName());
96 });
97
98 return (it != face.sentData.end());
99 }
100
101public:
102 ndn::util::DummyClientFace face;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600103
104 Nlsr nlsr;
105 NamePrefixList& namePrefixes;
106 NfdRibCommandProcessor& processor;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500107 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600108};
109
110typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands;
111
112BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
113
114BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
115{
116 ndn::nfd::ControlParameters parameters;
117 parameters.setName("/test/prefixA");
118
119 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
120}
121
122BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
123{
124 ndn::nfd::ControlParameters parameters;
125 parameters.setName("/test/prefixA").setCost(10);
126
127 bool wasValidated = true;
128 try {
129 processor.validateParameters<NfdRibCommand>(parameters);
130 }
131 catch (...) {
132 wasValidated = false;
133 }
134 BOOST_CHECK(!wasValidated);
135}
136
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600137BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
138{
139 ndn::Name name("/localhost/nlsr/rib/register");
140 ndn::Name prefixName("/test/prefixA");
141 ndn::nfd::ControlParameters parameters;
142
143 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
144 .wireEncode()), 0);
145
146 face.receive(*command);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500147 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600148
Nick Gordonf14ec352017-07-24 16:09:58 -0500149 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
150 std::list<ndn::Name> names = namePrefixes.getNames();
151 auto itr = std::find(names.begin(), names.end(), prefixName);
152 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600153 BOOST_FAIL("Prefix was not inserted!");
154 }
155 BOOST_CHECK_EQUAL((*itr), prefixName);
156 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500157 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600158}
159
160BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
161{
162 ndn::Name name("/localhost/nlsr/rib/unregister");
163 ndn::Name prefixName("/test/prefixA");
164 ndn::nfd::ControlParameters parameters;
165
Nick Gordonf14ec352017-07-24 16:09:58 -0500166 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600167
168 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
169 .wireEncode()), 0);
170
171 face.receive(ndn::Interest(name));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500172 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600173
Nick Gordonf14ec352017-07-24 16:09:58 -0500174 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600175 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500176 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600177}
178
179BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
180{
181 ndn::Name name("/localhost/invalid/rib/register");
182 ndn::Name prefixName("/test/prefixA");
183 ndn::nfd::ControlParameters parameters;
184
185 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
186 .wireEncode()), 0);
187
188 face.receive(ndn::Interest(name));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600190
Nick Gordonf14ec352017-07-24 16:09:58 -0500191 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500192
193 // Cannot use routingUpdatePublish test now since in
194 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
195 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600196}
197
198BOOST_AUTO_TEST_SUITE_END()
199
200} // namespace test
201} // namespace update
202} // namespace nlsr