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 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestConfigFile, 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 | |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 239 | BOOST_AUTO_TEST_CASE(OnConfigSection) |
| 240 | { |
| 241 | ConfigFile file; |
| 242 | DummyAllSubscriber sub(file); |
| 243 | |
| 244 | std::istringstream input(CONFIG); |
| 245 | ConfigSection section; |
| 246 | boost::property_tree::read_info(input, section); |
| 247 | |
| 248 | file.parse(section, false, "dummy-config"); |
| 249 | BOOST_CHECK(sub.allCallbacksFired()); |
| 250 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 251 | |
| 252 | BOOST_AUTO_TEST_CASE(OnConfigString) |
| 253 | { |
| 254 | ConfigFile file; |
| 255 | DummyAllSubscriber sub(file); |
| 256 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 257 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 258 | |
| 259 | BOOST_CHECK(sub.allCallbacksFired()); |
| 260 | } |
| 261 | |
| 262 | BOOST_AUTO_TEST_CASE(OnConfigStringEmpty) |
| 263 | { |
| 264 | ConfigFile file; |
| 265 | DummyAllSubscriber sub(file); |
| 266 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 267 | BOOST_CHECK_THROW(file.parse(std::string(), false, "dummy-config"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 268 | BOOST_CHECK(sub.noCallbacksFired()); |
| 269 | } |
| 270 | |
| 271 | BOOST_AUTO_TEST_CASE(OnConfigStringMalformed) |
| 272 | { |
| 273 | ConfigFile file; |
| 274 | DummyAllSubscriber sub(file); |
| 275 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 276 | BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG, false, "dummy-config"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 277 | BOOST_CHECK(sub.noCallbacksFired()); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(OnConfigStringDryRun) |
| 281 | { |
| 282 | ConfigFile file; |
| 283 | DummyAllSubscriber sub(file, true); |
| 284 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 285 | file.parse(CONFIG, true, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 286 | |
| 287 | BOOST_CHECK(sub.allCallbacksFired()); |
| 288 | } |
| 289 | |
| 290 | BOOST_AUTO_TEST_CASE(OnConfigFilename) |
| 291 | { |
| 292 | ConfigFile file; |
| 293 | DummyAllSubscriber sub(file); |
| 294 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 295 | file.parse("tests/core/config_example.info", false); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 296 | |
| 297 | BOOST_CHECK(sub.allCallbacksFired()); |
| 298 | } |
| 299 | |
| 300 | BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile) |
| 301 | { |
| 302 | ConfigFile file; |
| 303 | DummyAllSubscriber sub(file); |
| 304 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 305 | 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] | 306 | |
| 307 | BOOST_CHECK(sub.noCallbacksFired()); |
| 308 | } |
| 309 | |
| 310 | BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed) |
| 311 | { |
| 312 | ConfigFile file; |
| 313 | DummyAllSubscriber sub(file); |
| 314 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 315 | 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] | 316 | |
| 317 | BOOST_CHECK(sub.noCallbacksFired()); |
| 318 | } |
| 319 | |
| 320 | BOOST_AUTO_TEST_CASE(OnConfigStreamDryRun) |
| 321 | { |
| 322 | ConfigFile file; |
| 323 | DummyAllSubscriber sub(file, true); |
| 324 | std::ifstream input; |
| 325 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 326 | input.open("tests/core/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 327 | BOOST_REQUIRE(input.is_open()); |
| 328 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 329 | file.parse(input, true, "tests/core/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 330 | |
| 331 | BOOST_CHECK(sub.allCallbacksFired()); |
| 332 | |
| 333 | input.close(); |
| 334 | } |
| 335 | |
| 336 | BOOST_AUTO_TEST_CASE(OnConfigFilenameDryRun) |
| 337 | { |
| 338 | ConfigFile file; |
| 339 | DummyAllSubscriber sub(file, true); |
| 340 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 341 | file.parse("tests/core/config_example.info", true); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 342 | BOOST_CHECK(sub.allCallbacksFired()); |
| 343 | } |
| 344 | |
| 345 | BOOST_AUTO_TEST_CASE(OnConfigReplaceSubscriber) |
| 346 | { |
| 347 | ConfigFile file; |
| 348 | DummyAllSubscriber sub1(file); |
| 349 | DummyAllSubscriber sub2(file); |
| 350 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 351 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 352 | |
| 353 | BOOST_CHECK(sub1.noCallbacksFired()); |
| 354 | BOOST_CHECK(sub2.allCallbacksFired()); |
| 355 | } |
| 356 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 357 | class MissingCallbackFixture : public BaseFixture |
| 358 | { |
| 359 | public: |
| 360 | MissingCallbackFixture() |
| 361 | : m_missingFired(false) |
| 362 | { |
| 363 | } |
| 364 | |
| 365 | void |
| 366 | checkMissingHandler(const std::string& filename, |
| 367 | const std::string& sectionName, |
| 368 | const ConfigSection& section, |
| 369 | bool isDryRun) |
| 370 | { |
| 371 | m_missingFired = true; |
| 372 | } |
| 373 | |
| 374 | protected: |
| 375 | bool m_missingFired; |
| 376 | }; |
| 377 | |
| 378 | |
| 379 | |
| 380 | BOOST_FIXTURE_TEST_CASE(OnConfigUncoveredSections, MissingCallbackFixture) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 381 | { |
| 382 | ConfigFile file; |
| 383 | |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 384 | BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error); |
| 385 | |
| 386 | ConfigFile permissiveFile(bind(&MissingCallbackFixture::checkMissingHandler, |
| 387 | this, _1, _2, _3, _4)); |
| 388 | |
| 389 | DummyOneSubscriber subA(permissiveFile, "a"); |
| 390 | |
| 391 | BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config")); |
| 392 | BOOST_CHECK(subA.allCallbacksFired()); |
| 393 | BOOST_CHECK(m_missingFired); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers) |
| 397 | { |
| 398 | ConfigFile file; |
| 399 | DummyOneSubscriber subA(file, "a"); |
| 400 | DummyOneSubscriber subB(file, "b"); |
| 401 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 402 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 403 | |
| 404 | BOOST_CHECK(subA.allCallbacksFired()); |
| 405 | BOOST_CHECK(subB.allCallbacksFired()); |
| 406 | } |
| 407 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 408 | BOOST_AUTO_TEST_SUITE_END() |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 409 | |
| 410 | } // namespace tests |
| 411 | } // namespace nfd |