blob: 81b66ced7649c44a72767a3d4ba55f2266dda852 [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()
42 : face(g_ioService, keyChain, {true, true})
43 , nlsr(g_ioService, g_scheduler, face, g_keyChain)
44 , namePrefixes(nlsr.getNamePrefixList())
45 , processor(nlsr.getNfdRibCommandProcessor())
46 {
47 // Set the network so the LSA prefix is constructed
48 nlsr.getConfParameter().setNetwork("/ndn");
49
50 // Initialize NLSR so a sync socket is created
51 nlsr.initialize();
Ashlesh Gawande415676b2016-12-22 00:26:23 -060052
53 // Saving clock::now before any advanceClocks so that it will
54 // be the same value as what ChronoSync uses in setting the sessionName
55 sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
56
57 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -060058 face.sentInterests.clear();
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050059
60 nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060061 }
62
63 std::shared_ptr<ndn::Interest>
64 makeInterest(const ndn::Name& name, uint32_t nonce)
65 {
66 auto interest = std::make_shared<ndn::Interest>(name);
67 if (nonce != 0) {
68 interest->setNonce(nonce);
69 }
70 return interest;
71 }
72
Ashlesh Gawande415676b2016-12-22 00:26:23 -060073 void sendInterestForPublishedData() {
74 // Need to send an interest now since ChronoSync
75 // no longer does face->put(*data) in publishData.
76 // Instead it does it in onInterest
77 ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA");
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050078 lsaInterestName.append(NameLsa::TYPE_STRING);
79
Ashlesh Gawande415676b2016-12-22 00:26:23 -060080 // The part after LSA is Chronosync getSession
81 lsaInterestName.append(sessionTime);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050082 lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Ashlesh Gawande415676b2016-12-22 00:26:23 -060083 shared_ptr<ndn::Interest> lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
84
85 face.receive(*lsaInterest);
86 this->advanceClocks(ndn::time::milliseconds(10));
87 }
88
Nick Gordon4d2c6c02017-01-20 13:18:46 -060089 bool
90 wasRoutingUpdatePublished()
91 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060092 sendInterestForPublishedData();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060093 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
94
95 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050096 [&] (const ndn::Data& data) {
Nick Gordon4d2c6c02017-01-20 13:18:46 -060097 return lsaPrefix.isPrefixOf(data.getName());
98 });
99
100 return (it != face.sentData.end());
101 }
102
103public:
104 ndn::util::DummyClientFace face;
105 ndn::KeyChain keyChain;
106
107 Nlsr nlsr;
108 NamePrefixList& namePrefixes;
109 NfdRibCommandProcessor& processor;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600110 ndn::Name sessionTime;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500111 uint64_t nameLsaSeqNoBeforeInterest;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600112};
113
114typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands;
115
116BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
117
118BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
119{
120 ndn::nfd::ControlParameters parameters;
121 parameters.setName("/test/prefixA");
122
123 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
124}
125
126BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
127{
128 ndn::nfd::ControlParameters parameters;
129 parameters.setName("/test/prefixA").setCost(10);
130
131 bool wasValidated = true;
132 try {
133 processor.validateParameters<NfdRibCommand>(parameters);
134 }
135 catch (...) {
136 wasValidated = false;
137 }
138 BOOST_CHECK(!wasValidated);
139}
140
141BOOST_AUTO_TEST_CASE(InsertPrefix)
142{
143 ndn::nfd::ControlParameters parameters;
144 ndn::Name prefixName("/test/prefixA");
145 parameters.setName(prefixName);
146
147 processor.insertPrefix(parameters);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600148 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600149
150 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
151 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
152 prefixName);
153 if (itr == namePrefixes.getNameList().end()) {
154 BOOST_FAIL("Prefix was not inserted!");
155 }
156 BOOST_CHECK_EQUAL((*itr), parameters.getName());
157 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500158 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600159}
160
161BOOST_AUTO_TEST_CASE(RemovePrefix)
162{
163 ndn::Name prefixName("/test/prefixA");
164 namePrefixes.getNameList().push_back(prefixName);
165 ndn::nfd::ControlParameters parameters;
166 parameters.setName("/test/prefixA");
167
168 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
169 processor.removePrefix(parameters);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600170 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600171
172 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
173 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
174 prefixName);
175 if (itr != namePrefixes.getNameList().end()) {
176 BOOST_FAIL("Prefix was not removed!");
177 }
178 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500179 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600180}
181
182BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
183{
184 ndn::Name name("/localhost/nlsr/rib/register");
185 ndn::Name prefixName("/test/prefixA");
186 ndn::nfd::ControlParameters parameters;
187
188 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
189 .wireEncode()), 0);
190
191 face.receive(*command);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600192 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600193
194 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
195 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
196 prefixName);
197 if (itr == namePrefixes.getNameList().end()) {
198 BOOST_FAIL("Prefix was not inserted!");
199 }
200 BOOST_CHECK_EQUAL((*itr), prefixName);
201 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500202 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600203}
204
205BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
206{
207 ndn::Name name("/localhost/nlsr/rib/unregister");
208 ndn::Name prefixName("/test/prefixA");
209 ndn::nfd::ControlParameters parameters;
210
211 namePrefixes.getNameList().push_back(prefixName);
212
213 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
214 .wireEncode()), 0);
215
216 face.receive(ndn::Interest(name));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600217 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600218
219 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
220 BOOST_CHECK(wasRoutingUpdatePublished());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500221 BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600222}
223
224BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
225{
226 ndn::Name name("/localhost/invalid/rib/register");
227 ndn::Name prefixName("/test/prefixA");
228 ndn::nfd::ControlParameters parameters;
229
230 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
231 .wireEncode()), 0);
232
233 face.receive(ndn::Interest(name));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600234 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600235
236 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500237
238 // Cannot use routingUpdatePublish test now since in
239 // initialize nlsr calls buildOwnNameLsa which publishes the routing update
240 BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.getLsdb().getSequencingManager().getNameLsaSeq());
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600241}
242
243BOOST_AUTO_TEST_SUITE_END()
244
245} // namespace test
246} // namespace update
247} // namespace nlsr