blob: 1f7188a9e43065cb727c03e0f77174a5c5b219e4 [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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"
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
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060035namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036namespace general {
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060037namespace tests {
38
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080039using namespace nfd::tests;
40
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041class GeneralConfigSectionFixture : public GlobalIoFixture
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080042{
43public:
Davide Pesavento70156942018-09-15 18:40:21 -040044 GeneralConfigSectionFixture()
45 {
46 setConfigFile(configFile);
47 }
48
Davide Pesavento231ddd72016-09-02 22:20:00 +000049#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080050 ~GeneralConfigSectionFixture()
51 {
52 // revert changes to s_normalUid/s_normalGid, if any
53 PrivilegeHelper::s_normalUid = ::geteuid();
54 PrivilegeHelper::s_normalGid = ::getegid();
55 }
Davide Pesavento231ddd72016-09-02 22:20:00 +000056#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
Davide Pesavento70156942018-09-15 18:40:21 -040057
58protected:
59 ConfigFile configFile;
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080060};
61
Davide Pesavento231ddd72016-09-02 22:20:00 +000062BOOST_AUTO_TEST_SUITE(Mgmt)
63BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060064
Davide Pesavento70156942018-09-15 18:40:21 -040065BOOST_AUTO_TEST_CASE(EmptyConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060066{
Davide Pesavento70156942018-09-15 18:40:21 -040067 const std::string CONFIG = R"CONFIG(
68 general
69 {
70 }
71 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060072
Davide Pesavento70156942018-09-15 18:40:21 -040073 configFile.parse(CONFIG, true, "test-general-config-section");
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060074
Davide Pesavento70156942018-09-15 18:40:21 -040075 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
76 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060077}
78
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070079#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
80
Davide Pesavento70156942018-09-15 18:40:21 -040081BOOST_AUTO_TEST_CASE(UserConfig)
82{
83 const std::string CONFIG = R"CONFIG(
84 general
85 {
86 user daemon
87 }
88 )CONFIG";
89
90 configFile.parse(CONFIG, true, "test-general-config-section");
91
92 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
93 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
94}
95
96BOOST_AUTO_TEST_CASE(GroupConfig)
97{
98 const std::string CONFIG = R"CONFIG(
99 general
100 {
101 group daemon
102 }
103 )CONFIG";
104
105 configFile.parse(CONFIG, true, "test-general-config-section");
106
107 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
108 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
109}
110
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700111BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
112{
Davide Pesavento70156942018-09-15 18:40:21 -0400113 const std::string CONFIG = R"CONFIG(
114 general
115 {
116 user daemon
117 group daemon
118 }
119 )CONFIG";
Weiwei Liuf5aee942016-03-19 07:00:42 +0000120
Davide Pesavento70156942018-09-15 18:40:21 -0400121 configFile.parse(CONFIG, true, "test-general-config-section");
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700122
Davide Pesavento70156942018-09-15 18:40:21 -0400123 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
124 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600125}
126
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700127#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
128
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600129BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600130{
Davide Pesavento70156942018-09-15 18:40:21 -0400131 const std::string CONFIG = R"CONFIG(
132 general
133 {
134 user
135 }
136 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600137
Davide Pesavento231ddd72016-09-02 22:20:00 +0000138 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
139 ConfigFile::Error,
140 [] (const ConfigFile::Error& e) {
141 return std::strcmp(e.what(),
Davide Pesavento19779d82019-02-14 13:40:04 -0500142 "Invalid value for 'user' in section 'general'") == 0;
Davide Pesavento231ddd72016-09-02 22:20:00 +0000143 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600144}
145
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600146BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600147{
Davide Pesavento70156942018-09-15 18:40:21 -0400148 const std::string CONFIG = R"CONFIG(
149 general
150 {
151 group
152 }
153 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600154
Davide Pesavento231ddd72016-09-02 22:20:00 +0000155 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
156 ConfigFile::Error,
157 [] (const ConfigFile::Error& e) {
158 return std::strcmp(e.what(),
Davide Pesavento19779d82019-02-14 13:40:04 -0500159 "Invalid value for 'group' in section 'general'") == 0;
Davide Pesavento231ddd72016-09-02 22:20:00 +0000160 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600161}
162
Davide Pesavento231ddd72016-09-02 22:20:00 +0000163BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection
164BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600165
166} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800167} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600168} // namespace nfd