blob: ed52046f969a572756127c05cf55dc4be91b2119 [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -08003 * Copyright (c) 2014-2015, 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 * The University of Memphis.
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060010 *
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/>.
Steve DiBenedettob4336c22014-10-06 12:14:06 -060024 */
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060025
26#include "mgmt/general-config-section.hpp"
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080027#include "core/privilege-helper.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060028
29#include "tests/test-common.hpp"
30
31namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080032namespace general {
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060033namespace tests {
34
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080035using namespace nfd::tests;
36
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080037class GeneralConfigSectionFixture : public BaseFixture
38{
39public:
40 ~GeneralConfigSectionFixture()
41 {
42 // revert changes to s_normalUid/s_normalGid, if any
43 PrivilegeHelper::s_normalUid = ::geteuid();
44 PrivilegeHelper::s_normalGid = ::getegid();
45 }
46};
47
48BOOST_FIXTURE_TEST_SUITE(MgmtGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060049
Steve DiBenedettob4336c22014-10-06 12:14:06 -060050BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060051{
52 const std::string CONFIG =
53 "general\n"
54 "{\n"
55 " user nobody\n"
56 " group nogroup\n"
57 "}\n";
58
59 ConfigFile configFile;
60
61 general::setConfigFile(configFile);
62 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
63
64}
65
Steve DiBenedettob4336c22014-10-06 12:14:06 -060066BOOST_AUTO_TEST_CASE(DefaultConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060067{
68 const std::string CONFIG =
69 "general\n"
70 "{\n"
71 "}\n";
72
73 ConfigFile configFile;
74
75 general::setConfigFile(configFile);
76 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
Vince Lehmanee7c8442015-07-16 16:42:19 -050077
78 BOOST_CHECK(getRouterName().getName().empty());
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060079}
80
Steve DiBenedettob4336c22014-10-06 12:14:06 -060081BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060082{
83 const std::string CONFIG =
84 "general\n"
85 "{\n"
86 " group nogroup\n"
87 "}\n";
88
89 ConfigFile configFile;
90
91 general::setConfigFile(configFile);
92 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
93}
94
Steve DiBenedettob4336c22014-10-06 12:14:06 -060095BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060096{
97 const std::string CONFIG =
98 "general\n"
99 "{\n"
100 " user nobody\n"
101 "}\n";
102
103 ConfigFile configFile;
104
105 general::setConfigFile(configFile);
106 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
107}
108
109static bool
110checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
111{
112 return error.what() == expected;
113}
114
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600115BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600116{
117 const std::string CONFIG =
118 "general\n"
119 "{\n"
120 " user\n"
121 "}\n";
122
123 ConfigFile configFile;
124 general::setConfigFile(configFile);
125
126 const std::string expected = "Invalid value for \"user\" in \"general\" section";
127 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
128 ConfigFile::Error,
129 bind(&checkExceptionMessage, _1, expected));
130}
131
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600132BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600133{
134 const std::string CONFIG =
135 "general\n"
136 "{\n"
137 " group\n"
138 "}\n";
139
140 ConfigFile configFile;
141 general::setConfigFile(configFile);
142
143 const std::string expected = "Invalid value for \"group\" in \"general\" section";
144 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
145 ConfigFile::Error,
146 bind(&checkExceptionMessage, _1, expected));
147}
148
Vince Lehmanee7c8442015-07-16 16:42:19 -0500149BOOST_AUTO_TEST_CASE(RouterNameConfig)
150{
151 const std::string CONFIG =
152 "general\n"
153 "{\n"
154 " router_name\n"
155 " {\n"
156 " network ndn\n"
157 " site edu/site\n"
158 " router router/name\n"
159 " }\n"
160 "}\n";
161
162 ConfigFile configFile;
163 general::setConfigFile(configFile);
164
165 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
166
167 BOOST_CHECK_EQUAL(getRouterName().network, ndn::PartialName("ndn"));
168 BOOST_CHECK_EQUAL(getRouterName().site, ndn::PartialName("edu/site"));
169 BOOST_CHECK_EQUAL(getRouterName().router, ndn::PartialName("router/name"));
170 BOOST_CHECK_EQUAL(getRouterName().getName(), ndn::Name("/ndn/edu/site/%C1.Router/router/name"));
171}
172
173BOOST_AUTO_TEST_CASE(NoNetworkConfig)
174{
175 const std::string CONFIG =
176 "general\n"
177 "{\n"
178 " router_name\n"
179 " {\n"
180 " site edu/site\n"
181 " router router/name\n"
182 " }\n"
183 "}\n";
184
185 ConfigFile configFile;
186 general::setConfigFile(configFile);
187
188 const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section";
189 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
190 ConfigFile::Error,
191 bind(&checkExceptionMessage, _1, expected));
192}
193
194BOOST_AUTO_TEST_CASE(NoSiteConfig)
195{
196 const std::string CONFIG =
197 "general\n"
198 "{\n"
199 " router_name\n"
200 " {\n"
201 " network ndn\n"
202 " router router/name\n"
203 " }\n"
204 "}\n";
205
206 ConfigFile configFile;
207 general::setConfigFile(configFile);
208
209 const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section";
210 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
211 ConfigFile::Error,
212 bind(&checkExceptionMessage, _1, expected));
213}
214
215BOOST_AUTO_TEST_CASE(NoRouterConfig)
216{
217 const std::string CONFIG =
218 "general\n"
219 "{\n"
220 " router_name\n"
221 " {\n"
222 " network ndn\n"
223 " site edu/site\n"
224 " }\n"
225 "}\n";
226
227 ConfigFile configFile;
228 general::setConfigFile(configFile);
229
230 const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section";
231 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
232 ConfigFile::Error,
233 bind(&checkExceptionMessage, _1, expected));
234}
235
236BOOST_AUTO_TEST_CASE(InvalidNetworkConfig)
237{
238 const std::string CONFIG =
239 "general\n"
240 "{\n"
241 " router_name\n"
242 " {\n"
243 " network\n"
244 " site edu/site\n"
245 " router router/name\n"
246 " }\n"
247 "}\n";
248
249 ConfigFile configFile;
250 general::setConfigFile(configFile);
251
252 const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section";
253 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
254 ConfigFile::Error,
255 bind(&checkExceptionMessage, _1, expected));
256}
257
258BOOST_AUTO_TEST_CASE(InvalidSiteConfig)
259{
260 const std::string CONFIG =
261 "general\n"
262 "{\n"
263 " router_name\n"
264 " {\n"
265 " network ndn\n"
266 " site\n"
267 " router router/name\n"
268 " }\n"
269 "}\n";
270
271 ConfigFile configFile;
272 general::setConfigFile(configFile);
273
274 const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section";
275 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
276 ConfigFile::Error,
277 bind(&checkExceptionMessage, _1, expected));
278}
279
280BOOST_AUTO_TEST_CASE(InvalidRouterConfig)
281{
282 const std::string CONFIG =
283 "general\n"
284 "{\n"
285 " router_name\n"
286 " {\n"
287 " network ndn\n"
288 " site edu/site\n"
289 " router\n"
290 " }\n"
291 "}\n";
292
293 ConfigFile configFile;
294 general::setConfigFile(configFile);
295
296 const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section";
297 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
298 ConfigFile::Error,
299 bind(&checkExceptionMessage, _1, expected));
300}
301
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600302BOOST_AUTO_TEST_SUITE_END()
303
304} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800305} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600306} // namespace nfd