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