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