blob: d06d698b087f27e489f6411de7a1386a8c83c652 [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/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/config-file.hpp"
28#include "common/privilege-helper.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060029
30#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040031#include "tests/daemon/global-io-fixture.hpp"
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060032
Davide Pesavento231ddd72016-09-02 22:20:00 +000033#include <cstring>
34
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037class GeneralConfigSectionFixture : public GlobalIoFixture
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080038{
39public:
Davide Pesavento70156942018-09-15 18:40:21 -040040 GeneralConfigSectionFixture()
41 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042 general::setConfigFile(configFile);
Davide Pesavento70156942018-09-15 18:40:21 -040043 }
44
Davide Pesavento264af772021-02-09 21:48:24 -050045#ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080046 ~GeneralConfigSectionFixture()
47 {
48 // revert changes to s_normalUid/s_normalGid, if any
49 PrivilegeHelper::s_normalUid = ::geteuid();
50 PrivilegeHelper::s_normalGid = ::getegid();
51 }
Davide Pesavento264af772021-02-09 21:48:24 -050052#endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Davide Pesavento70156942018-09-15 18:40:21 -040053
54protected:
55 ConfigFile configFile;
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080056};
57
Davide Pesavento231ddd72016-09-02 22:20:00 +000058BOOST_AUTO_TEST_SUITE(Mgmt)
59BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060060
Davide Pesavento70156942018-09-15 18:40:21 -040061BOOST_AUTO_TEST_CASE(EmptyConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060062{
Davide Pesavento70156942018-09-15 18:40:21 -040063 const std::string CONFIG = R"CONFIG(
64 general
65 {
66 }
67 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060068
Davide Pesavento70156942018-09-15 18:40:21 -040069 configFile.parse(CONFIG, true, "test-general-config-section");
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060070
Davide Pesavento70156942018-09-15 18:40:21 -040071 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
72 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060073}
74
Davide Pesavento264af772021-02-09 21:48:24 -050075#ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070076
Davide Pesavento70156942018-09-15 18:40:21 -040077BOOST_AUTO_TEST_CASE(UserConfig)
78{
79 const std::string CONFIG = R"CONFIG(
80 general
81 {
82 user daemon
83 }
84 )CONFIG";
85
86 configFile.parse(CONFIG, true, "test-general-config-section");
87
88 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
89 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
90}
91
92BOOST_AUTO_TEST_CASE(GroupConfig)
93{
94 const std::string CONFIG = R"CONFIG(
95 general
96 {
97 group daemon
98 }
99 )CONFIG";
100
101 configFile.parse(CONFIG, true, "test-general-config-section");
102
103 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
104 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
105}
106
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700107BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
108{
Davide Pesavento70156942018-09-15 18:40:21 -0400109 const std::string CONFIG = R"CONFIG(
110 general
111 {
112 user daemon
113 group daemon
114 }
115 )CONFIG";
Weiwei Liuf5aee942016-03-19 07:00:42 +0000116
Davide Pesavento70156942018-09-15 18:40:21 -0400117 configFile.parse(CONFIG, true, "test-general-config-section");
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700118
Davide Pesavento70156942018-09-15 18:40:21 -0400119 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
120 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600121}
122
Davide Pesavento264af772021-02-09 21:48:24 -0500123#endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700124
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600125BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600126{
Davide Pesavento70156942018-09-15 18:40:21 -0400127 const std::string CONFIG = R"CONFIG(
128 general
129 {
130 user
131 }
132 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600133
Davide Pesavento231ddd72016-09-02 22:20:00 +0000134 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
135 ConfigFile::Error,
136 [] (const ConfigFile::Error& e) {
137 return std::strcmp(e.what(),
Davide Pesavento19779d82019-02-14 13:40:04 -0500138 "Invalid value for 'user' in section 'general'") == 0;
Davide Pesavento231ddd72016-09-02 22:20:00 +0000139 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600140}
141
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600142BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600143{
Davide Pesavento70156942018-09-15 18:40:21 -0400144 const std::string CONFIG = R"CONFIG(
145 general
146 {
147 group
148 }
149 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600150
Davide Pesavento231ddd72016-09-02 22:20:00 +0000151 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
152 ConfigFile::Error,
153 [] (const ConfigFile::Error& e) {
154 return std::strcmp(e.what(),
Davide Pesavento19779d82019-02-14 13:40:04 -0500155 "Invalid value for 'group' in section 'general'") == 0;
Davide Pesavento231ddd72016-09-02 22:20:00 +0000156 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600157}
158
Davide Pesavento231ddd72016-09-02 22:20:00 +0000159BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection
160BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600161
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400162} // namespace nfd::tests