blob: c8bbb2f61cfb07827dfb8db5c0be72e1a7808c38 [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/**
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -06003 * Copyright (c) 2014-2019, 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"
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050047 " sync-protocol psync\n"
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060048 " sync-interest-lifetime 10000\n"
Vince Lehman7b616582014-10-17 16:25:39 -050049 " seq-dir /tmp\n"
50 "}\n\n";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050051
Vince Lehman7b616582014-10-17 16:25:39 -050052const std::string SECTION_NEIGHBORS =
53 "neighbors\n"
54 "{\n"
55 " hello-retries 3\n"
56 " hello-timeout 1\n"
57 " hello-interval 60\n\n"
58 " adj-lsa-build-interval 3\n"
59 " first-hello-interval 6\n"
60 " neighbor\n"
61 " {\n"
62 " name /ndn/memphis.edu/cs/castor\n"
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -060063 " face-uri udp://10.0.0.1\n"
Vince Lehman7b616582014-10-17 16:25:39 -050064 " link-cost 20\n"
65 " }\n\n"
66 " neighbor\n"
67 " {\n"
68 " name /ndn/memphis.edu/cs/mira\n"
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -060069 " face-uri udp://10.0.0.2\n"
Vince Lehman7b616582014-10-17 16:25:39 -050070 " link-cost 30\n"
71 " }\n"
72 "}\n\n";
73
74const std::string SECTION_HYPERBOLIC_ON =
75 "hyperbolic\n"
76 "{\n"
77 " state on\n"
78 " radius 123.456\n"
79 " angle 1.45\n"
80 "}\n\n";
81
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060082const std::string SECTION_HYPERBOLIC_ANGLES_ON =
83 "hyperbolic\n"
84 "{\n"
85 " state on\n"
86 " radius 123.456\n"
87 " angle 1.45,2.25\n"
88 "}\n\n";
89
Vince Lehman7b616582014-10-17 16:25:39 -050090const std::string SECTION_HYPERBOLIC_OFF =
91 "hyperbolic\n"
92 "{\n"
93 " state off\n"
94 " radius 123.456\n"
95 " angle 1.45\n"
96 "}\n\n";
97
98const std::string SECTION_FIB =
99 "fib\n"
100 "{\n"
101 " max-faces-per-prefix 3\n"
102 " routing-calc-interval 9\n"
103 "}\n\n";
104
105const std::string SECTION_ADVERTISING =
106 "advertising\n"
107 "{\n"
108 " prefix /ndn/edu/memphis/cs/netlab\n"
109 " prefix /ndn/edu/memphis/sports/basketball\n"
110 "}\n";
111
112const std::string CONFIG_LINK_STATE = SECTION_GENERAL + SECTION_NEIGHBORS +
113 SECTION_HYPERBOLIC_OFF + SECTION_FIB + SECTION_ADVERTISING;
114
115const std::string CONFIG_HYPERBOLIC = SECTION_GENERAL + SECTION_NEIGHBORS +
116 SECTION_HYPERBOLIC_ON + SECTION_FIB + SECTION_ADVERTISING;
117
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600118const std::string CONFIG_HYPERBOLIC_ANGLES = SECTION_GENERAL + SECTION_NEIGHBORS +
119 SECTION_HYPERBOLIC_ANGLES_ON + SECTION_FIB +
120 SECTION_ADVERTISING;
121
Vince Lehman7b616582014-10-17 16:25:39 -0500122class ConfFileProcessorFixture : public BaseFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500123{
Vince Lehman7b616582014-10-17 16:25:39 -0500124public:
125 ConfFileProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500126 : face(m_ioService, m_keyChain)
127 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehman7b616582014-10-17 16:25:39 -0500128 , CONFIG_FILE("unit-test-nlsr.conf")
129 {
130 }
Vince Lehman904c2412014-09-23 19:36:11 -0500131
Vince Lehman7b616582014-10-17 16:25:39 -0500132 ~ConfFileProcessorFixture()
133 {
134 remove("unit-test-nlsr.conf");
135 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500136
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500137 bool
138 processConfigurationString(std::string confString)
Vince Lehman7b616582014-10-17 16:25:39 -0500139 {
140 std::ofstream config;
141 config.open("unit-test-nlsr.conf");
142 config << confString;
143 config.close();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500144
Vince Lehman7b616582014-10-17 16:25:39 -0500145 ConfFileProcessor processor(nlsr, CONFIG_FILE);
146 return processor.processConfFile();
147 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500148
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500149 void
alvya2228c62014-12-09 10:25:11 -0600150 commentOut(const std::string& key, std::string& config)
151 {
152 boost::replace_all(config, key, ";" + key);
153 }
154
Vince Lehman7b616582014-10-17 16:25:39 -0500155public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500156 ndn::util::DummyClientFace face;
Vince Lehman7b616582014-10-17 16:25:39 -0500157 Nlsr nlsr;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500158
Vince Lehman7b616582014-10-17 16:25:39 -0500159private:
160 const std::string CONFIG_FILE;
161};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500162
Vince Lehman7b616582014-10-17 16:25:39 -0500163BOOST_FIXTURE_TEST_SUITE(TestConfFileProcessor, ConfFileProcessorFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500164
Vince Lehman7b616582014-10-17 16:25:39 -0500165BOOST_AUTO_TEST_CASE(LinkState)
166{
167 processConfigurationString(CONFIG_LINK_STATE);
Vince Lehman7b616582014-10-17 16:25:39 -0500168 ConfParameter& conf = nlsr.getConfParameter();
169 conf.buildRouterPrefix();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500170
Vince Lehman7b616582014-10-17 16:25:39 -0500171 // General
172 BOOST_CHECK_EQUAL(conf.getNetwork(), "/ndn/");
173 BOOST_CHECK_EQUAL(conf.getSiteName(), "/memphis.edu/");
174 BOOST_CHECK_EQUAL(conf.getRouterName(), "/cs/pollux/");
175 BOOST_CHECK_EQUAL(conf.getRouterPrefix(), "/ndn/memphis.edu/cs/pollux/");
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500176 BOOST_CHECK_EQUAL(conf.getChronosyncPrefix(), ndn::Name("/localhop/ndn/nlsr/sync").appendVersion(ConfParameter::SYNC_VERSION));
177 BOOST_CHECK_EQUAL(conf.getLsaPrefix(), "/localhop/ndn/nlsr/LSA");
Vince Lehman7b616582014-10-17 16:25:39 -0500178 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), 1800);
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500179 BOOST_CHECK_EQUAL(conf.getSyncProtocol(), SYNC_PROTOCOL_PSYNC);
Vince Lehman7b616582014-10-17 16:25:39 -0500180 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(), ndn::time::seconds(3));
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700181 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), 86400);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600182 BOOST_CHECK_EQUAL(conf.getSyncInterestLifetime(), ndn::time::milliseconds(10000));
Vince Lehman7b616582014-10-17 16:25:39 -0500183 BOOST_CHECK_EQUAL(conf.getSeqFileDir(), "/tmp");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700184
Vince Lehman7b616582014-10-17 16:25:39 -0500185 // Neighbors
186 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), 3);
187 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), 1);
188 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), 60);
189
190 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(), 3);
191 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
192
193 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/mira"));
194 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/castor"));
195 BOOST_CHECK(!nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/fail"));
196
197 Adjacent mira = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/mira");
198 BOOST_CHECK_EQUAL(mira.getName(), "/ndn/memphis.edu/cs/mira");
199 BOOST_CHECK_EQUAL(mira.getLinkCost(), 30);
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600200 BOOST_CHECK_EQUAL(mira.getFaceUri().toString(), "udp4://10.0.0.2:6363");
Vince Lehman7b616582014-10-17 16:25:39 -0500201
202 Adjacent castor = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/castor");
203 BOOST_CHECK_EQUAL(castor.getName(), "/ndn/memphis.edu/cs/castor");
204 BOOST_CHECK_EQUAL(castor.getLinkCost(), 20);
Ashlesh Gawande7e3f6d72019-01-25 13:13:43 -0600205 BOOST_CHECK_EQUAL(castor.getFaceUri().toString(), "udp4://10.0.0.1:6363");
Vince Lehman7b616582014-10-17 16:25:39 -0500206
207 // Hyperbolic
208 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 0);
209
210 // FIB
211 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(), 3);
212 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(), 9);
213
214 // Advertising
Nick Gordonff9a6272017-10-12 13:38:29 -0500215 BOOST_CHECK_EQUAL(nlsr.getNamePrefixList().size(), 2);
Vince Lehman7b616582014-10-17 16:25:39 -0500216}
217
alvy2fe12872014-11-25 10:32:23 -0600218BOOST_AUTO_TEST_CASE(MalformedUri)
219{
220 const std::string MALFORMED_URI =
221 "neighbors\n"
222 "{\n"
223 " hello-retries 3\n"
224 " hello-timeout 1\n"
225 " hello-interval 60\n\n"
226 " adj-lsa-build-interval 3\n"
227 " first-hello-interval 6\n"
228 " neighbor\n"
229 " {\n"
230 " name /ndn/memphis.edu/cs/castor\n"
231 " face-uri udp4:malformed-uri\n"
232 " link-cost 20\n"
233 " }\n"
234 "}\n\n";
235
236 BOOST_CHECK_EQUAL(processConfigurationString(MALFORMED_URI), false);
237}
238
Vince Lehman7b616582014-10-17 16:25:39 -0500239BOOST_AUTO_TEST_CASE(Hyperbolic)
240{
241 processConfigurationString(CONFIG_HYPERBOLIC);
242
243 ConfParameter& conf = nlsr.getConfParameter();
244 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
245 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600246 std::vector<double> angles;
247 angles.push_back(1.45);
248 BOOST_CHECK(conf.getCorTheta() == angles);
249}
250
251BOOST_AUTO_TEST_CASE(Hyperbolic2)
252{
253 processConfigurationString(CONFIG_HYPERBOLIC_ANGLES);
254
255 ConfParameter& conf = nlsr.getConfParameter();
256 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
257 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
258 std::vector<double> angles;
259 angles.push_back(1.45);
260 angles.push_back(2.25);
261 BOOST_CHECK(conf.getCorTheta() == angles);
Vince Lehman7b616582014-10-17 16:25:39 -0500262}
263
alvya2228c62014-12-09 10:25:11 -0600264BOOST_AUTO_TEST_CASE(DefaultValuesGeneral)
Vince Lehman7b616582014-10-17 16:25:39 -0500265{
alvya2228c62014-12-09 10:25:11 -0600266 std::string config = SECTION_GENERAL;
Vince Lehman7b616582014-10-17 16:25:39 -0500267
alvya2228c62014-12-09 10:25:11 -0600268 commentOut("lsa-refresh-time", config);
269 commentOut("lsa-interest-lifetime", config);
270 commentOut("router-dead-interval", config);
271
272 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
Vince Lehman7b616582014-10-17 16:25:39 -0500273
274 ConfParameter& conf = nlsr.getConfParameter();
275
alvy5a454952014-12-15 12:49:54 -0600276 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), static_cast<uint32_t>(LSA_REFRESH_TIME_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600277 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(),
278 static_cast<ndn::time::seconds>(LSA_INTEREST_LIFETIME_DEFAULT));
279 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), (2*conf.getLsaRefreshTime()));
280}
281
282BOOST_AUTO_TEST_CASE(DefaultValuesNeighbors)
283{
284 std::string config = SECTION_NEIGHBORS;
285
286 commentOut("hello-retries", config);
287 commentOut("hello-timeout", config);
288 commentOut("hello-interval", config);
289 commentOut("first-hello-interval", config);
290 commentOut("adj-lsa-build-interval", config);
291
292 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
293
294 ConfParameter& conf = nlsr.getConfParameter();
295
296 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), static_cast<uint32_t>(HELLO_RETRIES_DEFAULT));
alvy5a454952014-12-15 12:49:54 -0600297 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), static_cast<uint32_t>(HELLO_TIMEOUT_DEFAULT));
298 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), static_cast<uint32_t>(HELLO_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600299 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(),
300 static_cast<uint32_t>(FIRST_HELLO_INTERVAL_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500301 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(),
302 static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600303}
Vince Lehman7b616582014-10-17 16:25:39 -0500304
alvya2228c62014-12-09 10:25:11 -0600305BOOST_AUTO_TEST_CASE(DefaultValuesFib)
306{
307 std::string config = SECTION_FIB;
308
309 commentOut("max-faces-per-prefix", config);
310 commentOut("routing-calc-interval", config);
311
312 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
313
314 ConfParameter& conf = nlsr.getConfParameter();
315
316 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(),
317 static_cast<uint32_t>(MAX_FACES_PER_PREFIX_DEFAULT));
318 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(),
319 static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT));
320}
321
322BOOST_AUTO_TEST_CASE(DefaultValuesHyperbolic)
323{
324 std::string config = SECTION_HYPERBOLIC_ON;
325
326 commentOut("state", config);
327
328 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
329
330 ConfParameter& conf = nlsr.getConfParameter();
331
332 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), static_cast<int32_t>(HYPERBOLIC_STATE_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500333}
334
335BOOST_AUTO_TEST_CASE(OutOfRangeValue)
336{
337 const std::string SECTION_FIB_OUT_OF_RANGE =
338 "fib\n"
339 "{\n"
340 " max-faces-per-prefix 3\n"
341 " routing-calc-interval 999\n" // Larger than max value
342 "}\n\n";
343
344 // Processing should fail due to out of range value
345 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_FIB_OUT_OF_RANGE), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500346}
347
alvy5a454952014-12-15 12:49:54 -0600348BOOST_AUTO_TEST_CASE(NegativeValue)
349{
350 const std::string SECTION_GENERAL_NEGATIVE_VALUE =
351 "general\n"
352 "{\n"
353 " network /ndn/\n"
354 " site /memphis.edu/\n"
355 " router /cs/pollux/\n"
356 " lsa-refresh-time -1800\n"
357 " lsa-interest-lifetime -3\n"
358 " router-dead-interval -86400\n"
359 "}\n\n";
360
361 // Processing should fail due to negative value
362 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_GENERAL_NEGATIVE_VALUE), false);
363}
364
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500365BOOST_AUTO_TEST_CASE(LoadCertToPublish)
366{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500367 auto identity = addIdentity("/TestNLSR/identity");
368 saveCertificate(identity, "cert-to-publish.cert");
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500369
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500370 const std::string SECTION_SECURITY = R"CONF(
371 security
372 {
373 validator
374 {
375 trust-anchor
376 {
377 type any
378 }
379 }
380 prefix-update-validator
381 {
382 trust-anchor
383 {
384 type any
385 }
386 }
387 cert-to-publish "cert-to-publish.cert"
388 }
389 )CONF";
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500390
391 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
392
393 // Certificate should now be in the CertificateStore
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500394 security::CertificateStore& certStore = nlsr.getCertificateStore();
395 BOOST_CHECK(certStore.find(identity.getDefaultKey().getName()) != nullptr);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500396}
397
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500398BOOST_AUTO_TEST_CASE(PrefixUpdateValidatorOptional) // Bug #2814
399{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500400 const std::string SECTION_SECURITY = R"CONF(
401 security
402 {
403 validator
404 {
405 trust-anchor
406 {
407 type any
408 }
409 }
410 }
411 )CONF";
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500412
413 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
414}
415
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500416BOOST_AUTO_TEST_SUITE_END()
417
Nick Gordonfad8e252016-08-11 14:21:38 -0500418} // namespace test
419} // namespace nlsr