| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2022, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "mgmt/general-config-section.hpp" |
| #include "common/config-file.hpp" |
| #include "common/privilege-helper.hpp" |
| |
| #include "tests/test-common.hpp" |
| #include "tests/daemon/global-io-fixture.hpp" |
| |
| #include <cstring> |
| |
| namespace nfd::tests { |
| |
| class GeneralConfigSectionFixture : public GlobalIoFixture |
| { |
| public: |
| GeneralConfigSectionFixture() |
| { |
| general::setConfigFile(configFile); |
| } |
| |
| #ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| ~GeneralConfigSectionFixture() |
| { |
| // revert changes to s_normalUid/s_normalGid, if any |
| PrivilegeHelper::s_normalUid = ::geteuid(); |
| PrivilegeHelper::s_normalGid = ::getegid(); |
| } |
| #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| |
| protected: |
| ConfigFile configFile; |
| }; |
| |
| BOOST_AUTO_TEST_SUITE(Mgmt) |
| BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture) |
| |
| BOOST_AUTO_TEST_CASE(EmptyConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| } |
| )CONFIG"; |
| |
| configFile.parse(CONFIG, true, "test-general-config-section"); |
| |
| BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
| } |
| |
| #ifdef NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| |
| BOOST_AUTO_TEST_CASE(UserConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| user daemon |
| } |
| )CONFIG"; |
| |
| configFile.parse(CONFIG, true, "test-general-config-section"); |
| |
| BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
| } |
| |
| BOOST_AUTO_TEST_CASE(GroupConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| group daemon |
| } |
| )CONFIG"; |
| |
| configFile.parse(CONFIG, true, "test-general-config-section"); |
| |
| BOOST_CHECK_EQUAL(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
| } |
| |
| BOOST_AUTO_TEST_CASE(UserAndGroupConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| user daemon |
| group daemon |
| } |
| )CONFIG"; |
| |
| configFile.parse(CONFIG, true, "test-general-config-section"); |
| |
| BOOST_CHECK_NE(PrivilegeHelper::s_normalUid, PrivilegeHelper::s_privilegedUid); |
| BOOST_CHECK_NE(PrivilegeHelper::s_normalGid, PrivilegeHelper::s_privilegedGid); |
| } |
| |
| #endif // NFD_HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| |
| BOOST_AUTO_TEST_CASE(InvalidUserConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| user |
| } |
| )CONFIG"; |
| |
| BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| ConfigFile::Error, |
| [] (const ConfigFile::Error& e) { |
| return std::strcmp(e.what(), |
| "Invalid value for 'user' in section 'general'") == 0; |
| }); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InvalidGroupConfig) |
| { |
| const std::string CONFIG = R"CONFIG( |
| general |
| { |
| group |
| } |
| )CONFIG"; |
| |
| BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| ConfigFile::Error, |
| [] (const ConfigFile::Error& e) { |
| return std::strcmp(e.what(), |
| "Invalid value for 'group' in section 'general'") == 0; |
| }); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection |
| BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| |
| } // namespace nfd::tests |