Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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" |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 27 | #include "core/privilege-helper.hpp" |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 34 | class GeneralConfigSectionFixture : public BaseFixture |
| 35 | { |
| 36 | public: |
| 37 | ~GeneralConfigSectionFixture() |
| 38 | { |
| 39 | // revert changes to s_normalUid/s_normalGid, if any |
| 40 | PrivilegeHelper::s_normalUid = ::geteuid(); |
| 41 | PrivilegeHelper::s_normalGid = ::getegid(); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | BOOST_FIXTURE_TEST_SUITE(MgmtGeneralConfigSection, GeneralConfigSectionFixture) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 46 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE(UserAndGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 48 | { |
| 49 | const std::string CONFIG = |
| 50 | "general\n" |
| 51 | "{\n" |
| 52 | " user nobody\n" |
| 53 | " group nogroup\n" |
| 54 | "}\n"; |
| 55 | |
| 56 | ConfigFile configFile; |
| 57 | |
| 58 | general::setConfigFile(configFile); |
| 59 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 60 | |
| 61 | } |
| 62 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 63 | BOOST_AUTO_TEST_CASE(DefaultConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 64 | { |
| 65 | const std::string CONFIG = |
| 66 | "general\n" |
| 67 | "{\n" |
| 68 | "}\n"; |
| 69 | |
| 70 | ConfigFile configFile; |
| 71 | |
| 72 | general::setConfigFile(configFile); |
| 73 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 74 | } |
| 75 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 76 | BOOST_AUTO_TEST_CASE(NoUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 77 | { |
| 78 | const std::string CONFIG = |
| 79 | "general\n" |
| 80 | "{\n" |
| 81 | " group nogroup\n" |
| 82 | "}\n"; |
| 83 | |
| 84 | ConfigFile configFile; |
| 85 | |
| 86 | general::setConfigFile(configFile); |
| 87 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 88 | } |
| 89 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 90 | BOOST_AUTO_TEST_CASE(NoGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 91 | { |
| 92 | const std::string CONFIG = |
| 93 | "general\n" |
| 94 | "{\n" |
| 95 | " user nobody\n" |
| 96 | "}\n"; |
| 97 | |
| 98 | ConfigFile configFile; |
| 99 | |
| 100 | general::setConfigFile(configFile); |
| 101 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 102 | } |
| 103 | |
| 104 | static bool |
| 105 | checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected) |
| 106 | { |
| 107 | return error.what() == expected; |
| 108 | } |
| 109 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 110 | BOOST_AUTO_TEST_CASE(InvalidUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 111 | { |
| 112 | const std::string CONFIG = |
| 113 | "general\n" |
| 114 | "{\n" |
| 115 | " user\n" |
| 116 | "}\n"; |
| 117 | |
| 118 | ConfigFile configFile; |
| 119 | general::setConfigFile(configFile); |
| 120 | |
| 121 | const std::string expected = "Invalid value for \"user\" in \"general\" section"; |
| 122 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 123 | ConfigFile::Error, |
| 124 | bind(&checkExceptionMessage, _1, expected)); |
| 125 | } |
| 126 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 127 | BOOST_AUTO_TEST_CASE(InvalidGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 128 | { |
| 129 | const std::string CONFIG = |
| 130 | "general\n" |
| 131 | "{\n" |
| 132 | " group\n" |
| 133 | "}\n"; |
| 134 | |
| 135 | ConfigFile configFile; |
| 136 | general::setConfigFile(configFile); |
| 137 | |
| 138 | const std::string expected = "Invalid value for \"group\" in \"general\" section"; |
| 139 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 140 | ConfigFile::Error, |
| 141 | bind(&checkExceptionMessage, _1, expected)); |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_SUITE_END() |
| 145 | |
| 146 | } // namespace tests |
| 147 | |
| 148 | } // namespace nfd |