blob: 7d3dddeb94fbcc85b1336da795f2f5cce19e883d [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettobb75b552014-02-08 12:12:11 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#include "core/config-file.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070026
Junxiao Shid9ee45c2014-02-27 15:38:11 -070027#include "tests/test-common.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070028
Davide Pesavento52a18f92014-04-10 00:55:01 +020029#include <fstream>
Alexander Afanasyevc0273e32015-01-06 13:05:50 -080030#include <boost/property_tree/info_parser.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020031
Steve DiBenedettobb75b552014-02-08 12:12:11 -070032namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070034
35NFD_LOG_INIT("ConfigFileTest");
36
Junxiao Shid9ee45c2014-02-27 15:38:11 -070037BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, BaseFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070038
39// a
40// {
41// akey "avalue"
42// }
43// b
44// {
45// bkey "bvalue"
46// }
47
48const std::string CONFIG =
49"a\n"
50"{\n"
51" akey \"avalue\"\n"
52"}\n"
53"b\n"
54"{\n"
55" bkey \"bvalue\"\n"
56"}\n";
57
58
59// a
60// {
61// akey "avalue"
62// }
63// b
64//
65// bkey "bvalue"
66// }
67
68const std::string MALFORMED_CONFIG =
69"a\n"
70"{\n"
71" akey \"avalue\"\n"
72"}\n"
73"b\n"
74"\n"
75" bkey \"bvalue\"\n"
76"}\n";
77
78// counts of the respective section counts in config_example.info
79
80const int CONFIG_N_A_SECTIONS = 1;
81const int CONFIG_N_B_SECTIONS = 1;
82
83class DummySubscriber
84{
85public:
86
87 DummySubscriber(ConfigFile& config,
88 int nASections,
89 int nBSections,
90 bool expectDryRun)
91 : m_nASections(nASections),
92 m_nBSections(nBSections),
93 m_nRemainingACallbacks(nASections),
94 m_nRemainingBCallbacks(nBSections),
95 m_expectDryRun(expectDryRun)
96 {
97
98 }
99
100 void
101 onA(const ConfigSection& section, bool isDryRun)
102 {
103 // NFD_LOG_DEBUG("a");
104 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
105 --m_nRemainingACallbacks;
106 }
107
108
109 void
110 onB(const ConfigSection& section, bool isDryRun)
111 {
112 // NFD_LOG_DEBUG("b");
113 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
114 --m_nRemainingBCallbacks;
115 }
116
117 bool
118 allCallbacksFired() const
119 {
120 return m_nRemainingACallbacks == 0 &&
121 m_nRemainingBCallbacks == 0;
122 }
123
124 bool
125 noCallbacksFired() const
126 {
127 return m_nRemainingACallbacks == m_nASections &&
128 m_nRemainingBCallbacks == m_nBSections;
129 }
130
131 virtual
132 ~DummySubscriber()
133 {
134
135 }
136
137private:
138 int m_nASections;
139 int m_nBSections;
140 int m_nRemainingACallbacks;
141 int m_nRemainingBCallbacks;
142 bool m_expectDryRun;
143};
144
145class DummyAllSubscriber : public DummySubscriber
146{
147public:
148 DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false)
149 : DummySubscriber(config,
150 CONFIG_N_A_SECTIONS,
151 CONFIG_N_B_SECTIONS,
152 expectDryRun)
153 {
154 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
155 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
156 }
157
158 virtual
159 ~DummyAllSubscriber()
160 {
161
162 }
163};
164
165class DummyOneSubscriber : public DummySubscriber
166{
167public:
168 DummyOneSubscriber(ConfigFile& config,
169 const std::string& sectionName,
170 bool expectDryRun=false)
171 : DummySubscriber(config,
172 (sectionName == "a"),
173 (sectionName == "b"),
174 expectDryRun)
175 {
176 if (sectionName == "a")
177 {
178 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
179 }
180 else if (sectionName == "b")
181 {
182 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
183 }
184 else
185 {
186 BOOST_FAIL("Test setup error: "
187 << "Unexpected section name "
188 <<"\"" << sectionName << "\"");
189 }
190
191 }
192
193 virtual
194 ~DummyOneSubscriber()
195 {
196
197 }
198};
199
200class DummyNoSubscriber : public DummySubscriber
201{
202public:
203 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
204 : DummySubscriber(config, 0, 0, expectDryRun)
205 {
206
207 }
208
209 virtual
210 ~DummyNoSubscriber()
211 {
212
213 }
214};
215
216BOOST_AUTO_TEST_CASE(OnConfigStream)
217{
218 ConfigFile file;
219 DummyAllSubscriber sub(file);
220 std::ifstream input;
221
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700222 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700223 BOOST_REQUIRE(input.is_open());
224
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600225 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700226 BOOST_CHECK(sub.allCallbacksFired());
227}
228
229BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
230{
231 ConfigFile file;
232 DummyAllSubscriber sub(file);
233
234 std::ifstream input;
235
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600236 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700237 BOOST_CHECK(sub.noCallbacksFired());
238}
239
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800240BOOST_AUTO_TEST_CASE(OnConfigSection)
241{
242 ConfigFile file;
243 DummyAllSubscriber sub(file);
244
245 std::istringstream input(CONFIG);
246 ConfigSection section;
247 boost::property_tree::read_info(input, section);
248
249 file.parse(section, false, "dummy-config");
250 BOOST_CHECK(sub.allCallbacksFired());
251}
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700252
253BOOST_AUTO_TEST_CASE(OnConfigString)
254{
255 ConfigFile file;
256 DummyAllSubscriber sub(file);
257
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600258 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700259
260 BOOST_CHECK(sub.allCallbacksFired());
261}
262
263BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
264{
265 ConfigFile file;
266 DummyAllSubscriber sub(file);
267
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600268 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700269 BOOST_CHECK(sub.noCallbacksFired());
270}
271
272BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
273{
274 ConfigFile file;
275 DummyAllSubscriber sub(file);
276
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600277 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700278 BOOST_CHECK(sub.noCallbacksFired());
279}
280
281BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
282{
283 ConfigFile file;
284 DummyAllSubscriber sub(file, true);
285
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600286 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700287
288 BOOST_CHECK(sub.allCallbacksFired());
289}
290
291BOOST_AUTO_TEST_CASE(OnConfigFilename)
292{
293 ConfigFile file;
294 DummyAllSubscriber sub(file);
295
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700296 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700297
298 BOOST_CHECK(sub.allCallbacksFired());
299}
300
301BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
302{
303 ConfigFile file;
304 DummyAllSubscriber sub(file);
305
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600306 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700307
308 BOOST_CHECK(sub.noCallbacksFired());
309}
310
311BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
312{
313 ConfigFile file;
314 DummyAllSubscriber sub(file);
315
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700316 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700317
318 BOOST_CHECK(sub.noCallbacksFired());
319}
320
321BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
322{
323 ConfigFile file;
324 DummyAllSubscriber sub(file, true);
325 std::ifstream input;
326
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700327 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700328 BOOST_REQUIRE(input.is_open());
329
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700330 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700331
332 BOOST_CHECK(sub.allCallbacksFired());
333
334 input.close();
335}
336
337BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
338{
339 ConfigFile file;
340 DummyAllSubscriber sub(file, true);
341
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700342 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700343 BOOST_CHECK(sub.allCallbacksFired());
344}
345
346BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
347{
348 ConfigFile file;
349 DummyAllSubscriber sub1(file);
350 DummyAllSubscriber sub2(file);
351
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600352 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700353
354 BOOST_CHECK(sub1.noCallbacksFired());
355 BOOST_CHECK(sub2.allCallbacksFired());
356}
357
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600358class MissingCallbackFixture : public BaseFixture
359{
360public:
361 MissingCallbackFixture()
362 : m_missingFired(false)
363 {
364 }
365
366 void
367 checkMissingHandler(const std::string& filename,
368 const std::string& sectionName,
369 const ConfigSection& section,
370 bool isDryRun)
371 {
372 m_missingFired = true;
373 }
374
375protected:
376 bool m_missingFired;
377};
378
379
380
381BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700382{
383 ConfigFile file;
384
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600385 BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
386
387 ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler,
388 this, _1, _2, _3, _4));
389
390 DummyOneSubscriber subA(permissiveFile, "a");
391
392 BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config"));
393 BOOST_CHECK(subA.allCallbacksFired());
394 BOOST_CHECK(m_missingFired);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700395}
396
397BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
398{
399 ConfigFile file;
400 DummyOneSubscriber subA(file, "a");
401 DummyOneSubscriber subB(file, "b");
402
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600403 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700404
405 BOOST_CHECK(subA.allCallbacksFired());
406 BOOST_CHECK(subB.allCallbacksFired());
407}
408
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700409BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700410
411} // namespace tests
412} // namespace nfd