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