blob: 15ae3774c16ee97dd90bee987e6b8e209c35fd8a [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080024 */
Steve DiBenedettobb75b552014-02-08 12:12:11 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#include "core/config-file.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070029
Davide Pesavento52a18f92014-04-10 00:55:01 +020030#include <fstream>
Alexander Afanasyevc0273e32015-01-06 13:05:50 -080031#include <boost/property_tree/info_parser.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020032
Steve DiBenedettobb75b552014-02-08 12:12:11 -070033namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070035
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036BOOST_FIXTURE_TEST_SUITE(TestConfigFile, BaseFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070037
38// a
39// {
40// akey "avalue"
41// }
42// b
43// {
44// bkey "bvalue"
45// }
46
47const std::string CONFIG =
48"a\n"
49"{\n"
50" akey \"avalue\"\n"
51"}\n"
52"b\n"
53"{\n"
54" bkey \"bvalue\"\n"
55"}\n";
56
57
58// a
59// {
60// akey "avalue"
61// }
62// b
63//
64// bkey "bvalue"
65// }
66
67const std::string MALFORMED_CONFIG =
68"a\n"
69"{\n"
70" akey \"avalue\"\n"
71"}\n"
72"b\n"
73"\n"
74" bkey \"bvalue\"\n"
75"}\n";
76
77// counts of the respective section counts in config_example.info
78
79const int CONFIG_N_A_SECTIONS = 1;
80const int CONFIG_N_B_SECTIONS = 1;
81
82class DummySubscriber
83{
84public:
85
86 DummySubscriber(ConfigFile& config,
87 int nASections,
88 int nBSections,
89 bool expectDryRun)
90 : m_nASections(nASections),
91 m_nBSections(nBSections),
92 m_nRemainingACallbacks(nASections),
93 m_nRemainingBCallbacks(nBSections),
94 m_expectDryRun(expectDryRun)
95 {
96
97 }
98
99 void
100 onA(const ConfigSection& section, bool isDryRun)
101 {
102 // NFD_LOG_DEBUG("a");
103 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
104 --m_nRemainingACallbacks;
105 }
106
107
108 void
109 onB(const ConfigSection& section, bool isDryRun)
110 {
111 // NFD_LOG_DEBUG("b");
112 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
113 --m_nRemainingBCallbacks;
114 }
115
116 bool
117 allCallbacksFired() const
118 {
119 return m_nRemainingACallbacks == 0 &&
120 m_nRemainingBCallbacks == 0;
121 }
122
123 bool
124 noCallbacksFired() const
125 {
126 return m_nRemainingACallbacks == m_nASections &&
127 m_nRemainingBCallbacks == m_nBSections;
128 }
129
130 virtual
131 ~DummySubscriber()
132 {
133
134 }
135
136private:
137 int m_nASections;
138 int m_nBSections;
139 int m_nRemainingACallbacks;
140 int m_nRemainingBCallbacks;
141 bool m_expectDryRun;
142};
143
144class DummyAllSubscriber : public DummySubscriber
145{
146public:
147 DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false)
148 : DummySubscriber(config,
149 CONFIG_N_A_SECTIONS,
150 CONFIG_N_B_SECTIONS,
151 expectDryRun)
152 {
153 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
154 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
155 }
156
157 virtual
158 ~DummyAllSubscriber()
159 {
160
161 }
162};
163
164class DummyOneSubscriber : public DummySubscriber
165{
166public:
167 DummyOneSubscriber(ConfigFile& config,
168 const std::string& sectionName,
169 bool expectDryRun=false)
170 : DummySubscriber(config,
171 (sectionName == "a"),
172 (sectionName == "b"),
173 expectDryRun)
174 {
175 if (sectionName == "a")
176 {
177 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
178 }
179 else if (sectionName == "b")
180 {
181 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
182 }
183 else
184 {
185 BOOST_FAIL("Test setup error: "
186 << "Unexpected section name "
187 <<"\"" << sectionName << "\"");
188 }
189
190 }
191
192 virtual
193 ~DummyOneSubscriber()
194 {
195
196 }
197};
198
199class DummyNoSubscriber : public DummySubscriber
200{
201public:
202 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
203 : DummySubscriber(config, 0, 0, expectDryRun)
204 {
205
206 }
207
208 virtual
209 ~DummyNoSubscriber()
210 {
211
212 }
213};
214
215BOOST_AUTO_TEST_CASE(OnConfigStream)
216{
217 ConfigFile file;
218 DummyAllSubscriber sub(file);
219 std::ifstream input;
220
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700221 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700222 BOOST_REQUIRE(input.is_open());
223
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600224 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700225 BOOST_CHECK(sub.allCallbacksFired());
226}
227
228BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
229{
230 ConfigFile file;
231 DummyAllSubscriber sub(file);
232
233 std::ifstream input;
234
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600235 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700236 BOOST_CHECK(sub.noCallbacksFired());
237}
238
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800239BOOST_AUTO_TEST_CASE(OnConfigSection)
240{
241 ConfigFile file;
242 DummyAllSubscriber sub(file);
243
244 std::istringstream input(CONFIG);
245 ConfigSection section;
246 boost::property_tree::read_info(input, section);
247
248 file.parse(section, false, "dummy-config");
249 BOOST_CHECK(sub.allCallbacksFired());
250}
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700251
252BOOST_AUTO_TEST_CASE(OnConfigString)
253{
254 ConfigFile file;
255 DummyAllSubscriber sub(file);
256
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600257 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700258
259 BOOST_CHECK(sub.allCallbacksFired());
260}
261
262BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
263{
264 ConfigFile file;
265 DummyAllSubscriber sub(file);
266
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600267 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700268 BOOST_CHECK(sub.noCallbacksFired());
269}
270
271BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
272{
273 ConfigFile file;
274 DummyAllSubscriber sub(file);
275
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600276 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700277 BOOST_CHECK(sub.noCallbacksFired());
278}
279
280BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
281{
282 ConfigFile file;
283 DummyAllSubscriber sub(file, true);
284
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600285 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700286
287 BOOST_CHECK(sub.allCallbacksFired());
288}
289
290BOOST_AUTO_TEST_CASE(OnConfigFilename)
291{
292 ConfigFile file;
293 DummyAllSubscriber sub(file);
294
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700295 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700296
297 BOOST_CHECK(sub.allCallbacksFired());
298}
299
300BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
301{
302 ConfigFile file;
303 DummyAllSubscriber sub(file);
304
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600305 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700306
307 BOOST_CHECK(sub.noCallbacksFired());
308}
309
310BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
311{
312 ConfigFile file;
313 DummyAllSubscriber sub(file);
314
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700315 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700316
317 BOOST_CHECK(sub.noCallbacksFired());
318}
319
320BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
321{
322 ConfigFile file;
323 DummyAllSubscriber sub(file, true);
324 std::ifstream input;
325
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700326 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700327 BOOST_REQUIRE(input.is_open());
328
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700329 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700330
331 BOOST_CHECK(sub.allCallbacksFired());
332
333 input.close();
334}
335
336BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
337{
338 ConfigFile file;
339 DummyAllSubscriber sub(file, true);
340
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700341 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700342 BOOST_CHECK(sub.allCallbacksFired());
343}
344
345BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
346{
347 ConfigFile file;
348 DummyAllSubscriber sub1(file);
349 DummyAllSubscriber sub2(file);
350
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600351 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700352
353 BOOST_CHECK(sub1.noCallbacksFired());
354 BOOST_CHECK(sub2.allCallbacksFired());
355}
356
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600357class MissingCallbackFixture : public BaseFixture
358{
359public:
360 MissingCallbackFixture()
361 : m_missingFired(false)
362 {
363 }
364
365 void
366 checkMissingHandler(const std::string& filename,
367 const std::string& sectionName,
368 const ConfigSection& section,
369 bool isDryRun)
370 {
371 m_missingFired = true;
372 }
373
374protected:
375 bool m_missingFired;
376};
377
378
379
380BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700381{
382 ConfigFile file;
383
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600384 BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
385
386 ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler,
387 this, _1, _2, _3, _4));
388
389 DummyOneSubscriber subA(permissiveFile, "a");
390
391 BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config"));
392 BOOST_CHECK(subA.allCallbacksFired());
393 BOOST_CHECK(m_missingFired);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700394}
395
396BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
397{
398 ConfigFile file;
399 DummyOneSubscriber subA(file, "a");
400 DummyOneSubscriber subB(file, "b");
401
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600402 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700403
404 BOOST_CHECK(subA.allCallbacksFired());
405 BOOST_CHECK(subB.allCallbacksFired());
406}
407
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700408BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700409
410} // namespace tests
411} // namespace nfd