blob: 25d14334d6686cb4bb70cd13d43e584663646943 [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/**
akmhoque3d06e792014-05-27 16:23:20 -05003 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author Ashlesh Gawande <agawande@memphis.edu>
21 *
22 **/
Vince Lehman7c603292014-09-11 17:48:16 -050023
24#include "test-common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "dummy-face.hpp"
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050026#include "logger.hpp"
Vince Lehman7c603292014-09-11 17:48:16 -050027
akmhoque157b0a42014-05-13 00:26:37 -050028#include <fstream>
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050029#include "conf-file-processor.hpp"
30#include "nlsr.hpp"
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050031
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032#include <boost/test/unit_test.hpp>
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050033#include <boost/filesystem.hpp>
34#include <boost/algorithm/string.hpp>
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035
36namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050037namespace test {
38
Vince Lehman904c2412014-09-23 19:36:11 -050039using ndn::DummyFace;
40using ndn::shared_ptr;
41
Vince Lehman7b616582014-10-17 16:25:39 -050042const std::string SECTION_GENERAL =
43 "general\n"
44 "{\n"
45 " network /ndn/\n"
46 " site /memphis.edu/\n"
47 " router /cs/pollux/\n"
48 " lsa-refresh-time 1800\n"
49 " lsa-interest-lifetime 3\n"
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -070050 " router-dead-interval 86400\n"
Vince Lehman7b616582014-10-17 16:25:39 -050051 " log-level INFO\n"
52 " log-dir /tmp\n"
53 " seq-dir /tmp\n"
54 "}\n\n";
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050055
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050056const std::string LOG4CXX_PLACEHOLDER = "$LOG4CXX$";
57
58const std::string SECTION_GENERAL_WITH_LOG4CXX =
59 "general\n"
60 "{\n"
61 " network /ndn/\n"
62 " site /memphis.edu/\n"
63 " router /cs/pollux/\n"
64 " lsa-refresh-time 1800\n"
65 " lsa-interest-lifetime 3\n"
66 " router-dead-interval 86400\n"
67 " log-level INFO\n"
68 " log-dir /tmp\n"
69 " seq-dir /tmp\n"
70 " log4cxx-conf " + LOG4CXX_PLACEHOLDER + "\n"
71 "}\n\n";
72
Vince Lehman7b616582014-10-17 16:25:39 -050073const std::string SECTION_NEIGHBORS =
74 "neighbors\n"
75 "{\n"
76 " hello-retries 3\n"
77 " hello-timeout 1\n"
78 " hello-interval 60\n\n"
79 " adj-lsa-build-interval 3\n"
80 " first-hello-interval 6\n"
81 " neighbor\n"
82 " {\n"
83 " name /ndn/memphis.edu/cs/castor\n"
84 " face-uri udp4://localhost\n"
85 " link-cost 20\n"
86 " }\n\n"
87 " neighbor\n"
88 " {\n"
89 " name /ndn/memphis.edu/cs/mira\n"
90 " face-uri udp4://localhost\n"
91 " link-cost 30\n"
92 " }\n"
93 "}\n\n";
94
95const std::string SECTION_HYPERBOLIC_ON =
96 "hyperbolic\n"
97 "{\n"
98 " state on\n"
99 " radius 123.456\n"
100 " angle 1.45\n"
101 "}\n\n";
102
103const std::string SECTION_HYPERBOLIC_OFF =
104 "hyperbolic\n"
105 "{\n"
106 " state off\n"
107 " radius 123.456\n"
108 " angle 1.45\n"
109 "}\n\n";
110
111const std::string SECTION_FIB =
112 "fib\n"
113 "{\n"
114 " max-faces-per-prefix 3\n"
115 " routing-calc-interval 9\n"
116 "}\n\n";
117
118const std::string SECTION_ADVERTISING =
119 "advertising\n"
120 "{\n"
121 " prefix /ndn/edu/memphis/cs/netlab\n"
122 " prefix /ndn/edu/memphis/sports/basketball\n"
123 "}\n";
124
125const std::string CONFIG_LINK_STATE = SECTION_GENERAL + SECTION_NEIGHBORS +
126 SECTION_HYPERBOLIC_OFF + SECTION_FIB + SECTION_ADVERTISING;
127
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500128const std::string CONFIG_LOG4CXX = SECTION_GENERAL_WITH_LOG4CXX;
129
Vince Lehman7b616582014-10-17 16:25:39 -0500130const std::string CONFIG_HYPERBOLIC = SECTION_GENERAL + SECTION_NEIGHBORS +
131 SECTION_HYPERBOLIC_ON + SECTION_FIB + SECTION_ADVERTISING;
132
133class ConfFileProcessorFixture : public BaseFixture
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500134{
Vince Lehman7b616582014-10-17 16:25:39 -0500135public:
136 ConfFileProcessorFixture()
137 : face(ndn::makeDummyFace())
138 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
139 , CONFIG_FILE("unit-test-nlsr.conf")
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500140 , m_logConfigFileName(boost::filesystem::unique_path().native())
141 , m_logFileName(boost::filesystem::unique_path().native())
Vince Lehman7b616582014-10-17 16:25:39 -0500142 {
143 }
Vince Lehman904c2412014-09-23 19:36:11 -0500144
Vince Lehman7b616582014-10-17 16:25:39 -0500145 ~ConfFileProcessorFixture()
146 {
147 remove("unit-test-nlsr.conf");
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500148 remove("/tmp/unit-test-log4cxx.xml");
149
150 boost::filesystem::remove(boost::filesystem::path(getLogConfigFileName()));
151 boost::filesystem::remove(boost::filesystem::path(getLogFileName()));
Vince Lehman7b616582014-10-17 16:25:39 -0500152 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500153
Vince Lehman7b616582014-10-17 16:25:39 -0500154 bool processConfigurationString(std::string confString)
155 {
156 std::ofstream config;
157 config.open("unit-test-nlsr.conf");
158 config << confString;
159 config.close();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500160
Vince Lehman7b616582014-10-17 16:25:39 -0500161 ConfFileProcessor processor(nlsr, CONFIG_FILE);
162 return processor.processConfFile();
163 }
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500164
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500165 void
166 verifyOutputLog4cxx(const std::string expected[], size_t nExpected)
167 {
168 std::ifstream is(getLogFileName().c_str());
169 std::string buffer((std::istreambuf_iterator<char>(is)),
170 (std::istreambuf_iterator<char>()));
171
172 std::vector<std::string> components;
173 boost::split(components, buffer, boost::is_any_of(" ,\n"));
174
175 // expected + number of timestamps (one per log statement) + trailing newline of last statement
176 BOOST_REQUIRE_EQUAL(components.size(), nExpected);
177
178 for (size_t i = 0; i < nExpected; ++i) {
179 if (expected[i] == "")
180 continue;
181
182 BOOST_CHECK_EQUAL(components[i], expected[i]);
183 }
184 }
185
186 const std::string&
187 getLogConfigFileName()
188 {
189 return m_logConfigFileName;
190 }
191
192 const std::string&
193 getLogFileName()
194 {
195 return m_logFileName;
196 }
197
Vince Lehman7b616582014-10-17 16:25:39 -0500198public:
199 shared_ptr<ndn::DummyFace> face;
200 Nlsr nlsr;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500201
Vince Lehman7b616582014-10-17 16:25:39 -0500202private:
203 const std::string CONFIG_FILE;
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500204 std::string m_logConfigFileName;
205 std::string m_logFileName;
Vince Lehman7b616582014-10-17 16:25:39 -0500206};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500207
Vince Lehman7b616582014-10-17 16:25:39 -0500208BOOST_FIXTURE_TEST_SUITE(TestConfFileProcessor, ConfFileProcessorFixture)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500209
Vince Lehman7b616582014-10-17 16:25:39 -0500210BOOST_AUTO_TEST_CASE(LinkState)
211{
212 processConfigurationString(CONFIG_LINK_STATE);
Vince Lehman7b616582014-10-17 16:25:39 -0500213 ConfParameter& conf = nlsr.getConfParameter();
214 conf.buildRouterPrefix();
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500215
Vince Lehman7b616582014-10-17 16:25:39 -0500216 // General
217 BOOST_CHECK_EQUAL(conf.getNetwork(), "/ndn/");
218 BOOST_CHECK_EQUAL(conf.getSiteName(), "/memphis.edu/");
219 BOOST_CHECK_EQUAL(conf.getRouterName(), "/cs/pollux/");
220 BOOST_CHECK_EQUAL(conf.getRouterPrefix(), "/ndn/memphis.edu/cs/pollux/");
221 BOOST_CHECK_EQUAL(conf.getChronosyncPrefix(), "/ndn/NLSR/sync");
222 BOOST_CHECK_EQUAL(conf.getLsaPrefix(), "/ndn/NLSR/LSA");
223 BOOST_CHECK_EQUAL(conf.getLsaRefreshTime(), 1800);
224 BOOST_CHECK_EQUAL(conf.getLsaInterestLifetime(), ndn::time::seconds(3));
Alexander Afanasyev1cf1e102014-08-17 19:47:57 -0700225 BOOST_CHECK_EQUAL(conf.getRouterDeadInterval(), 86400);
Vince Lehman7b616582014-10-17 16:25:39 -0500226 BOOST_CHECK_EQUAL(conf.getLogLevel(), "INFO");
227 BOOST_CHECK_EQUAL(conf.getLogDir(), "/tmp");
228 BOOST_CHECK_EQUAL(conf.getSeqFileDir(), "/tmp");
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700229
Vince Lehman7b616582014-10-17 16:25:39 -0500230 // Neighbors
231 BOOST_CHECK_EQUAL(conf.getInterestRetryNumber(), 3);
232 BOOST_CHECK_EQUAL(conf.getInterestResendTime(), 1);
233 BOOST_CHECK_EQUAL(conf.getInfoInterestInterval(), 60);
234
235 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(), 3);
236 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
237
238 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/mira"));
239 BOOST_CHECK(nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/castor"));
240 BOOST_CHECK(!nlsr.getAdjacencyList().isNeighbor("/ndn/memphis.edu/cs/fail"));
241
242 Adjacent mira = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/mira");
243 BOOST_CHECK_EQUAL(mira.getName(), "/ndn/memphis.edu/cs/mira");
244 BOOST_CHECK_EQUAL(mira.getLinkCost(), 30);
245
246 Adjacent castor = nlsr.getAdjacencyList().getAdjacent("/ndn/memphis.edu/cs/castor");
247 BOOST_CHECK_EQUAL(castor.getName(), "/ndn/memphis.edu/cs/castor");
248 BOOST_CHECK_EQUAL(castor.getLinkCost(), 20);
249
250 // Hyperbolic
251 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 0);
252
253 // FIB
254 BOOST_CHECK_EQUAL(conf.getMaxFacesPerPrefix(), 3);
255 BOOST_CHECK_EQUAL(conf.getRoutingCalcInterval(), 9);
256
257 // Advertising
258 BOOST_CHECK_EQUAL(nlsr.getNamePrefixList().getSize(), 2);
259}
260
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500261BOOST_AUTO_TEST_CASE(Log4cxxFileExists)
262{
263 std::string configPath = boost::filesystem::unique_path().native();
264
265 std::ofstream log4cxxConfFile;
266 log4cxxConfFile.open(configPath);
267 log4cxxConfFile.close();
268
269 std::string config = CONFIG_LOG4CXX;
270 boost::replace_all(config, LOG4CXX_PLACEHOLDER, configPath);
271
272 BOOST_CHECK_EQUAL(processConfigurationString(config), true);
273
274 ConfParameter& conf = nlsr.getConfParameter();
275 BOOST_CHECK_EQUAL(conf.getLog4CxxConfPath(), configPath);
276 BOOST_CHECK_EQUAL(conf.isLog4CxxConfAvailable(), true);
277
278 boost::filesystem::remove(boost::filesystem::path(configPath));
279}
280
281BOOST_AUTO_TEST_CASE(Log4cxxFileDoesNotExist)
282{
283 std::string configPath = boost::filesystem::unique_path().native();
284
285 std::string config = CONFIG_LOG4CXX;
286 boost::replace_all(config, LOG4CXX_PLACEHOLDER, configPath);
287
288 BOOST_CHECK_EQUAL(processConfigurationString(config), false);
289}
290
291BOOST_AUTO_TEST_CASE(Log4cxxNoValue)
292{
293 std::string config = CONFIG_LOG4CXX;
294 boost::replace_all(config, LOG4CXX_PLACEHOLDER, "");
295
296 BOOST_CHECK_EQUAL(processConfigurationString(config), false);
297}
298
299BOOST_AUTO_TEST_CASE(Log4cxxTestCase)
300{
301 {
302 std::ofstream of(getLogConfigFileName().c_str());
303 of << "log4j.rootLogger=TRACE, FILE\n"
304 << "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
305 << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n"
306 << "log4j.appender.FILE.File=" << getLogFileName() << "\n"
307 << "log4j.appender.FILE.ImmediateFlush=true\n"
308 << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n";
309 }
310
311 INIT_LOG4CXX(getLogConfigFileName());
312
313 INIT_LOGGER("DefaultConfig");
314
315 _LOG_TRACE("trace-message-JHGFDSR^1");
316 _LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17);
317 _LOG_INFO("info-message-Jjxjshj13");
318 _LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x");
319 _LOG_ERROR("error-message-!#$&^%$#@");
320 _LOG_FATAL("fatal-message-JJSjaamcng");
321
322 const std::string EXPECTED[] =
323 {
324 "", "TRACE", "DefaultConfig", "-", "trace-message-JHGFDSR^1",
325 "", "DEBUG", "DefaultConfig", "-", "debug-message-IGg2474fdksd-fo-151617",
326 "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13",
327 "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x",
328 "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@",
329 "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng",
330 "",
331 };
332
333 verifyOutputLog4cxx(EXPECTED, sizeof(EXPECTED) / sizeof(std::string));
334}
335
Vince Lehman7b616582014-10-17 16:25:39 -0500336BOOST_AUTO_TEST_CASE(Hyperbolic)
337{
338 processConfigurationString(CONFIG_HYPERBOLIC);
339
340 ConfParameter& conf = nlsr.getConfParameter();
341 BOOST_CHECK_EQUAL(conf.getHyperbolicState(), 1);
342 BOOST_CHECK_EQUAL(conf.getCorR(), 123.456);
343 BOOST_CHECK_EQUAL(conf.getCorTheta(), 1.45);
344}
345
346BOOST_AUTO_TEST_CASE(DefaultValues)
347{
348 // Missing adj-lsa-build-interval
349 const std::string SECTION_NEIGHBORS_DEFAULT_VALUES =
350 "neighbors\n"
351 "{\n"
352 " hello-retries 3\n"
353 " hello-timeout 1\n"
354 " hello-interval 60\n\n"
355 " first-hello-interval 6\n"
356 " neighbor\n"
357 " {\n"
358 " name /ndn/memphis.edu/cs/castor\n"
359 " face-uri udp4://localhost\n"
360 " link-cost 20\n"
361 " }\n\n"
362 " neighbor\n"
363 " {\n"
364 " name /ndn/memphis.edu/cs/mira\n"
365 " face-uri udp4://localhost\n"
366 " link-cost 30\n"
367 " }\n"
368 "}\n\n";
369
370 processConfigurationString(SECTION_NEIGHBORS_DEFAULT_VALUES);
371
372 ConfParameter& conf = nlsr.getConfParameter();
373
374 BOOST_CHECK_EQUAL(conf.getAdjLsaBuildInterval(),
375 static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT));
376
377 BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
378}
379
380BOOST_AUTO_TEST_CASE(OutOfRangeValue)
381{
382 const std::string SECTION_FIB_OUT_OF_RANGE =
383 "fib\n"
384 "{\n"
385 " max-faces-per-prefix 3\n"
386 " routing-calc-interval 999\n" // Larger than max value
387 "}\n\n";
388
389 // Processing should fail due to out of range value
390 BOOST_CHECK_EQUAL(processConfigurationString(SECTION_FIB_OUT_OF_RANGE), false);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500391}
392
393BOOST_AUTO_TEST_SUITE_END()
394
395} //namespace test
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -0500396} //namespace nlsr