Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Yingdi Yu <yingdi0@cs.ucla.edu> |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "security/validator-config.hpp" |
| 8 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 9 | #include "security/key-chain.hpp" |
| 10 | #include "util/io.hpp" |
| 11 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 12 | #include "boost-test.hpp" |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | namespace ndn { |
| 17 | |
| 18 | BOOST_AUTO_TEST_SUITE(TestValidatorConfig) |
| 19 | |
| 20 | void |
| 21 | onValidated(const shared_ptr<const Data>& data) |
| 22 | { |
| 23 | BOOST_CHECK(true); |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | onValidationFailed(const shared_ptr<const Data>& data, const string& failureInfo) |
| 28 | { |
| 29 | std::cerr << "Failure Info: " << failureInfo << std::endl; |
| 30 | BOOST_CHECK(false); |
| 31 | } |
| 32 | |
| 33 | void |
| 34 | onIntentionalFailureValidated(const shared_ptr<const Data>& data) |
| 35 | { |
| 36 | BOOST_CHECK(false); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | onIntentionalFailureInvalidated(const shared_ptr<const Data>& data, const string& failureInfo) |
| 41 | { |
| 42 | BOOST_CHECK(true); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | onValidated2(const shared_ptr<const Interest>& interest) |
| 47 | { |
| 48 | BOOST_CHECK(true); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | onValidationFailed2(const shared_ptr<const Interest>& interest, const string& failureInfo) |
| 53 | { |
| 54 | std::cerr << "Interest Name: " << interest->getName() << std::endl; |
| 55 | std::cerr << "Failure Info: " << failureInfo << std::endl; |
| 56 | BOOST_CHECK(false); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | onIntentionalFailureValidated2(const shared_ptr<const Interest>& interest) |
| 61 | { |
| 62 | BOOST_CHECK(false); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | onIntentionalFailureInvalidated2(const shared_ptr<const Interest>& interest, |
| 67 | const string& failureInfo) |
| 68 | { |
| 69 | BOOST_CHECK(true); |
| 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(NameFilter) |
| 73 | { |
| 74 | KeyChain keyChain; |
| 75 | |
| 76 | Name identity("/TestValidatorConfig/NameFilter"); |
| 77 | identity.appendVersion(); |
| 78 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 79 | Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 80 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 81 | io::save(*idCert, "trust-anchor-1.cert"); |
| 82 | |
| 83 | Name dataName1("/simple/equal"); |
| 84 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 85 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity)); |
| 86 | |
| 87 | Name dataName2("/simple/different"); |
| 88 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 89 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity)); |
| 90 | |
| 91 | std::string CONFIG_1 = |
| 92 | "rule\n" |
| 93 | "{\n" |
| 94 | " id \"Simple Rule\"\n" |
| 95 | " for data\n" |
| 96 | " filter" |
| 97 | " {\n" |
| 98 | " type name\n" |
| 99 | " name /simple/equal\n" |
| 100 | " relation equal\n" |
| 101 | " }\n" |
| 102 | " checker\n" |
| 103 | " {\n" |
| 104 | " type customized\n" |
| 105 | " sig-type rsa-sha256\n" |
| 106 | " key-locator\n" |
| 107 | " {\n" |
| 108 | " type name\n" |
| 109 | " name "; |
| 110 | |
| 111 | std::string CONFIG_2 = |
| 112 | "\n" |
| 113 | " relation equal\n" |
| 114 | " }\n" |
| 115 | " }\n" |
| 116 | "}\n" |
| 117 | "trust-anchor\n" |
| 118 | "{\n" |
| 119 | " type file\n" |
| 120 | " file-name \"trust-anchor-1.cert\"\n" |
| 121 | "}\n"; |
| 122 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 123 | |
| 124 | const boost::filesystem::path CONFIG_PATH = |
| 125 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 126 | |
| 127 | |
| 128 | shared_ptr<Face> face = make_shared<Face>(); |
| 129 | ValidatorConfig validator(face); |
| 130 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 131 | |
| 132 | validator.validate(*data1, |
| 133 | bind(&onValidated, _1), |
| 134 | bind(&onValidationFailed, _1, _2)); |
| 135 | |
| 136 | validator.validate(*data2, |
| 137 | bind(&onIntentionalFailureValidated, _1), |
| 138 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 139 | |
| 140 | keyChain.deleteIdentity(identity); |
| 141 | |
| 142 | const boost::filesystem::path CERT_PATH = |
| 143 | (boost::filesystem::current_path() / std::string("trust-anchor-1.cert")); |
| 144 | boost::filesystem::remove(CERT_PATH); |
| 145 | } |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(NameFilter2) |
| 148 | { |
| 149 | KeyChain keyChain; |
| 150 | |
| 151 | Name identity("/TestValidatorConfig/NameFilter2"); |
| 152 | identity.appendVersion(); |
| 153 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 154 | Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 155 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 156 | io::save(*idCert, "trust-anchor-2.cert"); |
| 157 | |
| 158 | Name dataName1("/simple/isPrefixOf"); |
| 159 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 160 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity)); |
| 161 | |
| 162 | Name dataName2("/simple/notPrefixOf"); |
| 163 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 164 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity)); |
| 165 | |
| 166 | Name dataName3("/simple/isPrefixOf/anotherLevel"); |
| 167 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
| 168 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data3, identity)); |
| 169 | |
| 170 | std::string CONFIG_1 = |
| 171 | "rule\n" |
| 172 | "{\n" |
| 173 | " id \"Simple2 Rule\"\n" |
| 174 | " for data\n" |
| 175 | " filter" |
| 176 | " {\n" |
| 177 | " type name\n" |
| 178 | " name /simple/isPrefixOf\n" |
| 179 | " relation is-prefix-of\n" |
| 180 | " }\n" |
| 181 | " checker\n" |
| 182 | " {\n" |
| 183 | " type customized\n" |
| 184 | " sig-type rsa-sha256\n" |
| 185 | " key-locator\n" |
| 186 | " {\n" |
| 187 | " type name\n" |
| 188 | " name "; |
| 189 | |
| 190 | std::string CONFIG_2 = |
| 191 | "\n" |
| 192 | " relation equal\n" |
| 193 | " }\n" |
| 194 | " }\n" |
| 195 | "}\n" |
| 196 | "trust-anchor\n" |
| 197 | "{\n" |
| 198 | " type file\n" |
| 199 | " file-name \"trust-anchor-2.cert\"\n" |
| 200 | "}\n"; |
| 201 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 202 | |
| 203 | const boost::filesystem::path CONFIG_PATH = |
| 204 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 205 | |
| 206 | |
| 207 | shared_ptr<Face> face = make_shared<Face>(); |
| 208 | ValidatorConfig validator(face); |
| 209 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 210 | |
| 211 | validator.validate(*data1, |
| 212 | bind(&onValidated, _1), |
| 213 | bind(&onValidationFailed, _1, _2)); |
| 214 | |
| 215 | validator.validate(*data2, |
| 216 | bind(&onIntentionalFailureValidated, _1), |
| 217 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 218 | |
| 219 | validator.validate(*data3, |
| 220 | bind(&onValidated, _1), |
| 221 | bind(&onValidationFailed, _1, _2)); |
| 222 | |
| 223 | keyChain.deleteIdentity(identity); |
| 224 | |
| 225 | const boost::filesystem::path CERT_PATH = |
| 226 | (boost::filesystem::current_path() / std::string("trust-anchor-2.cert")); |
| 227 | boost::filesystem::remove(CERT_PATH); |
| 228 | } |
| 229 | |
| 230 | BOOST_AUTO_TEST_CASE(NameFilter3) |
| 231 | { |
| 232 | KeyChain keyChain; |
| 233 | |
| 234 | Name identity("/TestValidatorConfig/NameFilter3"); |
| 235 | identity.appendVersion(); |
| 236 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 237 | Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 238 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 239 | io::save(*idCert, "trust-anchor-3.cert"); |
| 240 | |
| 241 | Name dataName1("/simple/isStrictPrefixOf"); |
| 242 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 243 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity)); |
| 244 | |
| 245 | Name dataName2("/simple"); |
| 246 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 247 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity)); |
| 248 | |
| 249 | Name dataName3("/simple/isStrictPrefixOf/anotherLevel"); |
| 250 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
| 251 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data3, identity)); |
| 252 | |
| 253 | std::string CONFIG_1 = |
| 254 | "rule\n" |
| 255 | "{\n" |
| 256 | " id \"Simple3 Rule\"\n" |
| 257 | " for data\n" |
| 258 | " filter" |
| 259 | " {\n" |
| 260 | " type name\n" |
| 261 | " name /simple/isStrictPrefixOf\n" |
| 262 | " relation is-strict-prefix-of\n" |
| 263 | " }\n" |
| 264 | " checker\n" |
| 265 | " {\n" |
| 266 | " type customized\n" |
| 267 | " sig-type rsa-sha256\n" |
| 268 | " key-locator\n" |
| 269 | " {\n" |
| 270 | " type name\n" |
| 271 | " name "; |
| 272 | |
| 273 | std::string CONFIG_2 = |
| 274 | "\n" |
| 275 | " relation equal\n" |
| 276 | " }\n" |
| 277 | " }\n" |
| 278 | "}\n" |
| 279 | "trust-anchor\n" |
| 280 | "{\n" |
| 281 | " type file\n" |
| 282 | " file-name \"trust-anchor-3.cert\"\n" |
| 283 | "}\n"; |
| 284 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 285 | |
| 286 | const boost::filesystem::path CONFIG_PATH = |
| 287 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 288 | |
| 289 | |
| 290 | shared_ptr<Face> face = make_shared<Face>(); |
| 291 | ValidatorConfig validator(face); |
| 292 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 293 | |
| 294 | validator.validate(*data1, |
| 295 | bind(&onIntentionalFailureValidated, _1), |
| 296 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 297 | |
| 298 | validator.validate(*data2, |
| 299 | bind(&onIntentionalFailureValidated, _1), |
| 300 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 301 | |
| 302 | validator.validate(*data3, |
| 303 | bind(&onValidated, _1), |
| 304 | bind(&onValidationFailed, _1, _2)); |
| 305 | |
| 306 | keyChain.deleteIdentity(identity); |
| 307 | |
| 308 | const boost::filesystem::path CERT_PATH = |
| 309 | (boost::filesystem::current_path() / std::string("trust-anchor-3.cert")); |
| 310 | boost::filesystem::remove(CERT_PATH); |
| 311 | } |
| 312 | |
| 313 | BOOST_AUTO_TEST_CASE(NameFilter4) |
| 314 | { |
| 315 | KeyChain keyChain; |
| 316 | |
| 317 | Name identity("/TestValidatorConfig/NameFilter4"); |
| 318 | identity.appendVersion(); |
| 319 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 320 | Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 321 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 322 | io::save(*idCert, "trust-anchor-4.cert"); |
| 323 | |
| 324 | Name dataName1("/simple/regex"); |
| 325 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 326 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity)); |
| 327 | |
| 328 | Name dataName2("/simple/regex-wrong"); |
| 329 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 330 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity)); |
| 331 | |
| 332 | Name dataName3("/simple/regex/correct"); |
| 333 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
| 334 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data3, identity)); |
| 335 | |
| 336 | std::string CONFIG_1 = |
| 337 | "rule\n" |
| 338 | "{\n" |
| 339 | " id \"Simple3 Rule\"\n" |
| 340 | " for data\n" |
| 341 | " filter" |
| 342 | " {\n" |
| 343 | " type name\n" |
| 344 | " regex ^<simple><regex>\n" |
| 345 | " }\n" |
| 346 | " checker\n" |
| 347 | " {\n" |
| 348 | " type customized\n" |
| 349 | " sig-type rsa-sha256\n" |
| 350 | " key-locator\n" |
| 351 | " {\n" |
| 352 | " type name\n" |
| 353 | " name "; |
| 354 | |
| 355 | std::string CONFIG_2 = |
| 356 | "\n" |
| 357 | " relation equal\n" |
| 358 | " }\n" |
| 359 | " }\n" |
| 360 | "}\n" |
| 361 | "trust-anchor\n" |
| 362 | "{\n" |
| 363 | " type file\n" |
| 364 | " file-name \"trust-anchor-4.cert\"\n" |
| 365 | "}\n"; |
| 366 | const std::string CONFIG = CONFIG_1 + certName.getPrefix(-1).toUri() + CONFIG_2; |
| 367 | |
| 368 | const boost::filesystem::path CONFIG_PATH = |
| 369 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 370 | |
| 371 | |
| 372 | shared_ptr<Face> face = make_shared<Face>(); |
| 373 | ValidatorConfig validator(face); |
| 374 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 375 | |
| 376 | validator.validate(*data1, |
| 377 | bind(&onValidated, _1), |
| 378 | bind(&onValidationFailed, _1, _2)); |
| 379 | |
| 380 | validator.validate(*data2, |
| 381 | bind(&onIntentionalFailureValidated, _1), |
| 382 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 383 | |
| 384 | validator.validate(*data3, |
| 385 | bind(&onValidated, _1), |
| 386 | bind(&onValidationFailed, _1, _2)); |
| 387 | |
| 388 | keyChain.deleteIdentity(identity); |
| 389 | |
| 390 | const boost::filesystem::path CERT_PATH = |
| 391 | (boost::filesystem::current_path() / std::string("trust-anchor-4.cert")); |
| 392 | boost::filesystem::remove(CERT_PATH); |
| 393 | } |
| 394 | |
| 395 | BOOST_AUTO_TEST_CASE(KeyLocatorNameChecker1) |
| 396 | { |
| 397 | KeyChain keyChain; |
| 398 | |
| 399 | Name identity("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 400 | identity.appendVersion(); |
| 401 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity)); |
| 402 | Name certName = keyChain.getDefaultCertificateNameForIdentity(identity); |
| 403 | shared_ptr<IdentityCertificate> idCert = keyChain.getCertificate(certName); |
| 404 | io::save(*idCert, "trust-anchor-5.cert"); |
| 405 | |
| 406 | Name dataName1 = identity; |
| 407 | dataName1.append("1"); |
| 408 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 409 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity)); |
| 410 | |
| 411 | Name dataName2 = identity; |
| 412 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 413 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity)); |
| 414 | |
| 415 | Name dataName3("/TestValidatorConfig/KeyLocatorNameChecker1"); |
| 416 | shared_ptr<Data> data3 = make_shared<Data>(dataName3); |
| 417 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data3, identity)); |
| 418 | |
| 419 | const std::string CONFIG = |
| 420 | "rule\n" |
| 421 | "{\n" |
| 422 | " id \"Simple3 Rule\"\n" |
| 423 | " for data\n" |
| 424 | " checker\n" |
| 425 | " {\n" |
| 426 | " type customized\n" |
| 427 | " sig-type rsa-sha256\n" |
| 428 | " key-locator\n" |
| 429 | " {\n" |
| 430 | " type name\n" |
| 431 | " hyper-relation\n" |
| 432 | " {\n" |
| 433 | " k-regex ^([^<KEY>]*)<KEY>(<>*)<><ID-CERT>$\n" |
| 434 | " k-expand \\\\1\\\\2\n" |
| 435 | " h-relation is-strict-prefix-of\n" |
| 436 | " p-regex ^(<>*)$\n" |
| 437 | " p-expand \\\\1\n" |
| 438 | " }\n" |
| 439 | " }\n" |
| 440 | " }\n" |
| 441 | "}\n" |
| 442 | "trust-anchor\n" |
| 443 | "{\n" |
| 444 | " type file\n" |
| 445 | " file-name \"trust-anchor-5.cert\"\n" |
| 446 | "}\n"; |
| 447 | const boost::filesystem::path CONFIG_PATH = |
| 448 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 449 | |
| 450 | |
| 451 | shared_ptr<Face> face = make_shared<Face>(); |
| 452 | ValidatorConfig validator(face); |
| 453 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 454 | |
| 455 | validator.validate(*data1, |
| 456 | bind(&onValidated, _1), |
| 457 | bind(&onValidationFailed, _1, _2)); |
| 458 | |
| 459 | validator.validate(*data2, |
| 460 | bind(&onIntentionalFailureValidated, _1), |
| 461 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 462 | |
| 463 | validator.validate(*data3, |
| 464 | bind(&onIntentionalFailureValidated, _1), |
| 465 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 466 | |
| 467 | keyChain.deleteIdentity(identity); |
| 468 | |
| 469 | const boost::filesystem::path CERT_PATH = |
| 470 | (boost::filesystem::current_path() / std::string("trust-anchor-5.cert")); |
| 471 | boost::filesystem::remove(CERT_PATH); |
| 472 | } |
| 473 | |
| 474 | struct FacesFixture |
| 475 | { |
| 476 | FacesFixture() |
| 477 | : regPrefixId(0) |
| 478 | , regPrefixId2(0) |
| 479 | {} |
| 480 | |
| 481 | void |
| 482 | onInterest(shared_ptr<Face> face, shared_ptr<Data> data) |
| 483 | { |
| 484 | face->put(*data); |
| 485 | face->unsetInterestFilter(regPrefixId); |
| 486 | } |
| 487 | |
| 488 | void |
| 489 | onInterest2(shared_ptr<Face> face, shared_ptr<Data> data) |
| 490 | { |
| 491 | face->put(*data); |
| 492 | face->unsetInterestFilter(regPrefixId2); |
| 493 | } |
| 494 | |
| 495 | void |
| 496 | onRegFailed() |
| 497 | {} |
| 498 | |
| 499 | void |
| 500 | validate1(shared_ptr<ValidatorConfig> validator, shared_ptr<Data> data) |
| 501 | { |
| 502 | validator->validate(*data, |
| 503 | bind(&onValidated, _1), |
| 504 | bind(&onValidationFailed, _1, _2)); |
| 505 | } |
| 506 | |
| 507 | void |
| 508 | validate2(shared_ptr<ValidatorConfig> validator, shared_ptr<Data> data) |
| 509 | { |
| 510 | validator->validate(*data, |
| 511 | bind(&onIntentionalFailureValidated, _1), |
| 512 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 513 | } |
| 514 | |
| 515 | void |
| 516 | validate3(shared_ptr<ValidatorConfig> validator, shared_ptr<Interest> interest) |
| 517 | { |
| 518 | validator->validate(*interest, |
| 519 | bind(&onValidated2, _1), |
| 520 | bind(&onValidationFailed2, _1, _2)); |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | validate4(shared_ptr<ValidatorConfig> validator, shared_ptr<Interest> interest) |
| 525 | { |
| 526 | validator->validate(*interest, |
| 527 | bind(&onIntentionalFailureValidated2, _1), |
| 528 | bind(&onIntentionalFailureInvalidated2, _1, _2)); |
| 529 | } |
| 530 | |
| 531 | void |
| 532 | terminate(shared_ptr<Face> face) |
| 533 | { |
| 534 | face->ioService()->stop(); |
| 535 | } |
| 536 | |
| 537 | const RegisteredPrefixId* regPrefixId; |
| 538 | const RegisteredPrefixId* regPrefixId2; |
| 539 | }; |
| 540 | |
| 541 | BOOST_FIXTURE_TEST_CASE(HierarchicalChecker, FacesFixture) |
| 542 | { |
| 543 | KeyChain keyChain; |
| 544 | std::vector<CertificateSubjectDescription> subjectDescription; |
| 545 | |
| 546 | Name root("/TestValidatorConfig"); |
| 547 | Name rootCertName = keyChain.createIdentity(root); |
| 548 | shared_ptr<IdentityCertificate> rootCert = |
| 549 | keyChain.getCertificate(rootCertName); |
| 550 | io::save(*rootCert, "trust-anchor-6.cert"); |
| 551 | |
| 552 | |
| 553 | Name sld("/TestValidatorConfig/HierarchicalChecker"); |
| 554 | Name sldKeyName = keyChain.generateRSAKeyPairAsDefault(sld, true); |
| 555 | shared_ptr<IdentityCertificate> sldCert = |
| 556 | keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 557 | root, |
| 558 | time::system_clock::now(), |
| 559 | time::system_clock::now() + time::days(7300), |
| 560 | subjectDescription); |
| 561 | keyChain.signByIdentity(*sldCert, root); |
| 562 | keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 563 | |
| 564 | Name nld("/TestValidatorConfig/HierarchicalChecker/NextLevel"); |
| 565 | Name nldKeyName = keyChain.generateRSAKeyPairAsDefault(nld, true); |
| 566 | shared_ptr<IdentityCertificate> nldCert = |
| 567 | keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 568 | sld, |
| 569 | time::system_clock::now(), |
| 570 | time::system_clock::now() + time::days(7300), |
| 571 | subjectDescription); |
| 572 | keyChain.signByIdentity(*nldCert, sld); |
| 573 | keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 574 | |
| 575 | shared_ptr<Face> face = make_shared<Face>(); |
| 576 | shared_ptr<Face> face2 = shared_ptr<Face>(new Face(face->ioService())); |
| 577 | Scheduler scheduler(*face->ioService()); |
| 578 | |
| 579 | scheduler.scheduleEvent(time::seconds(1), |
| 580 | bind(&FacesFixture::terminate, this, face)); |
| 581 | |
| 582 | regPrefixId = face->setInterestFilter(sldCert->getName().getPrefix(-1), |
| 583 | bind(&FacesFixture::onInterest, this, face, sldCert), |
| 584 | bind(&FacesFixture::onRegFailed, this)); |
| 585 | |
| 586 | regPrefixId2 = face->setInterestFilter(nldCert->getName().getPrefix(-1), |
| 587 | bind(&FacesFixture::onInterest2, this, face, nldCert), |
| 588 | bind(&FacesFixture::onRegFailed, this)); |
| 589 | |
| 590 | Name dataName1 = nld; |
| 591 | dataName1.append("data1"); |
| 592 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 593 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, nld)); |
| 594 | |
| 595 | Name dataName2("/ConfValidatorTest"); |
| 596 | dataName2.append("data1"); |
| 597 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 598 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, nld)); |
| 599 | |
| 600 | |
| 601 | const std::string CONFIG = |
| 602 | "rule\n" |
| 603 | "{\n" |
| 604 | " id \"Simple3 Rule\"\n" |
| 605 | " for data\n" |
| 606 | " checker\n" |
| 607 | " {\n" |
| 608 | " type hierarchical\n" |
| 609 | " sig-type rsa-sha256\n" |
| 610 | " }\n" |
| 611 | "}\n" |
| 612 | "trust-anchor\n" |
| 613 | "{\n" |
| 614 | " type file\n" |
| 615 | " file-name \"trust-anchor-6.cert\"\n" |
| 616 | "}\n"; |
| 617 | const boost::filesystem::path CONFIG_PATH = |
| 618 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 619 | |
| 620 | |
| 621 | shared_ptr<ValidatorConfig> validator = shared_ptr<ValidatorConfig>(new ValidatorConfig(face2)); |
| 622 | validator->load(CONFIG, CONFIG_PATH.native()); |
| 623 | |
| 624 | scheduler.scheduleEvent(time::milliseconds(200), |
| 625 | bind(&FacesFixture::validate1, this, |
| 626 | validator, data1)); |
| 627 | |
| 628 | scheduler.scheduleEvent(time::milliseconds(400), |
| 629 | bind(&FacesFixture::validate2, this, |
| 630 | validator, data2)); |
| 631 | |
| 632 | BOOST_REQUIRE_NO_THROW(face->processEvents()); |
| 633 | |
| 634 | keyChain.deleteIdentity(root); |
| 635 | keyChain.deleteIdentity(sld); |
| 636 | keyChain.deleteIdentity(nld); |
| 637 | |
| 638 | const boost::filesystem::path CERT_PATH = |
| 639 | (boost::filesystem::current_path() / std::string("trust-anchor-6.cert")); |
| 640 | boost::filesystem::remove(CERT_PATH); |
| 641 | } |
| 642 | |
| 643 | BOOST_AUTO_TEST_CASE(FixedSingerChecker) |
| 644 | { |
| 645 | KeyChain keyChain; |
| 646 | |
| 647 | Name identity("/TestValidatorConfig/FixedSingerChecker"); |
| 648 | |
| 649 | Name identity1 = identity; |
| 650 | identity1.append("1").appendVersion(); |
| 651 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity1)); |
| 652 | Name certName1 = keyChain.getDefaultCertificateNameForIdentity(identity1); |
| 653 | shared_ptr<IdentityCertificate> idCert1 = keyChain.getCertificate(certName1); |
| 654 | io::save(*idCert1, "trust-anchor-7.cert"); |
| 655 | |
| 656 | Name identity2 = identity; |
| 657 | identity2.append("2").appendVersion(); |
| 658 | BOOST_REQUIRE_NO_THROW(keyChain.createIdentity(identity2)); |
| 659 | |
| 660 | Name dataName1 = identity; |
| 661 | dataName1.append("data").appendVersion(); |
| 662 | shared_ptr<Data> data1 = make_shared<Data>(dataName1); |
| 663 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data1, identity1)); |
| 664 | |
| 665 | Name dataName2 = identity; |
| 666 | dataName2.append("data").appendVersion(); |
| 667 | shared_ptr<Data> data2 = make_shared<Data>(dataName2); |
| 668 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*data2, identity2)); |
| 669 | |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 670 | Name interestName("/TestValidatorConfig/FixedSingerChecker/fakeSigInfo/fakeSigValue"); |
| 671 | shared_ptr<Interest> interest = make_shared<Interest>(interestName); |
| 672 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 673 | const std::string CONFIG = |
| 674 | "rule\n" |
| 675 | "{\n" |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 676 | " id \"FixedSingerChecker Data Rule\"\n" |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 677 | " for data\n" |
| 678 | " filter" |
| 679 | " {\n" |
| 680 | " type name\n" |
| 681 | " name /TestValidatorConfig/FixedSingerChecker\n" |
| 682 | " relation is-strict-prefix-of\n" |
| 683 | " }\n" |
| 684 | " checker\n" |
| 685 | " {\n" |
| 686 | " type fixed-signer\n" |
| 687 | " sig-type rsa-sha256\n" |
| 688 | " signer\n" |
| 689 | " {\n" |
| 690 | " type file\n" |
| 691 | " file-name \"trust-anchor-7.cert\"\n" |
| 692 | " }\n" |
| 693 | " }\n" |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 694 | "}\n" |
| 695 | "rule\n" |
| 696 | "{\n" |
| 697 | " id \"FixedSingerChecker Interest Rule\"\n" |
| 698 | " for interest\n" |
| 699 | " filter" |
| 700 | " {\n" |
| 701 | " type name\n" |
| 702 | " name /TestValidatorConfig/FixedSingerChecker\n" |
| 703 | " relation is-strict-prefix-of\n" |
| 704 | " }\n" |
| 705 | " checker\n" |
| 706 | " {\n" |
| 707 | " type fixed-signer\n" |
| 708 | " sig-type rsa-sha256\n" |
| 709 | " signer\n" |
| 710 | " {\n" |
| 711 | " type file\n" |
| 712 | " file-name \"trust-anchor-7.cert\"\n" |
| 713 | " }\n" |
| 714 | " }\n" |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 715 | "}\n"; |
| 716 | const boost::filesystem::path CONFIG_PATH = |
| 717 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 718 | |
| 719 | |
| 720 | shared_ptr<Face> face = make_shared<Face>(); |
| 721 | ValidatorConfig validator(face); |
| 722 | validator.load(CONFIG, CONFIG_PATH.native()); |
| 723 | |
| 724 | validator.validate(*data1, |
| 725 | bind(&onValidated, _1), |
| 726 | bind(&onValidationFailed, _1, _2)); |
| 727 | |
| 728 | validator.validate(*data2, |
| 729 | bind(&onIntentionalFailureValidated, _1), |
| 730 | bind(&onIntentionalFailureInvalidated, _1, _2)); |
| 731 | |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 732 | validator.validate(*interest, |
| 733 | bind(&onIntentionalFailureValidated2, _1), |
| 734 | bind(&onIntentionalFailureInvalidated2, _1, _2)); |
| 735 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 736 | |
| 737 | keyChain.deleteIdentity(identity1); |
| 738 | keyChain.deleteIdentity(identity2); |
| 739 | |
| 740 | const boost::filesystem::path CERT_PATH = |
| 741 | (boost::filesystem::current_path() / std::string("trust-anchor-7.cert")); |
| 742 | boost::filesystem::remove(CERT_PATH); |
| 743 | } |
| 744 | |
| 745 | |
| 746 | BOOST_FIXTURE_TEST_CASE(Nrd, FacesFixture) |
| 747 | { |
| 748 | KeyChain keyChain; |
| 749 | std::vector<CertificateSubjectDescription> subjectDescription; |
| 750 | |
| 751 | Name root("/TestValidatorConfig"); |
| 752 | Name rootCertName = keyChain.createIdentity(root); |
| 753 | shared_ptr<IdentityCertificate> rootCert = |
| 754 | keyChain.getCertificate(rootCertName); |
| 755 | io::save(*rootCert, "trust-anchor-8.cert"); |
| 756 | |
| 757 | |
| 758 | Name sld("/TestValidatorConfig/Nrd-1"); |
| 759 | Name sldKeyName = keyChain.generateRSAKeyPairAsDefault(sld, true); |
| 760 | shared_ptr<IdentityCertificate> sldCert = |
| 761 | keyChain.prepareUnsignedIdentityCertificate(sldKeyName, |
| 762 | root, |
| 763 | time::system_clock::now(), |
| 764 | time::system_clock::now() + time::days(7300), |
| 765 | subjectDescription); |
| 766 | keyChain.signByIdentity(*sldCert, root); |
| 767 | keyChain.addCertificateAsIdentityDefault(*sldCert); |
| 768 | |
| 769 | Name nld("/TestValidatorConfig/Nrd-1/Nrd-2"); |
| 770 | Name nldKeyName = keyChain.generateRSAKeyPairAsDefault(nld, true); |
| 771 | shared_ptr<IdentityCertificate> nldCert = |
| 772 | keyChain.prepareUnsignedIdentityCertificate(nldKeyName, |
| 773 | sld, |
| 774 | time::system_clock::now(), |
| 775 | time::system_clock::now() + time::days(7300), |
| 776 | subjectDescription); |
| 777 | keyChain.signByIdentity(*nldCert, sld); |
| 778 | keyChain.addCertificateAsIdentityDefault(*nldCert); |
| 779 | |
| 780 | shared_ptr<Face> face = make_shared<Face>(); |
| 781 | shared_ptr<Face> face2 = shared_ptr<Face>(new Face(face->ioService())); |
| 782 | Scheduler scheduler(*face->ioService()); |
| 783 | |
| 784 | scheduler.scheduleEvent(time::seconds(1), |
| 785 | bind(&FacesFixture::terminate, this, face)); |
| 786 | |
| 787 | regPrefixId = face->setInterestFilter(sldCert->getName().getPrefix(-1), |
| 788 | bind(&FacesFixture::onInterest, this, face, sldCert), |
| 789 | bind(&FacesFixture::onRegFailed, this)); |
| 790 | |
| 791 | regPrefixId2 = face->setInterestFilter(nldCert->getName().getPrefix(-1), |
| 792 | bind(&FacesFixture::onInterest2, this, face, nldCert), |
| 793 | bind(&FacesFixture::onRegFailed, this)); |
| 794 | |
| 795 | Name interestName1("/localhost/nrd/register/option/timestamp/nonce"); |
| 796 | shared_ptr<Interest> interest1 = make_shared<Interest>(interestName1); |
| 797 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*interest1, nld)); |
| 798 | |
| 799 | Name interestName2("/localhost/nrd/non-register"); |
| 800 | shared_ptr<Interest> interest2 = make_shared<Interest>(interestName2); |
| 801 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*interest2, nld)); |
| 802 | |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 803 | Name interestName3("/localhost/nrd/register/option/timestamp/nonce"); |
| 804 | shared_ptr<Interest> interest3 = make_shared<Interest>(interestName3); |
| 805 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(*interest3, root)); |
| 806 | |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 807 | Name interestName4("/localhost/nrd/register/option/timestamp/nonce/fakeSigInfo/fakeSigValue"); |
| 808 | shared_ptr<Interest> interest4 = make_shared<Interest>(interestName4); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 809 | |
| 810 | const std::string CONFIG = |
| 811 | "rule\n" |
| 812 | "{\n" |
| 813 | " id \"NRD Prefix Registration Command Rule\"\n" |
| 814 | " for interest\n" |
| 815 | " filter\n" |
| 816 | " {\n" |
| 817 | " type name\n" |
| 818 | " regex ^<localhost><nrd>[<register><unregister><advertise><withdraw>]<>{3}$\n" |
| 819 | " }\n" |
| 820 | " checker\n" |
| 821 | " {\n" |
| 822 | " type customized\n" |
| 823 | " sig-type rsa-sha256\n" |
| 824 | " key-locator\n" |
| 825 | " {\n" |
| 826 | " type name\n" |
| 827 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT>$\n" |
| 828 | " }\n" |
| 829 | " }\n" |
| 830 | "}\n" |
| 831 | "rule\n" |
| 832 | "{\n" |
| 833 | " id \"Testbed Hierarchy Rule\"\n" |
| 834 | " for data\n" |
| 835 | " filter\n" |
| 836 | " {\n" |
| 837 | " type name\n" |
| 838 | " regex ^[^<KEY>]*<KEY><>*<ksk-.*><ID-CERT><>$\n" |
| 839 | " }\n" |
| 840 | " checker\n" |
| 841 | " {\n" |
| 842 | " type hierarchical\n" |
| 843 | " sig-type rsa-sha256\n" |
| 844 | " }\n" |
| 845 | "}\n" |
| 846 | "trust-anchor\n" |
| 847 | "{\n" |
| 848 | " type file\n" |
| 849 | " file-name \"trust-anchor-8.cert\"\n" |
| 850 | "}\n"; |
| 851 | const boost::filesystem::path CONFIG_PATH = |
| 852 | (boost::filesystem::current_path() / std::string("unit-test-nfd.conf")); |
| 853 | |
| 854 | |
| 855 | shared_ptr<ValidatorConfig> validator = shared_ptr<ValidatorConfig>(new ValidatorConfig(face2)); |
| 856 | validator->load(CONFIG, CONFIG_PATH.native()); |
| 857 | |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 858 | // should succeed |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 859 | scheduler.scheduleEvent(time::milliseconds(200), |
| 860 | bind(&FacesFixture::validate3, this, |
| 861 | validator, interest1)); |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 862 | // should fail |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 863 | scheduler.scheduleEvent(time::milliseconds(400), |
| 864 | bind(&FacesFixture::validate4, this, |
| 865 | validator, interest2)); |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 866 | // should succeed |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 867 | scheduler.scheduleEvent(time::milliseconds(600), |
| 868 | bind(&FacesFixture::validate3, this, |
| 869 | validator, interest3)); |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 870 | // should fail |
| 871 | scheduler.scheduleEvent(time::milliseconds(600), |
| 872 | bind(&FacesFixture::validate4, this, |
| 873 | validator, interest4)); |
Yingdi Yu | 3cca4ab | 2014-04-11 12:46:53 -0700 | [diff] [blame] | 874 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 875 | BOOST_REQUIRE_NO_THROW(face->processEvents()); |
| 876 | |
| 877 | keyChain.deleteIdentity(root); |
| 878 | keyChain.deleteIdentity(sld); |
| 879 | keyChain.deleteIdentity(nld); |
| 880 | |
| 881 | const boost::filesystem::path CERT_PATH = |
| 882 | (boost::filesystem::current_path() / std::string("trust-anchor-8.cert")); |
| 883 | boost::filesystem::remove(CERT_PATH); |
| 884 | } |
| 885 | |
| 886 | |
| 887 | |
| 888 | BOOST_AUTO_TEST_SUITE_END() |
| 889 | |
| 890 | } // namespace ndn |