blob: 834cc5b30cd85953179bf562707b91857099275c [file] [log] [blame]
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070024
25#include "mgmt/command-validator.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#include "core/config-file.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027
28#include "tests/test-common.hpp"
29
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/util/command-interest-generator.hpp>
31#include <ndn-cxx/util/io.hpp>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070032#include <boost/filesystem.hpp>
Davide Pesavento52a18f92014-04-10 00:55:01 +020033#include <fstream>
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070034
35namespace nfd {
36
37namespace tests {
38
39NFD_LOG_INIT("CommandValidatorTest");
40
41BOOST_FIXTURE_TEST_SUITE(MgmtCommandValidator, BaseFixture)
42
43// authorizations
44// {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070045// authorize
46// {
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070047// certfile "tests/daemon/mgmt/cert1.ndncert"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060048// privileges
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070049// {
50// fib
51// stats
52// }
53// }
54
55// authorize
56// {
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070057// certfile "tests/daemon/mgmt/cert2.ndncert"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060058// privileges
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070059// {
60// faces
61// }
62// }
63// }
64
65const std::string CONFIG =
66"authorizations\n"
67"{\n"
68" authorize\n"
69" {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070070" certfile \"tests/daemon/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070071" privileges\n"
72" {\n"
73" fib\n"
74" stats\n"
75" }\n"
76" }\n"
77" authorize\n"
78" {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070079" certfile \"tests/daemon/mgmt/cert2.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070080" privileges\n"
81" {\n"
82" faces\n"
83" }\n"
84" }\n"
85 "}\n";
86
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060087const boost::filesystem::path CONFIG_PATH =
88 boost::filesystem::current_path() /= std::string("unit-test-nfd.conf");
89
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070090class CommandValidatorTester
91{
92public:
93
94 CommandValidatorTester()
95 : m_validated(false),
96 m_validationFailed(false)
97 {
98
99 }
100
101 void
102 generateIdentity(const Name& prefix)
103 {
104 m_identityName = prefix;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700105 m_identityName.appendVersion();
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700106
107 const Name certName = m_keys.createIdentity(m_identityName);
108
109 m_certificate = m_keys.getCertificate(certName);
110 }
111
112 void
113 saveIdentityToFile(const char* filename)
114 {
115 std::ofstream out;
116 out.open(filename);
117
118 BOOST_REQUIRE(out.is_open());
119 BOOST_REQUIRE(static_cast<bool>(m_certificate));
120
121 ndn::io::save<ndn::IdentityCertificate>(*m_certificate, out);
122
123 out.close();
124 }
125
126 const Name&
127 getIdentityName() const
128 {
129 BOOST_REQUIRE_NE(m_identityName, Name());
130 return m_identityName;
131 }
132
133 const Name&
134 getPublicKeyName() const
135 {
136 BOOST_REQUIRE(static_cast<bool>(m_certificate));
137 return m_certificate->getPublicKeyName();
138 }
139
140 void
141 onValidated(const shared_ptr<const Interest>& interest)
142 {
143 // NFD_LOG_DEBUG("validated command");
144 m_validated = true;
145 }
146
147 void
148 onValidationFailed(const shared_ptr<const Interest>& interest, const std::string& info)
149 {
150 NFD_LOG_DEBUG("validation failed: " << info);
151 m_validationFailed = true;
152 }
153
154 bool
155 commandValidated() const
156 {
157 return m_validated;
158 }
159
160 bool
161 commandValidationFailed() const
162 {
163 return m_validationFailed;
164 }
165
166 void
167 resetValidation()
168 {
169 m_validated = false;
170 m_validationFailed = false;
171 }
172
173 ~CommandValidatorTester()
174 {
175 m_keys.deleteIdentity(m_identityName);
176 }
177
178private:
179 bool m_validated;
180 bool m_validationFailed;
181
182 ndn::KeyChain m_keys;
183 Name m_identityName;
184 shared_ptr<ndn::IdentityCertificate> m_certificate;
185};
186
187class TwoValidatorFixture : public BaseFixture
188{
189public:
190 TwoValidatorFixture()
191 {
192 m_tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1");
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700193 m_tester1.saveIdentityToFile("tests/daemon/mgmt/cert1.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700194
195 m_tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2");
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700196 m_tester2.saveIdentityToFile("tests/daemon/mgmt/cert2.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700197 }
198
199 ~TwoValidatorFixture()
200 {
201 boost::system::error_code error;
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700202 boost::filesystem::remove("tests/daemon/mgmt/cert1.ndncert", error);
203 boost::filesystem::remove("tests/daemon/mgmt/cert2.ndncert", error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700204 }
205
206protected:
207 CommandValidatorTester m_tester1;
208 CommandValidatorTester m_tester2;
209};
210
211BOOST_FIXTURE_TEST_CASE(TwoKeys, TwoValidatorFixture)
212{
213 shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
214 shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething");
215 shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create");
216
217 ndn::CommandInterestGenerator generator;
218 generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName());
219 generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName());
220 generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName());
221
222 ConfigFile config;
223 CommandValidator validator;
224 validator.addSupportedPrivilege("faces");
225 validator.addSupportedPrivilege("fib");
226 validator.addSupportedPrivilege("stats");
227
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600228 validator.setConfigFile(config);
229
230 config.parse(CONFIG, false, CONFIG_PATH.native());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700231
232 validator.validate(*fibCommand,
233 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
234 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
235
236 BOOST_REQUIRE(m_tester1.commandValidated());
237 m_tester1.resetValidation();
238
239 validator.validate(*statsCommand,
240 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
241 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
242
243 BOOST_REQUIRE(m_tester1.commandValidated());
244
245 validator.validate(*facesCommand,
246 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
247 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
248
249 BOOST_REQUIRE(m_tester2.commandValidated());
250 m_tester2.resetValidation();
251
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600252 // use cert2 for fib command (authorized for cert1 only)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700253 shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
254 generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName());
255
256 validator.validate(*unauthorizedFibCommand,
257 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
258 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
259
260 BOOST_REQUIRE(m_tester2.commandValidationFailed());
261}
262
263BOOST_FIXTURE_TEST_CASE(TwoKeysDryRun, TwoValidatorFixture)
264{
265 CommandValidatorTester tester1;
266 tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1");
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700267 tester1.saveIdentityToFile("tests/daemon/mgmt/cert1.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700268
269 CommandValidatorTester tester2;
270 tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2");
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700271 tester2.saveIdentityToFile("tests/daemon/mgmt/cert2.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700272
273 shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
274 shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething");
275 shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create");
276
277 ndn::CommandInterestGenerator generator;
278 generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName());
279 generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName());
280 generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName());
281
282 ConfigFile config;
283 CommandValidator validator;
284 validator.addSupportedPrivilege("faces");
285 validator.addSupportedPrivilege("fib");
286 validator.addSupportedPrivilege("stats");
287
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600288 validator.setConfigFile(config);
289
290 config.parse(CONFIG, true, CONFIG_PATH.native());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700291
292 validator.validate(*fibCommand,
293 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
294 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
295
296 BOOST_REQUIRE(m_tester1.commandValidationFailed());
297 m_tester1.resetValidation();
298
299 validator.validate(*statsCommand,
300 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
301 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
302
303 BOOST_REQUIRE(m_tester1.commandValidationFailed());
304
305 validator.validate(*facesCommand,
306 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
307 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
308
309 BOOST_REQUIRE(m_tester2.commandValidationFailed());
310 m_tester2.resetValidation();
311
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600312 // use cert2 for fib command (authorized for cert1 only)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700313 shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
314 generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName());
315
316 validator.validate(*unauthorizedFibCommand,
317 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
318 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
319
320 BOOST_REQUIRE(m_tester2.commandValidationFailed());
321}
322
323BOOST_AUTO_TEST_CASE(NoAuthorizeSections)
324{
325 const std::string NO_AUTHORIZE_CONFIG =
326 "authorizations\n"
327 "{\n"
328 "}\n";
329
330 ConfigFile config;
331 CommandValidator validator;
332
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600333 validator.setConfigFile(config);
334 BOOST_CHECK_THROW(config.parse(NO_AUTHORIZE_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700335}
336
337BOOST_AUTO_TEST_CASE(NoPrivilegesSections)
338{
339 const std::string NO_PRIVILEGES_CONFIG =
340 "authorizations\n"
341 "{\n"
342 " authorize\n"
343 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700344 " certfile \"tests/daemon/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700345 " }\n"
346 "}\n";
347
348 ConfigFile config;
349 CommandValidator validator;
350
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600351 validator.setConfigFile(config);
352
353 BOOST_CHECK_THROW(config.parse(NO_PRIVILEGES_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700354}
355
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600356BOOST_AUTO_TEST_CASE(InvalidCertfile)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700357{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600358 const std::string INVALID_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700359 "authorizations\n"
360 "{\n"
361 " authorize\n"
362 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700363 " certfile \"tests/daemon/mgmt/notacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700364 " privileges\n"
365 " {\n"
366 " fib\n"
367 " stats\n"
368 " }\n"
369 " }\n"
370 "}\n";
371
372 ConfigFile config;
373 CommandValidator validator;
374
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600375 validator.setConfigFile(config);
376 BOOST_CHECK_THROW(config.parse(INVALID_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700377}
378
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600379BOOST_AUTO_TEST_CASE(NoCertfile)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700380{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600381 const std::string NO_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700382 "authorizations\n"
383 "{\n"
384 " authorize\n"
385 " {\n"
386 " privileges\n"
387 " {\n"
388 " fib\n"
389 " stats\n"
390 " }\n"
391 " }\n"
392 "}\n";
393
394
395 ConfigFile config;
396 CommandValidator validator;
397
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600398 validator.setConfigFile(config);
399 BOOST_CHECK_THROW(config.parse(NO_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700400}
401
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600402BOOST_AUTO_TEST_CASE(MalformedCert)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700403{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600404 const std::string MALFORMED_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700405 "authorizations\n"
406 "{\n"
407 " authorize\n"
408 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700409 " certfile \"tests/daemon/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700410 " privileges\n"
411 " {\n"
412 " fib\n"
413 " stats\n"
414 " }\n"
415 " }\n"
416 "}\n";
417
418
419 ConfigFile config;
420 CommandValidator validator;
421
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600422 validator.setConfigFile(config);
423 BOOST_CHECK_THROW(config.parse(MALFORMED_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700424}
425
426bool
427validateErrorMessage(const std::string& expectedMessage, const ConfigFile::Error& error)
428{
429 bool gotExpected = error.what() == expectedMessage;
430 if (!gotExpected)
431 {
432 NFD_LOG_WARN("\ncaught exception: " << error.what()
433 << "\n\nexpected exception: " << expectedMessage);
434 }
435 return gotExpected;
436}
437
438BOOST_AUTO_TEST_CASE(NoAuthorizeSectionsDryRun)
439{
440 const std::string NO_AUTHORIZE_CONFIG =
441 "authorizations\n"
442 "{\n"
443 "}\n";
444
445 ConfigFile config;
446 CommandValidator validator;
447
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600448 validator.setConfigFile(config);
449 BOOST_CHECK_EXCEPTION(config.parse(NO_AUTHORIZE_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700450 ConfigFile::Error,
451 bind(&validateErrorMessage,
452 "No authorize sections found", _1));
453}
454
455BOOST_FIXTURE_TEST_CASE(NoPrivilegesSectionsDryRun, TwoValidatorFixture)
456{
457 const std::string NO_PRIVILEGES_CONFIG =
458 "authorizations\n"
459 "{\n"
460 " authorize\n"
461 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700462 " certfile \"tests/daemon/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700463 " }\n"
464 " authorize\n"
465 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700466 " certfile \"tests/daemon/mgmt/cert2.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700467 " }\n"
468 "}\n";
469
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700470 ConfigFile config;
471 CommandValidator validator;
472
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600473 validator.setConfigFile(config);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700474
475 std::stringstream expectedError;
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700476 expectedError << "No privileges section found for certificate file tests/daemon/mgmt/cert1.ndncert "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700477 << "(" << m_tester1.getPublicKeyName().toUri() << ")\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700478 << "No privileges section found for certificate file tests/daemon/mgmt/cert2.ndncert "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700479 << "(" << m_tester2.getPublicKeyName().toUri() << ")";
480
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600481 BOOST_CHECK_EXCEPTION(config.parse(NO_PRIVILEGES_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700482 ConfigFile::Error,
483 bind(&validateErrorMessage, expectedError.str(), _1));
484}
485
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600486BOOST_AUTO_TEST_CASE(InvalidCertfileDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700487{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600488 using namespace boost::filesystem;
489
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700490 const std::string INVALID_KEY_CONFIG =
491 "authorizations\n"
492 "{\n"
493 " authorize\n"
494 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700495 " certfile \"tests/daemon/mgmt/notacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700496 " privileges\n"
497 " {\n"
498 " fib\n"
499 " stats\n"
500 " }\n"
501 " }\n"
502 " authorize\n"
503 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700504 " certfile \"tests/daemon/mgmt/stillnotacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700505 " privileges\n"
506 " {\n"
507 " }\n"
508 " }\n"
509 "}\n";
510
511 ConfigFile config;
512 CommandValidator validator;
513
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600514 validator.setConfigFile(config);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700515
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600516 std::stringstream error;
517 error << "Unable to open certificate file "
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700518 << absolute("tests/daemon/mgmt/notacertfile.ndncert").native() << "\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600519 << "Unable to open certificate file "
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700520 << absolute("tests/daemon/mgmt/stillnotacertfile.ndncert").native();
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600521
522 BOOST_CHECK_EXCEPTION(config.parse(INVALID_KEY_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700523 ConfigFile::Error,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600524 bind(&validateErrorMessage, error.str(), _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700525}
526
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600527BOOST_AUTO_TEST_CASE(NoCertfileDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700528{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600529 const std::string NO_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700530 "authorizations\n"
531 "{\n"
532 " authorize\n"
533 " {\n"
534 " privileges\n"
535 " {\n"
536 " fib\n"
537 " stats\n"
538 " }\n"
539 " }\n"
540 " authorize\n"
541 " {\n"
542 " }\n"
543 "}\n";
544
545
546 ConfigFile config;
547 CommandValidator validator;
548
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600549 validator.setConfigFile(config);
550 BOOST_CHECK_EXCEPTION(config.parse(NO_CERT_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700551 ConfigFile::Error,
552 bind(&validateErrorMessage,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600553 "No certfile specified\n"
554 "No certfile specified", _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700555}
556
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600557BOOST_AUTO_TEST_CASE(MalformedCertDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700558{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600559 using namespace boost::filesystem;
560
561 const std::string MALFORMED_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700562 "authorizations\n"
563 "{\n"
564 " authorize\n"
565 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700566 " certfile \"tests/daemon/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700567 " privileges\n"
568 " {\n"
569 " fib\n"
570 " stats\n"
571 " }\n"
572 " }\n"
573 " authorize\n"
574 " {\n"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700575 " certfile \"tests/daemon/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700576 " }\n"
577 "}\n";
578
579
580 ConfigFile config;
581 CommandValidator validator;
582
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600583 validator.setConfigFile(config);
584
585 std::stringstream error;
586 error << "Malformed certificate file "
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700587 << absolute("tests/daemon/mgmt/malformed.ndncert").native() << "\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600588 << "Malformed certificate file "
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700589 << absolute("tests/daemon/mgmt/malformed.ndncert").native();
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600590
591 BOOST_CHECK_EXCEPTION(config.parse(MALFORMED_CERT_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700592 ConfigFile::Error,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600593 bind(&validateErrorMessage, error.str(), _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700594}
595
Yingdi Yuc8f214c2014-04-29 20:39:37 -0700596BOOST_FIXTURE_TEST_CASE(Wildcard, TwoValidatorFixture)
597{
598 const std::string WILDCARD_CERT_CONFIG =
599 "authorizations\n"
600 "{\n"
601 " authorize\n"
602 " {\n"
603 " certfile any\n"
604 " privileges\n"
605 " {\n"
606 " faces\n"
607 " stats\n"
608 " }\n"
609 " }\n"
610 "}\n";
611
612 shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
613 shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething");
614 shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create");
615
616 ndn::CommandInterestGenerator generator;
617 generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName());
618 generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName());
619 generator.generateWithIdentity(*facesCommand, m_tester1.getIdentityName());
620
621 ConfigFile config;
622 CommandValidator validator;
623 validator.addSupportedPrivilege("faces");
624 validator.addSupportedPrivilege("fib");
625 validator.addSupportedPrivilege("stats");
626
627 validator.setConfigFile(config);
628
629 config.parse(WILDCARD_CERT_CONFIG, false, CONFIG_PATH.native());
630
631 validator.validate(*fibCommand,
632 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
633 bind(&CommandValidatorTester::onValidationFailed,
634 boost::ref(m_tester1), _1, _2));
635
636 BOOST_REQUIRE(m_tester1.commandValidationFailed());
637 m_tester1.resetValidation();
638
639 validator.validate(*statsCommand,
640 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
641 bind(&CommandValidatorTester::onValidationFailed,
642 boost::ref(m_tester1), _1, _2));
643
644 BOOST_REQUIRE(m_tester1.commandValidated());
645 m_tester1.resetValidation();
646
647 validator.validate(*facesCommand,
648 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
649 bind(&CommandValidatorTester::onValidationFailed,
650 boost::ref(m_tester1), _1, _2));
651
652 BOOST_REQUIRE(m_tester1.commandValidated());
653 m_tester1.resetValidation();
654}
655
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700656BOOST_AUTO_TEST_SUITE_END()
657
658} // namespace tests
659
660} // namespace nfd