blob: a12b09b5d17e5bba7b2af3e7f9dbce9d60cc456f [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 Pesavento70156942018-09-15 18:40:21 -040043 GeneralConfigSectionFixture()
44 {
45 setConfigFile(configFile);
46 }
47
Davide Pesavento231ddd72016-09-02 22:20:00 +000048#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080049 ~GeneralConfigSectionFixture()
50 {
51 // revert changes to s_normalUid/s_normalGid, if any
52 PrivilegeHelper::s_normalUid = ::geteuid();
53 PrivilegeHelper::s_normalGid = ::getegid();
54 }
Davide Pesavento231ddd72016-09-02 22:20:00 +000055#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
Davide Pesavento70156942018-09-15 18:40:21 -040056
57protected:
58 ConfigFile configFile;
Alexander Afanasyev4d384fc2015-02-13 19:01:38 -080059};
60
Davide Pesavento231ddd72016-09-02 22:20:00 +000061BOOST_AUTO_TEST_SUITE(Mgmt)
62BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060063
Davide Pesavento70156942018-09-15 18:40:21 -040064BOOST_AUTO_TEST_CASE(EmptyConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060065{
Davide Pesavento70156942018-09-15 18:40:21 -040066 const std::string CONFIG = R"CONFIG(
67 general
68 {
69 }
70 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060071
Davide Pesavento70156942018-09-15 18:40:21 -040072 configFile.parse(CONFIG, true, "test-general-config-section");
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060073
Davide Pesavento70156942018-09-15 18:40:21 -040074 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
75 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060076}
77
Alexander Afanasyev66c569d2015-08-09 23:45:13 -070078#ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
79
Davide Pesavento70156942018-09-15 18:40:21 -040080BOOST_AUTO_TEST_CASE(UserConfig)
81{
82 const std::string CONFIG = R"CONFIG(
83 general
84 {
85 user daemon
86 }
87 )CONFIG";
88
89 configFile.parse(CONFIG, true, "test-general-config-section");
90
91 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
92 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
93}
94
95BOOST_AUTO_TEST_CASE(GroupConfig)
96{
97 const std::string CONFIG = R"CONFIG(
98 general
99 {
100 group daemon
101 }
102 )CONFIG";
103
104 configFile.parse(CONFIG, true, "test-general-config-section");
105
106 BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
107 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
108}
109
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700110BOOST_AUTO_TEST_CASE(UserAndGroupConfig)
111{
Davide Pesavento70156942018-09-15 18:40:21 -0400112 const std::string CONFIG = R"CONFIG(
113 general
114 {
115 user daemon
116 group daemon
117 }
118 )CONFIG";
Weiwei Liuf5aee942016-03-19 07:00:42 +0000119
Davide Pesavento70156942018-09-15 18:40:21 -0400120 configFile.parse(CONFIG, true, "test-general-config-section");
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700121
Davide Pesavento70156942018-09-15 18:40:21 -0400122 BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid);
123 BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid);
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600124}
125
Alexander Afanasyev66c569d2015-08-09 23:45:13 -0700126#endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
127
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600128BOOST_AUTO_TEST_CASE(InvalidUserConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600129{
Davide Pesavento70156942018-09-15 18:40:21 -0400130 const std::string CONFIG = R"CONFIG(
131 general
132 {
133 user
134 }
135 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600136
Davide Pesavento231ddd72016-09-02 22:20:00 +0000137 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
138 ConfigFile::Error,
139 [] (const ConfigFile::Error& e) {
140 return std::strcmp(e.what(),
141 "Invalid value for \"user\" in \"general\" section") == 0;
142 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600143}
144
Steve DiBenedettob4336c22014-10-06 12:14:06 -0600145BOOST_AUTO_TEST_CASE(InvalidGroupConfig)
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600146{
Davide Pesavento70156942018-09-15 18:40:21 -0400147 const std::string CONFIG = R"CONFIG(
148 general
149 {
150 group
151 }
152 )CONFIG";
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600153
Davide Pesavento231ddd72016-09-02 22:20:00 +0000154 BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"),
155 ConfigFile::Error,
156 [] (const ConfigFile::Error& e) {
157 return std::strcmp(e.what(),
158 "Invalid value for \"group\" in \"general\" section") == 0;
159 });
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600160}
161
Davide Pesavento231ddd72016-09-02 22:20:00 +0000162BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection
163BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600164
165} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800166} // namespace general
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600167} // namespace nfd