blob: 469c3337a894933558ea487b12afc8dee3c9b455 [file] [log] [blame]
Junxiao Shid7631272016-08-17 04:16:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shia9079802017-08-26 14:12:30 +00002/*
Alexander Afanasyev635bf202017-03-09 21:57:34 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shid7631272016-08-17 04:16:31 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "mgmt/command-authenticator.hpp"
Junxiao Shid7631272016-08-17 04:16:31 +000027
28#include "tests/test-common.hpp"
Junxiao Shi8a1f1702017-07-03 00:05:08 +000029#include "tests/manager-common-fixture.hpp"
Junxiao Shid7631272016-08-17 04:16:31 +000030
Junxiao Shia9079802017-08-26 14:12:30 +000031#include <boost/filesystem.hpp>
32
Junxiao Shid7631272016-08-17 04:16:31 +000033namespace nfd {
34namespace tests {
35
Junxiao Shi8a1f1702017-07-03 00:05:08 +000036class CommandAuthenticatorFixture : public CommandInterestSignerFixture
Junxiao Shid7631272016-08-17 04:16:31 +000037{
38protected:
39 CommandAuthenticatorFixture()
40 : authenticator(CommandAuthenticator::create())
41 {
42 }
43
44 void
45 makeModules(std::initializer_list<std::string> modules)
46 {
47 for (const std::string& module : modules) {
48 authorizations.emplace(module, authenticator->makeAuthorization(module, "verb"));
49 }
50 }
51
52 void
53 loadConfig(const std::string& config)
54 {
55 boost::filesystem::path configPath = boost::filesystem::current_path() /=
56 "command-authenticator-test.conf";
57 ConfigFile cf;
58 authenticator->setConfigFile(cf);
59 cf.parse(config, false, configPath.c_str());
60 }
61
62 bool
63 authorize(const std::string& module, const Name& identity,
64 const function<void(Interest&)>& modifyInterest = nullptr)
65 {
Junxiao Shi8a1f1702017-07-03 00:05:08 +000066 Interest interest = this->makeControlCommandRequest(Name("/prefix/" + module + "/verb"),
67 ControlParameters(), identity);
Junxiao Shid7631272016-08-17 04:16:31 +000068 if (modifyInterest != nullptr) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +000069 modifyInterest(interest);
Junxiao Shid7631272016-08-17 04:16:31 +000070 }
71
72 ndn::mgmt::Authorization authorization = authorizations.at(module);
73
74 bool isAccepted = false;
75 bool isRejected = false;
Junxiao Shi8a1f1702017-07-03 00:05:08 +000076 authorization(Name("/prefix"), interest, nullptr,
Junxiao Shid7631272016-08-17 04:16:31 +000077 [this, &isAccepted, &isRejected] (const std::string& requester) {
78 BOOST_REQUIRE_MESSAGE(!isAccepted && !isRejected,
79 "authorization function should invoke only one continuation");
80 isAccepted = true;
81 lastRequester = requester;
82 },
83 [this, &isAccepted, &isRejected] (ndn::mgmt::RejectReply act) {
84 BOOST_REQUIRE_MESSAGE(!isAccepted && !isRejected,
85 "authorization function should invoke only one continuation");
86 isRejected = true;
87 lastRejectReply = act;
88 });
89
90 this->advanceClocks(time::milliseconds(1), 10);
91 BOOST_REQUIRE_MESSAGE(isAccepted || isRejected,
92 "authorization function should invoke one continuation");
93 return isAccepted;
94 }
95
96protected:
97 shared_ptr<CommandAuthenticator> authenticator;
98 std::unordered_map<std::string, ndn::mgmt::Authorization> authorizations;
99 std::string lastRequester;
100 ndn::mgmt::RejectReply lastRejectReply;
101};
102
103BOOST_AUTO_TEST_SUITE(Mgmt)
104BOOST_FIXTURE_TEST_SUITE(TestCommandAuthenticator, CommandAuthenticatorFixture)
105
106BOOST_AUTO_TEST_CASE(Certs)
107{
108 Name id0("/localhost/CommandAuthenticator/0");
109 Name id1("/localhost/CommandAuthenticator/1");
110 Name id2("/localhost/CommandAuthenticator/2");
111 BOOST_REQUIRE(addIdentity(id0));
112 BOOST_REQUIRE(saveIdentityCertificate(id1, "1.ndncert", true));
113 BOOST_REQUIRE(saveIdentityCertificate(id2, "2.ndncert", true));
114
115 makeModules({"module0", "module1", "module2", "module3", "module4", "module5", "module6", "module7"});
116 const std::string& config = R"CONFIG(
117 authorizations
118 {
119 authorize
120 {
121 certfile any
122 privileges
123 {
124 module1
125 module3
126 module5
127 module7
128 }
129 }
130 authorize
131 {
132 certfile "1.ndncert"
133 privileges
134 {
135 module2
136 module3
137 module6
138 module7
139 }
140 }
141 authorize
142 {
143 certfile "2.ndncert"
144 privileges
145 {
146 module4
147 module5
148 module6
149 module7
150 }
151 }
152 }
153 )CONFIG";
154 loadConfig(config);
155
156 // module0: none
157 BOOST_CHECK_EQUAL(authorize("module0", id0), false);
158 BOOST_CHECK_EQUAL(authorize("module0", id1), false);
159 BOOST_CHECK_EQUAL(authorize("module0", id2), false);
160
161 // module1: any
162 BOOST_CHECK_EQUAL(authorize("module1", id0), true);
163 BOOST_CHECK_EQUAL(authorize("module1", id1), true);
164 BOOST_CHECK_EQUAL(authorize("module1", id2), true);
165
166 // module2: id1
167 BOOST_CHECK_EQUAL(authorize("module2", id0), false);
168 BOOST_CHECK_EQUAL(authorize("module2", id1), true);
169 BOOST_CHECK_EQUAL(authorize("module2", id2), false);
170
171 // module3: any,id1
172 BOOST_CHECK_EQUAL(authorize("module3", id0), true);
173 BOOST_CHECK_EQUAL(authorize("module3", id1), true);
174 BOOST_CHECK_EQUAL(authorize("module3", id2), true);
175
176 // module4: id2
177 BOOST_CHECK_EQUAL(authorize("module4", id0), false);
178 BOOST_CHECK_EQUAL(authorize("module4", id1), false);
179 BOOST_CHECK_EQUAL(authorize("module4", id2), true);
180
181 // module5: any,id2
182 BOOST_CHECK_EQUAL(authorize("module5", id0), true);
183 BOOST_CHECK_EQUAL(authorize("module5", id1), true);
184 BOOST_CHECK_EQUAL(authorize("module5", id2), true);
185
186 // module6: id1,id2
187 BOOST_CHECK_EQUAL(authorize("module6", id0), false);
188 BOOST_CHECK_EQUAL(authorize("module6", id1), true);
189 BOOST_CHECK_EQUAL(authorize("module6", id2), true);
190
191 // module7: any,id1,id2
192 BOOST_CHECK_EQUAL(authorize("module7", id0), true);
193 BOOST_CHECK_EQUAL(authorize("module7", id1), true);
194 BOOST_CHECK_EQUAL(authorize("module7", id2), true);
195}
196
197BOOST_AUTO_TEST_CASE(Requester)
198{
199 Name id0("/localhost/CommandAuthenticator/0");
200 Name id1("/localhost/CommandAuthenticator/1");
201 BOOST_REQUIRE(addIdentity(id0));
202 BOOST_REQUIRE(saveIdentityCertificate(id1, "1.ndncert", true));
203
204 makeModules({"module0", "module1"});
205 const std::string& config = R"CONFIG(
206 authorizations
207 {
208 authorize
209 {
210 certfile any
211 privileges
212 {
213 module0
214 }
215 }
216 authorize
217 {
218 certfile "1.ndncert"
219 privileges
220 {
221 module1
222 }
223 }
224 }
225 )CONFIG";
226 loadConfig(config);
227
228 // module0: any
229 BOOST_CHECK_EQUAL(authorize("module0", id0), true);
230 BOOST_CHECK_EQUAL(lastRequester, "*");
231 BOOST_CHECK_EQUAL(authorize("module0", id1), true);
232 BOOST_CHECK_EQUAL(lastRequester, "*");
233
234 // module1: id1
235 BOOST_CHECK_EQUAL(authorize("module1", id0), false);
236 BOOST_CHECK_EQUAL(authorize("module1", id1), true);
237 BOOST_CHECK(id1.isPrefixOf(lastRequester));
238}
239
240class IdentityAuthorizedFixture : public CommandAuthenticatorFixture
241{
242protected:
243 IdentityAuthorizedFixture()
244 : id1("/localhost/CommandAuthenticator/1")
245 {
246 BOOST_REQUIRE(saveIdentityCertificate(id1, "1.ndncert", true));
247
248 makeModules({"module1"});
249 const std::string& config = R"CONFIG(
250 authorizations
251 {
252 authorize
253 {
254 certfile "1.ndncert"
255 privileges
256 {
257 module1
258 }
259 }
260 }
261 )CONFIG";
262 loadConfig(config);
263 }
264
265 bool
266 authorize1(const function<void(Interest&)>& modifyInterest)
267 {
268 return authorize("module1", id1, modifyInterest);
269 }
270
271protected:
272 Name id1;
273};
274
275BOOST_FIXTURE_TEST_SUITE(Rejects, IdentityAuthorizedFixture)
276
277BOOST_AUTO_TEST_CASE(BadKeyLocator_NameTooShort)
278{
279 BOOST_CHECK_EQUAL(authorize1(
280 [] (Interest& interest) {
281 interest.setName("/prefix");
282 }
283 ), false);
284 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::SILENT);
285}
286
287BOOST_AUTO_TEST_CASE(BadKeyLocator_BadSigInfo)
288{
289 BOOST_CHECK_EQUAL(authorize1(
290 [] (Interest& interest) {
291 setNameComponent(interest, ndn::signed_interest::POS_SIG_INFO, "not-SignatureInfo");
292 }
293 ), false);
294 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::SILENT);
295}
296
297BOOST_AUTO_TEST_CASE(BadKeyLocator_MissingKeyLocator)
298{
299 BOOST_CHECK_EQUAL(authorize1(
300 [] (Interest& interest) {
Junxiao Shia9079802017-08-26 14:12:30 +0000301 ndn::SignatureInfo sigInfo(tlv::SignatureSha256WithRsa);
Junxiao Shid7631272016-08-17 04:16:31 +0000302 setNameComponent(interest, ndn::signed_interest::POS_SIG_INFO,
303 sigInfo.wireEncode().begin(), sigInfo.wireEncode().end());
304 }
305 ), false);
306 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::SILENT);
307}
308
309BOOST_AUTO_TEST_CASE(BadKeyLocator_BadKeyLocatorType)
310{
311 BOOST_CHECK_EQUAL(authorize1(
312 [] (Interest& interest) {
313 ndn::KeyLocator kl;
314 kl.setKeyDigest(ndn::encoding::makeBinaryBlock(tlv::KeyDigest, "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD", 8));
Junxiao Shia9079802017-08-26 14:12:30 +0000315 ndn::SignatureInfo sigInfo(tlv::SignatureSha256WithRsa);
Junxiao Shid7631272016-08-17 04:16:31 +0000316 sigInfo.setKeyLocator(kl);
317 setNameComponent(interest, ndn::signed_interest::POS_SIG_INFO,
318 sigInfo.wireEncode().begin(), sigInfo.wireEncode().end());
319 }
320 ), false);
321 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::SILENT);
322}
323
Junxiao Shid7631272016-08-17 04:16:31 +0000324BOOST_AUTO_TEST_CASE(NotAuthorized)
325{
326 Name id0("/localhost/CommandAuthenticator/0");
327 BOOST_REQUIRE(addIdentity(id0));
328
329 BOOST_CHECK_EQUAL(authorize("module1", id0), false);
330 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::STATUS403);
331}
332
333BOOST_AUTO_TEST_CASE(BadSig)
334{
335 BOOST_CHECK_EQUAL(authorize1(
336 [] (Interest& interest) {
Alexander Afanasyev635bf202017-03-09 21:57:34 +0000337 setNameComponent(interest, ndn::command_interest::POS_SIG_VALUE, "bad-signature-bits");
Junxiao Shid7631272016-08-17 04:16:31 +0000338 }
339 ), false);
340 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::STATUS403);
341}
342
343BOOST_AUTO_TEST_CASE(InvalidTimestamp)
344{
345 name::Component timestampComp;
346 BOOST_CHECK_EQUAL(authorize1(
347 [&timestampComp] (const Interest& interest) {
Alexander Afanasyev635bf202017-03-09 21:57:34 +0000348 timestampComp = interest.getName().at(ndn::command_interest::POS_TIMESTAMP);
Junxiao Shid7631272016-08-17 04:16:31 +0000349 }
350 ), true); // accept first command
351 BOOST_CHECK_EQUAL(authorize1(
352 [&timestampComp] (Interest& interest) {
Alexander Afanasyev635bf202017-03-09 21:57:34 +0000353 setNameComponent(interest, ndn::command_interest::POS_TIMESTAMP, timestampComp);
Junxiao Shid7631272016-08-17 04:16:31 +0000354 }
355 ), false); // reject second command because timestamp equals first command
356 BOOST_CHECK(lastRejectReply == ndn::mgmt::RejectReply::STATUS403);
357}
358
359BOOST_AUTO_TEST_SUITE_END() // Rejects
360
361BOOST_AUTO_TEST_SUITE(BadConfig)
362
363BOOST_AUTO_TEST_CASE(EmptyAuthorizationsSection)
364{
365 const std::string& config = R"CONFIG(
366 authorizations
367 {
368 }
369 )CONFIG";
370
371 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
372}
373
374BOOST_AUTO_TEST_CASE(UnrecognizedKey)
375{
376 const std::string& config = R"CONFIG(
377 authorizations
378 {
379 unrecognized_key
380 {
381 }
382 }
383 )CONFIG";
384
385 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
386}
387
388BOOST_AUTO_TEST_CASE(CertfileMissing)
389{
390 const std::string& config = R"CONFIG(
391 authorizations
392 {
393 authorize
394 {
395 privileges
396 {
397 }
398 }
399 }
400 )CONFIG";
401
402 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
403}
404
405BOOST_AUTO_TEST_CASE(CertUnreadable)
406{
407 const std::string& config = R"CONFIG(
408 authorizations
409 {
410 authorize
411 {
412 certfile "1.ndncert"
413 privileges
414 {
415 }
416 }
417 }
418 )CONFIG";
419
420 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
421}
422
423BOOST_AUTO_TEST_CASE(PrivilegesMissing)
424{
425 const std::string& config = R"CONFIG(
426 authorizations
427 {
428 authorize
429 {
430 certfile any
431 }
432 }
433 )CONFIG";
434
435 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
436}
437
438BOOST_AUTO_TEST_CASE(UnregisteredModule)
439{
440 const std::string& config = R"CONFIG(
441 authorizations
442 {
443 authorize
444 {
445 certfile any
446 privileges
447 {
448 nosuchmodule
449 }
450 }
451 }
452 )CONFIG";
453
454 BOOST_CHECK_THROW(loadConfig(config), ConfigFile::Error);
455}
456
457BOOST_AUTO_TEST_SUITE_END() // BadConfig
458
459BOOST_AUTO_TEST_SUITE_END() // TestCommandAuthenticator
460BOOST_AUTO_TEST_SUITE_END() // Mgmt
461
462} // namespace tests
463} // namespace nfd