Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | #include "mgmt/config-file.hpp" |
| 9 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame^] | 10 | #include "tests/test-common.hpp" |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame^] | 13 | namespace tests { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 14 | |
| 15 | NFD_LOG_INIT("ConfigFileTest"); |
| 16 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame^] | 17 | BOOST_FIXTURE_TEST_SUITE(MgmtConfigFile, BaseFixture) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 18 | |
| 19 | // a |
| 20 | // { |
| 21 | // akey "avalue" |
| 22 | // } |
| 23 | // b |
| 24 | // { |
| 25 | // bkey "bvalue" |
| 26 | // } |
| 27 | |
| 28 | const std::string CONFIG = |
| 29 | "a\n" |
| 30 | "{\n" |
| 31 | " akey \"avalue\"\n" |
| 32 | "}\n" |
| 33 | "b\n" |
| 34 | "{\n" |
| 35 | " bkey \"bvalue\"\n" |
| 36 | "}\n"; |
| 37 | |
| 38 | |
| 39 | // a |
| 40 | // { |
| 41 | // akey "avalue" |
| 42 | // } |
| 43 | // b |
| 44 | // |
| 45 | // bkey "bvalue" |
| 46 | // } |
| 47 | |
| 48 | const std::string MALFORMED_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 | // counts of the respective section counts in config_example.info |
| 59 | |
| 60 | const int CONFIG_N_A_SECTIONS = 1; |
| 61 | const int CONFIG_N_B_SECTIONS = 1; |
| 62 | |
| 63 | class DummySubscriber |
| 64 | { |
| 65 | public: |
| 66 | |
| 67 | DummySubscriber(ConfigFile& config, |
| 68 | int nASections, |
| 69 | int nBSections, |
| 70 | bool expectDryRun) |
| 71 | : m_nASections(nASections), |
| 72 | m_nBSections(nBSections), |
| 73 | m_nRemainingACallbacks(nASections), |
| 74 | m_nRemainingBCallbacks(nBSections), |
| 75 | m_expectDryRun(expectDryRun) |
| 76 | { |
| 77 | |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | onA(const ConfigSection& section, bool isDryRun) |
| 82 | { |
| 83 | // NFD_LOG_DEBUG("a"); |
| 84 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 85 | --m_nRemainingACallbacks; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void |
| 90 | onB(const ConfigSection& section, bool isDryRun) |
| 91 | { |
| 92 | // NFD_LOG_DEBUG("b"); |
| 93 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 94 | --m_nRemainingBCallbacks; |
| 95 | } |
| 96 | |
| 97 | bool |
| 98 | allCallbacksFired() const |
| 99 | { |
| 100 | return m_nRemainingACallbacks == 0 && |
| 101 | m_nRemainingBCallbacks == 0; |
| 102 | } |
| 103 | |
| 104 | bool |
| 105 | noCallbacksFired() const |
| 106 | { |
| 107 | return m_nRemainingACallbacks == m_nASections && |
| 108 | m_nRemainingBCallbacks == m_nBSections; |
| 109 | } |
| 110 | |
| 111 | virtual |
| 112 | ~DummySubscriber() |
| 113 | { |
| 114 | |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | int m_nASections; |
| 119 | int m_nBSections; |
| 120 | int m_nRemainingACallbacks; |
| 121 | int m_nRemainingBCallbacks; |
| 122 | bool m_expectDryRun; |
| 123 | }; |
| 124 | |
| 125 | class DummyAllSubscriber : public DummySubscriber |
| 126 | { |
| 127 | public: |
| 128 | DummyAllSubscriber(ConfigFile& config, bool expectDryRun=false) |
| 129 | : DummySubscriber(config, |
| 130 | CONFIG_N_A_SECTIONS, |
| 131 | CONFIG_N_B_SECTIONS, |
| 132 | expectDryRun) |
| 133 | { |
| 134 | config.addSectionHandler("a", bind(&DummySubscriber::onA, this, _1, _2)); |
| 135 | config.addSectionHandler("b", bind(&DummySubscriber::onB, this, _1, _2)); |
| 136 | } |
| 137 | |
| 138 | virtual |
| 139 | ~DummyAllSubscriber() |
| 140 | { |
| 141 | |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | class DummyOneSubscriber : public DummySubscriber |
| 146 | { |
| 147 | public: |
| 148 | DummyOneSubscriber(ConfigFile& config, |
| 149 | const std::string& sectionName, |
| 150 | bool expectDryRun=false) |
| 151 | : DummySubscriber(config, |
| 152 | (sectionName == "a"), |
| 153 | (sectionName == "b"), |
| 154 | expectDryRun) |
| 155 | { |
| 156 | if (sectionName == "a") |
| 157 | { |
| 158 | config.addSectionHandler(sectionName, bind(&DummySubscriber::onA, this, _1, _2)); |
| 159 | } |
| 160 | else if (sectionName == "b") |
| 161 | { |
| 162 | config.addSectionHandler(sectionName, bind(&DummySubscriber::onB, this, _1, _2)); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | BOOST_FAIL("Test setup error: " |
| 167 | << "Unexpected section name " |
| 168 | <<"\"" << sectionName << "\""); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | virtual |
| 174 | ~DummyOneSubscriber() |
| 175 | { |
| 176 | |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | class DummyNoSubscriber : public DummySubscriber |
| 181 | { |
| 182 | public: |
| 183 | DummyNoSubscriber(ConfigFile& config, bool expectDryRun) |
| 184 | : DummySubscriber(config, 0, 0, expectDryRun) |
| 185 | { |
| 186 | |
| 187 | } |
| 188 | |
| 189 | virtual |
| 190 | ~DummyNoSubscriber() |
| 191 | { |
| 192 | |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(OnConfigStream) |
| 197 | { |
| 198 | ConfigFile file; |
| 199 | DummyAllSubscriber sub(file); |
| 200 | std::ifstream input; |
| 201 | |
| 202 | input.open("tests/mgmt/config_example.info"); |
| 203 | BOOST_REQUIRE(input.is_open()); |
| 204 | |
| 205 | file.parse(input); |
| 206 | |
| 207 | BOOST_CHECK(sub.allCallbacksFired()); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(OnConfigStreamEmptyStream) |
| 211 | { |
| 212 | ConfigFile file; |
| 213 | DummyAllSubscriber sub(file); |
| 214 | |
| 215 | std::ifstream input; |
| 216 | |
| 217 | BOOST_CHECK_THROW(file.parse(input), ConfigFile::Error); |
| 218 | BOOST_CHECK(sub.noCallbacksFired()); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | BOOST_AUTO_TEST_CASE(OnConfigString) |
| 223 | { |
| 224 | ConfigFile file; |
| 225 | DummyAllSubscriber sub(file); |
| 226 | |
| 227 | file.parse(CONFIG); |
| 228 | |
| 229 | BOOST_CHECK(sub.allCallbacksFired()); |
| 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_CASE(OnConfigStringEmpty) |
| 233 | { |
| 234 | ConfigFile file; |
| 235 | DummyAllSubscriber sub(file); |
| 236 | |
| 237 | BOOST_CHECK_THROW(file.parse(std::string()), ConfigFile::Error); |
| 238 | BOOST_CHECK(sub.noCallbacksFired()); |
| 239 | } |
| 240 | |
| 241 | BOOST_AUTO_TEST_CASE(OnConfigStringMalformed) |
| 242 | { |
| 243 | ConfigFile file; |
| 244 | DummyAllSubscriber sub(file); |
| 245 | |
| 246 | BOOST_CHECK_THROW(file.parse(MALFORMED_CONFIG), ConfigFile::Error); |
| 247 | BOOST_CHECK(sub.noCallbacksFired()); |
| 248 | } |
| 249 | |
| 250 | BOOST_AUTO_TEST_CASE(OnConfigStringDryRun) |
| 251 | { |
| 252 | ConfigFile file; |
| 253 | DummyAllSubscriber sub(file, true); |
| 254 | |
| 255 | file.parse(CONFIG, true); |
| 256 | |
| 257 | BOOST_CHECK(sub.allCallbacksFired()); |
| 258 | } |
| 259 | |
| 260 | BOOST_AUTO_TEST_CASE(OnConfigFilename) |
| 261 | { |
| 262 | ConfigFile file; |
| 263 | DummyAllSubscriber sub(file); |
| 264 | |
| 265 | file.parse("tests/mgmt/config_example.info"); |
| 266 | |
| 267 | BOOST_CHECK(sub.allCallbacksFired()); |
| 268 | } |
| 269 | |
| 270 | BOOST_AUTO_TEST_CASE(OnConfigFilenameNoFile) |
| 271 | { |
| 272 | ConfigFile file; |
| 273 | DummyAllSubscriber sub(file); |
| 274 | |
| 275 | BOOST_CHECK_THROW(file.parse("i_made_this_up.info"), ConfigFile::Error); |
| 276 | |
| 277 | BOOST_CHECK(sub.noCallbacksFired()); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(OnConfigFilenameMalformed) |
| 281 | { |
| 282 | ConfigFile file; |
| 283 | DummyAllSubscriber sub(file); |
| 284 | |
| 285 | BOOST_CHECK_THROW(file.parse("tests/mgmt/config_malformed.info"), ConfigFile::Error); |
| 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 | |
| 296 | input.open("tests/mgmt/config_example.info"); |
| 297 | BOOST_REQUIRE(input.is_open()); |
| 298 | |
| 299 | file.parse(input, true); |
| 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 | |
| 311 | file.parse("tests/mgmt/config_example.info", true); |
| 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 | |
| 321 | file.parse(CONFIG); |
| 322 | |
| 323 | BOOST_CHECK(sub1.noCallbacksFired()); |
| 324 | BOOST_CHECK(sub2.allCallbacksFired()); |
| 325 | } |
| 326 | |
| 327 | BOOST_AUTO_TEST_CASE(OnConfigUncoveredSections) |
| 328 | { |
| 329 | ConfigFile file; |
| 330 | |
| 331 | BOOST_CHECK_THROW(file.parse(CONFIG), ConfigFile::Error); |
| 332 | } |
| 333 | |
| 334 | BOOST_AUTO_TEST_CASE(OnConfigCoveredByPartialSubscribers) |
| 335 | { |
| 336 | ConfigFile file; |
| 337 | DummyOneSubscriber subA(file, "a"); |
| 338 | DummyOneSubscriber subB(file, "b"); |
| 339 | |
| 340 | file.parse(CONFIG); |
| 341 | |
| 342 | BOOST_CHECK(subA.allCallbacksFired()); |
| 343 | BOOST_CHECK(subB.allCallbacksFired()); |
| 344 | } |
| 345 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 346 | BOOST_AUTO_TEST_SUITE_END() |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame^] | 347 | |
| 348 | } // namespace tests |
| 349 | } // namespace nfd |