blob: 3428624d15d2c2b5b81048e1340257ad7239186e [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 {
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070042#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080043 // revert changes to s_normalUid/s_normalGid, if any
44 PrivilegeHelper::s_normalUid = ::geteuid();
45 PrivilegeHelper::s_normalGid = ::getegid();
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070046#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080047 }
48};
49
50BOOST_FIXTURE_TEST_SUITE(MgmtGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060051
Steve DiBenedettob4336c22014-10-06 12:14:06 -060052BOOST_AUTO_TEST_CASE(DefaultConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060053{
54 const std::string CONFIG =
55 "general\n"
56 "{\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"));
Vince Lehmanee7c8442015-07-16 16:42:19 -050063
64 BOOST_CHECK(getRouterName().getName().empty());
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060065}
66
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070067#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
68
69BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
70{
71 const std::string CONFIG =
72 "general\n"
73 "{\n"
74 " user nobody\n"
75 " group nogroup\n"
76 "}\n";
77
78 ConfigFile configFile;
79
80 general::setConfigFile(configFile);
81 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
82}
83
Steve DiBenedettob4336c22014-10-06 12:14:06 -060084BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060085{
86 const std::string CONFIG =
87 "general\n"
88 "{\n"
89 " group nogroup\n"
90 "}\n";
91
92 ConfigFile configFile;
93
94 general::setConfigFile(configFile);
95 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
96}
97
Steve DiBenedettob4336c22014-10-06 12:14:06 -060098BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060099{
100 const std::string CONFIG =
101 "general\n"
102 "{\n"
103 " user nobody\n"
104 "}\n";
105
106 ConfigFile configFile;
107
108 general::setConfigFile(configFile);
109 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
110}
111
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700112#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
113
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600114static bool
115checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
116{
117 return error.what() == expected;
118}
119
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600120BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600121{
122 const std::string CONFIG =
123 "general\n"
124 "{\n"
125 " user\n"
126 "}\n";
127
128 ConfigFile configFile;
129 general::setConfigFile(configFile);
130
131 const std::string expected = "Invalid value for \"user\" in \"general\" section";
132 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
133 ConfigFile::Error,
134 bind(&checkExceptionMessage, _1, expected));
135}
136
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600137BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600138{
139 const std::string CONFIG =
140 "general\n"
141 "{\n"
142 " group\n"
143 "}\n";
144
145 ConfigFile configFile;
146 general::setConfigFile(configFile);
147
148 const std::string expected = "Invalid value for \"group\" in \"general\" section";
149 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
150 ConfigFile::Error,
151 bind(&checkExceptionMessage, _1, expected));
152}
153
Vince Lehmanee7c8442015-07-16 16:42:19 -0500154BOOST_AUTO_TEST_CASE(RouterNameConfig)
155{
156 const std::string CONFIG =
157 "general\n"
158 "{\n"
159 " router_name\n"
160 " {\n"
161 " network ndn\n"
162 " site edu/site\n"
163 " router router/name\n"
164 " }\n"
165 "}\n";
166
167 ConfigFile configFile;
168 general::setConfigFile(configFile);
169
170 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
171
172 BOOST_CHECK_EQUAL(getRouterName().network, ndn::PartialName("ndn"));
173 BOOST_CHECK_EQUAL(getRouterName().site, ndn::PartialName("edu/site"));
174 BOOST_CHECK_EQUAL(getRouterName().router, ndn::PartialName("router/name"));
175 BOOST_CHECK_EQUAL(getRouterName().getName(), ndn::Name("/ndn/edu/site/%C1.Router/router/name"));
176}
177
178BOOST_AUTO_TEST_CASE(NoNetworkConfig)
179{
180 const std::string CONFIG =
181 "general\n"
182 "{\n"
183 " router_name\n"
184 " {\n"
185 " site edu/site\n"
186 " router router/name\n"
187 " }\n"
188 "}\n";
189
190 ConfigFile configFile;
191 general::setConfigFile(configFile);
192
193 const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section";
194 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
195 ConfigFile::Error,
196 bind(&checkExceptionMessage, _1, expected));
197}
198
199BOOST_AUTO_TEST_CASE(NoSiteConfig)
200{
201 const std::string CONFIG =
202 "general\n"
203 "{\n"
204 " router_name\n"
205 " {\n"
206 " network ndn\n"
207 " router router/name\n"
208 " }\n"
209 "}\n";
210
211 ConfigFile configFile;
212 general::setConfigFile(configFile);
213
214 const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section";
215 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
216 ConfigFile::Error,
217 bind(&checkExceptionMessage, _1, expected));
218}
219
220BOOST_AUTO_TEST_CASE(NoRouterConfig)
221{
222 const std::string CONFIG =
223 "general\n"
224 "{\n"
225 " router_name\n"
226 " {\n"
227 " network ndn\n"
228 " site edu/site\n"
229 " }\n"
230 "}\n";
231
232 ConfigFile configFile;
233 general::setConfigFile(configFile);
234
235 const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section";
236 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
237 ConfigFile::Error,
238 bind(&checkExceptionMessage, _1, expected));
239}
240
241BOOST_AUTO_TEST_CASE(InvalidNetworkConfig)
242{
243 const std::string CONFIG =
244 "general\n"
245 "{\n"
246 " router_name\n"
247 " {\n"
248 " network\n"
249 " site edu/site\n"
250 " router router/name\n"
251 " }\n"
252 "}\n";
253
254 ConfigFile configFile;
255 general::setConfigFile(configFile);
256
257 const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section";
258 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
259 ConfigFile::Error,
260 bind(&checkExceptionMessage, _1, expected));
261}
262
263BOOST_AUTO_TEST_CASE(InvalidSiteConfig)
264{
265 const std::string CONFIG =
266 "general\n"
267 "{\n"
268 " router_name\n"
269 " {\n"
270 " network ndn\n"
271 " site\n"
272 " router router/name\n"
273 " }\n"
274 "}\n";
275
276 ConfigFile configFile;
277 general::setConfigFile(configFile);
278
279 const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section";
280 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
281 ConfigFile::Error,
282 bind(&checkExceptionMessage, _1, expected));
283}
284
285BOOST_AUTO_TEST_CASE(InvalidRouterConfig)
286{
287 const std::string CONFIG =
288 "general\n"
289 "{\n"
290 " router_name\n"
291 " {\n"
292 " network ndn\n"
293 " site edu/site\n"
294 " router\n"
295 " }\n"
296 "}\n";
297
298 ConfigFile configFile;
299 general::setConfigFile(configFile);
300
301 const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section";
302 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
303 ConfigFile::Error,
304 bind(&checkExceptionMessage, _1, expected));
305}
306
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600307BOOST_AUTO_TEST_SUITE_END()
308
309} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800310} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600311} // namespace nfd