blob: debd2b88e845f2a21bc95fe8ae19d194634d001b [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"));
63}
64
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070065#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
66
67BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
68{
69 const std::string CONFIG =
70 "general\n"
71 "{\n"
72 " user nobody\n"
73 " group nogroup\n"
74 "}\n";
75
76 ConfigFile configFile;
77
78 general::setConfigFile(configFile);
79 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
80}
81
Steve DiBenedettob4336c22014-10-06 12:14:06 -060082BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060083{
84 const std::string CONFIG =
85 "general\n"
86 "{\n"
87 " group nogroup\n"
88 "}\n";
89
90 ConfigFile configFile;
91
92 general::setConfigFile(configFile);
93 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
94}
95
Steve DiBenedettob4336c22014-10-06 12:14:06 -060096BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060097{
98 const std::string CONFIG =
99 "general\n"
100 "{\n"
101 " user nobody\n"
102 "}\n";
103
104 ConfigFile configFile;
105
106 general::setConfigFile(configFile);
107 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
108}
109
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700110#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
111
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600112static bool
113checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
114{
115 return error.what() == expected;
116}
117
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600118BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600119{
120 const std::string CONFIG =
121 "general\n"
122 "{\n"
123 " user\n"
124 "}\n";
125
126 ConfigFile configFile;
127 general::setConfigFile(configFile);
128
129 const std::string expected = "Invalid value for \"user\" in \"general\" section";
130 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
131 ConfigFile::Error,
132 bind(&checkExceptionMessage, _1, expected));
133}
134
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600135BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600136{
137 const std::string CONFIG =
138 "general\n"
139 "{\n"
140 " group\n"
141 "}\n";
142
143 ConfigFile configFile;
144 general::setConfigFile(configFile);
145
146 const std::string expected = "Invalid value for \"group\" in \"general\" section";
147 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
148 ConfigFile::Error,
149 bind(&checkExceptionMessage, _1, expected));
150}
151
152BOOST_AUTO_TEST_SUITE_END()
153
154} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800155} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600156} // namespace nfd