blob: cb017241d8723d2c29f0e582b2fb8b3f56079dd5 [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
36NFD_LOG_INIT("ConfigFileTest");
37
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080038BOOST_FIXTURE_TEST_SUITE(TestConfigFile, BaseFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070039
40// a
41// {
42// akey "avalue"
43// }
44// b
45// {
46// bkey "bvalue"
47// }
48
49const std::string CONFIG =
50"a\n"
51"{\n"
52" akey \"avalue\"\n"
53"}\n"
54"b\n"
55"{\n"
56" bkey \"bvalue\"\n"
57"}\n";
58
59
60// a
61// {
62// akey "avalue"
63// }
64// b
65//
66// bkey "bvalue"
67// }
68
69const std::string MALFORMED_CONFIG =
70"a\n"
71"{\n"
72" akey \"avalue\"\n"
73"}\n"
74"b\n"
75"\n"
76" bkey \"bvalue\"\n"
77"}\n";
78
79// counts of the respective section counts in config_example.info
80
81const int CONFIG_N_A_SECTIONS = 1;
82const int CONFIG_N_B_SECTIONS = 1;
83
84class DummySubscriber
85{
86public:
87
88 DummySubscriber(ConfigFile& config,
89 int nASections,
90 int nBSections,
91 bool expectDryRun)
92 : m_nASections(nASections),
93 m_nBSections(nBSections),
94 m_nRemainingACallbacks(nASections),
95 m_nRemainingBCallbacks(nBSections),
96 m_expectDryRun(expectDryRun)
97 {
98
99 }
100
101 void
102 onA(const ConfigSection& section, bool isDryRun)
103 {
104 // NFD_LOG_DEBUG("a");
105 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
106 --m_nRemainingACallbacks;
107 }
108
109
110 void
111 onB(const ConfigSection& section, bool isDryRun)
112 {
113 // NFD_LOG_DEBUG("b");
114 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
115 --m_nRemainingBCallbacks;
116 }
117
118 bool
119 allCallbacksFired() const
120 {
121 return m_nRemainingACallbacks == 0 &&
122 m_nRemainingBCallbacks == 0;
123 }
124
125 bool
126 noCallbacksFired() const
127 {
128 return m_nRemainingACallbacks == m_nASections &&
129 m_nRemainingBCallbacks == m_nBSections;
130 }
131
132 virtual
133 ~DummySubscriber()
134 {
135
136 }
137
138private:
139 int m_nASections;
140 int m_nBSections;
141 int m_nRemainingACallbacks;
142 int m_nRemainingBCallbacks;
143 bool m_expectDryRun;
144};
145
146class DummyAllSubscriber : public DummySubscriber
147{
148public:
149 DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false)
150 : DummySubscriber(config,
151 CONFIG_N_A_SECTIONS,
152 CONFIG_N_B_SECTIONS,
153 expectDryRun)
154 {
155 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
156 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
157 }
158
159 virtual
160 ~DummyAllSubscriber()
161 {
162
163 }
164};
165
166class DummyOneSubscriber : public DummySubscriber
167{
168public:
169 DummyOneSubscriber(ConfigFile& config,
170 const std::string& sectionName,
171 bool expectDryRun=false)
172 : DummySubscriber(config,
173 (sectionName == "a"),
174 (sectionName == "b"),
175 expectDryRun)
176 {
177 if (sectionName == "a")
178 {
179 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
180 }
181 else if (sectionName == "b")
182 {
183 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
184 }
185 else
186 {
187 BOOST_FAIL("Test setup error: "
188 << "Unexpected section name "
189 <<"\"" << sectionName << "\"");
190 }
191
192 }
193
194 virtual
195 ~DummyOneSubscriber()
196 {
197
198 }
199};
200
201class DummyNoSubscriber : public DummySubscriber
202{
203public:
204 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
205 : DummySubscriber(config, 0, 0, expectDryRun)
206 {
207
208 }
209
210 virtual
211 ~DummyNoSubscriber()
212 {
213
214 }
215};
216
217BOOST_AUTO_TEST_CASE(OnConfigStream)
218{
219 ConfigFile file;
220 DummyAllSubscriber sub(file);
221 std::ifstream input;
222
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700223 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700224 BOOST_REQUIRE(input.is_open());
225
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600226 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700227 BOOST_CHECK(sub.allCallbacksFired());
228}
229
230BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
231{
232 ConfigFile file;
233 DummyAllSubscriber sub(file);
234
235 std::ifstream input;
236
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600237 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700238 BOOST_CHECK(sub.noCallbacksFired());
239}
240
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800241BOOST_AUTO_TEST_CASE(OnConfigSection)
242{
243 ConfigFile file;
244 DummyAllSubscriber sub(file);
245
246 std::istringstream input(CONFIG);
247 ConfigSection section;
248 boost::property_tree::read_info(input, section);
249
250 file.parse(section, false, "dummy-config");
251 BOOST_CHECK(sub.allCallbacksFired());
252}
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700253
254BOOST_AUTO_TEST_CASE(OnConfigString)
255{
256 ConfigFile file;
257 DummyAllSubscriber sub(file);
258
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600259 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700260
261 BOOST_CHECK(sub.allCallbacksFired());
262}
263
264BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
265{
266 ConfigFile file;
267 DummyAllSubscriber sub(file);
268
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600269 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700270 BOOST_CHECK(sub.noCallbacksFired());
271}
272
273BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
274{
275 ConfigFile file;
276 DummyAllSubscriber sub(file);
277
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600278 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700279 BOOST_CHECK(sub.noCallbacksFired());
280}
281
282BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
283{
284 ConfigFile file;
285 DummyAllSubscriber sub(file, true);
286
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600287 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700288
289 BOOST_CHECK(sub.allCallbacksFired());
290}
291
292BOOST_AUTO_TEST_CASE(OnConfigFilename)
293{
294 ConfigFile file;
295 DummyAllSubscriber sub(file);
296
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700297 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700298
299 BOOST_CHECK(sub.allCallbacksFired());
300}
301
302BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
303{
304 ConfigFile file;
305 DummyAllSubscriber sub(file);
306
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600307 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700308
309 BOOST_CHECK(sub.noCallbacksFired());
310}
311
312BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
313{
314 ConfigFile file;
315 DummyAllSubscriber sub(file);
316
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700317 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700318
319 BOOST_CHECK(sub.noCallbacksFired());
320}
321
322BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
323{
324 ConfigFile file;
325 DummyAllSubscriber sub(file, true);
326 std::ifstream input;
327
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700328 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700329 BOOST_REQUIRE(input.is_open());
330
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700331 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700332
333 BOOST_CHECK(sub.allCallbacksFired());
334
335 input.close();
336}
337
338BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
339{
340 ConfigFile file;
341 DummyAllSubscriber sub(file, true);
342
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700343 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700344 BOOST_CHECK(sub.allCallbacksFired());
345}
346
347BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
348{
349 ConfigFile file;
350 DummyAllSubscriber sub1(file);
351 DummyAllSubscriber sub2(file);
352
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600353 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700354
355 BOOST_CHECK(sub1.noCallbacksFired());
356 BOOST_CHECK(sub2.allCallbacksFired());
357}
358
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600359class MissingCallbackFixture : public BaseFixture
360{
361public:
362 MissingCallbackFixture()
363 : m_missingFired(false)
364 {
365 }
366
367 void
368 checkMissingHandler(const std::string& filename,
369 const std::string& sectionName,
370 const ConfigSection& section,
371 bool isDryRun)
372 {
373 m_missingFired = true;
374 }
375
376protected:
377 bool m_missingFired;
378};
379
380
381
382BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700383{
384 ConfigFile file;
385
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600386 BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
387
388 ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler,
389 this, _1, _2, _3, _4));
390
391 DummyOneSubscriber subA(permissiveFile, "a");
392
393 BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config"));
394 BOOST_CHECK(subA.allCallbacksFired());
395 BOOST_CHECK(m_missingFired);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700396}
397
398BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
399{
400 ConfigFile file;
401 DummyOneSubscriber subA(file, "a");
402 DummyOneSubscriber subB(file, "b");
403
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600404 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700405
406 BOOST_CHECK(subA.allCallbacksFired());
407 BOOST_CHECK(subB.allCallbacksFired());
408}
409
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700410BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700411
412} // namespace tests
413} // namespace nfd