Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #include "core/config-file.hpp" |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 27 | #include "tests/test-common.hpp" |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 29 | #include <fstream> |
| 30 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 33 | |
| 34 | NFD_LOG_INIT("ConfigFileTest"); |
| 35 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, BaseFixture) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 37 | |
| 38 | // a |
| 39 | // { |
| 40 | // akey "avalue" |
| 41 | // } |
| 42 | // b |
| 43 | // { |
| 44 | // bkey "bvalue" |
| 45 | // } |
| 46 | |
| 47 | const std::string CONFIG = |
| 48 | "a\n" |
| 49 | "{\n" |
| 50 | " akey \"avalue\"\n" |
| 51 | "}\n" |
| 52 | "b\n" |
| 53 | "{\n" |
| 54 | " bkey \"bvalue\"\n" |
| 55 | "}\n"; |
| 56 | |
| 57 | |
| 58 | // a |
| 59 | // { |
| 60 | // akey "avalue" |
| 61 | // } |
| 62 | // b |
| 63 | // |
| 64 | // bkey "bvalue" |
| 65 | // } |
| 66 | |
| 67 | const std::string MALFORMED_CONFIG = |
| 68 | "a\n" |
| 69 | "{\n" |
| 70 | " akey \"avalue\"\n" |
| 71 | "}\n" |
| 72 | "b\n" |
| 73 | "\n" |
| 74 | " bkey \"bvalue\"\n" |
| 75 | "}\n"; |
| 76 | |
| 77 | // counts of the respective section counts in config_example.info |
| 78 | |
| 79 | const int CONFIG_N_A_SECTIONS = 1; |
| 80 | const int CONFIG_N_B_SECTIONS = 1; |
| 81 | |
| 82 | class DummySubscriber |
| 83 | { |
| 84 | public: |
| 85 | |
| 86 | DummySubscriber(ConfigFile& config, |
| 87 | int nASections, |
| 88 | int nBSections, |
| 89 | bool expectDryRun) |
| 90 | : m_nASections(nASections), |
| 91 | m_nBSections(nBSections), |
| 92 | m_nRemainingACallbacks(nASections), |
| 93 | m_nRemainingBCallbacks(nBSections), |
| 94 | m_expectDryRun(expectDryRun) |
| 95 | { |
| 96 | |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | onA(const ConfigSection& section, bool isDryRun) |
| 101 | { |
| 102 | // NFD_LOG_DEBUG("a"); |
| 103 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 104 | --m_nRemainingACallbacks; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void |
| 109 | onB(const ConfigSection& section, bool isDryRun) |
| 110 | { |
| 111 | // NFD_LOG_DEBUG("b"); |
| 112 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 113 | --m_nRemainingBCallbacks; |
| 114 | } |
| 115 | |
| 116 | bool |
| 117 | allCallbacksFired() const |
| 118 | { |
| 119 | return m_nRemainingACallbacks == 0 && |
| 120 | m_nRemainingBCallbacks == 0; |
| 121 | } |
| 122 | |
| 123 | bool |
| 124 | noCallbacksFired() const |
| 125 | { |
| 126 | return m_nRemainingACallbacks == m_nASections && |
| 127 | m_nRemainingBCallbacks == m_nBSections; |
| 128 | } |
| 129 | |
| 130 | virtual |
| 131 | ~DummySubscriber() |
| 132 | { |
| 133 | |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | int m_nASections; |
| 138 | int m_nBSections; |
| 139 | int m_nRemainingACallbacks; |
| 140 | int m_nRemainingBCallbacks; |
| 141 | bool m_expectDryRun; |
| 142 | }; |
| 143 | |
| 144 | class DummyAllSubscriber : public DummySubscriber |
| 145 | { |
| 146 | public: |
| 147 | DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false) |
| 148 | : DummySubscriber(config, |
| 149 | CONFIG_N_A_SECTIONS, |
| 150 | CONFIG_N_B_SECTIONS, |
| 151 | expectDryRun) |
| 152 | { |
| 153 | config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2)); |
| 154 | config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2)); |
| 155 | } |
| 156 | |
| 157 | virtual |
| 158 | ~DummyAllSubscriber() |
| 159 | { |
| 160 | |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | class DummyOneSubscriber : public DummySubscriber |
| 165 | { |
| 166 | public: |
| 167 | DummyOneSubscriber(ConfigFile& config, |
| 168 | const std::string& sectionName, |
| 169 | bool expectDryRun=false) |
| 170 | : DummySubscriber(config, |
| 171 | (sectionName == "a"), |
| 172 | (sectionName == "b"), |
| 173 | expectDryRun) |
| 174 | { |
| 175 | if (sectionName == "a") |
| 176 | { |
| 177 | config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2)); |
| 178 | } |
| 179 | else if (sectionName == "b") |
| 180 | { |
| 181 | config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2)); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | BOOST_FAIL("Test setup error: " |
| 186 | << "Unexpected section name " |
| 187 | <<"\"" << sectionName << "\""); |
| 188 | } |
| 189 | |
| 190 | } |
| 191 | |
| 192 | virtual |
| 193 | ~DummyOneSubscriber() |
| 194 | { |
| 195 | |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | class DummyNoSubscriber : public DummySubscriber |
| 200 | { |
| 201 | public: |
| 202 | DummyNoSubscriber(ConfigFile& config, bool expectDryRun) |
| 203 | : DummySubscriber(config, 0, 0, expectDryRun) |
| 204 | { |
| 205 | |
| 206 | } |
| 207 | |
| 208 | virtual |
| 209 | ~DummyNoSubscriber() |
| 210 | { |
| 211 | |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(OnConfigStream) |
| 216 | { |
| 217 | ConfigFile file; |
| 218 | DummyAllSubscriber sub(file); |
| 219 | std::ifstream input; |
| 220 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 221 | input.open("tests/core/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 222 | BOOST_REQUIRE(input.is_open()); |
| 223 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 224 | file.parse(input, false, "config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 225 | BOOST_CHECK(sub.allCallbacksFired()); |
| 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream) |
| 229 | { |
| 230 | ConfigFile file; |
| 231 | DummyAllSubscriber sub(file); |
| 232 | |
| 233 | std::ifstream input; |
| 234 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 235 | BOOST_CHECK_THROW(file.parse(input, false, "unknown"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 236 | BOOST_CHECK(sub.noCallbacksFired()); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(OnConfigString) |
| 241 | { |
| 242 | ConfigFile file; |
| 243 | DummyAllSubscriber sub(file); |
| 244 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 245 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 246 | |
| 247 | BOOST_CHECK(sub.allCallbacksFired()); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(OnConfigStringEmpty) |
| 251 | { |
| 252 | ConfigFile file; |
| 253 | DummyAllSubscriber sub(file); |
| 254 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 255 | BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 256 | BOOST_CHECK(sub.noCallbacksFired()); |
| 257 | } |
| 258 | |
| 259 | BOOST_AUTO_TEST_CASE(OnConfigStringMalformed) |
| 260 | { |
| 261 | ConfigFile file; |
| 262 | DummyAllSubscriber sub(file); |
| 263 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 264 | BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 265 | BOOST_CHECK(sub.noCallbacksFired()); |
| 266 | } |
| 267 | |
| 268 | BOOST_AUTO_TEST_CASE(OnConfigStringDryRun) |
| 269 | { |
| 270 | ConfigFile file; |
| 271 | DummyAllSubscriber sub(file, true); |
| 272 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 273 | file.parse(CONFIG, true, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 274 | |
| 275 | BOOST_CHECK(sub.allCallbacksFired()); |
| 276 | } |
| 277 | |
| 278 | BOOST_AUTO_TEST_CASE(OnConfigFilename) |
| 279 | { |
| 280 | ConfigFile file; |
| 281 | DummyAllSubscriber sub(file); |
| 282 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 283 | file.parse("tests/core/config_example.info", false); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 284 | |
| 285 | BOOST_CHECK(sub.allCallbacksFired()); |
| 286 | } |
| 287 | |
| 288 | BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile) |
| 289 | { |
| 290 | ConfigFile file; |
| 291 | DummyAllSubscriber sub(file); |
| 292 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 293 | BOOST_CHECK_THROW(file.parse("i_made_this_up.info", false), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 294 | |
| 295 | BOOST_CHECK(sub.noCallbacksFired()); |
| 296 | } |
| 297 | |
| 298 | BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed) |
| 299 | { |
| 300 | ConfigFile file; |
| 301 | DummyAllSubscriber sub(file); |
| 302 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 303 | BOOST_CHECK_THROW(file.parse("tests/core/config_malformed.info", false), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 304 | |
| 305 | BOOST_CHECK(sub.noCallbacksFired()); |
| 306 | } |
| 307 | |
| 308 | BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun) |
| 309 | { |
| 310 | ConfigFile file; |
| 311 | DummyAllSubscriber sub(file, true); |
| 312 | std::ifstream input; |
| 313 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 314 | input.open("tests/core/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 315 | BOOST_REQUIRE(input.is_open()); |
| 316 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 317 | file.parse(input, true, "tests/core/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 318 | |
| 319 | BOOST_CHECK(sub.allCallbacksFired()); |
| 320 | |
| 321 | input.close(); |
| 322 | } |
| 323 | |
| 324 | BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun) |
| 325 | { |
| 326 | ConfigFile file; |
| 327 | DummyAllSubscriber sub(file, true); |
| 328 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 329 | file.parse("tests/core/config_example.info", true); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 330 | BOOST_CHECK(sub.allCallbacksFired()); |
| 331 | } |
| 332 | |
| 333 | BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber) |
| 334 | { |
| 335 | ConfigFile file; |
| 336 | DummyAllSubscriber sub1(file); |
| 337 | DummyAllSubscriber sub2(file); |
| 338 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 339 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 340 | |
| 341 | BOOST_CHECK(sub1.noCallbacksFired()); |
| 342 | BOOST_CHECK(sub2.allCallbacksFired()); |
| 343 | } |
| 344 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame^] | 345 | class MissingCallbackFixture : public BaseFixture |
| 346 | { |
| 347 | public: |
| 348 | MissingCallbackFixture() |
| 349 | : m_missingFired(false) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | void |
| 354 | checkMissingHandler(const std::string& filename, |
| 355 | const std::string& sectionName, |
| 356 | const ConfigSection& section, |
| 357 | bool isDryRun) |
| 358 | { |
| 359 | m_missingFired = true; |
| 360 | } |
| 361 | |
| 362 | protected: |
| 363 | bool m_missingFired; |
| 364 | }; |
| 365 | |
| 366 | |
| 367 | |
| 368 | BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 369 | { |
| 370 | ConfigFile file; |
| 371 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame^] | 372 | BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error); |
| 373 | |
| 374 | ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler, |
| 375 | this, _1, _2, _3, _4)); |
| 376 | |
| 377 | DummyOneSubscriber subA(permissiveFile, "a"); |
| 378 | |
| 379 | BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config")); |
| 380 | BOOST_CHECK(subA.allCallbacksFired()); |
| 381 | BOOST_CHECK(m_missingFired); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers) |
| 385 | { |
| 386 | ConfigFile file; |
| 387 | DummyOneSubscriber subA(file, "a"); |
| 388 | DummyOneSubscriber subB(file, "b"); |
| 389 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 390 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 391 | |
| 392 | BOOST_CHECK(subA.allCallbacksFired()); |
| 393 | BOOST_CHECK(subB.allCallbacksFired()); |
| 394 | } |
| 395 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 396 | BOOST_AUTO_TEST_SUITE_END() |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 397 | |
| 398 | } // namespace tests |
| 399 | } // namespace nfd |