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 { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 32 | namespace general { |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 35 | using namespace nfd::tests; |
| 36 | |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 37 | class GeneralConfigSectionFixture : public BaseFixture |
| 38 | { |
| 39 | public: |
| 40 | ~GeneralConfigSectionFixture() |
| 41 | { |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame^] | 42 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 43 | // revert changes to s_normalUid/s_normalGid, if any |
| 44 | PrivilegeHelper::s_normalUid = ::geteuid(); |
| 45 | PrivilegeHelper::s_normalGid = ::getegid(); |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame^] | 46 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
Alexander Afanasyev | 4d384fc | 2015-02-13 19:01:38 -0800 | [diff] [blame] | 47 | } |
| 48 | }; |
| 49 | |
| 50 | BOOST_FIXTURE_TEST_SUITE(MgmtGeneralConfigSection, GeneralConfigSectionFixture) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 51 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 52 | BOOST_AUTO_TEST_CASE(DefaultConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 53 | { |
| 54 | const std::string CONFIG = |
| 55 | "general\n" |
| 56 | "{\n" |
| 57 | "}\n"; |
| 58 | |
| 59 | ConfigFile configFile; |
| 60 | |
| 61 | general::setConfigFile(configFile); |
| 62 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 63 | |
| 64 | BOOST_CHECK(getRouterName().getName().empty()); |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame^] | 67 | #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(UserAndGroupConfig) |
| 70 | { |
| 71 | const std::string CONFIG = |
| 72 | "general\n" |
| 73 | "{\n" |
| 74 | " user nobody\n" |
| 75 | " group nogroup\n" |
| 76 | "}\n"; |
| 77 | |
| 78 | ConfigFile configFile; |
| 79 | |
| 80 | general::setConfigFile(configFile); |
| 81 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 82 | } |
| 83 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(NoUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 85 | { |
| 86 | const std::string CONFIG = |
| 87 | "general\n" |
| 88 | "{\n" |
| 89 | " group nogroup\n" |
| 90 | "}\n"; |
| 91 | |
| 92 | ConfigFile configFile; |
| 93 | |
| 94 | general::setConfigFile(configFile); |
| 95 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 96 | } |
| 97 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(NoGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 99 | { |
| 100 | const std::string CONFIG = |
| 101 | "general\n" |
| 102 | "{\n" |
| 103 | " user nobody\n" |
| 104 | "}\n"; |
| 105 | |
| 106 | ConfigFile configFile; |
| 107 | |
| 108 | general::setConfigFile(configFile); |
| 109 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 110 | } |
| 111 | |
Alexander Afanasyev | 66c569d | 2015-08-09 23:45:13 -0700 | [diff] [blame^] | 112 | #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE |
| 113 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 114 | static bool |
| 115 | checkExceptionMessage(const ConfigFile::Error& error, const std::string& expected) |
| 116 | { |
| 117 | return error.what() == expected; |
| 118 | } |
| 119 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE(InvalidUserConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 121 | { |
| 122 | const std::string CONFIG = |
| 123 | "general\n" |
| 124 | "{\n" |
| 125 | " user\n" |
| 126 | "}\n"; |
| 127 | |
| 128 | ConfigFile configFile; |
| 129 | general::setConfigFile(configFile); |
| 130 | |
| 131 | const std::string expected = "Invalid value for \"user\" in \"general\" section"; |
| 132 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 133 | ConfigFile::Error, |
| 134 | bind(&checkExceptionMessage, _1, expected)); |
| 135 | } |
| 136 | |
Steve DiBenedetto | b4336c2 | 2014-10-06 12:14:06 -0600 | [diff] [blame] | 137 | BOOST_AUTO_TEST_CASE(InvalidGroupConfig) |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 138 | { |
| 139 | const std::string CONFIG = |
| 140 | "general\n" |
| 141 | "{\n" |
| 142 | " group\n" |
| 143 | "}\n"; |
| 144 | |
| 145 | ConfigFile configFile; |
| 146 | general::setConfigFile(configFile); |
| 147 | |
| 148 | const std::string expected = "Invalid value for \"group\" in \"general\" section"; |
| 149 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 150 | ConfigFile::Error, |
| 151 | bind(&checkExceptionMessage, _1, expected)); |
| 152 | } |
| 153 | |
Vince Lehman | ee7c844 | 2015-07-16 16:42:19 -0500 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(RouterNameConfig) |
| 155 | { |
| 156 | const std::string CONFIG = |
| 157 | "general\n" |
| 158 | "{\n" |
| 159 | " router_name\n" |
| 160 | " {\n" |
| 161 | " network ndn\n" |
| 162 | " site edu/site\n" |
| 163 | " router router/name\n" |
| 164 | " }\n" |
| 165 | "}\n"; |
| 166 | |
| 167 | ConfigFile configFile; |
| 168 | general::setConfigFile(configFile); |
| 169 | |
| 170 | BOOST_CHECK_NO_THROW(configFile.parse(CONFIG, true, "test-general-config-section")); |
| 171 | |
| 172 | BOOST_CHECK_EQUAL(getRouterName().network, ndn::PartialName("ndn")); |
| 173 | BOOST_CHECK_EQUAL(getRouterName().site, ndn::PartialName("edu/site")); |
| 174 | BOOST_CHECK_EQUAL(getRouterName().router, ndn::PartialName("router/name")); |
| 175 | BOOST_CHECK_EQUAL(getRouterName().getName(), ndn::Name("/ndn/edu/site/%C1.Router/router/name")); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(NoNetworkConfig) |
| 179 | { |
| 180 | const std::string CONFIG = |
| 181 | "general\n" |
| 182 | "{\n" |
| 183 | " router_name\n" |
| 184 | " {\n" |
| 185 | " site edu/site\n" |
| 186 | " router router/name\n" |
| 187 | " }\n" |
| 188 | "}\n"; |
| 189 | |
| 190 | ConfigFile configFile; |
| 191 | general::setConfigFile(configFile); |
| 192 | |
| 193 | const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section"; |
| 194 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 195 | ConfigFile::Error, |
| 196 | bind(&checkExceptionMessage, _1, expected)); |
| 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE(NoSiteConfig) |
| 200 | { |
| 201 | const std::string CONFIG = |
| 202 | "general\n" |
| 203 | "{\n" |
| 204 | " router_name\n" |
| 205 | " {\n" |
| 206 | " network ndn\n" |
| 207 | " router router/name\n" |
| 208 | " }\n" |
| 209 | "}\n"; |
| 210 | |
| 211 | ConfigFile configFile; |
| 212 | general::setConfigFile(configFile); |
| 213 | |
| 214 | const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section"; |
| 215 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 216 | ConfigFile::Error, |
| 217 | bind(&checkExceptionMessage, _1, expected)); |
| 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(NoRouterConfig) |
| 221 | { |
| 222 | const std::string CONFIG = |
| 223 | "general\n" |
| 224 | "{\n" |
| 225 | " router_name\n" |
| 226 | " {\n" |
| 227 | " network ndn\n" |
| 228 | " site edu/site\n" |
| 229 | " }\n" |
| 230 | "}\n"; |
| 231 | |
| 232 | ConfigFile configFile; |
| 233 | general::setConfigFile(configFile); |
| 234 | |
| 235 | const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section"; |
| 236 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 237 | ConfigFile::Error, |
| 238 | bind(&checkExceptionMessage, _1, expected)); |
| 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(InvalidNetworkConfig) |
| 242 | { |
| 243 | const std::string CONFIG = |
| 244 | "general\n" |
| 245 | "{\n" |
| 246 | " router_name\n" |
| 247 | " {\n" |
| 248 | " network\n" |
| 249 | " site edu/site\n" |
| 250 | " router router/name\n" |
| 251 | " }\n" |
| 252 | "}\n"; |
| 253 | |
| 254 | ConfigFile configFile; |
| 255 | general::setConfigFile(configFile); |
| 256 | |
| 257 | const std::string expected = "Invalid value for \"router_name.network\" in \"general\" section"; |
| 258 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 259 | ConfigFile::Error, |
| 260 | bind(&checkExceptionMessage, _1, expected)); |
| 261 | } |
| 262 | |
| 263 | BOOST_AUTO_TEST_CASE(InvalidSiteConfig) |
| 264 | { |
| 265 | const std::string CONFIG = |
| 266 | "general\n" |
| 267 | "{\n" |
| 268 | " router_name\n" |
| 269 | " {\n" |
| 270 | " network ndn\n" |
| 271 | " site\n" |
| 272 | " router router/name\n" |
| 273 | " }\n" |
| 274 | "}\n"; |
| 275 | |
| 276 | ConfigFile configFile; |
| 277 | general::setConfigFile(configFile); |
| 278 | |
| 279 | const std::string expected = "Invalid value for \"router_name.site\" in \"general\" section"; |
| 280 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 281 | ConfigFile::Error, |
| 282 | bind(&checkExceptionMessage, _1, expected)); |
| 283 | } |
| 284 | |
| 285 | BOOST_AUTO_TEST_CASE(InvalidRouterConfig) |
| 286 | { |
| 287 | const std::string CONFIG = |
| 288 | "general\n" |
| 289 | "{\n" |
| 290 | " router_name\n" |
| 291 | " {\n" |
| 292 | " network ndn\n" |
| 293 | " site edu/site\n" |
| 294 | " router\n" |
| 295 | " }\n" |
| 296 | "}\n"; |
| 297 | |
| 298 | ConfigFile configFile; |
| 299 | general::setConfigFile(configFile); |
| 300 | |
| 301 | const std::string expected = "Invalid value for \"router_name.router\" in \"general\" section"; |
| 302 | BOOST_REQUIRE_EXCEPTION(configFile.parse(CONFIG, true, "test-general-config-section"), |
| 303 | ConfigFile::Error, |
| 304 | bind(&checkExceptionMessage, _1, expected)); |
| 305 | } |
| 306 | |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 307 | BOOST_AUTO_TEST_SUITE_END() |
| 308 | |
| 309 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 310 | } // namespace general |
Steve DiBenedetto | 24b9a64 | 2014-04-07 15:45:39 -0600 | [diff] [blame] | 311 | } // namespace nfd |