blob: 26e87ea9d9ca49aa5a8d8b8c56f10caa75a348aa [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
Davide Pesavento231ddd72016-09-02 22:20:00 +000031#include <cstring>
32
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060033namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080034namespace general {
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060035namespace tests {
36
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080037using namespace nfd::tests;
38
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080039class GeneralConfigSectionFixture : public BaseFixture
40{
41public:
Davide Pesavento231ddd72016-09-02 22:20:00 +000042#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080043 ~GeneralConfigSectionFixture()
44 {
45 // revert changes to s_normalUid/s_normalGid, if any
46 PrivilegeHelper::s_normalUid = ::geteuid();
47 PrivilegeHelper::s_normalGid = ::getegid();
48 }
Davide Pesavento231ddd72016-09-02 22:20:00 +000049#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080050};
51
Davide Pesavento231ddd72016-09-02 22:20:00 +000052BOOST_AUTO_TEST_SUITE(Mgmt)
53BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060054
Steve DiBenedettob4336c22014-10-06 12:14:06 -060055BOOST_AUTO_TEST_CASE(DefaultConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060056{
57 const std::string CONFIG =
58 "general\n"
59 "{\n"
60 "}\n";
61
62 ConfigFile configFile;
63
64 general::setConfigFile(configFile);
65 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
66}
67
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070068#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
69
70BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
71{
Weiwei Liuf5aee942016-03-19 07:00:42 +000072 SKIP_IF_NOT_SUPERUSER();
73
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070074 const std::string CONFIG =
75 "general\n"
76 "{\n"
77 " user nobody\n"
78 " group nogroup\n"
79 "}\n";
80
81 ConfigFile configFile;
82
83 general::setConfigFile(configFile);
84 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
85}
86
Steve DiBenedettob4336c22014-10-06 12:14:06 -060087BOOST_AUTO_TEST_CASE(NoUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060088{
Weiwei Liuf5aee942016-03-19 07:00:42 +000089 SKIP_IF_NOT_SUPERUSER();
90
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060091 const std::string CONFIG =
92 "general\n"
93 "{\n"
94 " group nogroup\n"
95 "}\n";
96
97 ConfigFile configFile;
98
99 general::setConfigFile(configFile);
100 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
101}
102
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600103BOOST_AUTO_TEST_CASE(NoGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600104{
105 const std::string CONFIG =
106 "general\n"
107 "{\n"
108 " user nobody\n"
109 "}\n";
110
111 ConfigFile configFile;
112
113 general::setConfigFile(configFile);
114 BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section"));
115}
116
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700117#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
118
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600119BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600120{
121 const std::string CONFIG =
122 "general\n"
123 "{\n"
124 " user\n"
125 "}\n";
126
127 ConfigFile configFile;
128 general::setConfigFile(configFile);
129
Davide Pesavento231ddd72016-09-02 22:20:00 +0000130 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
131 ConfigFile::Error,
132 [] (const ConfigFile::Error& e) {
133 return std::strcmp(e.what(),
134 "Invalid value for \"user\" in \"general\" section") == 0;
135 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600136}
137
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600138BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600139{
140 const std::string CONFIG =
141 "general\n"
142 "{\n"
143 " group\n"
144 "}\n";
145
146 ConfigFile configFile;
147 general::setConfigFile(configFile);
148
Davide Pesavento231ddd72016-09-02 22:20:00 +0000149 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
150 ConfigFile::Error,
151 [] (const ConfigFile::Error& e) {
152 return std::strcmp(e.what(),
153 "Invalid value for \"group\" in \"general\" section") == 0;
154 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600155}
156
Davide Pesavento231ddd72016-09-02 22:20:00 +0000157BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection
158BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600159
160} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800161} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600162} // namespace nfd