blob: 2249dab5046b3165337a36fc7a43c48f8f9c9776 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
dmcoomescf8d0ed2017-02-21 11:39:01 -06003 * Copyright (c) 2014-2018, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
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/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehman7c603292014-09-11 17:48:16 -050021
Nick Gordonff9a6272017-10-12 13:38:29 -050022#include "conf-file-processor.hpp"
Vince Lehman7c603292014-09-11 17:48:16 -050023#include "test-common.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050024#include "nlsr.hpp"
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050025
Nick Gordonff9a6272017-10-12 13:38:29 -050026#include <fstream>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050027
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050028#include <boost/test/unit_test.hpp>
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050029#include <boost/filesystem.hpp>
30#include <boost/algorithm/string.hpp>
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060031#include <ndn-cxx/util/dummy-client-face.hpp>
32
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050034namespace test {
35
dmcoomes9f936662017-03-02 10:33:09 -060036using std::shared_ptr;
Vince Lehman904c2412014-09-23 19:36:11 -050037
Vince Lehman7b616582014-10-17 16:25:39 -050038const std::string SECTION_GENERAL =
39 "general\n"
40 "{\n"
41 " network /ndn/\n"
42 " site /memphis.edu/\n"
43 " router /cs/pollux/\n"
44 " lsa-refresh-time 1800\n"
45 " lsa-interest-lifetime 3\n"
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -070046 " router-dead-interval 86400\n"
Vince Lehman7b616582014-10-17 16:25:39 -050047 " seq-dir /tmp\n"
48 "}\n\n";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050049
Vince Lehman7b616582014-10-17 16:25:39 -050050const std::string SECTION_NEIGHBORS =
51 "neighbors\n"
52 "{\n"
53 " hello-retries 3\n"
54 " hello-timeout 1\n"
55 " hello-interval 60\n\n"
56 " adj-lsa-build-interval 3\n"
57 " first-hello-interval 6\n"
58 " neighbor\n"
59 " {\n"
60 " name /ndn/memphis.edu/cs/castor\n"
61 " face-uri udp4://localhost\n"
62 " link-cost 20\n"
63 " }\n\n"
64 " neighbor\n"
65 " {\n"
66 " name /ndn/memphis.edu/cs/mira\n"
67 " face-uri udp4://localhost\n"
68 " link-cost 30\n"
69 " }\n"
70 "}\n\n";
71
72const std::string SECTION_HYPERBOLIC_ON =
73 "hyperbolic\n"
74 "{\n"
75 " state on\n"
76 " radius 123.456\n"
77 " angle 1.45\n"
78 "}\n\n";
79
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060080const std::string SECTION_HYPERBOLIC_ANGLES_ON =
81 "hyperbolic\n"
82 "{\n"
83 " state on\n"
84 " radius 123.456\n"
85 " angle 1.45,2.25\n"
86 "}\n\n";
87
Vince Lehman7b616582014-10-17 16:25:39 -050088const std::string SECTION_HYPERBOLIC_OFF =
89 "hyperbolic\n"
90 "{\n"
91 " state off\n"
92 " radius 123.456\n"
93 " angle 1.45\n"
94 "}\n\n";
95
96const std::string SECTION_FIB =
97 "fib\n"
98 "{\n"
99 " max-faces-per-prefix 3\n"
100 " routing-calc-interval 9\n"
101 "}\n\n";
102
103const std::string SECTION_ADVERTISING =
104 "advertising\n"
105 "{\n"
106 " prefix /ndn/edu/memphis/cs/netlab\n"
107 " prefix /ndn/edu/memphis/sports/basketball\n"
108 "}\n";
109
110const std::string CONFIG_LINK_STATE = SECTION_GENERAL + SECTION_NEIGHBORS +
111 SECTION_HYPERBOLIC_OFF + SECTION_FIB + SECTION_ADVERTISING;
112
113const std::string CONFIG_HYPERBOLIC = SECTION_GENERAL + SECTION_NEIGHBORS +
114 SECTION_HYPERBOLIC_ON + SECTION_FIB + SECTION_ADVERTISING;
115
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600116const std::string CONFIG_HYPERBOLIC_ANGLES = SECTION_GENERAL + SECTION_NEIGHBORS +
117 SECTION_HYPERBOLIC_ANGLES_ON + SECTION_FIB +
118 SECTION_ADVERTISING;
119
Vince Lehman7b616582014-10-17 16:25:39 -0500120class ConfFileProcessorFixture : public BaseFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500121{
Vince Lehman7b616582014-10-17 16:25:39 -0500122public:
123 ConfFileProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500124 : face(m_ioService, m_keyChain)
125 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehman7b616582014-10-17 16:25:39 -0500126 , CONFIG_FILE("unit-test-nlsr.conf")
127 {
128 }
Vince Lehman904c2412014-09-23 19:36:11 -0500129
Vince Lehman7b616582014-10-17 16:25:39 -0500130 ~ConfFileProcessorFixture()
131 {
132 remove("unit-test-nlsr.conf");
133 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500134
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500135 bool
136 processConfigurationString(std::string confString)
Vince Lehman7b616582014-10-17 16:25:39 -0500137 {
138 std::ofstream config;
139 config.open("unit-test-nlsr.conf");
140 config << confString;
141 config.close();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500142
Vince Lehman7b616582014-10-17 16:25:39 -0500143 ConfFileProcessor processor(nlsr, CONFIG_FILE);
144 return processor.processConfFile();
145 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500146
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500147 void
alvya2228c62014-12-09 10:25:11 -0600148 commentOut(const std::string& key, std::string& config)
149 {
150 boost::replace_all(config, key, ";" + key);
151 }
152
Vince Lehman7b616582014-10-17 16:25:39 -0500153public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500154 ndn::util::DummyClientFace face;
Vince Lehman7b616582014-10-17 16:25:39 -0500155 Nlsr nlsr;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500156
Vince Lehman7b616582014-10-17 16:25:39 -0500157private:
158 const std::string CONFIG_FILE;
159};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500160
Vince Lehman7b616582014-10-17 16:25:39 -0500161BOOST_FIXTURE_TEST_SUITE(TestConfFileProcessor, ConfFileProcessorFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500162
Vince Lehman7b616582014-10-17 16:25:39 -0500163BOOST_AUTO_TEST_CASE(LinkState)
164{
165 processConfigurationString(CONFIG_LINK_STATE);
Vince Lehman7b616582014-10-17 16:25:39 -0500166 ConfParameter& conf = nlsr.getConfParameter();
167 conf.buildRouterPrefix();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500168
Vince Lehman7b616582014-10-17 16:25:39 -0500169 // General
170 BOOST_CHECK_EQUAL(conf.getNetwork(), "/ndn/");
171 BOOST_CHECK_EQUAL(conf.getSiteName(), "/memphis.edu/");
172 BOOST_CHECK_EQUAL(conf.getRouterName(), "/cs/pollux/");
173 BOOST_CHECK_EQUAL(conf.getRouterPrefix(), "/ndn/memphis.edu/cs/pollux/");
Ashlesh Gawande8bfd1242017-06-21 14:55:27 -0500174 BOOST_CHECK_EQUAL(conf.getChronosyncPrefix(), "/localhop/ndn/NLSR/sync");
Ashlesh Gawande6077ea92017-01-19 11:48:29 -0600175 BOOST_CHECK_EQUAL(conf.getLsaPrefix(), "/localhop/ndn/NLSR/LSA");
Vince Lehman7b616582014-10-17 16:25:39 -0500176 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), 1800);
177 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(), ndn::time::seconds(3));
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700178 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), 86400);
Vince Lehman7b616582014-10-17 16:25:39 -0500179 BOOST_CHECK_EQUAL(conf.getSeqFileDir(), "/tmp");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700180
Vince Lehman7b616582014-10-17 16:25:39 -0500181 // Neighbors
182 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), 3);
183 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), 1);
184 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), 60);
185
186 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(), 3);
187 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
188
189 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/mira"));
190 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/castor"));
191 BOOST_CHECK(!nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/fail"));
192
193 Adjacent mira = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/mira");
194 BOOST_CHECK_EQUAL(mira.getName(), "/ndn/memphis.edu/cs/mira");
195 BOOST_CHECK_EQUAL(mira.getLinkCost(), 30);
196
197 Adjacent castor = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/castor");
198 BOOST_CHECK_EQUAL(castor.getName(), "/ndn/memphis.edu/cs/castor");
199 BOOST_CHECK_EQUAL(castor.getLinkCost(), 20);
200
201 // Hyperbolic
202 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 0);
203
204 // FIB
205 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(), 3);
206 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(), 9);
207
208 // Advertising
Nick Gordonff9a6272017-10-12 13:38:29 -0500209 BOOST_CHECK_EQUAL(nlsr.getNamePrefixList().size(), 2);
Vince Lehman7b616582014-10-17 16:25:39 -0500210}
211
alvy2fe12872014-11-25 10:32:23 -0600212BOOST_AUTO_TEST_CASE(MalformedUri)
213{
214 const std::string MALFORMED_URI =
215 "neighbors\n"
216 "{\n"
217 " hello-retries 3\n"
218 " hello-timeout 1\n"
219 " hello-interval 60\n\n"
220 " adj-lsa-build-interval 3\n"
221 " first-hello-interval 6\n"
222 " neighbor\n"
223 " {\n"
224 " name /ndn/memphis.edu/cs/castor\n"
225 " face-uri udp4:malformed-uri\n"
226 " link-cost 20\n"
227 " }\n"
228 "}\n\n";
229
230 BOOST_CHECK_EQUAL(processConfigurationString(MALFORMED_URI), false);
231}
232
Vince Lehman7b616582014-10-17 16:25:39 -0500233BOOST_AUTO_TEST_CASE(Hyperbolic)
234{
235 processConfigurationString(CONFIG_HYPERBOLIC);
236
237 ConfParameter& conf = nlsr.getConfParameter();
238 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
239 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600240 std::vector<double> angles;
241 angles.push_back(1.45);
242 BOOST_CHECK(conf.getCorTheta() == angles);
243}
244
245BOOST_AUTO_TEST_CASE(Hyperbolic2)
246{
247 processConfigurationString(CONFIG_HYPERBOLIC_ANGLES);
248
249 ConfParameter& conf = nlsr.getConfParameter();
250 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
251 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
252 std::vector<double> angles;
253 angles.push_back(1.45);
254 angles.push_back(2.25);
255 BOOST_CHECK(conf.getCorTheta() == angles);
Vince Lehman7b616582014-10-17 16:25:39 -0500256}
257
alvya2228c62014-12-09 10:25:11 -0600258BOOST_AUTO_TEST_CASE(DefaultValuesGeneral)
Vince Lehman7b616582014-10-17 16:25:39 -0500259{
alvya2228c62014-12-09 10:25:11 -0600260 std::string config = SECTION_GENERAL;
Vince Lehman7b616582014-10-17 16:25:39 -0500261
alvya2228c62014-12-09 10:25:11 -0600262 commentOut("lsa-refresh-time", config);
263 commentOut("lsa-interest-lifetime", config);
264 commentOut("router-dead-interval", config);
265
266 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
Vince Lehman7b616582014-10-17 16:25:39 -0500267
268 ConfParameter& conf = nlsr.getConfParameter();
269
alvy5a454952014-12-15 12:49:54 -0600270 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), static_cast<uint32_t>(LSA_REFRESH_TIME_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600271 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(),
272 static_cast<ndn::time::seconds>(LSA_INTEREST_LIFETIME_DEFAULT));
273 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), (2*conf.getLsaRefreshTime()));
274}
275
276BOOST_AUTO_TEST_CASE(DefaultValuesNeighbors)
277{
278 std::string config = SECTION_NEIGHBORS;
279
280 commentOut("hello-retries", config);
281 commentOut("hello-timeout", config);
282 commentOut("hello-interval", config);
283 commentOut("first-hello-interval", config);
284 commentOut("adj-lsa-build-interval", config);
285
286 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
287
288 ConfParameter& conf = nlsr.getConfParameter();
289
290 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), static_cast<uint32_t>(HELLO_RETRIES_DEFAULT));
alvy5a454952014-12-15 12:49:54 -0600291 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), static_cast<uint32_t>(HELLO_TIMEOUT_DEFAULT));
292 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), static_cast<uint32_t>(HELLO_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600293 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(),
294 static_cast<uint32_t>(FIRST_HELLO_INTERVAL_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500295 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(),
296 static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600297}
Vince Lehman7b616582014-10-17 16:25:39 -0500298
alvya2228c62014-12-09 10:25:11 -0600299BOOST_AUTO_TEST_CASE(DefaultValuesFib)
300{
301 std::string config = SECTION_FIB;
302
303 commentOut("max-faces-per-prefix", config);
304 commentOut("routing-calc-interval", config);
305
306 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
307
308 ConfParameter& conf = nlsr.getConfParameter();
309
310 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(),
311 static_cast<uint32_t>(MAX_FACES_PER_PREFIX_DEFAULT));
312 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(),
313 static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT));
314}
315
316BOOST_AUTO_TEST_CASE(DefaultValuesHyperbolic)
317{
318 std::string config = SECTION_HYPERBOLIC_ON;
319
320 commentOut("state", config);
321
322 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
323
324 ConfParameter& conf = nlsr.getConfParameter();
325
326 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), static_cast<int32_t>(HYPERBOLIC_STATE_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500327}
328
329BOOST_AUTO_TEST_CASE(OutOfRangeValue)
330{
331 const std::string SECTION_FIB_OUT_OF_RANGE =
332 "fib\n"
333 "{\n"
334 " max-faces-per-prefix 3\n"
335 " routing-calc-interval 999\n" // Larger than max value
336 "}\n\n";
337
338 // Processing should fail due to out of range value
339 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_FIB_OUT_OF_RANGE), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500340}
341
alvy5a454952014-12-15 12:49:54 -0600342BOOST_AUTO_TEST_CASE(NegativeValue)
343{
344 const std::string SECTION_GENERAL_NEGATIVE_VALUE =
345 "general\n"
346 "{\n"
347 " network /ndn/\n"
348 " site /memphis.edu/\n"
349 " router /cs/pollux/\n"
350 " lsa-refresh-time -1800\n"
351 " lsa-interest-lifetime -3\n"
352 " router-dead-interval -86400\n"
353 "}\n\n";
354
355 // Processing should fail due to negative value
356 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_GENERAL_NEGATIVE_VALUE), false);
357}
358
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500359BOOST_AUTO_TEST_CASE(LoadCertToPublish)
360{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500361 auto identity = addIdentity("/TestNLSR/identity");
362 saveCertificate(identity, "cert-to-publish.cert");
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500363
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500364 const std::string SECTION_SECURITY = R"CONF(
365 security
366 {
367 validator
368 {
369 trust-anchor
370 {
371 type any
372 }
373 }
374 prefix-update-validator
375 {
376 trust-anchor
377 {
378 type any
379 }
380 }
381 cert-to-publish "cert-to-publish.cert"
382 }
383 )CONF";
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500384
385 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
386
387 // Certificate should now be in the CertificateStore
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500388 security::CertificateStore& certStore = nlsr.getCertificateStore();
389 BOOST_CHECK(certStore.find(identity.getDefaultKey().getName()) != nullptr);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500390}
391
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500392BOOST_AUTO_TEST_CASE(PrefixUpdateValidatorOptional) // Bug #2814
393{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500394 const std::string SECTION_SECURITY = R"CONF(
395 security
396 {
397 validator
398 {
399 trust-anchor
400 {
401 type any
402 }
403 }
404 }
405 )CONF";
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500406
407 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
408}
409
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500410BOOST_AUTO_TEST_SUITE_END()
411
Nick Gordonfad8e252016-08-11 14:21:38 -0500412} // namespace test
413} // namespace nlsr