blob: 8a41ac2e6772ecd7f334880569e4b5e8e21052d8 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento97210d52016-10-14 15:45:48 +02003 * Copyright (c) 2014-2016, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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
Steve DiBenedettobb75b552014-02-08 12:12:11 -070078const int CONFIG_N_A_SECTIONS = 1;
79const int CONFIG_N_B_SECTIONS = 1;
80
81class DummySubscriber
82{
83public:
Davide Pesavento97210d52016-10-14 15:45:48 +020084 DummySubscriber(ConfigFile& config, int nASections, int nBSections, bool expectDryRun)
85 : m_nASections(nASections)
86 , m_nBSections(nBSections)
87 , m_nRemainingACallbacks(nASections)
88 , m_nRemainingBCallbacks(nBSections)
89 , m_expectDryRun(expectDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070090 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070091 }
92
Davide Pesavento97210d52016-10-14 15:45:48 +020093 virtual
94 ~DummySubscriber() = default;
95
Steve DiBenedettobb75b552014-02-08 12:12:11 -070096 void
97 onA(const ConfigSection& section, bool isDryRun)
98 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070099 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
100 --m_nRemainingACallbacks;
101 }
102
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700103 void
104 onB(const ConfigSection& section, bool isDryRun)
105 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700106 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
107 --m_nRemainingBCallbacks;
108 }
109
110 bool
111 allCallbacksFired() const
112 {
113 return m_nRemainingACallbacks == 0 &&
114 m_nRemainingBCallbacks == 0;
115 }
116
117 bool
118 noCallbacksFired() const
119 {
120 return m_nRemainingACallbacks == m_nASections &&
121 m_nRemainingBCallbacks == m_nBSections;
122 }
123
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700124private:
125 int m_nASections;
126 int m_nBSections;
127 int m_nRemainingACallbacks;
128 int m_nRemainingBCallbacks;
129 bool m_expectDryRun;
130};
131
132class DummyAllSubscriber : public DummySubscriber
133{
134public:
Davide Pesavento97210d52016-10-14 15:45:48 +0200135 DummyAllSubscriber(ConfigFile& config, bool expectDryRun = false)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700136 : DummySubscriber(config,
137 CONFIG_N_A_SECTIONS,
138 CONFIG_N_B_SECTIONS,
139 expectDryRun)
140 {
141 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
142 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
143 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700144};
145
146class DummyOneSubscriber : public DummySubscriber
147{
148public:
149 DummyOneSubscriber(ConfigFile& config,
150 const std::string& sectionName,
Davide Pesavento97210d52016-10-14 15:45:48 +0200151 bool expectDryRun = false)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700152 : DummySubscriber(config,
153 (sectionName == "a"),
154 (sectionName == "b"),
155 expectDryRun)
156 {
157 if (sectionName == "a")
158 {
159 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
160 }
161 else if (sectionName == "b")
162 {
163 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
164 }
165 else
166 {
167 BOOST_FAIL("Test setup error: "
168 << "Unexpected section name "
169 <<"\"" << sectionName << "\"");
170 }
171
172 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700173};
174
175class DummyNoSubscriber : public DummySubscriber
176{
177public:
178 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
179 : DummySubscriber(config, 0, 0, expectDryRun)
180 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700181 }
182};
183
184BOOST_AUTO_TEST_CASE(OnConfigStream)
185{
186 ConfigFile file;
187 DummyAllSubscriber sub(file);
188 std::ifstream input;
189
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700190 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700191 BOOST_REQUIRE(input.is_open());
192
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600193 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700194 BOOST_CHECK(sub.allCallbacksFired());
195}
196
197BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
198{
199 ConfigFile file;
200 DummyAllSubscriber sub(file);
201
202 std::ifstream input;
203
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600204 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700205 BOOST_CHECK(sub.noCallbacksFired());
206}
207
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800208BOOST_AUTO_TEST_CASE(OnConfigSection)
209{
210 ConfigFile file;
211 DummyAllSubscriber sub(file);
212
213 std::istringstream input(CONFIG);
214 ConfigSection section;
215 boost::property_tree::read_info(input, section);
216
217 file.parse(section, false, "dummy-config");
218 BOOST_CHECK(sub.allCallbacksFired());
219}
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700220
221BOOST_AUTO_TEST_CASE(OnConfigString)
222{
223 ConfigFile file;
224 DummyAllSubscriber sub(file);
225
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600226 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700227
228 BOOST_CHECK(sub.allCallbacksFired());
229}
230
231BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
232{
233 ConfigFile file;
234 DummyAllSubscriber sub(file);
235
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600236 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700237 BOOST_CHECK(sub.noCallbacksFired());
238}
239
240BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
241{
242 ConfigFile file;
243 DummyAllSubscriber sub(file);
244
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600245 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700246 BOOST_CHECK(sub.noCallbacksFired());
247}
248
249BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
250{
251 ConfigFile file;
252 DummyAllSubscriber sub(file, true);
253
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600254 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700255
256 BOOST_CHECK(sub.allCallbacksFired());
257}
258
259BOOST_AUTO_TEST_CASE(OnConfigFilename)
260{
261 ConfigFile file;
262 DummyAllSubscriber sub(file);
263
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700264 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700265
266 BOOST_CHECK(sub.allCallbacksFired());
267}
268
269BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
270{
271 ConfigFile file;
272 DummyAllSubscriber sub(file);
273
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600274 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700275
276 BOOST_CHECK(sub.noCallbacksFired());
277}
278
279BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
280{
281 ConfigFile file;
282 DummyAllSubscriber sub(file);
283
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700284 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700285
286 BOOST_CHECK(sub.noCallbacksFired());
287}
288
289BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
290{
291 ConfigFile file;
292 DummyAllSubscriber sub(file, true);
293 std::ifstream input;
294
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700295 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700296 BOOST_REQUIRE(input.is_open());
297
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700298 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700299
300 BOOST_CHECK(sub.allCallbacksFired());
301
302 input.close();
303}
304
305BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
306{
307 ConfigFile file;
308 DummyAllSubscriber sub(file, true);
309
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700310 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700311 BOOST_CHECK(sub.allCallbacksFired());
312}
313
314BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
315{
316 ConfigFile file;
317 DummyAllSubscriber sub1(file);
318 DummyAllSubscriber sub2(file);
319
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600320 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700321
322 BOOST_CHECK(sub1.noCallbacksFired());
323 BOOST_CHECK(sub2.allCallbacksFired());
324}
325
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600326class MissingCallbackFixture : public BaseFixture
327{
328public:
329 MissingCallbackFixture()
330 : m_missingFired(false)
331 {
332 }
333
334 void
335 checkMissingHandler(const std::string& filename,
336 const std::string& sectionName,
337 const ConfigSection& section,
338 bool isDryRun)
339 {
340 m_missingFired = true;
341 }
342
343protected:
344 bool m_missingFired;
345};
346
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600347BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700348{
349 ConfigFile file;
350
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600351 BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
352
353 ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler,
354 this, _1, _2, _3, _4));
355
356 DummyOneSubscriber subA(permissiveFile, "a");
357
358 BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config"));
359 BOOST_CHECK(subA.allCallbacksFired());
360 BOOST_CHECK(m_missingFired);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700361}
362
363BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
364{
365 ConfigFile file;
366 DummyOneSubscriber subA(file, "a");
367 DummyOneSubscriber subB(file, "b");
368
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600369 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700370
371 BOOST_CHECK(subA.allCallbacksFired());
372 BOOST_CHECK(subB.allCallbacksFired());
373}
374
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700375BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700376
377} // namespace tests
378} // namespace nfd