blob: 0a570219b9090c075119445ce24f3a1f4a98c840 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa997d292017-08-24 20:16:59 -04002/*
3 * Copyright (c) 2014-2017, 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
Alexander Afanasyevc0273e32015-01-06 13:05:50 -080030#include <boost/property_tree/info_parser.hpp>
Davide Pesaventoa997d292017-08-24 20:16:59 -040031#include <fstream>
32#include <sstream>
Davide Pesavento52a18f92014-04-10 00:55:01 +020033
Steve DiBenedettobb75b552014-02-08 12:12:11 -070034namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070036
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080037BOOST_FIXTURE_TEST_SUITE(TestConfigFile, 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
Steve DiBenedettobb75b552014-02-08 12:12:11 -070079const int CONFIG_N_A_SECTIONS = 1;
80const int CONFIG_N_B_SECTIONS = 1;
81
82class DummySubscriber
83{
84public:
Davide Pesavento97210d52016-10-14 15:45:48 +020085 DummySubscriber(ConfigFile& config, int nASections, int nBSections, bool expectDryRun)
86 : m_nASections(nASections)
87 , m_nBSections(nBSections)
88 , m_nRemainingACallbacks(nASections)
89 , m_nRemainingBCallbacks(nBSections)
90 , m_expectDryRun(expectDryRun)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070091 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070092 }
93
Davide Pesavento97210d52016-10-14 15:45:48 +020094 virtual
95 ~DummySubscriber() = default;
96
Steve DiBenedettobb75b552014-02-08 12:12:11 -070097 void
98 onA(const ConfigSection& section, bool isDryRun)
99 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700100 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
101 --m_nRemainingACallbacks;
102 }
103
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700104 void
105 onB(const ConfigSection& section, bool isDryRun)
106 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700107 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
108 --m_nRemainingBCallbacks;
109 }
110
111 bool
112 allCallbacksFired() const
113 {
114 return m_nRemainingACallbacks == 0 &&
115 m_nRemainingBCallbacks == 0;
116 }
117
118 bool
119 noCallbacksFired() const
120 {
121 return m_nRemainingACallbacks == m_nASections &&
122 m_nRemainingBCallbacks == m_nBSections;
123 }
124
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700125private:
126 int m_nASections;
127 int m_nBSections;
128 int m_nRemainingACallbacks;
129 int m_nRemainingBCallbacks;
130 bool m_expectDryRun;
131};
132
133class DummyAllSubscriber : public DummySubscriber
134{
135public:
Davide Pesavento97210d52016-10-14 15:45:48 +0200136 DummyAllSubscriber(ConfigFile& config, bool expectDryRun = false)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700137 : DummySubscriber(config,
138 CONFIG_N_A_SECTIONS,
139 CONFIG_N_B_SECTIONS,
140 expectDryRun)
141 {
142 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
143 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
144 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700145};
146
147class DummyOneSubscriber : public DummySubscriber
148{
149public:
150 DummyOneSubscriber(ConfigFile& config,
151 const std::string& sectionName,
Davide Pesavento97210d52016-10-14 15:45:48 +0200152 bool expectDryRun = false)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700153 : DummySubscriber(config,
154 (sectionName == "a"),
155 (sectionName == "b"),
156 expectDryRun)
157 {
158 if (sectionName == "a")
159 {
160 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
161 }
162 else if (sectionName == "b")
163 {
164 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
165 }
166 else
167 {
168 BOOST_FAIL("Test setup error: "
169 << "Unexpected section name "
170 <<"\"" << sectionName << "\"");
171 }
172
173 }
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700174};
175
176class DummyNoSubscriber : public DummySubscriber
177{
178public:
179 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
180 : DummySubscriber(config, 0, 0, expectDryRun)
181 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700182 }
183};
184
185BOOST_AUTO_TEST_CASE(OnConfigStream)
186{
187 ConfigFile file;
188 DummyAllSubscriber sub(file);
189 std::ifstream input;
190
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700191 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700192 BOOST_REQUIRE(input.is_open());
193
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600194 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700195 BOOST_CHECK(sub.allCallbacksFired());
196}
197
198BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
199{
200 ConfigFile file;
201 DummyAllSubscriber sub(file);
202
203 std::ifstream input;
204
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600205 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700206 BOOST_CHECK(sub.noCallbacksFired());
207}
208
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800209BOOST_AUTO_TEST_CASE(OnConfigSection)
210{
211 ConfigFile file;
212 DummyAllSubscriber sub(file);
213
214 std::istringstream input(CONFIG);
215 ConfigSection section;
216 boost::property_tree::read_info(input, section);
217
218 file.parse(section, false, "dummy-config");
219 BOOST_CHECK(sub.allCallbacksFired());
220}
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700221
222BOOST_AUTO_TEST_CASE(OnConfigString)
223{
224 ConfigFile file;
225 DummyAllSubscriber sub(file);
226
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600227 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700228
229 BOOST_CHECK(sub.allCallbacksFired());
230}
231
232BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
233{
234 ConfigFile file;
235 DummyAllSubscriber sub(file);
236
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600237 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700238 BOOST_CHECK(sub.noCallbacksFired());
239}
240
241BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
242{
243 ConfigFile file;
244 DummyAllSubscriber sub(file);
245
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600246 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700247 BOOST_CHECK(sub.noCallbacksFired());
248}
249
250BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
251{
252 ConfigFile file;
253 DummyAllSubscriber sub(file, true);
254
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600255 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700256
257 BOOST_CHECK(sub.allCallbacksFired());
258}
259
260BOOST_AUTO_TEST_CASE(OnConfigFilename)
261{
262 ConfigFile file;
263 DummyAllSubscriber sub(file);
264
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700265 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700266
267 BOOST_CHECK(sub.allCallbacksFired());
268}
269
270BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
271{
272 ConfigFile file;
273 DummyAllSubscriber sub(file);
274
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600275 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700276
277 BOOST_CHECK(sub.noCallbacksFired());
278}
279
280BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
281{
282 ConfigFile file;
283 DummyAllSubscriber sub(file);
284
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700285 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700286
287 BOOST_CHECK(sub.noCallbacksFired());
288}
289
290BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
291{
292 ConfigFile file;
293 DummyAllSubscriber sub(file, true);
294 std::ifstream input;
295
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700296 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700297 BOOST_REQUIRE(input.is_open());
298
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700299 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700300
301 BOOST_CHECK(sub.allCallbacksFired());
302
303 input.close();
304}
305
306BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
307{
308 ConfigFile file;
309 DummyAllSubscriber sub(file, true);
310
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700311 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700312 BOOST_CHECK(sub.allCallbacksFired());
313}
314
315BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
316{
317 ConfigFile file;
318 DummyAllSubscriber sub1(file);
319 DummyAllSubscriber sub2(file);
320
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600321 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700322
323 BOOST_CHECK(sub1.noCallbacksFired());
324 BOOST_CHECK(sub2.allCallbacksFired());
325}
326
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600327class MissingCallbackFixture : public BaseFixture
328{
329public:
330 MissingCallbackFixture()
331 : m_missingFired(false)
332 {
333 }
334
335 void
336 checkMissingHandler(const std::string& filename,
337 const std::string& sectionName,
338 const ConfigSection& section,
339 bool isDryRun)
340 {
341 m_missingFired = true;
342 }
343
344protected:
345 bool m_missingFired;
346};
347
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600348BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700349{
350 ConfigFile file;
351
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600352 BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
353
354 ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler,
355 this, _1, _2, _3, _4));
356
357 DummyOneSubscriber subA(permissiveFile, "a");
358
359 BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config"));
360 BOOST_CHECK(subA.allCallbacksFired());
361 BOOST_CHECK(m_missingFired);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700362}
363
364BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
365{
366 ConfigFile file;
367 DummyOneSubscriber subA(file, "a");
368 DummyOneSubscriber subB(file, "b");
369
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600370 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700371
372 BOOST_CHECK(subA.allCallbacksFired());
373 BOOST_CHECK(subB.allCallbacksFired());
374}
375
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700376BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700377
378} // namespace tests
379} // namespace nfd