blob: 80f55a4c0ab6cd07911ef9841349ab0e8f6005ae [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 Yu17bc3012014-02-10 17:37:12 -080024 Name identityName("/TestSignedInterest/SignVerify");
25 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 Yu4270f202014-01-28 14:19:16 -080029 keyChain.signByIdentity(interest, identityName);
30
31 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
32
33 Interest interest2;
34 interest2.wireDecode(interestBlock);
35
Yingdi Yu17bc3012014-02-10 17:37:12 -080036 shared_ptr<PublicKey> publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName));
Yingdi Yu6ac97982014-01-30 14:49:21 -080037 bool result = Validator::verifySignature(interest2, *publicKey);
Yingdi Yu4270f202014-01-28 14:19:16 -080038
Yingdi Yu17bc3012014-02-10 17:37:12 -080039 BOOST_CHECK_EQUAL(result, true);
40
41 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080042}
43
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080044class CommandInterestFixture
45{
46public:
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
66BOOST_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 Yu4270f202014-01-28 14:19:16 -0800132BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800133
134} // namespace ndn