blob: d3af36692102578b2df974bc2cf3bf5c5fe3af93 [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Weiwei Liuf5aee942016-03-19 07:00:42 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -08004 * 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"));
63}
64
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070065#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
66
67BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
68{
Weiwei Liuf5aee942016-03-19 07:00:42 +000069 SKIP_IF_NOT_SUPERUSER();
70
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070071 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{
Weiwei Liuf5aee942016-03-19 07:00:42 +000086 SKIP_IF_NOT_SUPERUSER();
87
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060088 const std::string CONFIG =
89 "general\n"
90 "{\n"
91 " group nogroup\n"
92 "}\n";
93
94 ConfigFile configFile;
95
96 general::setConfigFile(configFile);
97 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
98}
99
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600100BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600101{
102 const std::string CONFIG =
103 "general\n"
104 "{\n"
105 " user nobody\n"
106 "}\n";
107
108 ConfigFile configFile;
109
110 general::setConfigFile(configFile);
111 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
112}
113
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700114#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
115
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600116static bool
117checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
118{
119 return error.what() == expected;
120}
121
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600122BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600123{
124 const std::string CONFIG =
125 "general\n"
126 "{\n"
127 " user\n"
128 "}\n";
129
130 ConfigFile configFile;
131 general::setConfigFile(configFile);
132
133 const std::string expected = "Invalid value for \"user\" in \"general\" section";
134 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
135 ConfigFile::Error,
136 bind(&checkExceptionMessage, _1, expected));
137}
138
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600139BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600140{
141 const std::string CONFIG =
142 "general\n"
143 "{\n"
144 " group\n"
145 "}\n";
146
147 ConfigFile configFile;
148 general::setConfigFile(configFile);
149
150 const std::string expected = "Invalid value for \"group\" in \"general\" section";
151 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
152 ConfigFile::Error,
153 bind(&checkExceptionMessage, _1, expected));
154}
155
156BOOST_AUTO_TEST_SUITE_END()
157
158} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800159} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600160} // namespace nfd