blob: 83063b9523d35565d8d6779700aaba4c54793b22 [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"));
77}
78
Steve DiBenedettob4336c22014-10-06 12:14:06 -060079BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060080{
81 const std::string CONFIG =
82 "general\n"
83 "{\n"
84 " group nogroup\n"
85 "}\n";
86
87 ConfigFile configFile;
88
89 general::setConfigFile(configFile);
90 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
91}
92
Steve DiBenedettob4336c22014-10-06 12:14:06 -060093BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060094{
95 const std::string CONFIG =
96 "general\n"
97 "{\n"
98 " user nobody\n"
99 "}\n";
100
101 ConfigFile configFile;
102
103 general::setConfigFile(configFile);
104 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
105}
106
107static bool
108checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
109{
110 return error.what() == expected;
111}
112
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600113BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600114{
115 const std::string CONFIG =
116 "general\n"
117 "{\n"
118 " user\n"
119 "}\n";
120
121 ConfigFile configFile;
122 general::setConfigFile(configFile);
123
124 const std::string expected = "Invalid value for \"user\" in \"general\" section";
125 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
126 ConfigFile::Error,
127 bind(&checkExceptionMessage, _1, expected));
128}
129
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600130BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600131{
132 const std::string CONFIG =
133 "general\n"
134 "{\n"
135 " group\n"
136 "}\n";
137
138 ConfigFile configFile;
139 general::setConfigFile(configFile);
140
141 const std::string expected = "Invalid value for \"group\" in \"general\" section";
142 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
143 ConfigFile::Error,
144 bind(&checkExceptionMessage, _1, expected));
145}
146
147BOOST_AUTO_TEST_SUITE_END()
148
149} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800150} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600151} // namespace nfd