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