Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -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 | #include "mgmt/command-validator.hpp" |
| 8 | #include "mgmt/config-file.hpp" |
| 9 | |
| 10 | #include "tests/test-common.hpp" |
| 11 | |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | #include <ndn-cpp-dev/util/command-interest-generator.hpp> |
| 14 | #include <ndn-cpp-dev/util/io.hpp> |
| 15 | #include <boost/filesystem.hpp> |
| 16 | |
| 17 | namespace nfd { |
| 18 | |
| 19 | namespace tests { |
| 20 | |
| 21 | NFD_LOG_INIT("CommandValidatorTest"); |
| 22 | |
| 23 | BOOST_FIXTURE_TEST_SUITE(MgmtCommandValidator, BaseFixture) |
| 24 | |
| 25 | // authorizations |
| 26 | // { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 27 | // authorize |
| 28 | // { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 29 | // certfile "tests/mgmt/cert1.ndncert" |
| 30 | // privileges |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 31 | // { |
| 32 | // fib |
| 33 | // stats |
| 34 | // } |
| 35 | // } |
| 36 | |
| 37 | // authorize |
| 38 | // { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 39 | // certfile "tests/mgmt/cert2.ndncert" |
| 40 | // privileges |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 41 | // { |
| 42 | // faces |
| 43 | // } |
| 44 | // } |
| 45 | // } |
| 46 | |
| 47 | const std::string CONFIG = |
| 48 | "authorizations\n" |
| 49 | "{\n" |
| 50 | " authorize\n" |
| 51 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 52 | " certfile \"tests/mgmt/cert1.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 53 | " privileges\n" |
| 54 | " {\n" |
| 55 | " fib\n" |
| 56 | " stats\n" |
| 57 | " }\n" |
| 58 | " }\n" |
| 59 | " authorize\n" |
| 60 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 61 | " certfile \"tests/mgmt/cert2.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 62 | " privileges\n" |
| 63 | " {\n" |
| 64 | " faces\n" |
| 65 | " }\n" |
| 66 | " }\n" |
| 67 | "}\n"; |
| 68 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 69 | const boost::filesystem::path CONFIG_PATH = |
| 70 | boost::filesystem::current_path() /= std::string("unit-test-nfd.conf"); |
| 71 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 72 | class CommandValidatorTester |
| 73 | { |
| 74 | public: |
| 75 | |
| 76 | CommandValidatorTester() |
| 77 | : m_validated(false), |
| 78 | m_validationFailed(false) |
| 79 | { |
| 80 | |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | generateIdentity(const Name& prefix) |
| 85 | { |
| 86 | m_identityName = prefix; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame^] | 87 | m_identityName.appendVersion(); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 88 | |
| 89 | const Name certName = m_keys.createIdentity(m_identityName); |
| 90 | |
| 91 | m_certificate = m_keys.getCertificate(certName); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | saveIdentityToFile(const char* filename) |
| 96 | { |
| 97 | std::ofstream out; |
| 98 | out.open(filename); |
| 99 | |
| 100 | BOOST_REQUIRE(out.is_open()); |
| 101 | BOOST_REQUIRE(static_cast<bool>(m_certificate)); |
| 102 | |
| 103 | ndn::io::save<ndn::IdentityCertificate>(*m_certificate, out); |
| 104 | |
| 105 | out.close(); |
| 106 | } |
| 107 | |
| 108 | const Name& |
| 109 | getIdentityName() const |
| 110 | { |
| 111 | BOOST_REQUIRE_NE(m_identityName, Name()); |
| 112 | return m_identityName; |
| 113 | } |
| 114 | |
| 115 | const Name& |
| 116 | getPublicKeyName() const |
| 117 | { |
| 118 | BOOST_REQUIRE(static_cast<bool>(m_certificate)); |
| 119 | return m_certificate->getPublicKeyName(); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | onValidated(const shared_ptr<const Interest>& interest) |
| 124 | { |
| 125 | // NFD_LOG_DEBUG("validated command"); |
| 126 | m_validated = true; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | onValidationFailed(const shared_ptr<const Interest>& interest, const std::string& info) |
| 131 | { |
| 132 | NFD_LOG_DEBUG("validation failed: " << info); |
| 133 | m_validationFailed = true; |
| 134 | } |
| 135 | |
| 136 | bool |
| 137 | commandValidated() const |
| 138 | { |
| 139 | return m_validated; |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | commandValidationFailed() const |
| 144 | { |
| 145 | return m_validationFailed; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | resetValidation() |
| 150 | { |
| 151 | m_validated = false; |
| 152 | m_validationFailed = false; |
| 153 | } |
| 154 | |
| 155 | ~CommandValidatorTester() |
| 156 | { |
| 157 | m_keys.deleteIdentity(m_identityName); |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | bool m_validated; |
| 162 | bool m_validationFailed; |
| 163 | |
| 164 | ndn::KeyChain m_keys; |
| 165 | Name m_identityName; |
| 166 | shared_ptr<ndn::IdentityCertificate> m_certificate; |
| 167 | }; |
| 168 | |
| 169 | class TwoValidatorFixture : public BaseFixture |
| 170 | { |
| 171 | public: |
| 172 | TwoValidatorFixture() |
| 173 | { |
| 174 | m_tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1"); |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 175 | m_tester1.saveIdentityToFile("tests/mgmt/cert1.ndncert"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 176 | |
| 177 | m_tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2"); |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 178 | m_tester2.saveIdentityToFile("tests/mgmt/cert2.ndncert"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | ~TwoValidatorFixture() |
| 182 | { |
| 183 | boost::system::error_code error; |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 184 | boost::filesystem::remove("tests/mgmt/cert1.ndncert", error); |
| 185 | boost::filesystem::remove("tests/mgmt/cert2.ndncert", error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | protected: |
| 189 | CommandValidatorTester m_tester1; |
| 190 | CommandValidatorTester m_tester2; |
| 191 | }; |
| 192 | |
| 193 | BOOST_FIXTURE_TEST_CASE(TwoKeys, TwoValidatorFixture) |
| 194 | { |
| 195 | shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert"); |
| 196 | shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething"); |
| 197 | shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create"); |
| 198 | |
| 199 | ndn::CommandInterestGenerator generator; |
| 200 | generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName()); |
| 201 | generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName()); |
| 202 | generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName()); |
| 203 | |
| 204 | ConfigFile config; |
| 205 | CommandValidator validator; |
| 206 | validator.addSupportedPrivilege("faces"); |
| 207 | validator.addSupportedPrivilege("fib"); |
| 208 | validator.addSupportedPrivilege("stats"); |
| 209 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 210 | validator.setConfigFile(config); |
| 211 | |
| 212 | config.parse(CONFIG, false, CONFIG_PATH.native()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 213 | |
| 214 | validator.validate(*fibCommand, |
| 215 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1), |
| 216 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2)); |
| 217 | |
| 218 | BOOST_REQUIRE(m_tester1.commandValidated()); |
| 219 | m_tester1.resetValidation(); |
| 220 | |
| 221 | validator.validate(*statsCommand, |
| 222 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1), |
| 223 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2)); |
| 224 | |
| 225 | BOOST_REQUIRE(m_tester1.commandValidated()); |
| 226 | |
| 227 | validator.validate(*facesCommand, |
| 228 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1), |
| 229 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2)); |
| 230 | |
| 231 | BOOST_REQUIRE(m_tester2.commandValidated()); |
| 232 | m_tester2.resetValidation(); |
| 233 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 234 | // use cert2 for fib command (authorized for cert1 only) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 235 | shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert"); |
| 236 | generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName()); |
| 237 | |
| 238 | validator.validate(*unauthorizedFibCommand, |
| 239 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1), |
| 240 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2)); |
| 241 | |
| 242 | BOOST_REQUIRE(m_tester2.commandValidationFailed()); |
| 243 | } |
| 244 | |
| 245 | BOOST_FIXTURE_TEST_CASE(TwoKeysDryRun, TwoValidatorFixture) |
| 246 | { |
| 247 | CommandValidatorTester tester1; |
| 248 | tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1"); |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 249 | tester1.saveIdentityToFile("tests/mgmt/cert1.ndncert"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 250 | |
| 251 | CommandValidatorTester tester2; |
| 252 | tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2"); |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 253 | tester2.saveIdentityToFile("tests/mgmt/cert2.ndncert"); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 254 | |
| 255 | shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert"); |
| 256 | shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething"); |
| 257 | shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create"); |
| 258 | |
| 259 | ndn::CommandInterestGenerator generator; |
| 260 | generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName()); |
| 261 | generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName()); |
| 262 | generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName()); |
| 263 | |
| 264 | ConfigFile config; |
| 265 | CommandValidator validator; |
| 266 | validator.addSupportedPrivilege("faces"); |
| 267 | validator.addSupportedPrivilege("fib"); |
| 268 | validator.addSupportedPrivilege("stats"); |
| 269 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 270 | validator.setConfigFile(config); |
| 271 | |
| 272 | config.parse(CONFIG, true, CONFIG_PATH.native()); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 273 | |
| 274 | validator.validate(*fibCommand, |
| 275 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1), |
| 276 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2)); |
| 277 | |
| 278 | BOOST_REQUIRE(m_tester1.commandValidationFailed()); |
| 279 | m_tester1.resetValidation(); |
| 280 | |
| 281 | validator.validate(*statsCommand, |
| 282 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1), |
| 283 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2)); |
| 284 | |
| 285 | BOOST_REQUIRE(m_tester1.commandValidationFailed()); |
| 286 | |
| 287 | validator.validate(*facesCommand, |
| 288 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1), |
| 289 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2)); |
| 290 | |
| 291 | BOOST_REQUIRE(m_tester2.commandValidationFailed()); |
| 292 | m_tester2.resetValidation(); |
| 293 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 294 | // use cert2 for fib command (authorized for cert1 only) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 295 | shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert"); |
| 296 | generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName()); |
| 297 | |
| 298 | validator.validate(*unauthorizedFibCommand, |
| 299 | bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1), |
| 300 | bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2)); |
| 301 | |
| 302 | BOOST_REQUIRE(m_tester2.commandValidationFailed()); |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE(NoAuthorizeSections) |
| 306 | { |
| 307 | const std::string NO_AUTHORIZE_CONFIG = |
| 308 | "authorizations\n" |
| 309 | "{\n" |
| 310 | "}\n"; |
| 311 | |
| 312 | ConfigFile config; |
| 313 | CommandValidator validator; |
| 314 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 315 | validator.setConfigFile(config); |
| 316 | BOOST_CHECK_THROW(config.parse(NO_AUTHORIZE_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | BOOST_AUTO_TEST_CASE(NoPrivilegesSections) |
| 320 | { |
| 321 | const std::string NO_PRIVILEGES_CONFIG = |
| 322 | "authorizations\n" |
| 323 | "{\n" |
| 324 | " authorize\n" |
| 325 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 326 | " certfile \"tests/mgmt/cert1.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 327 | " }\n" |
| 328 | "}\n"; |
| 329 | |
| 330 | ConfigFile config; |
| 331 | CommandValidator validator; |
| 332 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 333 | validator.setConfigFile(config); |
| 334 | |
| 335 | BOOST_CHECK_THROW(config.parse(NO_PRIVILEGES_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(InvalidCertfile) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 339 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 340 | const std::string INVALID_CERT_CONFIG = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 341 | "authorizations\n" |
| 342 | "{\n" |
| 343 | " authorize\n" |
| 344 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 345 | " certfile \"tests/mgmt/notacertfile.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 346 | " privileges\n" |
| 347 | " {\n" |
| 348 | " fib\n" |
| 349 | " stats\n" |
| 350 | " }\n" |
| 351 | " }\n" |
| 352 | "}\n"; |
| 353 | |
| 354 | ConfigFile config; |
| 355 | CommandValidator validator; |
| 356 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 357 | validator.setConfigFile(config); |
| 358 | BOOST_CHECK_THROW(config.parse(INVALID_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 361 | BOOST_AUTO_TEST_CASE(NoCertfile) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 362 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 363 | const std::string NO_CERT_CONFIG = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 364 | "authorizations\n" |
| 365 | "{\n" |
| 366 | " authorize\n" |
| 367 | " {\n" |
| 368 | " privileges\n" |
| 369 | " {\n" |
| 370 | " fib\n" |
| 371 | " stats\n" |
| 372 | " }\n" |
| 373 | " }\n" |
| 374 | "}\n"; |
| 375 | |
| 376 | |
| 377 | ConfigFile config; |
| 378 | CommandValidator validator; |
| 379 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 380 | validator.setConfigFile(config); |
| 381 | BOOST_CHECK_THROW(config.parse(NO_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 384 | BOOST_AUTO_TEST_CASE(MalformedCert) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 385 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 386 | const std::string MALFORMED_CERT_CONFIG = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 387 | "authorizations\n" |
| 388 | "{\n" |
| 389 | " authorize\n" |
| 390 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 391 | " certfile \"tests/mgmt/malformed.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 392 | " privileges\n" |
| 393 | " {\n" |
| 394 | " fib\n" |
| 395 | " stats\n" |
| 396 | " }\n" |
| 397 | " }\n" |
| 398 | "}\n"; |
| 399 | |
| 400 | |
| 401 | ConfigFile config; |
| 402 | CommandValidator validator; |
| 403 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 404 | validator.setConfigFile(config); |
| 405 | BOOST_CHECK_THROW(config.parse(MALFORMED_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | bool |
| 409 | validateErrorMessage(const std::string& expectedMessage, const ConfigFile::Error& error) |
| 410 | { |
| 411 | bool gotExpected = error.what() == expectedMessage; |
| 412 | if (!gotExpected) |
| 413 | { |
| 414 | NFD_LOG_WARN("\ncaught exception: " << error.what() |
| 415 | << "\n\nexpected exception: " << expectedMessage); |
| 416 | } |
| 417 | return gotExpected; |
| 418 | } |
| 419 | |
| 420 | BOOST_AUTO_TEST_CASE(NoAuthorizeSectionsDryRun) |
| 421 | { |
| 422 | const std::string NO_AUTHORIZE_CONFIG = |
| 423 | "authorizations\n" |
| 424 | "{\n" |
| 425 | "}\n"; |
| 426 | |
| 427 | ConfigFile config; |
| 428 | CommandValidator validator; |
| 429 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 430 | validator.setConfigFile(config); |
| 431 | BOOST_CHECK_EXCEPTION(config.parse(NO_AUTHORIZE_CONFIG, true, CONFIG_PATH.native()), |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 432 | ConfigFile::Error, |
| 433 | bind(&validateErrorMessage, |
| 434 | "No authorize sections found", _1)); |
| 435 | } |
| 436 | |
| 437 | BOOST_FIXTURE_TEST_CASE(NoPrivilegesSectionsDryRun, TwoValidatorFixture) |
| 438 | { |
| 439 | const std::string NO_PRIVILEGES_CONFIG = |
| 440 | "authorizations\n" |
| 441 | "{\n" |
| 442 | " authorize\n" |
| 443 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 444 | " certfile \"tests/mgmt/cert1.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 445 | " }\n" |
| 446 | " authorize\n" |
| 447 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 448 | " certfile \"tests/mgmt/cert2.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 449 | " }\n" |
| 450 | "}\n"; |
| 451 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 452 | ConfigFile config; |
| 453 | CommandValidator validator; |
| 454 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 455 | validator.setConfigFile(config); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 456 | |
| 457 | std::stringstream expectedError; |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 458 | expectedError << "No privileges section found for certificate file tests/mgmt/cert1.ndncert " |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 459 | << "(" << m_tester1.getPublicKeyName().toUri() << ")\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 460 | << "No privileges section found for certificate file tests/mgmt/cert2.ndncert " |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 461 | << "(" << m_tester2.getPublicKeyName().toUri() << ")"; |
| 462 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 463 | BOOST_CHECK_EXCEPTION(config.parse(NO_PRIVILEGES_CONFIG, true, CONFIG_PATH.native()), |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 464 | ConfigFile::Error, |
| 465 | bind(&validateErrorMessage, expectedError.str(), _1)); |
| 466 | } |
| 467 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 468 | BOOST_AUTO_TEST_CASE(InvalidCertfileDryRun) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 469 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 470 | using namespace boost::filesystem; |
| 471 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 472 | const std::string INVALID_KEY_CONFIG = |
| 473 | "authorizations\n" |
| 474 | "{\n" |
| 475 | " authorize\n" |
| 476 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 477 | " certfile \"tests/mgmt/notacertfile.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 478 | " privileges\n" |
| 479 | " {\n" |
| 480 | " fib\n" |
| 481 | " stats\n" |
| 482 | " }\n" |
| 483 | " }\n" |
| 484 | " authorize\n" |
| 485 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 486 | " certfile \"tests/mgmt/stillnotacertfile.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 487 | " privileges\n" |
| 488 | " {\n" |
| 489 | " }\n" |
| 490 | " }\n" |
| 491 | "}\n"; |
| 492 | |
| 493 | ConfigFile config; |
| 494 | CommandValidator validator; |
| 495 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 496 | validator.setConfigFile(config); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 497 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 498 | std::stringstream error; |
| 499 | error << "Unable to open certificate file " |
| 500 | << absolute("tests/mgmt/notacertfile.ndncert").native() << "\n" |
| 501 | << "Unable to open certificate file " |
| 502 | << absolute("tests/mgmt/stillnotacertfile.ndncert").native(); |
| 503 | |
| 504 | BOOST_CHECK_EXCEPTION(config.parse(INVALID_KEY_CONFIG, true, CONFIG_PATH.native()), |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 505 | ConfigFile::Error, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 506 | bind(&validateErrorMessage, error.str(), _1)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 509 | BOOST_AUTO_TEST_CASE(NoCertfileDryRun) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 510 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 511 | const std::string NO_CERT_CONFIG = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 512 | "authorizations\n" |
| 513 | "{\n" |
| 514 | " authorize\n" |
| 515 | " {\n" |
| 516 | " privileges\n" |
| 517 | " {\n" |
| 518 | " fib\n" |
| 519 | " stats\n" |
| 520 | " }\n" |
| 521 | " }\n" |
| 522 | " authorize\n" |
| 523 | " {\n" |
| 524 | " }\n" |
| 525 | "}\n"; |
| 526 | |
| 527 | |
| 528 | ConfigFile config; |
| 529 | CommandValidator validator; |
| 530 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 531 | validator.setConfigFile(config); |
| 532 | BOOST_CHECK_EXCEPTION(config.parse(NO_CERT_CONFIG, true, CONFIG_PATH.native()), |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 533 | ConfigFile::Error, |
| 534 | bind(&validateErrorMessage, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 535 | "No certfile specified\n" |
| 536 | "No certfile specified", _1)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 539 | BOOST_AUTO_TEST_CASE(MalformedCertDryRun) |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 540 | { |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 541 | using namespace boost::filesystem; |
| 542 | |
| 543 | const std::string MALFORMED_CERT_CONFIG = |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 544 | "authorizations\n" |
| 545 | "{\n" |
| 546 | " authorize\n" |
| 547 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 548 | " certfile \"tests/mgmt/malformed.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 549 | " privileges\n" |
| 550 | " {\n" |
| 551 | " fib\n" |
| 552 | " stats\n" |
| 553 | " }\n" |
| 554 | " }\n" |
| 555 | " authorize\n" |
| 556 | " {\n" |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 557 | " certfile \"tests/mgmt/malformed.ndncert\"\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 558 | " }\n" |
| 559 | "}\n"; |
| 560 | |
| 561 | |
| 562 | ConfigFile config; |
| 563 | CommandValidator validator; |
| 564 | |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 565 | validator.setConfigFile(config); |
| 566 | |
| 567 | std::stringstream error; |
| 568 | error << "Malformed certificate file " |
| 569 | << absolute("tests/mgmt/malformed.ndncert").native() << "\n" |
| 570 | << "Malformed certificate file " |
| 571 | << absolute("tests/mgmt/malformed.ndncert").native(); |
| 572 | |
| 573 | BOOST_CHECK_EXCEPTION(config.parse(MALFORMED_CERT_CONFIG, true, CONFIG_PATH.native()), |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 574 | ConfigFile::Error, |
Steve DiBenedetto | 1a3c673 | 2014-03-13 06:44:05 -0600 | [diff] [blame] | 575 | bind(&validateErrorMessage, error.str(), _1)); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | BOOST_AUTO_TEST_SUITE_END() |
| 579 | |
| 580 | } // namespace tests |
| 581 | |
| 582 | } // namespace nfd |
| 583 | |