Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
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 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 31 | #include <cstring> |
| 32 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 33 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 34 | namespace general { |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 35 | namespace tests { |
| 36 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 37 | using namespace nfd::tests; |
| 38 | |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 39 | class GeneralConfigSectionFixture : public BaseFixture |
| 40 | { |
| 41 | public: |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 42 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 43 | ~GeneralConfigSectionFixture() |
| 44 | { |
| 45 | // revert changes to s_normalUid/s_normalGid, if any |
| 46 | PrivilegeHelper::s_normalUid = ::geteuid(); |
| 47 | PrivilegeHelper::s_normalGid = ::getegid(); |
| 48 | } |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 49 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 52 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 53 | BOOST_FIXTURE_TEST_SUITE(TestGeneralConfigSection, GeneralConfigSectionFixture) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 54 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(DefaultConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 56 | { |
| 57 | const std::string CONFIG = |
| 58 | "general\n" |
| 59 | "{\n" |
| 60 | "}\n"; |
| 61 | |
| 62 | ConfigFile configFile; |
| 63 | |
| 64 | general::setConfigFile(configFile); |
| 65 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 66 | } |
| 67 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 68 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(UserAndGroupConfig) |
| 71 | { |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 72 | SKIP_IF_NOT_SUPERUSER(); |
| 73 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 74 | const std::string CONFIG = |
| 75 | "general\n" |
| 76 | "{\n" |
| 77 | " user nobody\n" |
| 78 | " group nogroup\n" |
| 79 | "}\n"; |
| 80 | |
| 81 | ConfigFile configFile; |
| 82 | |
| 83 | general::setConfigFile(configFile); |
| 84 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 85 | } |
| 86 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(NoUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 88 | { |
Weiwei Liu | f5aee94 | 2016-03-19 07:00:42 +0000 | [diff] [blame] | 89 | SKIP_IF_NOT_SUPERUSER(); |
| 90 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 91 | const std::string CONFIG = |
| 92 | "general\n" |
| 93 | "{\n" |
| 94 | " group nogroup\n" |
| 95 | "}\n"; |
| 96 | |
| 97 | ConfigFile configFile; |
| 98 | |
| 99 | general::setConfigFile(configFile); |
| 100 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 101 | } |
| 102 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(NoGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 104 | { |
| 105 | const std::string CONFIG = |
| 106 | "general\n" |
| 107 | "{\n" |
| 108 | " user nobody\n" |
| 109 | "}\n"; |
| 110 | |
| 111 | ConfigFile configFile; |
| 112 | |
| 113 | general::setConfigFile(configFile); |
| 114 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 115 | } |
| 116 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame] | 117 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 118 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 119 | BOOST_AUTO_TEST_CASE(InvalidUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 120 | { |
| 121 | const std::string CONFIG = |
| 122 | "general\n" |
| 123 | "{\n" |
| 124 | " user\n" |
| 125 | "}\n"; |
| 126 | |
| 127 | ConfigFile configFile; |
| 128 | general::setConfigFile(configFile); |
| 129 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 130 | BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 131 | ConfigFile::Error, |
| 132 | [] (const ConfigFile::Error& e) { |
| 133 | return std::strcmp(e.what(), |
| 134 | "Invalid value for \"user\" in \"general\" section") == 0; |
| 135 | }); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 136 | } |
| 137 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 138 | BOOST_AUTO_TEST_CASE(InvalidGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 139 | { |
| 140 | const std::string CONFIG = |
| 141 | "general\n" |
| 142 | "{\n" |
| 143 | " group\n" |
| 144 | "}\n"; |
| 145 | |
| 146 | ConfigFile configFile; |
| 147 | general::setConfigFile(configFile); |
| 148 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 149 | BOOST_CHECK_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 150 | ConfigFile::Error, |
| 151 | [] (const ConfigFile::Error& e) { |
| 152 | return std::strcmp(e.what(), |
| 153 | "Invalid value for \"group\" in \"general\" section") == 0; |
| 154 | }); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Davide Pesavento | 231ddd7 | 2016-09-02 22:20:00 +0000 | [diff] [blame] | 157 | BOOST_AUTO_TEST_SUITE_END() // TestGeneralConfigSection |
| 158 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 159 | |
| 160 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 161 | } // namespace general |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 162 | } // namespace nfd |