Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -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 | 3a4f83d | 2014-06-02 14:58:54 -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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "mgmt/tables-config-section.hpp" |
| 27 | #include "fw/forwarder.hpp" |
| 28 | |
| 29 | |
| 30 | #include "tests/test-common.hpp" |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 31 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
| 34 | namespace tests { |
| 35 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 36 | NFD_LOG_INIT("MgmtTablesConfigSection"); |
| 37 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 38 | class TablesConfigSectionFixture : protected BaseFixture |
| 39 | { |
| 40 | public: |
| 41 | |
| 42 | TablesConfigSectionFixture() |
| 43 | : m_cs(m_forwarder.getCs()) |
| 44 | , m_pit(m_forwarder.getPit()) |
| 45 | , m_fib(m_forwarder.getFib()) |
| 46 | , m_strategyChoice(m_forwarder.getStrategyChoice()) |
| 47 | , m_measurements(m_forwarder.getMeasurements()) |
| 48 | , m_tablesConfig(m_cs, m_pit, m_fib, m_strategyChoice, m_measurements) |
| 49 | { |
| 50 | m_tablesConfig.setConfigFile(m_config); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | runConfig(const std::string& CONFIG, bool isDryRun) |
| 55 | { |
| 56 | m_config.parse(CONFIG, isDryRun, "dummy-config"); |
| 57 | } |
| 58 | |
| 59 | bool |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 60 | validateException(const std::runtime_error& exception, const std::string& expectedMsg) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 61 | { |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 62 | if (exception.what() != expectedMsg) |
| 63 | { |
| 64 | NFD_LOG_DEBUG("exception.what(): " << exception.what()); |
| 65 | NFD_LOG_DEBUG("msg: : " << expectedMsg); |
| 66 | |
| 67 | return false; |
| 68 | } |
| 69 | return true; |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | protected: |
| 73 | Forwarder m_forwarder; |
| 74 | |
| 75 | Cs& m_cs; |
| 76 | Pit& m_pit; |
| 77 | Fib& m_fib; |
| 78 | StrategyChoice& m_strategyChoice; |
| 79 | Measurements& m_measurements; |
| 80 | |
| 81 | TablesConfigSection m_tablesConfig; |
| 82 | ConfigFile m_config; |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 83 | }; |
| 84 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 85 | BOOST_FIXTURE_TEST_SUITE(MgmtTableConfigSection, TablesConfigSectionFixture) |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 86 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(ConfigureTablesWithDefaults) |
| 88 | { |
| 89 | const size_t initialLimit = m_cs.getLimit(); |
| 90 | |
| 91 | m_tablesConfig.ensureTablesAreConfigured(); |
| 92 | BOOST_CHECK_NE(initialLimit, m_cs.getLimit()); |
| 93 | } |
| 94 | |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(EmptyTablesSection) |
| 96 | { |
| 97 | const std::string CONFIG = |
| 98 | "tables\n" |
| 99 | "{\n" |
| 100 | "}\n"; |
| 101 | |
| 102 | const size_t nCsMaxPackets = m_cs.getLimit(); |
| 103 | |
| 104 | BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, true)); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(m_cs.getLimit(), nCsMaxPackets); |
| 106 | |
| 107 | BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, false)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 108 | BOOST_CHECK_NE(m_cs.getLimit(), nCsMaxPackets); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 109 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 110 | const size_t defaultLimit = m_cs.getLimit(); |
| 111 | |
| 112 | m_tablesConfig.ensureTablesAreConfigured(); |
| 113 | BOOST_CHECK_EQUAL(defaultLimit, m_cs.getLimit()); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(ValidCsMaxPackets) |
| 117 | { |
| 118 | const std::string CONFIG = |
| 119 | "tables\n" |
| 120 | "{\n" |
| 121 | " cs_max_packets 101\n" |
| 122 | "}\n"; |
| 123 | |
| 124 | BOOST_REQUIRE_NE(m_cs.getLimit(), 101); |
| 125 | |
| 126 | BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, true)); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 127 | BOOST_CHECK_NE(m_cs.getLimit(), 101); |
| 128 | |
| 129 | BOOST_REQUIRE_NO_THROW(runConfig(CONFIG, false)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 101); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 131 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 132 | m_tablesConfig.ensureTablesAreConfigured(); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 101); |
| 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(MissingValueCsMaxPackets) |
| 137 | { |
| 138 | const std::string CONFIG = |
| 139 | "tables\n" |
| 140 | "{\n" |
| 141 | " cs_max_packets\n" |
| 142 | "}\n"; |
| 143 | |
| 144 | const std::string expectedMsg = |
| 145 | "Invalid value for option \"cs_max_packets\" in \"tables\" section"; |
| 146 | |
| 147 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true), |
| 148 | ConfigFile::Error, |
| 149 | bind(&TablesConfigSectionFixture::validateException, |
| 150 | this, _1, expectedMsg)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 151 | |
| 152 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false), |
| 153 | ConfigFile::Error, |
| 154 | bind(&TablesConfigSectionFixture::validateException, |
| 155 | this, _1, expectedMsg)); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | BOOST_AUTO_TEST_CASE(InvalidValueCsMaxPackets) |
| 159 | { |
| 160 | const std::string CONFIG = |
| 161 | "tables\n" |
| 162 | "{\n" |
| 163 | " cs_max_packets invalid\n" |
| 164 | "}\n"; |
| 165 | |
| 166 | const std::string expectedMsg = |
| 167 | "Invalid value for option \"cs_max_packets\" in \"tables\" section"; |
| 168 | |
| 169 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true), |
| 170 | ConfigFile::Error, |
| 171 | bind(&TablesConfigSectionFixture::validateException, |
| 172 | this, _1, expectedMsg)); |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 173 | |
| 174 | |
| 175 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false), |
| 176 | ConfigFile::Error, |
| 177 | bind(&TablesConfigSectionFixture::validateException, |
| 178 | this, _1, expectedMsg)); |
| 179 | } |
| 180 | |
Steve DiBenedetto | c0640f5 | 2014-11-03 15:55:43 -0700 | [diff] [blame] | 181 | BOOST_AUTO_TEST_CASE(ConfigStrategy) |
| 182 | { |
| 183 | const std::string CONFIG = |
| 184 | "tables\n" |
| 185 | "{\n" |
| 186 | "strategy_choice\n" |
| 187 | "{\n" |
| 188 | " / /localhost/nfd/strategy/test-strategy-a\n" |
| 189 | " /a /localhost/nfd/strategy/test-strategy-b\n" |
| 190 | "}\n" |
| 191 | "}\n"; |
| 192 | |
| 193 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 194 | "/localhost/nfd/strategy/test-strategy-a")); |
| 195 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 196 | "/localhost/nfd/strategy/test-strategy-b")); |
| 197 | |
| 198 | runConfig(CONFIG, true); |
| 199 | { |
| 200 | fw::Strategy& rootStrategy = m_strategyChoice.findEffectiveStrategy("/"); |
| 201 | BOOST_REQUIRE_NE(rootStrategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 202 | BOOST_REQUIRE_NE(rootStrategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
| 203 | |
| 204 | fw::Strategy& aStrategy = m_strategyChoice.findEffectiveStrategy("/a"); |
| 205 | BOOST_REQUIRE_NE(aStrategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
| 206 | BOOST_REQUIRE_NE(aStrategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 207 | } |
| 208 | |
| 209 | runConfig(CONFIG, false); |
| 210 | { |
| 211 | fw::Strategy& rootStrategy = m_strategyChoice.findEffectiveStrategy("/"); |
| 212 | BOOST_REQUIRE_EQUAL(rootStrategy.getName(), "/localhost/nfd/strategy/test-strategy-a"); |
| 213 | |
| 214 | fw::Strategy& aStrategy = m_strategyChoice.findEffectiveStrategy("/a"); |
| 215 | BOOST_REQUIRE_EQUAL(aStrategy.getName(), "/localhost/nfd/strategy/test-strategy-b"); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_CASE(ConfigVersionedStrategy) |
| 220 | { |
| 221 | const std::string CONFIG = |
| 222 | "tables\n" |
| 223 | "{\n" |
| 224 | "strategy_choice\n" |
| 225 | "{\n" |
| 226 | " /test/latest /localhost/nfd/strategy/test-strategy-a\n" |
| 227 | " /test/old /localhost/nfd/strategy/test-strategy-a/%FD%01\n" |
| 228 | "}\n" |
| 229 | "}\n"; |
| 230 | |
| 231 | |
| 232 | auto version1 = make_shared<DummyStrategy>(ref(m_forwarder), |
| 233 | "/localhost/nfd/strategy/test-strategy-a/%FD%01"); |
| 234 | |
| 235 | auto version2 = make_shared<DummyStrategy>(ref(m_forwarder), |
| 236 | "/localhost/nfd/strategy/test-strategy-a/%FD%02"); |
| 237 | m_strategyChoice.install(version1); |
| 238 | m_strategyChoice.install(version2); |
| 239 | |
| 240 | runConfig(CONFIG, true); |
| 241 | { |
| 242 | fw::Strategy& testLatestStrategy = m_strategyChoice.findEffectiveStrategy("/test/latest"); |
| 243 | BOOST_REQUIRE_NE(testLatestStrategy.getName(), |
| 244 | "/localhost/nfd/strategy/test-strategy-a/%FD%01"); |
| 245 | BOOST_REQUIRE_NE(testLatestStrategy.getName(), |
| 246 | "/localhost/nfd/strategy/test-strategy-a/%FD%02"); |
| 247 | |
| 248 | fw::Strategy& testOldStrategy = m_strategyChoice.findEffectiveStrategy("/test/old"); |
| 249 | BOOST_REQUIRE_NE(testOldStrategy.getName(), |
| 250 | "/localhost/nfd/strategy/test-strategy-a/%FD%01"); |
| 251 | BOOST_REQUIRE_NE(testOldStrategy.getName(), |
| 252 | "/localhost/nfd/strategy/test-strategy-a/%FD%02"); |
| 253 | } |
| 254 | |
| 255 | runConfig(CONFIG, false); |
| 256 | { |
| 257 | fw::Strategy& testLatestStrategy = m_strategyChoice.findEffectiveStrategy("/test/latest"); |
| 258 | BOOST_REQUIRE_EQUAL(testLatestStrategy.getName(), |
| 259 | "/localhost/nfd/strategy/test-strategy-a/%FD%02"); |
| 260 | |
| 261 | fw::Strategy& testOldStrategy = m_strategyChoice.findEffectiveStrategy("/test/old"); |
| 262 | BOOST_REQUIRE_EQUAL(testOldStrategy.getName(), |
| 263 | "/localhost/nfd/strategy/test-strategy-a/%FD%01"); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE(InvalidStrategy) |
| 268 | { |
| 269 | const std::string CONFIG = |
| 270 | "tables\n" |
| 271 | "{\n" |
| 272 | "strategy_choice\n" |
| 273 | "{\n" |
| 274 | " / /localhost/nfd/strategy/test-doesnotexist\n" |
| 275 | "}\n" |
| 276 | "}\n"; |
| 277 | |
| 278 | |
| 279 | const std::string expectedMsg = |
| 280 | "Invalid strategy choice \"/localhost/nfd/strategy/test-doesnotexist\" " |
| 281 | "for prefix \"/\" in \"strategy_choice\" section"; |
| 282 | |
| 283 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true), |
| 284 | ConfigFile::Error, |
| 285 | bind(&TablesConfigSectionFixture::validateException, |
| 286 | this, _1, expectedMsg)); |
| 287 | |
| 288 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false), |
| 289 | ConfigFile::Error, |
| 290 | bind(&TablesConfigSectionFixture::validateException, |
| 291 | this, _1, expectedMsg)); |
| 292 | } |
| 293 | |
| 294 | BOOST_AUTO_TEST_CASE(MissingStrategyPrefix) |
| 295 | { |
| 296 | const std::string CONFIG = |
| 297 | "tables\n" |
| 298 | "{\n" |
| 299 | "strategy_choice\n" |
| 300 | "{\n" |
| 301 | " /localhost/nfd/strategy/test-strategy-a\n" |
| 302 | "}\n" |
| 303 | "}\n"; |
| 304 | |
| 305 | |
| 306 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 307 | "/localhost/nfd/strategy/test-strategy-a")); |
| 308 | |
| 309 | |
| 310 | const std::string expectedMsg = "Invalid strategy choice \"\" for prefix " |
| 311 | "\"/localhost/nfd/strategy/test-strategy-a\" in \"strategy_choice\" section"; |
| 312 | |
| 313 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true), |
| 314 | ConfigFile::Error, |
| 315 | bind(&TablesConfigSectionFixture::validateException, |
| 316 | this, _1, expectedMsg)); |
| 317 | |
| 318 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false), |
| 319 | ConfigFile::Error, |
| 320 | bind(&TablesConfigSectionFixture::validateException, |
| 321 | this, _1, expectedMsg)); |
| 322 | } |
| 323 | |
| 324 | BOOST_AUTO_TEST_CASE(DuplicateStrategy) |
| 325 | { |
| 326 | const std::string CONFIG = |
| 327 | "tables\n" |
| 328 | "{\n" |
| 329 | "strategy_choice\n" |
| 330 | "{\n" |
| 331 | " / /localhost/nfd/strategy/test-strategy-a\n" |
| 332 | " /a /localhost/nfd/strategy/test-strategy-b\n" |
| 333 | " / /localhost/nfd/strategy/test-strategy-b\n" |
| 334 | "}\n" |
| 335 | "}\n"; |
| 336 | |
| 337 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 338 | "/localhost/nfd/strategy/test-strategy-a")); |
| 339 | m_strategyChoice.install(make_shared<DummyStrategy>(ref(m_forwarder), |
| 340 | "/localhost/nfd/strategy/test-strategy-b")); |
| 341 | |
| 342 | const std::string expectedMsg = |
| 343 | "Duplicate strategy choice for prefix \"/\" in \"strategy_choice\" section"; |
| 344 | |
| 345 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, true), |
| 346 | ConfigFile::Error, |
| 347 | bind(&TablesConfigSectionFixture::validateException, |
| 348 | this, _1, expectedMsg)); |
| 349 | |
| 350 | BOOST_CHECK_EXCEPTION(runConfig(CONFIG, false), |
| 351 | ConfigFile::Error, |
| 352 | bind(&TablesConfigSectionFixture::validateException, |
| 353 | this, _1, expectedMsg)); |
| 354 | } |
| 355 | |
Steve DiBenedetto | 9bcc88f | 2014-07-08 09:52:13 -0600 | [diff] [blame] | 356 | class IgnoreNotTablesSection |
| 357 | { |
| 358 | public: |
| 359 | void |
| 360 | operator()(const std::string& filename, |
| 361 | const std::string& sectionName, |
| 362 | const ConfigSection& section, |
| 363 | bool isDryRun) |
| 364 | |
| 365 | { |
| 366 | // Ignore "not_tables" section |
| 367 | if (sectionName == "not_tables") |
| 368 | { |
| 369 | // do nothing |
| 370 | } |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | BOOST_AUTO_TEST_CASE(MissingTablesSection) |
| 375 | { |
| 376 | const std::string CONFIG = |
| 377 | "not_tables\n" |
| 378 | "{\n" |
| 379 | " some_other_field 0\n" |
| 380 | "}\n"; |
| 381 | |
| 382 | ConfigFile passiveConfig((IgnoreNotTablesSection())); |
| 383 | |
| 384 | const size_t initialLimit = m_cs.getLimit(); |
| 385 | |
| 386 | passiveConfig.parse(CONFIG, true, "dummy-config"); |
| 387 | BOOST_REQUIRE_EQUAL(initialLimit, m_cs.getLimit()); |
| 388 | |
| 389 | passiveConfig.parse(CONFIG, false, "dummy-config"); |
| 390 | BOOST_REQUIRE_EQUAL(initialLimit, m_cs.getLimit()); |
| 391 | |
| 392 | m_tablesConfig.ensureTablesAreConfigured(); |
| 393 | BOOST_CHECK_NE(initialLimit, m_cs.getLimit()); |
Steve DiBenedetto | 3a4f83d | 2014-06-02 14:58:54 -0600 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_SUITE_END() |
| 397 | |
| 398 | } // namespace tests |
| 399 | } // namespace nfd |