blob: bb75b77921ca802ea8360d7e86174b9b2c7bcca9 [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>
30
Steve DiBenedettobb75b552014-02-08 12:12:11 -070031namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070033
34NFD_LOG_INIT("ConfigFileTest");
35
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, 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
239
240BOOST_AUTO_TEST_CASE(OnConfigString)
241{
242 ConfigFile file;
243 DummyAllSubscriber sub(file);
244
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600245 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700246
247 BOOST_CHECK(sub.allCallbacksFired());
248}
249
250BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
251{
252 ConfigFile file;
253 DummyAllSubscriber sub(file);
254
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600255 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700256 BOOST_CHECK(sub.noCallbacksFired());
257}
258
259BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
260{
261 ConfigFile file;
262 DummyAllSubscriber sub(file);
263
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600264 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700265 BOOST_CHECK(sub.noCallbacksFired());
266}
267
268BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
269{
270 ConfigFile file;
271 DummyAllSubscriber sub(file, true);
272
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600273 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700274
275 BOOST_CHECK(sub.allCallbacksFired());
276}
277
278BOOST_AUTO_TEST_CASE(OnConfigFilename)
279{
280 ConfigFile file;
281 DummyAllSubscriber sub(file);
282
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700283 file.parse("tests/core/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700284
285 BOOST_CHECK(sub.allCallbacksFired());
286}
287
288BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
289{
290 ConfigFile file;
291 DummyAllSubscriber sub(file);
292
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600293 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700294
295 BOOST_CHECK(sub.noCallbacksFired());
296}
297
298BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
299{
300 ConfigFile file;
301 DummyAllSubscriber sub(file);
302
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700303 BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700304
305 BOOST_CHECK(sub.noCallbacksFired());
306}
307
308BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
309{
310 ConfigFile file;
311 DummyAllSubscriber sub(file, true);
312 std::ifstream input;
313
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700314 input.open("tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700315 BOOST_REQUIRE(input.is_open());
316
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700317 file.parse(input, true, "tests/core/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700318
319 BOOST_CHECK(sub.allCallbacksFired());
320
321 input.close();
322}
323
324BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
325{
326 ConfigFile file;
327 DummyAllSubscriber sub(file, true);
328
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700329 file.parse("tests/core/config_example.info", true);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700330 BOOST_CHECK(sub.allCallbacksFired());
331}
332
333BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
334{
335 ConfigFile file;
336 DummyAllSubscriber sub1(file);
337 DummyAllSubscriber sub2(file);
338
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600339 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700340
341 BOOST_CHECK(sub1.noCallbacksFired());
342 BOOST_CHECK(sub2.allCallbacksFired());
343}
344
345BOOST_AUTO_TEST_CASE(OnConfigUncoveredSections)
346{
347 ConfigFile file;
348
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600349 BOOST_CHECK_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700350}
351
352BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
353{
354 ConfigFile file;
355 DummyOneSubscriber subA(file, "a");
356 DummyOneSubscriber subB(file, "b");
357
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600358 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700359
360 BOOST_CHECK(subA.allCallbacksFired());
361 BOOST_CHECK(subB.allCallbacksFired());
362}
363
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700364BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700365
366} // namespace tests
367} // namespace nfd