blob: f6898b9b5984cb4e17eb51a4ff98ce892558417e [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
Yingdi Yuc4f6fd72014-02-26 12:48:44 -080012#include "util/command-interest-generator.hpp"
13#include "util/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
Yingdi Yu40587c02014-02-21 16:40:48 -080057 validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo)
58 {
59 m_validity = false;
60 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080061
62 void
63 reset()
64 { m_validity = false; }
65
66 bool m_validity;
67};
68
69BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
70{
71 KeyChain keyChain;
Yingdi Yu40587c02014-02-21 16:40:48 -080072 Name identity("/TestCommandInterest/Validation/" + boost::lexical_cast<string>(time::now()));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080073 Name certName;
74 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
75
76 CommandInterestGenerator generator;
77 CommandInterestValidator validator;
78
79 validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName));
80
81 //Test a legitimate command
82 shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1");
83 generator.generateWithIdentity(*commandInterest1, identity);
84 validator.validate(*commandInterest1,
85 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -080086 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080087
88 BOOST_CHECK_EQUAL(m_validity, true);
89
90 //Test an outdated command
91 reset();
92 shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
93 int64_t timestamp = time::now() / 1000000;
94 timestamp -= 5000;
95 Name commandName = commandInterest2->getName();
96 commandName
97 .append(name::Component::fromNumber(timestamp))
98 .append(name::Component::fromNumber(random::generateWord64()));
99 commandInterest2->setName(commandName);
100
101 keyChain.signByIdentity(*commandInterest2, identity);
102 validator.validate(*commandInterest2,
103 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800104 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800105
106 BOOST_CHECK_EQUAL(m_validity, false);
107
108 //Test an unauthorized command
109 Name identity2("/TestCommandInterest/Validation2");
110 Name certName2;
111 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
112
113 shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
114 generator.generateWithIdentity(*commandInterest3, identity2);
115 validator.validate(*commandInterest3,
116 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800117 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800118
119 BOOST_CHECK_EQUAL(m_validity, false);
120
121 //Test another unauthorized command
122 shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command");
123 generator.generateWithIdentity(*commandInterest4, identity);
124 validator.validate(*commandInterest4,
125 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800126 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800127
128 BOOST_CHECK_EQUAL(m_validity, false);
129
130 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
131 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
132}
133
134
Yingdi Yu4270f202014-01-28 14:19:16 -0800135BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800136
137} // namespace ndn