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 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 26 | #include "common/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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tests { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(TestConfigFile) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 38 | const std::string CONFIG = R"CONFIG( |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 39 | a |
| 40 | { |
| 41 | akey avalue |
| 42 | } |
| 43 | b |
| 44 | { |
| 45 | bkey bvalue |
| 46 | } |
| 47 | )CONFIG"; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 48 | |
| 49 | // counts of the respective section counts in config_example.info |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 50 | constexpr int CONFIG_N_A_SECTIONS = 1; |
| 51 | constexpr int CONFIG_N_B_SECTIONS = 1; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 52 | |
| 53 | class DummySubscriber |
| 54 | { |
| 55 | public: |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 56 | DummySubscriber(ConfigFile& config, int nASections, int nBSections, bool expectDryRun) |
| 57 | : m_nASections(nASections) |
| 58 | , m_nBSections(nBSections) |
| 59 | , m_nRemainingACallbacks(nASections) |
| 60 | , m_nRemainingBCallbacks(nBSections) |
| 61 | , m_expectDryRun(expectDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 62 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
| 66 | onA(const ConfigSection& section, bool isDryRun) |
| 67 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 69 | --m_nRemainingACallbacks; |
| 70 | } |
| 71 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 72 | void |
| 73 | onB(const ConfigSection& section, bool isDryRun) |
| 74 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(isDryRun, m_expectDryRun); |
| 76 | --m_nRemainingBCallbacks; |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | allCallbacksFired() const |
| 81 | { |
| 82 | return m_nRemainingACallbacks == 0 && |
| 83 | m_nRemainingBCallbacks == 0; |
| 84 | } |
| 85 | |
| 86 | bool |
| 87 | noCallbacksFired() const |
| 88 | { |
| 89 | return m_nRemainingACallbacks == m_nASections && |
| 90 | m_nRemainingBCallbacks == m_nBSections; |
| 91 | } |
| 92 | |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 93 | private: |
| 94 | int m_nASections; |
| 95 | int m_nBSections; |
| 96 | int m_nRemainingACallbacks; |
| 97 | int m_nRemainingBCallbacks; |
| 98 | bool m_expectDryRun; |
| 99 | }; |
| 100 | |
| 101 | class DummyAllSubscriber : public DummySubscriber |
| 102 | { |
| 103 | public: |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 104 | explicit |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 105 | DummyAllSubscriber(ConfigFile& config, bool expectDryRun = false) |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 106 | : DummySubscriber(config, CONFIG_N_A_SECTIONS, CONFIG_N_B_SECTIONS, expectDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 107 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 108 | config.addSectionHandler("a", std::bind(&DummySubscriber::onA, this, _1, _2)); |
| 109 | config.addSectionHandler("b", std::bind(&DummySubscriber::onB, this, _1, _2)); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 110 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | class DummyOneSubscriber : public DummySubscriber |
| 114 | { |
| 115 | public: |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 116 | DummyOneSubscriber(ConfigFile& config, const std::string& sectionName, bool expectDryRun = false) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 117 | : DummySubscriber(config, |
| 118 | (sectionName == "a"), |
| 119 | (sectionName == "b"), |
| 120 | expectDryRun) |
| 121 | { |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 122 | if (sectionName == "a") { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 123 | config.addSectionHandler(sectionName, std::bind(&DummySubscriber::onA, this, _1, _2)); |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 124 | } |
| 125 | else if (sectionName == "b") { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 126 | config.addSectionHandler(sectionName, std::bind(&DummySubscriber::onB, this, _1, _2)); |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 127 | } |
| 128 | else { |
| 129 | BOOST_FAIL("Test setup error: Unexpected section name '" << sectionName << "'"); |
| 130 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 131 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | class DummyNoSubscriber : public DummySubscriber |
| 135 | { |
| 136 | public: |
| 137 | DummyNoSubscriber(ConfigFile& config, bool expectDryRun) |
| 138 | : DummySubscriber(config, 0, 0, expectDryRun) |
| 139 | { |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(ParseFromStream) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 144 | { |
| 145 | ConfigFile file; |
| 146 | DummyAllSubscriber sub(file); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 147 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 148 | std::ifstream input("tests/daemon/common/config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 149 | BOOST_REQUIRE(input.is_open()); |
| 150 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 151 | file.parse(input, false, "config_example.info"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 152 | BOOST_CHECK(sub.allCallbacksFired()); |
| 153 | } |
| 154 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(ParseFromEmptyStream) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 156 | { |
| 157 | ConfigFile file; |
| 158 | DummyAllSubscriber sub(file); |
| 159 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 160 | std::istringstream input; |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 161 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 162 | file.parse(input, false, "empty"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 163 | BOOST_CHECK(sub.noCallbacksFired()); |
| 164 | } |
| 165 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(ParseFromStreamDryRun) |
| 167 | { |
| 168 | ConfigFile file; |
| 169 | DummyAllSubscriber sub(file, true); |
| 170 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 171 | std::ifstream input("tests/daemon/common/config_example.info"); |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 172 | BOOST_REQUIRE(input.is_open()); |
| 173 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 174 | file.parse(input, true, "tests/daemon/common/config_example.info"); |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 175 | BOOST_CHECK(sub.allCallbacksFired()); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(ParseFromConfigSection) |
Alexander Afanasyev | c0273e3 | 2015-01-06 13:05:50 -0800 | [diff] [blame] | 179 | { |
| 180 | ConfigFile file; |
| 181 | DummyAllSubscriber sub(file); |
| 182 | |
| 183 | std::istringstream input(CONFIG); |
| 184 | ConfigSection section; |
| 185 | boost::property_tree::read_info(input, section); |
| 186 | |
| 187 | file.parse(section, false, "dummy-config"); |
| 188 | BOOST_CHECK(sub.allCallbacksFired()); |
| 189 | } |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 190 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(ParseFromString) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 192 | { |
| 193 | ConfigFile file; |
| 194 | DummyAllSubscriber sub(file); |
| 195 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 196 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 197 | BOOST_CHECK(sub.allCallbacksFired()); |
| 198 | } |
| 199 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 200 | BOOST_AUTO_TEST_CASE(ParseFromEmptyString) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 201 | { |
| 202 | ConfigFile file; |
| 203 | DummyAllSubscriber sub(file); |
| 204 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 205 | file.parse("", false, "empty"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 206 | BOOST_CHECK(sub.noCallbacksFired()); |
| 207 | } |
| 208 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 209 | BOOST_AUTO_TEST_CASE(ParseFromMalformedString) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 210 | { |
| 211 | ConfigFile file; |
| 212 | DummyAllSubscriber sub(file); |
| 213 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 214 | const std::string malformed = R"CONFIG( |
| 215 | a |
| 216 | { |
| 217 | akey avalue |
| 218 | } |
| 219 | b |
| 220 | bkey bvalue |
| 221 | } |
| 222 | )CONFIG"; |
| 223 | |
| 224 | BOOST_CHECK_THROW(file.parse(malformed, false, "dummy-config"), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 225 | BOOST_CHECK(sub.noCallbacksFired()); |
| 226 | } |
| 227 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 228 | BOOST_AUTO_TEST_CASE(ParseFromStringDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 229 | { |
| 230 | ConfigFile file; |
| 231 | DummyAllSubscriber sub(file, true); |
| 232 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 233 | file.parse(CONFIG, true, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 234 | BOOST_CHECK(sub.allCallbacksFired()); |
| 235 | } |
| 236 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 237 | BOOST_AUTO_TEST_CASE(ParseFromFilename) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 238 | { |
| 239 | ConfigFile file; |
| 240 | DummyAllSubscriber sub(file); |
| 241 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 242 | file.parse("tests/daemon/common/config_example.info", false); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 243 | BOOST_CHECK(sub.allCallbacksFired()); |
| 244 | } |
| 245 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 246 | BOOST_AUTO_TEST_CASE(ParseFromFilenameNonExistent) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 247 | { |
| 248 | ConfigFile file; |
| 249 | DummyAllSubscriber sub(file); |
| 250 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 251 | 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] | 252 | BOOST_CHECK(sub.noCallbacksFired()); |
| 253 | } |
| 254 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 255 | BOOST_AUTO_TEST_CASE(ParseFromFilenameMalformed) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 256 | { |
| 257 | ConfigFile file; |
| 258 | DummyAllSubscriber sub(file); |
| 259 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 260 | BOOST_CHECK_THROW(file.parse("tests/daemon/common/config_malformed.info", false), ConfigFile::Error); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 261 | BOOST_CHECK(sub.noCallbacksFired()); |
| 262 | } |
| 263 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 264 | BOOST_AUTO_TEST_CASE(ParseFromFilenameDryRun) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 265 | { |
| 266 | ConfigFile file; |
| 267 | DummyAllSubscriber sub(file, true); |
| 268 | |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 269 | file.parse("tests/daemon/common/config_example.info", true); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 270 | BOOST_CHECK(sub.allCallbacksFired()); |
| 271 | } |
| 272 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 273 | BOOST_AUTO_TEST_CASE(ReplaceSubscriber) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 274 | { |
| 275 | ConfigFile file; |
| 276 | DummyAllSubscriber sub1(file); |
| 277 | DummyAllSubscriber sub2(file); |
| 278 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 279 | file.parse(CONFIG, false, "dummy-config"); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 280 | |
| 281 | BOOST_CHECK(sub1.noCallbacksFired()); |
| 282 | BOOST_CHECK(sub2.allCallbacksFired()); |
| 283 | } |
| 284 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 285 | class MissingCallbackFixture |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 286 | { |
| 287 | public: |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 288 | void |
| 289 | checkMissingHandler(const std::string& filename, |
| 290 | const std::string& sectionName, |
| 291 | const ConfigSection& section, |
| 292 | bool isDryRun) |
| 293 | { |
| 294 | m_missingFired = true; |
| 295 | } |
| 296 | |
| 297 | protected: |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 298 | bool m_missingFired = false; |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 299 | }; |
| 300 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 301 | BOOST_FIXTURE_TEST_CASE(UncoveredSections, MissingCallbackFixture) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 302 | { |
| 303 | ConfigFile file; |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 304 | BOOST_REQUIRE_THROW(file.parse(CONFIG, false, "dummy-config"), ConfigFile::Error); |
| 305 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 306 | ConfigFile permissiveFile(std::bind(&MissingCallbackFixture::checkMissingHandler, |
| 307 | this, _1, _2, _3, _4)); |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 308 | DummyOneSubscriber subA(permissiveFile, "a"); |
| 309 | |
| 310 | BOOST_REQUIRE_NO_THROW(permissiveFile.parse(CONFIG, false, "dummy-config")); |
| 311 | BOOST_CHECK(subA.allCallbacksFired()); |
| 312 | BOOST_CHECK(m_missingFired); |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Davide Pesavento | 8524437 | 2018-02-03 19:10:55 -0500 | [diff] [blame] | 315 | BOOST_AUTO_TEST_CASE(CoveredByPartialSubscribers) |
Steve DiBenedetto | bb75b55 | 2014-02-08 12:12:11 -0700 | [diff] [blame] | 316 | { |
| 317 | ConfigFile file; |
| 318 | DummyOneSubscriber subA(file, "a"); |
| 319 | DummyOneSubscriber subB(file, "b"); |
| 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(subA.allCallbacksFired()); |
| 324 | BOOST_CHECK(subB.allCallbacksFired()); |
| 325 | } |
| 326 | |
Eric Newberry | cb27d91 | 2020-04-08 19:38:18 -0700 | [diff] [blame] | 327 | BOOST_AUTO_TEST_CASE(ParseNumber) |
| 328 | { |
| 329 | std::istringstream input(R"CONFIG( |
| 330 | a -125 |
| 331 | b 156 |
| 332 | c 100000 |
| 333 | d efg |
| 334 | e 123.456e-10 |
| 335 | f 123abc |
| 336 | g "" |
| 337 | h " -1" |
| 338 | )CONFIG"); |
| 339 | ConfigSection section; |
| 340 | boost::property_tree::read_info(input, section); |
| 341 | |
| 342 | // Read various types and ensure they return the correct value |
| 343 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<short>(section.get_child("a"), "a", "section"), -125); |
| 344 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<int>(section.get_child("a"), "a", "section"), -125); |
| 345 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<long>(section.get_child("a"), "a", "section"), -125); |
| 346 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<double>(section.get_child("a"), "a", "section"), -125.0); |
| 347 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<float>(section.get_child("a"), "a", "section"), -125.0); |
| 348 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<short>(section.get_child("b"), "b", "section"), 156); |
| 349 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<int>(section.get_child("b"), "b", "section"), 156); |
| 350 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<long>(section.get_child("b"), "b", "section"), 156); |
| 351 | BOOST_CHECK_CLOSE(ConfigFile::parseNumber<double>(section.get_child("e"), "e", "section"), 123.456e-10, 0.0001); |
| 352 | BOOST_CHECK_CLOSE(ConfigFile::parseNumber<float>(section.get_child("e"), "e", "section"), 123.456e-10, 0.0001); |
| 353 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<int>(section.get_child("h"), "h", "section"), -1); |
| 354 | |
| 355 | // Check throw on out of range |
| 356 | BOOST_CHECK_THROW(ConfigFile::parseNumber<int16_t>(section.get_child("c"), "c", "section"), |
| 357 | ConfigFile::Error); |
| 358 | |
| 359 | // Check throw on non-integer for integer types |
| 360 | BOOST_CHECK_THROW(ConfigFile::parseNumber<int>(section.get_child("d"), "d", "section"), |
| 361 | ConfigFile::Error); |
| 362 | BOOST_CHECK_THROW(ConfigFile::parseNumber<int>(section.get_child("e"), "e", "section"), |
| 363 | ConfigFile::Error); |
| 364 | BOOST_CHECK_THROW(ConfigFile::parseNumber<int>(section.get_child("f"), "f", "section"), |
| 365 | ConfigFile::Error); |
| 366 | BOOST_CHECK_THROW(ConfigFile::parseNumber<int>(section.get_child("g"), "g", "section"), |
| 367 | ConfigFile::Error); |
| 368 | |
| 369 | // Should throw exception if try to read unsigned from negative value |
| 370 | BOOST_CHECK_THROW(ConfigFile::parseNumber<uint16_t>(section.get_child("a"), "a", "section"), |
| 371 | ConfigFile::Error); |
| 372 | BOOST_CHECK_EQUAL(ConfigFile::parseNumber<uint16_t>(section.get_child("b"), "b", "section"), 156); |
| 373 | BOOST_CHECK_THROW(ConfigFile::parseNumber<uint32_t>(section.get_child("h"), "h", "section"), |
| 374 | ConfigFile::Error); |
| 375 | } |
| 376 | |
Junxiao Shi | a6286a9 | 2021-02-23 06:43:52 -0700 | [diff] [blame] | 377 | BOOST_AUTO_TEST_CASE(CheckRange) |
| 378 | { |
| 379 | { |
| 380 | uint32_t value = 8000; |
| 381 | uint32_t min = 8000; |
| 382 | uint32_t max = 8999; |
| 383 | ConfigFile::checkRange(value, min, max, "key", "section"); |
| 384 | } |
| 385 | |
| 386 | { |
| 387 | size_t value = 8999; |
| 388 | size_t min = 8000; |
| 389 | size_t max = 8999; |
| 390 | ConfigFile::checkRange(value, min, max, "key", "section"); |
| 391 | } |
| 392 | |
| 393 | { |
| 394 | int64_t value = -7000; |
| 395 | int64_t min = -7999; |
| 396 | int64_t max = 1000; |
| 397 | ConfigFile::checkRange(value, min, max, "key", "section"); |
| 398 | } |
| 399 | |
| 400 | { |
| 401 | uint32_t value = 7999; |
| 402 | uint32_t min = 8000; |
| 403 | uint32_t max = 8999; |
| 404 | BOOST_CHECK_THROW(ConfigFile::checkRange(value, min, max, "key", "section"), ConfigFile::Error); |
| 405 | } |
| 406 | |
| 407 | { |
| 408 | int16_t value = 9000; |
| 409 | int16_t min = 8000; |
| 410 | int16_t max = 8999; |
| 411 | BOOST_CHECK_THROW(ConfigFile::checkRange(value, min, max, "key", "section"), ConfigFile::Error); |
| 412 | } |
| 413 | |
| 414 | { |
| 415 | int32_t value = -8000; |
| 416 | int32_t min = -7999; |
| 417 | int32_t max = 1000; |
| 418 | BOOST_CHECK_THROW(ConfigFile::checkRange(value, min, max, "key", "section"), ConfigFile::Error); |
| 419 | } |
| 420 | |
| 421 | { |
| 422 | int64_t value = 0x1001; |
| 423 | int64_t min = -0x7fff; |
| 424 | int64_t max = 0x1000; |
| 425 | BOOST_CHECK_THROW(ConfigFile::checkRange(value, min, max, "key", "section"), ConfigFile::Error); |
| 426 | } |
| 427 | } |
| 428 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 429 | BOOST_AUTO_TEST_SUITE_END() // TestConfigFile |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 430 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 431 | } // namespace nfd::tests |