blob: 830ebbb91abfd0de13439655756fde471933994b [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7
8#include "mgmt/config-file.hpp"
9
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010#include "tests/test-common.hpp"
Steve DiBenedettobb75b552014-02-08 12:12:11 -070011
12namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013namespace tests {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070014
15NFD_LOG_INIT("ConfigFileTest");
16
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, BaseFixture)
Steve DiBenedettobb75b552014-02-08 12:12:11 -070018
19// a
20// {
21// akey "avalue"
22// }
23// b
24// {
25// bkey "bvalue"
26// }
27
28const std::string CONFIG =
29"a\n"
30"{\n"
31" akey \"avalue\"\n"
32"}\n"
33"b\n"
34"{\n"
35" bkey \"bvalue\"\n"
36"}\n";
37
38
39// a
40// {
41// akey "avalue"
42// }
43// b
44//
45// bkey "bvalue"
46// }
47
48const std::string MALFORMED_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// counts of the respective section counts in config_example.info
59
60const int CONFIG_N_A_SECTIONS = 1;
61const int CONFIG_N_B_SECTIONS = 1;
62
63class DummySubscriber
64{
65public:
66
67 DummySubscriber(ConfigFile& config,
68 int nASections,
69 int nBSections,
70 bool expectDryRun)
71 : m_nASections(nASections),
72 m_nBSections(nBSections),
73 m_nRemainingACallbacks(nASections),
74 m_nRemainingBCallbacks(nBSections),
75 m_expectDryRun(expectDryRun)
76 {
77
78 }
79
80 void
81 onA(const ConfigSection& section, bool isDryRun)
82 {
83 // NFD_LOG_DEBUG("a");
84 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
85 --m_nRemainingACallbacks;
86 }
87
88
89 void
90 onB(const ConfigSection& section, bool isDryRun)
91 {
92 // NFD_LOG_DEBUG("b");
93 BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun);
94 --m_nRemainingBCallbacks;
95 }
96
97 bool
98 allCallbacksFired() const
99 {
100 return m_nRemainingACallbacks == 0 &&
101 m_nRemainingBCallbacks == 0;
102 }
103
104 bool
105 noCallbacksFired() const
106 {
107 return m_nRemainingACallbacks == m_nASections &&
108 m_nRemainingBCallbacks == m_nBSections;
109 }
110
111 virtual
112 ~DummySubscriber()
113 {
114
115 }
116
117private:
118 int m_nASections;
119 int m_nBSections;
120 int m_nRemainingACallbacks;
121 int m_nRemainingBCallbacks;
122 bool m_expectDryRun;
123};
124
125class DummyAllSubscriber : public DummySubscriber
126{
127public:
128 DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false)
129 : DummySubscriber(config,
130 CONFIG_N_A_SECTIONS,
131 CONFIG_N_B_SECTIONS,
132 expectDryRun)
133 {
134 config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2));
135 config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2));
136 }
137
138 virtual
139 ~DummyAllSubscriber()
140 {
141
142 }
143};
144
145class DummyOneSubscriber : public DummySubscriber
146{
147public:
148 DummyOneSubscriber(ConfigFile& config,
149 const std::string& sectionName,
150 bool expectDryRun=false)
151 : DummySubscriber(config,
152 (sectionName == "a"),
153 (sectionName == "b"),
154 expectDryRun)
155 {
156 if (sectionName == "a")
157 {
158 config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2));
159 }
160 else if (sectionName == "b")
161 {
162 config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2));
163 }
164 else
165 {
166 BOOST_FAIL("Test setup error: "
167 << "Unexpected section name "
168 <<"\"" << sectionName << "\"");
169 }
170
171 }
172
173 virtual
174 ~DummyOneSubscriber()
175 {
176
177 }
178};
179
180class DummyNoSubscriber : public DummySubscriber
181{
182public:
183 DummyNoSubscriber(ConfigFile& config, bool expectDryRun)
184 : DummySubscriber(config, 0, 0, expectDryRun)
185 {
186
187 }
188
189 virtual
190 ~DummyNoSubscriber()
191 {
192
193 }
194};
195
196BOOST_AUTO_TEST_CASE(OnConfigStream)
197{
198 ConfigFile file;
199 DummyAllSubscriber sub(file);
200 std::ifstream input;
201
202 input.open("tests/mgmt/config_example.info");
203 BOOST_REQUIRE(input.is_open());
204
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600205 file.parse(input, false, "config_example.info");
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700206 BOOST_CHECK(sub.allCallbacksFired());
207}
208
209BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream)
210{
211 ConfigFile file;
212 DummyAllSubscriber sub(file);
213
214 std::ifstream input;
215
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600216 BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700217 BOOST_CHECK(sub.noCallbacksFired());
218}
219
220
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
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600264 file.parse("tests/mgmt/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
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600284 BOOST_CHECK_THROW(file.parse("tests/mgmt/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
295 input.open("tests/mgmt/config_example.info");
296 BOOST_REQUIRE(input.is_open());
297
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600298 file.parse(input, true, "tests/mgmt/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
310 file.parse("tests/mgmt/config_example.info", true);
311 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
326BOOST_AUTO_TEST_CASE(OnConfigUncoveredSections)
327{
328 ConfigFile file;
329
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600330 BOOST_CHECK_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700331}
332
333BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers)
334{
335 ConfigFile file;
336 DummyOneSubscriber subA(file, "a");
337 DummyOneSubscriber subB(file, "b");
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(subA.allCallbacksFired());
342 BOOST_CHECK(subB.allCallbacksFired());
343}
344
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700345BOOST_AUTO_TEST_SUITE_END()
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700346
347} // namespace tests
348} // namespace nfd