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