blob: 226a54143f49a796d69e6be00c5d62e05fd3d0d4 [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
3 * Copyright (c) 2014-2018, 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"
Davide Pesaventoa3148082018-04-12 18:21:54 -040027#include "core/config-file.hpp"
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080028#include "core/privilege-helper.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060029
30#include "tests/test-common.hpp"
31
Davide Pesavento231ddd72016-09-02 22:20:00 +000032#include <cstring>
33
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060034namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080035namespace general {
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060036namespace tests {
37
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080038using namespace nfd::tests;
39
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080040class GeneralConfigSectionFixture : public BaseFixture
41{
42public:
Davide Pesavento231ddd72016-09-02 22:20:00 +000043#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080044 ~GeneralConfigSectionFixture()
45 {
46 // revert changes to s_normalUid/s_normalGid, if any
47 PrivilegeHelper::s_normalUid = ::geteuid();
48 PrivilegeHelper::s_normalGid = ::getegid();
49 }
Davide Pesavento231ddd72016-09-02 22:20:00 +000050#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080051};
52
Davide Pesavento231ddd72016-09-02 22:20:00 +000053BOOST_AUTO_TEST_SUITE(Mgmt)
54BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060055
Steve DiBenedettob4336c22014-10-06 12:14:06 -060056BOOST_AUTO_TEST_CASE(DefaultConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060057{
58 const std::string CONFIG =
59 "general\n"
60 "{\n"
61 "}\n";
62
63 ConfigFile configFile;
64
65 general::setConfigFile(configFile);
66 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
67}
68
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070069#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
70
71BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
72{
Weiwei Liuf5aee942016-03-19 07:00:42 +000073 SKIP_IF_NOT_SUPERUSER();
74
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070075 const std::string CONFIG =
76 "general\n"
77 "{\n"
78 " user nobody\n"
79 " group nogroup\n"
80 "}\n";
81
82 ConfigFile configFile;
83
84 general::setConfigFile(configFile);
85 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
86}
87
Steve DiBenedettob4336c22014-10-06 12:14:06 -060088BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060089{
Weiwei Liuf5aee942016-03-19 07:00:42 +000090 SKIP_IF_NOT_SUPERUSER();
91
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060092 const std::string CONFIG =
93 "general\n"
94 "{\n"
95 " group nogroup\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
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600104BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600105{
106 const std::string CONFIG =
107 "general\n"
108 "{\n"
109 " user nobody\n"
110 "}\n";
111
112 ConfigFile configFile;
113
114 general::setConfigFile(configFile);
115 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
116}
117
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700118#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
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
Davide Pesavento231ddd72016-09-02 22:20:00 +0000131 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
132 ConfigFile::Error,
133 [] (const ConfigFile::Error& e) {
134 return std::strcmp(e.what(),
135 "Invalid value for \"user\" in \"general\" section") == 0;
136 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600137}
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
Davide Pesavento231ddd72016-09-02 22:20:00 +0000150 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
151 ConfigFile::Error,
152 [] (const ConfigFile::Error& e) {
153 return std::strcmp(e.what(),
154 "Invalid value for \"group\" in \"general\" section") == 0;
155 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600156}
157
Davide Pesavento231ddd72016-09-02 22:20:00 +0000158BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection
159BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600160
161} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800162} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600163} // namespace nfd