blob: df0d9c3b94f5c52df3e6353f3d3fc78c5c0745c2 [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
25
26#include "mgmt/config-file.hpp"
27
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070029
30namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070032
33NFD_LOG_INIT("ConfigFileTest");
34
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, BaseFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070036
37// a
38// {
39// akey "avalue"
40// }
41// b
42// {
43// bkey "bvalue"
44// }
45
46const std::string CONFIG =
47"a\n"
48"{\n"
49" akey \"avalue\"\n"
50"}\n"
51"b\n"
52"{\n"
53" bkey \"bvalue\"\n"
54"}\n";
55
56
57// a
58// {
59// akey "avalue"
60// }
61// b
62//
63// bkey "bvalue"
64// }
65
66const std::string MALFORMED_CONFIG =
67"a\n"
68"{\n"
69" akey \"avalue\"\n"
70"}\n"
71"b\n"
72"\n"
73" bkey \"bvalue\"\n"
74"}\n";
75
76// counts of the respective section counts in config_example.info
77
78const int CONFIG_N_A_SECTIONS = 1;
79const int CONFIG_N_B_SECTIONS = 1;
80
81class DummySubscriber
82{
83public:
84
85 DummySubscriber(ConfigFile& config,
86 int nASections,
87 int nBSections,
88 bool expectDryRun)
89 : m_nASections(nASections),
90 m_nBSections(nBSections),
91 m_nRemainingACallbacks(nASections),
92 m_nRemainingBCallbacks(nBSections),
93 m_expectDryRun(expectDryRun)
94 {
95
96 }
97
98 void
99 onA(const ConfigSection& section, bool isDryRun)
100 {
101 // NFD_LOG_DEBUG("a");
102 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
103 --m_nRemainingACallbacks;
104 }
105
106
107 void
108 onB(const ConfigSection& section, bool isDryRun)
109 {
110 // NFD_LOG_DEBUG("b");
111 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
112 --m_nRemainingBCallbacks;
113 }
114
115 bool
116 allCallbacksFired() const
117 {
118 return m_nRemainingACallbacks == 0 &&
119 m_nRemainingBCallbacks == 0;
120 }
121
122 bool
123 noCallbacksFired() const
124 {
125 return m_nRemainingACallbacks == m_nASections &&
126 m_nRemainingBCallbacks == m_nBSections;
127 }
128
129 virtual
130 ~DummySubscriber()
131 {
132
133 }
134
135private:
136 int m_nASections;
137 int m_nBSections;
138 int m_nRemainingACallbacks;
139 int m_nRemainingBCallbacks;
140 bool m_expectDryRun;
141};
142
143class DummyAllSubscriber : public DummySubscriber
144{
145public:
146 DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false)
147 : DummySubscriber(config,
148 CONFIG_N_A_SECTIONS,
149 CONFIG_N_B_SECTIONS,
150 expectDryRun)
151 {
152 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
153 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
154 }
155
156 virtual
157 ~DummyAllSubscriber()
158 {
159
160 }
161};
162
163class DummyOneSubscriber : public DummySubscriber
164{
165public:
166 DummyOneSubscriber(ConfigFile& config,
167 const std::string& sectionName,
168 bool expectDryRun=false)
169 : DummySubscriber(config,
170 (sectionName == "a"),
171 (sectionName == "b"),
172 expectDryRun)
173 {
174 if (sectionName == "a")
175 {
176 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
177 }
178 else if (sectionName == "b")
179 {
180 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
181 }
182 else
183 {
184 BOOST_FAIL("Test setup error: "
185 << "Unexpected section name "
186 <<"\"" << sectionName << "\"");
187 }
188
189 }
190
191 virtual
192 ~DummyOneSubscriber()
193 {
194
195 }
196};
197
198class DummyNoSubscriber : public DummySubscriber
199{
200public:
201 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
202 : DummySubscriber(config, 0, 0, expectDryRun)
203 {
204
205 }
206
207 virtual
208 ~DummyNoSubscriber()
209 {
210
211 }
212};
213
214BOOST_AUTO_TEST_CASE(OnConfigStream)
215{
216 ConfigFile file;
217 DummyAllSubscriber sub(file);
218 std::ifstream input;
219
220 input.open("tests/mgmt/config_example.info");
221 BOOST_REQUIRE(input.is_open());
222
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600223 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700224 BOOST_CHECK(sub.allCallbacksFired());
225}
226
227BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
228{
229 ConfigFile file;
230 DummyAllSubscriber sub(file);
231
232 std::ifstream input;
233
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600234 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700235 BOOST_CHECK(sub.noCallbacksFired());
236}
237
238
239BOOST_AUTO_TEST_CASE(OnConfigString)
240{
241 ConfigFile file;
242 DummyAllSubscriber sub(file);
243
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600244 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700245
246 BOOST_CHECK(sub.allCallbacksFired());
247}
248
249BOOST_AUTO_TEST_CASE(OnConfigStringEmpty)
250{
251 ConfigFile file;
252 DummyAllSubscriber sub(file);
253
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600254 BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700255 BOOST_CHECK(sub.noCallbacksFired());
256}
257
258BOOST_AUTO_TEST_CASE(OnConfigStringMalformed)
259{
260 ConfigFile file;
261 DummyAllSubscriber sub(file);
262
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600263 BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700264 BOOST_CHECK(sub.noCallbacksFired());
265}
266
267BOOST_AUTO_TEST_CASE(OnConfigStringDryRun)
268{
269 ConfigFile file;
270 DummyAllSubscriber sub(file, true);
271
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600272 file.parse(CONFIG, true, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700273
274 BOOST_CHECK(sub.allCallbacksFired());
275}
276
277BOOST_AUTO_TEST_CASE(OnConfigFilename)
278{
279 ConfigFile file;
280 DummyAllSubscriber sub(file);
281
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600282 file.parse("tests/mgmt/config_example.info", false);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700283
284 BOOST_CHECK(sub.allCallbacksFired());
285}
286
287BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile)
288{
289 ConfigFile file;
290 DummyAllSubscriber sub(file);
291
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600292 BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700293
294 BOOST_CHECK(sub.noCallbacksFired());
295}
296
297BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed)
298{
299 ConfigFile file;
300 DummyAllSubscriber sub(file);
301
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600302 BOOST_CHECK_THROW(file.parse("tests/mgmt/config_malformed.info", false), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700303
304 BOOST_CHECK(sub.noCallbacksFired());
305}
306
307BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun)
308{
309 ConfigFile file;
310 DummyAllSubscriber sub(file, true);
311 std::ifstream input;
312
313 input.open("tests/mgmt/config_example.info");
314 BOOST_REQUIRE(input.is_open());
315
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600316 file.parse(input, true, "tests/mgmt/config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700317
318 BOOST_CHECK(sub.allCallbacksFired());
319
320 input.close();
321}
322
323BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun)
324{
325 ConfigFile file;
326 DummyAllSubscriber sub(file, true);
327
328 file.parse("tests/mgmt/config_example.info", true);
329 BOOST_CHECK(sub.allCallbacksFired());
330}
331
332BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber)
333{
334 ConfigFile file;
335 DummyAllSubscriber sub1(file);
336 DummyAllSubscriber sub2(file);
337
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600338 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700339
340 BOOST_CHECK(sub1.noCallbacksFired());
341 BOOST_CHECK(sub2.allCallbacksFired());
342}
343
344BOOST_AUTO_TEST_CASE(OnConfigUncoveredSections)
345{
346 ConfigFile file;
347
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600348 BOOST_CHECK_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700349}
350
351BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
352{
353 ConfigFile file;
354 DummyOneSubscriber subA(file, "a");
355 DummyOneSubscriber subB(file, "b");
356
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600357 file.parse(CONFIG, false, "dummy-config");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700358
359 BOOST_CHECK(subA.allCallbacksFired());
360 BOOST_CHECK(subB.allCallbacksFired());
361}
362
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700363BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700364
365} // namespace tests
366} // namespace nfd