blob: 3c0ae47eec4db389be579b8e28e97e086f6480fb [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();
59 }
60
61 std::shared_ptr<ndn::Interest>
62 makeInterest(const ndn::Name& name, uint32_t nonce)
63 {
64 auto interest = std::make_shared<ndn::Interest>(name);
65 if (nonce != 0) {
66 interest->setNonce(nonce);
67 }
68 return interest;
69 }
70
Ashlesh Gawande415676b2016-12-22 00:26:23 -060071 void sendInterestForPublishedData() {
72 // Need to send an interest now since ChronoSync
73 // no longer does face->put(*data) in publishData.
74 // Instead it does it in onInterest
75 ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA");
76 // The part after LSA is Chronosync getSession
77 lsaInterestName.append(sessionTime);
78 lsaInterestName.appendNumber(nlsr.getSequencingManager().getCombinedSeqNo());
79 shared_ptr<ndn::Interest> lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
80
81 face.receive(*lsaInterest);
82 this->advanceClocks(ndn::time::milliseconds(10));
83 }
84
Nick Gordon4d2c6c02017-01-20 13:18:46 -060085 bool
86 wasRoutingUpdatePublished()
87 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -060088 sendInterestForPublishedData();
Nick Gordon4d2c6c02017-01-20 13:18:46 -060089 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
90
91 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
92 [lsaPrefix] (const ndn::Data& data) {
93 return lsaPrefix.isPrefixOf(data.getName());
94 });
95
96 return (it != face.sentData.end());
97 }
98
99public:
100 ndn::util::DummyClientFace face;
101 ndn::KeyChain keyChain;
102
103 Nlsr nlsr;
104 NamePrefixList& namePrefixes;
105 NfdRibCommandProcessor& processor;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600106 ndn::Name sessionTime;
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600107};
108
109typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands;
110
111BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture)
112
113BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands)
114{
115 ndn::nfd::ControlParameters parameters;
116 parameters.setName("/test/prefixA");
117
118 BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters));
119}
120
121BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands)
122{
123 ndn::nfd::ControlParameters parameters;
124 parameters.setName("/test/prefixA").setCost(10);
125
126 bool wasValidated = true;
127 try {
128 processor.validateParameters<NfdRibCommand>(parameters);
129 }
130 catch (...) {
131 wasValidated = false;
132 }
133 BOOST_CHECK(!wasValidated);
134}
135
136BOOST_AUTO_TEST_CASE(InsertPrefix)
137{
138 ndn::nfd::ControlParameters parameters;
139 ndn::Name prefixName("/test/prefixA");
140 parameters.setName(prefixName);
141
142 processor.insertPrefix(parameters);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600143 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600144
145 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
146 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
147 prefixName);
148 if (itr == namePrefixes.getNameList().end()) {
149 BOOST_FAIL("Prefix was not inserted!");
150 }
151 BOOST_CHECK_EQUAL((*itr), parameters.getName());
152 BOOST_CHECK(wasRoutingUpdatePublished());
153}
154
155BOOST_AUTO_TEST_CASE(RemovePrefix)
156{
157 ndn::Name prefixName("/test/prefixA");
158 namePrefixes.getNameList().push_back(prefixName);
159 ndn::nfd::ControlParameters parameters;
160 parameters.setName("/test/prefixA");
161
162 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
163 processor.removePrefix(parameters);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600164 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600165
166 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
167 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
168 prefixName);
169 if (itr != namePrefixes.getNameList().end()) {
170 BOOST_FAIL("Prefix was not removed!");
171 }
172 BOOST_CHECK(wasRoutingUpdatePublished());
173}
174
175BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand)
176{
177 ndn::Name name("/localhost/nlsr/rib/register");
178 ndn::Name prefixName("/test/prefixA");
179 ndn::nfd::ControlParameters parameters;
180
181 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
182 .wireEncode()), 0);
183
184 face.receive(*command);
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600185 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600186
187 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1);
188 auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(),
189 prefixName);
190 if (itr == namePrefixes.getNameList().end()) {
191 BOOST_FAIL("Prefix was not inserted!");
192 }
193 BOOST_CHECK_EQUAL((*itr), prefixName);
194 BOOST_CHECK(wasRoutingUpdatePublished());
195}
196
197BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
198{
199 ndn::Name name("/localhost/nlsr/rib/unregister");
200 ndn::Name prefixName("/test/prefixA");
201 ndn::nfd::ControlParameters parameters;
202
203 namePrefixes.getNameList().push_back(prefixName);
204
205 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
206 .wireEncode()), 0);
207
208 face.receive(ndn::Interest(name));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600209 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600210
211 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
212 BOOST_CHECK(wasRoutingUpdatePublished());
213}
214
215BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
216{
217 ndn::Name name("/localhost/invalid/rib/register");
218 ndn::Name prefixName("/test/prefixA");
219 ndn::nfd::ControlParameters parameters;
220
221 shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName)
222 .wireEncode()), 0);
223
224 face.receive(ndn::Interest(name));
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600225 this->advanceClocks(ndn::time::milliseconds(10));
Nick Gordon4d2c6c02017-01-20 13:18:46 -0600226
227 BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0);
228 BOOST_CHECK(!wasRoutingUpdatePublished());
229}
230
231BOOST_AUTO_TEST_SUITE_END()
232
233} // namespace test
234} // namespace update
235} // namespace nlsr