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