blob: d7f3f6cd5afc2d65072b094a0187e73dedb064f9 [file] [log] [blame]
Yingdi Yu0f5fb692014-06-10 12:07:28 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/command-interest-generator.hpp"
23#include "util/command-interest-validator.hpp"
24
25#include "boost-test.hpp"
26
27namespace ndn {
28
29BOOST_AUTO_TEST_SUITE(SecurityTestCommandInterest)
30
31class CommandInterestFixture
32{
33public:
34 CommandInterestFixture()
35 : m_validity(false)
36 {
37 }
38
39 void
40 validated(const shared_ptr<const Interest>& interest)
41 {
42 m_validity = true;
43 }
44
45 void
46 validationFailed(const shared_ptr<const Interest>& interest, const std::string& failureInfo)
47 {
48 m_validity = false;
49 }
50
51 void
52 reset()
53 {
54 m_validity = false;
55 }
56
57 bool m_validity;
58};
59
60BOOST_FIXTURE_TEST_CASE(CommandInterest, CommandInterestFixture)
61{
62 KeyChain keyChain;
63 Name identity("/TestCommandInterest/Validation");
64 identity.appendVersion();
65
66 Name certName;
67 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
68
69 CommandInterestGenerator generator;
70 CommandInterestValidator validator;
71
72 validator.addInterestRule("^<TestCommandInterest><Validation>",
73 *keyChain.getCertificate(certName));
74
75 //Test a legitimate command
76 shared_ptr<Interest> commandInterest1 =
77 make_shared<Interest>("/TestCommandInterest/Validation/Command1");
78 generator.generateWithIdentity(*commandInterest1, identity);
79 validator.validate(*commandInterest1,
80 bind(&CommandInterestFixture::validated, this, _1),
81 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
82
83 BOOST_CHECK_EQUAL(m_validity, true);
84
85 //Test an outdated command
86 reset();
87 shared_ptr<Interest> commandInterest2 =
88 make_shared<Interest>("/TestCommandInterest/Validation/Command2");
89 keyChain.signByIdentity(*commandInterest2, identity);
90
91 sleep(1);
92
93 shared_ptr<Interest> commandInterest21 =
94 make_shared<Interest>("/TestCommandInterest/Validation/Command3");
95 keyChain.signByIdentity(*commandInterest21, identity);
96
97 reset();
98 validator.validate(*commandInterest21,
99 bind(&CommandInterestFixture::validated, this, _1),
100 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
101 BOOST_CHECK_EQUAL(m_validity, true);
102
103 reset();
104 validator.validate(*commandInterest2,
105 bind(&CommandInterestFixture::validated, this, _1),
106 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
107 BOOST_CHECK_EQUAL(m_validity, false);
108
109 //Test an unauthorized command
110 Name identity2("/TestCommandInterest/Validation2");
111 Name certName2;
112 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
113
114 shared_ptr<Interest> commandInterest3 =
115 make_shared<Interest>("/TestCommandInterest/Validation/Command3");
116 generator.generateWithIdentity(*commandInterest3, identity2);
117 validator.validate(*commandInterest3,
118 bind(&CommandInterestFixture::validated, this, _1),
119 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
120
121 BOOST_CHECK_EQUAL(m_validity, false);
122
123 //Test another unauthorized command
124 shared_ptr<Interest> commandInterest4 =
125 make_shared<Interest>("/TestCommandInterest/Validation2/Command");
126 generator.generateWithIdentity(*commandInterest4, identity);
127 validator.validate(*commandInterest4,
128 bind(&CommandInterestFixture::validated, this, _1),
129 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
130
131 BOOST_CHECK_EQUAL(m_validity, false);
132
133 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
134 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
135}
136
137BOOST_FIXTURE_TEST_CASE(Exemption, CommandInterestFixture)
138{
139 KeyChain keyChain;
140 Name identity("/TestCommandInterest/AnyKey");
141
142 Name certName;
143 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
144
145 CommandInterestGenerator generator;
146 CommandInterestValidator validator;
147
148 validator.addInterestBypassRule("^<TestCommandInterest><Exemption>");
149
150 //Test a legitimate command
151 shared_ptr<Interest> commandInterest1 =
152 make_shared<Interest>("/TestCommandInterest/Exemption/Command1");
153 generator.generateWithIdentity(*commandInterest1, identity);
154 validator.validate(*commandInterest1,
155 bind(&CommandInterestFixture::validated, this, _1),
156 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
157
158 BOOST_CHECK_EQUAL(m_validity, true);
159
160 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
161}
162
163BOOST_AUTO_TEST_SUITE_END()
164
165}