blob: b18e2743ef3d63956f6249f3cc970a18db4cf493 [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 {
32namespace tests {
33
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080034class GeneralConfigSectionFixture : public BaseFixture
35{
36public:
37 ~GeneralConfigSectionFixture()
38 {
39 // revert changes to s_normalUid/s_normalGid, if any
40 PrivilegeHelper::s_normalUid = ::geteuid();
41 PrivilegeHelper::s_normalGid = ::getegid();
42 }
43};
44
45BOOST_FIXTURE_TEST_SUITE(MgmtGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060046
Steve DiBenedettob4336c22014-10-06 12:14:06 -060047BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060048{
49 const std::string CONFIG =
50 "general\n"
51 "{\n"
52 " user nobody\n"
53 " group nogroup\n"
54 "}\n";
55
56 ConfigFile configFile;
57
58 general::setConfigFile(configFile);
59 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
60
61}
62
Steve DiBenedettob4336c22014-10-06 12:14:06 -060063BOOST_AUTO_TEST_CASE(DefaultConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060064{
65 const std::string CONFIG =
66 "general\n"
67 "{\n"
68 "}\n";
69
70 ConfigFile configFile;
71
72 general::setConfigFile(configFile);
73 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
74}
75
Steve DiBenedettob4336c22014-10-06 12:14:06 -060076BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060077{
78 const std::string CONFIG =
79 "general\n"
80 "{\n"
81 " group nogroup\n"
82 "}\n";
83
84 ConfigFile configFile;
85
86 general::setConfigFile(configFile);
87 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
88}
89
Steve DiBenedettob4336c22014-10-06 12:14:06 -060090BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060091{
92 const std::string CONFIG =
93 "general\n"
94 "{\n"
95 " user nobody\n"
96 "}\n";
97
98 ConfigFile configFile;
99
100 general::setConfigFile(configFile);
101 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
102}
103
104static bool
105checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected)
106{
107 return error.what() == expected;
108}
109
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600110BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600111{
112 const std::string CONFIG =
113 "general\n"
114 "{\n"
115 " user\n"
116 "}\n";
117
118 ConfigFile configFile;
119 general::setConfigFile(configFile);
120
121 const std::string expected = "Invalid value for \"user\" in \"general\" section";
122 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
123 ConfigFile::Error,
124 bind(&checkExceptionMessage, _1, expected));
125}
126
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600127BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600128{
129 const std::string CONFIG =
130 "general\n"
131 "{\n"
132 " group\n"
133 "}\n";
134
135 ConfigFile configFile;
136 general::setConfigFile(configFile);
137
138 const std::string expected = "Invalid value for \"group\" in \"general\" section";
139 BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
140 ConfigFile::Error,
141 bind(&checkExceptionMessage, _1, expected));
142}
143
144BOOST_AUTO_TEST_SUITE_END()
145
146} // namespace tests
147
148} // namespace nfd