Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 4 | * 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 DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 10 | * |
| 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 DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 25 | |
| 26 | #include "mgmt/general-config-section.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 27 | #include "core/config-file.hpp" |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 28 | #include "core/privilege-helper.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 29 | |
| 30 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 31 | #include "tests/daemon/global-io-fixture.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 32 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 33 | #include <cstring> |
| 34 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 35 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 36 | namespace general { |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 37 | namespace tests { |
| 38 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 39 | using namespace nfd::tests; |
| 40 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 41 | class GeneralConfigSectionFixture : public GlobalIoFixture |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 44 | GeneralConfigSectionFixture() |
| 45 | { |
| 46 | setConfigFile(configFile); |
| 47 | } |
| 48 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 49 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 50 | ~GeneralConfigSectionFixture() |
| 51 | { |
| 52 | // revert changes to s_normalUid/s_normalGid, if any |
| 53 | PrivilegeHelper::s_normalUid = ::geteuid(); |
| 54 | PrivilegeHelper::s_normalGid = ::getegid(); |
| 55 | } |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 56 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 57 | |
| 58 | protected: |
| 59 | ConfigFile configFile; |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 62 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 63 | BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 64 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(EmptyConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 66 | { |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 67 | const std::string CONFIG = R"CONFIG( |
| 68 | general |
| 69 | { |
| 70 | } |
| 71 | )CONFIG"; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 72 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 73 | configFile.parse(CONFIG, true, "test-general-config-section"); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 74 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| 76 | BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 79 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 80 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 81 | BOOST_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 | |
| 96 | BOOST_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 Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(UserAndGroupConfig) |
| 112 | { |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 113 | const std::string CONFIG = R"CONFIG( |
| 114 | general |
| 115 | { |
| 116 | user daemon |
| 117 | group daemon |
| 118 | } |
| 119 | )CONFIG"; |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 120 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 121 | configFile.parse(CONFIG, true, "test-general-config-section"); |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 122 | |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 123 | BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| 124 | BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 127 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 128 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(InvalidUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 130 | { |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 131 | const std::string CONFIG = R"CONFIG( |
| 132 | general |
| 133 | { |
| 134 | user |
| 135 | } |
| 136 | )CONFIG"; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 137 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 138 | 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 142 | "Invalid value for 'user' in section 'general'") == 0; |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 143 | }); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 146 | BOOST_AUTO_TEST_CASE(InvalidGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 147 | { |
Davide Pesavento | 7015694 | 2018-09-15 18:40:21 -0400 | [diff] [blame] | 148 | const std::string CONFIG = R"CONFIG( |
| 149 | general |
| 150 | { |
| 151 | group |
| 152 | } |
| 153 | )CONFIG"; |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 154 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 155 | 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 Pesavento | 19779d8 | 2019-02-14 13:40:04 -0500 | [diff] [blame] | 159 | "Invalid value for 'group' in section 'general'") == 0; |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 160 | }); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 161 | } |
| 162 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 163 | BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection |
| 164 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 165 | |
| 166 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 167 | } // namespace general |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 168 | } // namespace nfd |