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