blob: 14fce9aa395f1171689f2674f39b622e0b6948c3 [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"
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -060047 " sync-interest-lifetime 10000\n"
Vince Lehman7b616582014-10-17 16:25:39 -050048 " seq-dir /tmp\n"
49 "}\n\n";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050050
Vince Lehman7b616582014-10-17 16:25:39 -050051const std::string SECTION_NEIGHBORS =
52 "neighbors\n"
53 "{\n"
54 " hello-retries 3\n"
55 " hello-timeout 1\n"
56 " hello-interval 60\n\n"
57 " adj-lsa-build-interval 3\n"
58 " first-hello-interval 6\n"
59 " neighbor\n"
60 " {\n"
61 " name /ndn/memphis.edu/cs/castor\n"
62 " face-uri udp4://localhost\n"
63 " link-cost 20\n"
64 " }\n\n"
65 " neighbor\n"
66 " {\n"
67 " name /ndn/memphis.edu/cs/mira\n"
68 " face-uri udp4://localhost\n"
69 " link-cost 30\n"
70 " }\n"
71 "}\n\n";
72
73const std::string SECTION_HYPERBOLIC_ON =
74 "hyperbolic\n"
75 "{\n"
76 " state on\n"
77 " radius 123.456\n"
78 " angle 1.45\n"
79 "}\n\n";
80
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060081const std::string SECTION_HYPERBOLIC_ANGLES_ON =
82 "hyperbolic\n"
83 "{\n"
84 " state on\n"
85 " radius 123.456\n"
86 " angle 1.45,2.25\n"
87 "}\n\n";
88
Vince Lehman7b616582014-10-17 16:25:39 -050089const std::string SECTION_HYPERBOLIC_OFF =
90 "hyperbolic\n"
91 "{\n"
92 " state off\n"
93 " radius 123.456\n"
94 " angle 1.45\n"
95 "}\n\n";
96
97const std::string SECTION_FIB =
98 "fib\n"
99 "{\n"
100 " max-faces-per-prefix 3\n"
101 " routing-calc-interval 9\n"
102 "}\n\n";
103
104const std::string SECTION_ADVERTISING =
105 "advertising\n"
106 "{\n"
107 " prefix /ndn/edu/memphis/cs/netlab\n"
108 " prefix /ndn/edu/memphis/sports/basketball\n"
109 "}\n";
110
111const std::string CONFIG_LINK_STATE = SECTION_GENERAL + SECTION_NEIGHBORS +
112 SECTION_HYPERBOLIC_OFF + SECTION_FIB + SECTION_ADVERTISING;
113
114const std::string CONFIG_HYPERBOLIC = SECTION_GENERAL + SECTION_NEIGHBORS +
115 SECTION_HYPERBOLIC_ON + SECTION_FIB + SECTION_ADVERTISING;
116
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600117const std::string CONFIG_HYPERBOLIC_ANGLES = SECTION_GENERAL + SECTION_NEIGHBORS +
118 SECTION_HYPERBOLIC_ANGLES_ON + SECTION_FIB +
119 SECTION_ADVERTISING;
120
Vince Lehman7b616582014-10-17 16:25:39 -0500121class ConfFileProcessorFixture : public BaseFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500122{
Vince Lehman7b616582014-10-17 16:25:39 -0500123public:
124 ConfFileProcessorFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500125 : face(m_ioService, m_keyChain)
126 , nlsr(m_ioService, m_scheduler, face, m_keyChain)
Vince Lehman7b616582014-10-17 16:25:39 -0500127 , CONFIG_FILE("unit-test-nlsr.conf")
128 {
129 }
Vince Lehman904c2412014-09-23 19:36:11 -0500130
Vince Lehman7b616582014-10-17 16:25:39 -0500131 ~ConfFileProcessorFixture()
132 {
133 remove("unit-test-nlsr.conf");
134 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500135
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500136 bool
137 processConfigurationString(std::string confString)
Vince Lehman7b616582014-10-17 16:25:39 -0500138 {
139 std::ofstream config;
140 config.open("unit-test-nlsr.conf");
141 config << confString;
142 config.close();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500143
Vince Lehman7b616582014-10-17 16:25:39 -0500144 ConfFileProcessor processor(nlsr, CONFIG_FILE);
145 return processor.processConfFile();
146 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500147
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500148 void
alvya2228c62014-12-09 10:25:11 -0600149 commentOut(const std::string& key, std::string& config)
150 {
151 boost::replace_all(config, key, ";" + key);
152 }
153
Vince Lehman7b616582014-10-17 16:25:39 -0500154public:
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500155 ndn::util::DummyClientFace face;
Vince Lehman7b616582014-10-17 16:25:39 -0500156 Nlsr nlsr;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500157
Vince Lehman7b616582014-10-17 16:25:39 -0500158private:
159 const std::string CONFIG_FILE;
160};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500161
Vince Lehman7b616582014-10-17 16:25:39 -0500162BOOST_FIXTURE_TEST_SUITE(TestConfFileProcessor, ConfFileProcessorFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500163
Vince Lehman7b616582014-10-17 16:25:39 -0500164BOOST_AUTO_TEST_CASE(LinkState)
165{
166 processConfigurationString(CONFIG_LINK_STATE);
Vince Lehman7b616582014-10-17 16:25:39 -0500167 ConfParameter& conf = nlsr.getConfParameter();
168 conf.buildRouterPrefix();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500169
Vince Lehman7b616582014-10-17 16:25:39 -0500170 // General
171 BOOST_CHECK_EQUAL(conf.getNetwork(), "/ndn/");
172 BOOST_CHECK_EQUAL(conf.getSiteName(), "/memphis.edu/");
173 BOOST_CHECK_EQUAL(conf.getRouterName(), "/cs/pollux/");
174 BOOST_CHECK_EQUAL(conf.getRouterPrefix(), "/ndn/memphis.edu/cs/pollux/");
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500175 BOOST_CHECK_EQUAL(conf.getChronosyncPrefix(), ndn::Name("/localhop/ndn/nlsr/sync").appendVersion(ConfParameter::SYNC_VERSION));
176 BOOST_CHECK_EQUAL(conf.getLsaPrefix(), "/localhop/ndn/nlsr/LSA");
Vince Lehman7b616582014-10-17 16:25:39 -0500177 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), 1800);
178 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(), ndn::time::seconds(3));
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700179 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), 86400);
Ashlesh Gawandef7da9c52018-02-06 17:36:46 -0600180 BOOST_CHECK_EQUAL(conf.getSyncInterestLifetime(), ndn::time::milliseconds(10000));
Vince Lehman7b616582014-10-17 16:25:39 -0500181 BOOST_CHECK_EQUAL(conf.getSeqFileDir(), "/tmp");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700182
Vince Lehman7b616582014-10-17 16:25:39 -0500183 // Neighbors
184 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), 3);
185 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), 1);
186 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), 60);
187
188 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(), 3);
189 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
190
191 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/mira"));
192 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/castor"));
193 BOOST_CHECK(!nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/fail"));
194
195 Adjacent mira = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/mira");
196 BOOST_CHECK_EQUAL(mira.getName(), "/ndn/memphis.edu/cs/mira");
197 BOOST_CHECK_EQUAL(mira.getLinkCost(), 30);
198
199 Adjacent castor = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/castor");
200 BOOST_CHECK_EQUAL(castor.getName(), "/ndn/memphis.edu/cs/castor");
201 BOOST_CHECK_EQUAL(castor.getLinkCost(), 20);
202
203 // Hyperbolic
204 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 0);
205
206 // FIB
207 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(), 3);
208 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(), 9);
209
210 // Advertising
Nick Gordonff9a6272017-10-12 13:38:29 -0500211 BOOST_CHECK_EQUAL(nlsr.getNamePrefixList().size(), 2);
Vince Lehman7b616582014-10-17 16:25:39 -0500212}
213
alvy2fe12872014-11-25 10:32:23 -0600214BOOST_AUTO_TEST_CASE(MalformedUri)
215{
216 const std::string MALFORMED_URI =
217 "neighbors\n"
218 "{\n"
219 " hello-retries 3\n"
220 " hello-timeout 1\n"
221 " hello-interval 60\n\n"
222 " adj-lsa-build-interval 3\n"
223 " first-hello-interval 6\n"
224 " neighbor\n"
225 " {\n"
226 " name /ndn/memphis.edu/cs/castor\n"
227 " face-uri udp4:malformed-uri\n"
228 " link-cost 20\n"
229 " }\n"
230 "}\n\n";
231
232 BOOST_CHECK_EQUAL(processConfigurationString(MALFORMED_URI), false);
233}
234
Vince Lehman7b616582014-10-17 16:25:39 -0500235BOOST_AUTO_TEST_CASE(Hyperbolic)
236{
237 processConfigurationString(CONFIG_HYPERBOLIC);
238
239 ConfParameter& conf = nlsr.getConfParameter();
240 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
241 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600242 std::vector<double> angles;
243 angles.push_back(1.45);
244 BOOST_CHECK(conf.getCorTheta() == angles);
245}
246
247BOOST_AUTO_TEST_CASE(Hyperbolic2)
248{
249 processConfigurationString(CONFIG_HYPERBOLIC_ANGLES);
250
251 ConfParameter& conf = nlsr.getConfParameter();
252 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
253 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
254 std::vector<double> angles;
255 angles.push_back(1.45);
256 angles.push_back(2.25);
257 BOOST_CHECK(conf.getCorTheta() == angles);
Vince Lehman7b616582014-10-17 16:25:39 -0500258}
259
alvya2228c62014-12-09 10:25:11 -0600260BOOST_AUTO_TEST_CASE(DefaultValuesGeneral)
Vince Lehman7b616582014-10-17 16:25:39 -0500261{
alvya2228c62014-12-09 10:25:11 -0600262 std::string config = SECTION_GENERAL;
Vince Lehman7b616582014-10-17 16:25:39 -0500263
alvya2228c62014-12-09 10:25:11 -0600264 commentOut("lsa-refresh-time", config);
265 commentOut("lsa-interest-lifetime", config);
266 commentOut("router-dead-interval", config);
267
268 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
Vince Lehman7b616582014-10-17 16:25:39 -0500269
270 ConfParameter& conf = nlsr.getConfParameter();
271
alvy5a454952014-12-15 12:49:54 -0600272 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), static_cast<uint32_t>(LSA_REFRESH_TIME_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600273 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(),
274 static_cast<ndn::time::seconds>(LSA_INTEREST_LIFETIME_DEFAULT));
275 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), (2*conf.getLsaRefreshTime()));
276}
277
278BOOST_AUTO_TEST_CASE(DefaultValuesNeighbors)
279{
280 std::string config = SECTION_NEIGHBORS;
281
282 commentOut("hello-retries", config);
283 commentOut("hello-timeout", config);
284 commentOut("hello-interval", config);
285 commentOut("first-hello-interval", config);
286 commentOut("adj-lsa-build-interval", config);
287
288 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
289
290 ConfParameter& conf = nlsr.getConfParameter();
291
292 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), static_cast<uint32_t>(HELLO_RETRIES_DEFAULT));
alvy5a454952014-12-15 12:49:54 -0600293 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), static_cast<uint32_t>(HELLO_TIMEOUT_DEFAULT));
294 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), static_cast<uint32_t>(HELLO_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600295 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(),
296 static_cast<uint32_t>(FIRST_HELLO_INTERVAL_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500297 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(),
298 static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT));
alvya2228c62014-12-09 10:25:11 -0600299}
Vince Lehman7b616582014-10-17 16:25:39 -0500300
alvya2228c62014-12-09 10:25:11 -0600301BOOST_AUTO_TEST_CASE(DefaultValuesFib)
302{
303 std::string config = SECTION_FIB;
304
305 commentOut("max-faces-per-prefix", config);
306 commentOut("routing-calc-interval", config);
307
308 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
309
310 ConfParameter& conf = nlsr.getConfParameter();
311
312 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(),
313 static_cast<uint32_t>(MAX_FACES_PER_PREFIX_DEFAULT));
314 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(),
315 static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT));
316}
317
318BOOST_AUTO_TEST_CASE(DefaultValuesHyperbolic)
319{
320 std::string config = SECTION_HYPERBOLIC_ON;
321
322 commentOut("state", config);
323
324 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
325
326 ConfParameter& conf = nlsr.getConfParameter();
327
328 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), static_cast<int32_t>(HYPERBOLIC_STATE_DEFAULT));
Vince Lehman7b616582014-10-17 16:25:39 -0500329}
330
331BOOST_AUTO_TEST_CASE(OutOfRangeValue)
332{
333 const std::string SECTION_FIB_OUT_OF_RANGE =
334 "fib\n"
335 "{\n"
336 " max-faces-per-prefix 3\n"
337 " routing-calc-interval 999\n" // Larger than max value
338 "}\n\n";
339
340 // Processing should fail due to out of range value
341 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_FIB_OUT_OF_RANGE), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500342}
343
alvy5a454952014-12-15 12:49:54 -0600344BOOST_AUTO_TEST_CASE(NegativeValue)
345{
346 const std::string SECTION_GENERAL_NEGATIVE_VALUE =
347 "general\n"
348 "{\n"
349 " network /ndn/\n"
350 " site /memphis.edu/\n"
351 " router /cs/pollux/\n"
352 " lsa-refresh-time -1800\n"
353 " lsa-interest-lifetime -3\n"
354 " router-dead-interval -86400\n"
355 "}\n\n";
356
357 // Processing should fail due to negative value
358 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_GENERAL_NEGATIVE_VALUE), false);
359}
360
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500361BOOST_AUTO_TEST_CASE(LoadCertToPublish)
362{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500363 auto identity = addIdentity("/TestNLSR/identity");
364 saveCertificate(identity, "cert-to-publish.cert");
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500365
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500366 const std::string SECTION_SECURITY = R"CONF(
367 security
368 {
369 validator
370 {
371 trust-anchor
372 {
373 type any
374 }
375 }
376 prefix-update-validator
377 {
378 trust-anchor
379 {
380 type any
381 }
382 }
383 cert-to-publish "cert-to-publish.cert"
384 }
385 )CONF";
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500386
387 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
388
389 // Certificate should now be in the CertificateStore
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500390 security::CertificateStore& certStore = nlsr.getCertificateStore();
391 BOOST_CHECK(certStore.find(identity.getDefaultKey().getName()) != nullptr);
Vince Lehmanc2acdcb2015-04-29 11:14:35 -0500392}
393
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500394BOOST_AUTO_TEST_CASE(PrefixUpdateValidatorOptional) // Bug #2814
395{
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500396 const std::string SECTION_SECURITY = R"CONF(
397 security
398 {
399 validator
400 {
401 trust-anchor
402 {
403 type any
404 }
405 }
406 }
407 )CONF";
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500408
409 BOOST_CHECK(processConfigurationString(SECTION_SECURITY));
410}
411
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500412BOOST_AUTO_TEST_SUITE_END()
413
Nick Gordonfad8e252016-08-11 14:21:38 -0500414} // namespace test
415} // namespace nlsr