blob: 3adb0a44a9322a1b44c24ded573441ce32b30243 [file] [log] [blame]
Yingdi Yu4270f202014-01-28 14:19:16 -08001/**
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 Yu6ac97982014-01-30 14:49:21 -080010#include "security/validator.hpp"
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080011
12#include "helpers/command-interest-generator.hpp"
13#include "helpers/command-interest-validator.hpp"
Yingdi Yu4270f202014-01-28 14:19:16 -080014
15using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080016namespace ndn {
Yingdi Yu4270f202014-01-28 14:19:16 -080017
18BOOST_AUTO_TEST_SUITE(TestSignedInterest)
19
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080020BOOST_AUTO_TEST_CASE (SignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080021{
22 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
23
Yingdi Yu2e57a582014-02-20 23:34:43 -080024 Name identityName("/TestSignedInterest/SignVerify/" + boost::lexical_cast<string>(time::now()));
Yingdi Yu17bc3012014-02-10 17:37:12 -080025 Name certificateName;
26 BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080027
Yingdi Yu17bc3012014-02-10 17:37:12 -080028 Interest interest("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu2e57a582014-02-20 23:34:43 -080029 BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080030
31 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
32
33 Interest interest2;
34 interest2.wireDecode(interestBlock);
35
Yingdi Yu2e57a582014-02-20 23:34:43 -080036 shared_ptr<PublicKey> publicKey;
37 BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName)));
Yingdi Yu6ac97982014-01-30 14:49:21 -080038 bool result = Validator::verifySignature(interest2, *publicKey);
Yingdi Yu4270f202014-01-28 14:19:16 -080039
Yingdi Yu17bc3012014-02-10 17:37:12 -080040 BOOST_CHECK_EQUAL(result, true);
41
Yingdi Yu2e57a582014-02-20 23:34:43 -080042 keyChain.deleteIdentity(identityName);
Yingdi Yu4270f202014-01-28 14:19:16 -080043}
44
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080045class CommandInterestFixture
46{
47public:
48 CommandInterestFixture()
49 : m_validity(false)
50 {}
51
52 void
53 validated(const shared_ptr<const Interest>& interest)
54 { m_validity = true; }
55
56 void
57 validationFailed(const shared_ptr<const Interest>& interest)
58 { m_validity = false; }
59
60 void
61 reset()
62 { m_validity = false; }
63
64 bool m_validity;
65};
66
67BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
68{
69 KeyChain keyChain;
70 Name identity("/TestCommandInterest/Validation");
71 Name certName;
72 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
73
74 CommandInterestGenerator generator;
75 CommandInterestValidator validator;
76
77 validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName));
78
79 //Test a legitimate command
80 shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1");
81 generator.generateWithIdentity(*commandInterest1, identity);
82 validator.validate(*commandInterest1,
83 bind(&CommandInterestFixture::validated, this, _1),
84 bind(&CommandInterestFixture::validationFailed, this, _1));
85
86 BOOST_CHECK_EQUAL(m_validity, true);
87
88 //Test an outdated command
89 reset();
90 shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
91 int64_t timestamp = time::now() / 1000000;
92 timestamp -= 5000;
93 Name commandName = commandInterest2->getName();
94 commandName
95 .append(name::Component::fromNumber(timestamp))
96 .append(name::Component::fromNumber(random::generateWord64()));
97 commandInterest2->setName(commandName);
98
99 keyChain.signByIdentity(*commandInterest2, identity);
100 validator.validate(*commandInterest2,
101 bind(&CommandInterestFixture::validated, this, _1),
102 bind(&CommandInterestFixture::validationFailed, this, _1));
103
104 BOOST_CHECK_EQUAL(m_validity, false);
105
106 //Test an unauthorized command
107 Name identity2("/TestCommandInterest/Validation2");
108 Name certName2;
109 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
110
111 shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
112 generator.generateWithIdentity(*commandInterest3, identity2);
113 validator.validate(*commandInterest3,
114 bind(&CommandInterestFixture::validated, this, _1),
115 bind(&CommandInterestFixture::validationFailed, this, _1));
116
117 BOOST_CHECK_EQUAL(m_validity, false);
118
119 //Test another unauthorized command
120 shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command");
121 generator.generateWithIdentity(*commandInterest4, identity);
122 validator.validate(*commandInterest4,
123 bind(&CommandInterestFixture::validated, this, _1),
124 bind(&CommandInterestFixture::validationFailed, this, _1));
125
126 BOOST_CHECK_EQUAL(m_validity, false);
127
128 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
129 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
130}
131
132
Yingdi Yu4270f202014-01-28 14:19:16 -0800133BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800134
135} // namespace ndn