blob: 2a781716a27c9aa95d682194959869f7b26a54d3 [file] [log] [blame]
Nick Gordon4d2c6c02017-01-20 13:18:46 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, The University of Memphis,
4 * 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
56 // Saving clock::now before any advanceClocks so that it will
57 // be the same value as what ChronoSync uses in setting the sessionName
58 sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
59
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050060 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060061 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050062
63 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060064 }
65
66 std::shared_ptr<ndn::Interest>
67 makeInterest(const ndn::Name& name, uint32_t nonce)
68 {
69 auto interest = std::make_shared<ndn::Interest>(name);
70 if (nonce != 0) {
71 interest->setNonce(nonce);
72 }
73 return interest;
74 }
75
Ashlesh Gawande415676b2016-12-22 00:26:23 -060076 void sendInterestForPublishedData() {
77 // Need to send an interest now since ChronoSync
78 // no longer does face->put(*data) in publishData.
79 // Instead it does it in onInterest
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050080 ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA/This/router");
Nick Gordon727d4832017-10-13 18:04:25 -050081 lsaInterestName.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050082
Ashlesh Gawande415676b2016-12-22 00:26:23 -060083 // The part after LSA is Chronosync getSession
84 lsaInterestName.append(sessionTime);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050085 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -060086 shared_ptr<ndn::Interest> lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
87
88 face.receive(*lsaInterest);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050089 this->advanceClocks(ndn::time::milliseconds(10), 10);
Ashlesh Gawande415676b2016-12-22 00:26:23 -060090 }
91
Nick Gordon4d2c6c02017-01-20 13:18:46 -060092 bool
93 wasRoutingUpdatePublished()
94 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060095 sendInterestForPublishedData();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050096
Nick Gordon4d2c6c02017-01-20 13:18:46 -060097 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
98
99 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500100 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600101 return lsaPrefix.isPrefixOf(data.getName());
102 });
103
104 return (it != face.sentData.end());
105 }
106
107public:
108 ndn::util::DummyClientFace face;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600109
110 Nlsr nlsr;
111 NamePrefixList& namePrefixes;
112 NfdRibCommandProcessor& processor;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600113 ndn::Name sessionTime;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500114 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600115};
116
117typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands;
118
119BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
120
121BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
122{
123 ndn::nfd::ControlParameters parameters;
124 parameters.setName("/test/prefixA");
125
126 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
127}
128
129BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
130{
131 ndn::nfd::ControlParameters parameters;
132 parameters.setName("/test/prefixA").setCost(10);
133
134 bool wasValidated = true;
135 try {
136 processor.validateParameters<NfdRibCommand>(parameters);
137 }
138 catch (...) {
139 wasValidated = false;
140 }
141 BOOST_CHECK(!wasValidated);
142}
143
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600144BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
145{
146 ndn::Name name("/localhost/nlsr/rib/register");
147 ndn::Name prefixName("/test/prefixA");
148 ndn::nfd::ControlParameters parameters;
149
150 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
151 .wireEncode()), 0);
152
153 face.receive(*command);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600155
Nick Gordonf14ec352017-07-24 16:09:58 -0500156 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
157 std::list<ndn::Name> names = namePrefixes.getNames();
158 auto itr = std::find(names.begin(), names.end(), prefixName);
159 if (itr == namePrefixes.getNames().end()) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600160 BOOST_FAIL("Prefix was not inserted!");
161 }
162 BOOST_CHECK_EQUAL((*itr), prefixName);
163 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500164 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600165}
166
167BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
168{
169 ndn::Name name("/localhost/nlsr/rib/unregister");
170 ndn::Name prefixName("/test/prefixA");
171 ndn::nfd::ControlParameters parameters;
172
Nick Gordonf14ec352017-07-24 16:09:58 -0500173 namePrefixes.insert(prefixName);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600174
175 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
176 .wireEncode()), 0);
177
178 face.receive(ndn::Interest(name));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500179 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600180
Nick Gordonf14ec352017-07-24 16:09:58 -0500181 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600182 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500183 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600184}
185
186BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
187{
188 ndn::Name name("/localhost/invalid/rib/register");
189 ndn::Name prefixName("/test/prefixA");
190 ndn::nfd::ControlParameters parameters;
191
192 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
193 .wireEncode()), 0);
194
195 face.receive(ndn::Interest(name));
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500196 this->advanceClocks(ndn::time::milliseconds(10), 10);
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600197
Nick Gordonf14ec352017-07-24 16:09:58 -0500198 BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500199
200 // Cannot use routingUpdatePublish test now since in
201 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
202 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600203}
204
205BOOST_AUTO_TEST_SUITE_END()
206
207} // namespace test
208} // namespace update
209} // namespace nlsr