Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [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 <boost/test/unit_test.hpp> |
| 8 | |
| 9 | #include "security/key-chain.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 10 | #include "security/validator.hpp" |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 11 | |
| 12 | #include "helpers/command-interest-generator.hpp" |
| 13 | #include "helpers/command-interest-validator.hpp" |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 14 | |
| 15 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 16 | namespace ndn { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 17 | |
| 18 | BOOST_AUTO_TEST_SUITE(TestSignedInterest) |
| 19 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 20 | BOOST_AUTO_TEST_CASE (SignedInterest) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 21 | { |
| 22 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
| 23 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 24 | Name identityName("/TestSignedInterest/SignVerify"); |
| 25 | Name certificateName; |
| 26 | BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName)); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 27 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 28 | Interest interest("/TestSignedInterest/SignVerify/Interest1"); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 29 | keyChain.signByIdentity(interest, identityName); |
| 30 | |
| 31 | Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size()); |
| 32 | |
| 33 | Interest interest2; |
| 34 | interest2.wireDecode(interestBlock); |
| 35 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 36 | shared_ptr<PublicKey> publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName)); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 37 | bool result = Validator::verifySignature(interest2, *publicKey); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 38 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 39 | BOOST_CHECK_EQUAL(result, true); |
| 40 | |
| 41 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identityName)); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 44 | class CommandInterestFixture |
| 45 | { |
| 46 | public: |
| 47 | CommandInterestFixture() |
| 48 | : m_validity(false) |
| 49 | {} |
| 50 | |
| 51 | void |
| 52 | validated(const shared_ptr<const Interest>& interest) |
| 53 | { m_validity = true; } |
| 54 | |
| 55 | void |
| 56 | validationFailed(const shared_ptr<const Interest>& interest) |
| 57 | { m_validity = false; } |
| 58 | |
| 59 | void |
| 60 | reset() |
| 61 | { m_validity = false; } |
| 62 | |
| 63 | bool m_validity; |
| 64 | }; |
| 65 | |
| 66 | BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture) |
| 67 | { |
| 68 | KeyChain keyChain; |
| 69 | Name identity("/TestCommandInterest/Validation"); |
| 70 | Name certName; |
| 71 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 72 | |
| 73 | CommandInterestGenerator generator; |
| 74 | CommandInterestValidator validator; |
| 75 | |
| 76 | validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName)); |
| 77 | |
| 78 | //Test a legitimate command |
| 79 | shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1"); |
| 80 | generator.generateWithIdentity(*commandInterest1, identity); |
| 81 | validator.validate(*commandInterest1, |
| 82 | bind(&CommandInterestFixture::validated, this, _1), |
| 83 | bind(&CommandInterestFixture::validationFailed, this, _1)); |
| 84 | |
| 85 | BOOST_CHECK_EQUAL(m_validity, true); |
| 86 | |
| 87 | //Test an outdated command |
| 88 | reset(); |
| 89 | shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2"); |
| 90 | int64_t timestamp = time::now() / 1000000; |
| 91 | timestamp -= 5000; |
| 92 | Name commandName = commandInterest2->getName(); |
| 93 | commandName |
| 94 | .append(name::Component::fromNumber(timestamp)) |
| 95 | .append(name::Component::fromNumber(random::generateWord64())); |
| 96 | commandInterest2->setName(commandName); |
| 97 | |
| 98 | keyChain.signByIdentity(*commandInterest2, identity); |
| 99 | validator.validate(*commandInterest2, |
| 100 | bind(&CommandInterestFixture::validated, this, _1), |
| 101 | bind(&CommandInterestFixture::validationFailed, this, _1)); |
| 102 | |
| 103 | BOOST_CHECK_EQUAL(m_validity, false); |
| 104 | |
| 105 | //Test an unauthorized command |
| 106 | Name identity2("/TestCommandInterest/Validation2"); |
| 107 | Name certName2; |
| 108 | BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2)); |
| 109 | |
| 110 | shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3"); |
| 111 | generator.generateWithIdentity(*commandInterest3, identity2); |
| 112 | validator.validate(*commandInterest3, |
| 113 | bind(&CommandInterestFixture::validated, this, _1), |
| 114 | bind(&CommandInterestFixture::validationFailed, this, _1)); |
| 115 | |
| 116 | BOOST_CHECK_EQUAL(m_validity, false); |
| 117 | |
| 118 | //Test another unauthorized command |
| 119 | shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command"); |
| 120 | generator.generateWithIdentity(*commandInterest4, identity); |
| 121 | validator.validate(*commandInterest4, |
| 122 | bind(&CommandInterestFixture::validated, this, _1), |
| 123 | bind(&CommandInterestFixture::validationFailed, this, _1)); |
| 124 | |
| 125 | BOOST_CHECK_EQUAL(m_validity, false); |
| 126 | |
| 127 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 128 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2)); |
| 129 | } |
| 130 | |
| 131 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 132 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 133 | |
| 134 | } // namespace ndn |