blob: e2cf51c0f6677f4715f058ffeee204121532a9d0 [file] [log] [blame]
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/command-validator.hpp"
8#include "mgmt/config-file.hpp"
9
10#include "tests/test-common.hpp"
11
12#include <boost/test/unit_test.hpp>
13#include <ndn-cpp-dev/util/command-interest-generator.hpp>
14#include <ndn-cpp-dev/util/io.hpp>
15#include <boost/filesystem.hpp>
16
17namespace nfd {
18
19namespace tests {
20
21NFD_LOG_INIT("CommandValidatorTest");
22
23BOOST_FIXTURE_TEST_SUITE(MgmtCommandValidator, BaseFixture)
24
25// authorizations
26// {
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027// authorize
28// {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060029// certfile "tests/mgmt/cert1.ndncert"
30// privileges
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070031// {
32// fib
33// stats
34// }
35// }
36
37// authorize
38// {
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060039// certfile "tests/mgmt/cert2.ndncert"
40// privileges
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070041// {
42// faces
43// }
44// }
45// }
46
47const std::string CONFIG =
48"authorizations\n"
49"{\n"
50" authorize\n"
51" {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060052" certfile \"tests/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070053" privileges\n"
54" {\n"
55" fib\n"
56" stats\n"
57" }\n"
58" }\n"
59" authorize\n"
60" {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060061" certfile \"tests/mgmt/cert2.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070062" privileges\n"
63" {\n"
64" faces\n"
65" }\n"
66" }\n"
67 "}\n";
68
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060069const boost::filesystem::path CONFIG_PATH =
70 boost::filesystem::current_path() /= std::string("unit-test-nfd.conf");
71
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070072class CommandValidatorTester
73{
74public:
75
76 CommandValidatorTester()
77 : m_validated(false),
78 m_validationFailed(false)
79 {
80
81 }
82
83 void
84 generateIdentity(const Name& prefix)
85 {
86 m_identityName = prefix;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070087 m_identityName.appendVersion();
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070088
89 const Name certName = m_keys.createIdentity(m_identityName);
90
91 m_certificate = m_keys.getCertificate(certName);
92 }
93
94 void
95 saveIdentityToFile(const char* filename)
96 {
97 std::ofstream out;
98 out.open(filename);
99
100 BOOST_REQUIRE(out.is_open());
101 BOOST_REQUIRE(static_cast<bool>(m_certificate));
102
103 ndn::io::save<ndn::IdentityCertificate>(*m_certificate, out);
104
105 out.close();
106 }
107
108 const Name&
109 getIdentityName() const
110 {
111 BOOST_REQUIRE_NE(m_identityName, Name());
112 return m_identityName;
113 }
114
115 const Name&
116 getPublicKeyName() const
117 {
118 BOOST_REQUIRE(static_cast<bool>(m_certificate));
119 return m_certificate->getPublicKeyName();
120 }
121
122 void
123 onValidated(const shared_ptr<const Interest>& interest)
124 {
125 // NFD_LOG_DEBUG("validated command");
126 m_validated = true;
127 }
128
129 void
130 onValidationFailed(const shared_ptr<const Interest>& interest, const std::string& info)
131 {
132 NFD_LOG_DEBUG("validation failed: " << info);
133 m_validationFailed = true;
134 }
135
136 bool
137 commandValidated() const
138 {
139 return m_validated;
140 }
141
142 bool
143 commandValidationFailed() const
144 {
145 return m_validationFailed;
146 }
147
148 void
149 resetValidation()
150 {
151 m_validated = false;
152 m_validationFailed = false;
153 }
154
155 ~CommandValidatorTester()
156 {
157 m_keys.deleteIdentity(m_identityName);
158 }
159
160private:
161 bool m_validated;
162 bool m_validationFailed;
163
164 ndn::KeyChain m_keys;
165 Name m_identityName;
166 shared_ptr<ndn::IdentityCertificate> m_certificate;
167};
168
169class TwoValidatorFixture : public BaseFixture
170{
171public:
172 TwoValidatorFixture()
173 {
174 m_tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1");
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600175 m_tester1.saveIdentityToFile("tests/mgmt/cert1.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700176
177 m_tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2");
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600178 m_tester2.saveIdentityToFile("tests/mgmt/cert2.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700179 }
180
181 ~TwoValidatorFixture()
182 {
183 boost::system::error_code error;
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600184 boost::filesystem::remove("tests/mgmt/cert1.ndncert", error);
185 boost::filesystem::remove("tests/mgmt/cert2.ndncert", error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700186 }
187
188protected:
189 CommandValidatorTester m_tester1;
190 CommandValidatorTester m_tester2;
191};
192
193BOOST_FIXTURE_TEST_CASE(TwoKeys, TwoValidatorFixture)
194{
195 shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
196 shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething");
197 shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create");
198
199 ndn::CommandInterestGenerator generator;
200 generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName());
201 generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName());
202 generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName());
203
204 ConfigFile config;
205 CommandValidator validator;
206 validator.addSupportedPrivilege("faces");
207 validator.addSupportedPrivilege("fib");
208 validator.addSupportedPrivilege("stats");
209
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600210 validator.setConfigFile(config);
211
212 config.parse(CONFIG, false, CONFIG_PATH.native());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700213
214 validator.validate(*fibCommand,
215 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
216 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
217
218 BOOST_REQUIRE(m_tester1.commandValidated());
219 m_tester1.resetValidation();
220
221 validator.validate(*statsCommand,
222 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
223 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
224
225 BOOST_REQUIRE(m_tester1.commandValidated());
226
227 validator.validate(*facesCommand,
228 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
229 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
230
231 BOOST_REQUIRE(m_tester2.commandValidated());
232 m_tester2.resetValidation();
233
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600234 // use cert2 for fib command (authorized for cert1 only)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700235 shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
236 generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName());
237
238 validator.validate(*unauthorizedFibCommand,
239 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
240 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
241
242 BOOST_REQUIRE(m_tester2.commandValidationFailed());
243}
244
245BOOST_FIXTURE_TEST_CASE(TwoKeysDryRun, TwoValidatorFixture)
246{
247 CommandValidatorTester tester1;
248 tester1.generateIdentity("/test/CommandValidator/TwoKeys/id1");
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600249 tester1.saveIdentityToFile("tests/mgmt/cert1.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700250
251 CommandValidatorTester tester2;
252 tester2.generateIdentity("/test/CommandValidator/TwoKeys/id2");
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600253 tester2.saveIdentityToFile("tests/mgmt/cert2.ndncert");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700254
255 shared_ptr<Interest> fibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
256 shared_ptr<Interest> statsCommand = make_shared<Interest>("/localhost/nfd/stats/dosomething");
257 shared_ptr<Interest> facesCommand = make_shared<Interest>("/localhost/nfd/faces/create");
258
259 ndn::CommandInterestGenerator generator;
260 generator.generateWithIdentity(*fibCommand, m_tester1.getIdentityName());
261 generator.generateWithIdentity(*statsCommand, m_tester1.getIdentityName());
262 generator.generateWithIdentity(*facesCommand, m_tester2.getIdentityName());
263
264 ConfigFile config;
265 CommandValidator validator;
266 validator.addSupportedPrivilege("faces");
267 validator.addSupportedPrivilege("fib");
268 validator.addSupportedPrivilege("stats");
269
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600270 validator.setConfigFile(config);
271
272 config.parse(CONFIG, true, CONFIG_PATH.native());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700273
274 validator.validate(*fibCommand,
275 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
276 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
277
278 BOOST_REQUIRE(m_tester1.commandValidationFailed());
279 m_tester1.resetValidation();
280
281 validator.validate(*statsCommand,
282 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester1), _1),
283 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester1), _1, _2));
284
285 BOOST_REQUIRE(m_tester1.commandValidationFailed());
286
287 validator.validate(*facesCommand,
288 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
289 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
290
291 BOOST_REQUIRE(m_tester2.commandValidationFailed());
292 m_tester2.resetValidation();
293
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600294 // use cert2 for fib command (authorized for cert1 only)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700295 shared_ptr<Interest> unauthorizedFibCommand = make_shared<Interest>("/localhost/nfd/fib/insert");
296 generator.generateWithIdentity(*unauthorizedFibCommand, m_tester2.getIdentityName());
297
298 validator.validate(*unauthorizedFibCommand,
299 bind(&CommandValidatorTester::onValidated, boost::ref(m_tester2), _1),
300 bind(&CommandValidatorTester::onValidationFailed, boost::ref(m_tester2), _1, _2));
301
302 BOOST_REQUIRE(m_tester2.commandValidationFailed());
303}
304
305BOOST_AUTO_TEST_CASE(NoAuthorizeSections)
306{
307 const std::string NO_AUTHORIZE_CONFIG =
308 "authorizations\n"
309 "{\n"
310 "}\n";
311
312 ConfigFile config;
313 CommandValidator validator;
314
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600315 validator.setConfigFile(config);
316 BOOST_CHECK_THROW(config.parse(NO_AUTHORIZE_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700317}
318
319BOOST_AUTO_TEST_CASE(NoPrivilegesSections)
320{
321 const std::string NO_PRIVILEGES_CONFIG =
322 "authorizations\n"
323 "{\n"
324 " authorize\n"
325 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600326 " certfile \"tests/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700327 " }\n"
328 "}\n";
329
330 ConfigFile config;
331 CommandValidator validator;
332
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600333 validator.setConfigFile(config);
334
335 BOOST_CHECK_THROW(config.parse(NO_PRIVILEGES_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700336}
337
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600338BOOST_AUTO_TEST_CASE(InvalidCertfile)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700339{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600340 const std::string INVALID_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700341 "authorizations\n"
342 "{\n"
343 " authorize\n"
344 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600345 " certfile \"tests/mgmt/notacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700346 " privileges\n"
347 " {\n"
348 " fib\n"
349 " stats\n"
350 " }\n"
351 " }\n"
352 "}\n";
353
354 ConfigFile config;
355 CommandValidator validator;
356
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600357 validator.setConfigFile(config);
358 BOOST_CHECK_THROW(config.parse(INVALID_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700359}
360
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600361BOOST_AUTO_TEST_CASE(NoCertfile)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700362{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600363 const std::string NO_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700364 "authorizations\n"
365 "{\n"
366 " authorize\n"
367 " {\n"
368 " privileges\n"
369 " {\n"
370 " fib\n"
371 " stats\n"
372 " }\n"
373 " }\n"
374 "}\n";
375
376
377 ConfigFile config;
378 CommandValidator validator;
379
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600380 validator.setConfigFile(config);
381 BOOST_CHECK_THROW(config.parse(NO_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700382}
383
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600384BOOST_AUTO_TEST_CASE(MalformedCert)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700385{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600386 const std::string MALFORMED_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700387 "authorizations\n"
388 "{\n"
389 " authorize\n"
390 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600391 " certfile \"tests/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700392 " privileges\n"
393 " {\n"
394 " fib\n"
395 " stats\n"
396 " }\n"
397 " }\n"
398 "}\n";
399
400
401 ConfigFile config;
402 CommandValidator validator;
403
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600404 validator.setConfigFile(config);
405 BOOST_CHECK_THROW(config.parse(MALFORMED_CERT_CONFIG, false, CONFIG_PATH.native()), ConfigFile::Error);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700406}
407
408bool
409validateErrorMessage(const std::string& expectedMessage, const ConfigFile::Error& error)
410{
411 bool gotExpected = error.what() == expectedMessage;
412 if (!gotExpected)
413 {
414 NFD_LOG_WARN("\ncaught exception: " << error.what()
415 << "\n\nexpected exception: " << expectedMessage);
416 }
417 return gotExpected;
418}
419
420BOOST_AUTO_TEST_CASE(NoAuthorizeSectionsDryRun)
421{
422 const std::string NO_AUTHORIZE_CONFIG =
423 "authorizations\n"
424 "{\n"
425 "}\n";
426
427 ConfigFile config;
428 CommandValidator validator;
429
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600430 validator.setConfigFile(config);
431 BOOST_CHECK_EXCEPTION(config.parse(NO_AUTHORIZE_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700432 ConfigFile::Error,
433 bind(&validateErrorMessage,
434 "No authorize sections found", _1));
435}
436
437BOOST_FIXTURE_TEST_CASE(NoPrivilegesSectionsDryRun, TwoValidatorFixture)
438{
439 const std::string NO_PRIVILEGES_CONFIG =
440 "authorizations\n"
441 "{\n"
442 " authorize\n"
443 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600444 " certfile \"tests/mgmt/cert1.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700445 " }\n"
446 " authorize\n"
447 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600448 " certfile \"tests/mgmt/cert2.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700449 " }\n"
450 "}\n";
451
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700452 ConfigFile config;
453 CommandValidator validator;
454
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600455 validator.setConfigFile(config);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700456
457 std::stringstream expectedError;
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600458 expectedError << "No privileges section found for certificate file tests/mgmt/cert1.ndncert "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700459 << "(" << m_tester1.getPublicKeyName().toUri() << ")\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600460 << "No privileges section found for certificate file tests/mgmt/cert2.ndncert "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700461 << "(" << m_tester2.getPublicKeyName().toUri() << ")";
462
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600463 BOOST_CHECK_EXCEPTION(config.parse(NO_PRIVILEGES_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700464 ConfigFile::Error,
465 bind(&validateErrorMessage, expectedError.str(), _1));
466}
467
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600468BOOST_AUTO_TEST_CASE(InvalidCertfileDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700469{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600470 using namespace boost::filesystem;
471
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700472 const std::string INVALID_KEY_CONFIG =
473 "authorizations\n"
474 "{\n"
475 " authorize\n"
476 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600477 " certfile \"tests/mgmt/notacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700478 " privileges\n"
479 " {\n"
480 " fib\n"
481 " stats\n"
482 " }\n"
483 " }\n"
484 " authorize\n"
485 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600486 " certfile \"tests/mgmt/stillnotacertfile.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700487 " privileges\n"
488 " {\n"
489 " }\n"
490 " }\n"
491 "}\n";
492
493 ConfigFile config;
494 CommandValidator validator;
495
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600496 validator.setConfigFile(config);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700497
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600498 std::stringstream error;
499 error << "Unable to open certificate file "
500 << absolute("tests/mgmt/notacertfile.ndncert").native() << "\n"
501 << "Unable to open certificate file "
502 << absolute("tests/mgmt/stillnotacertfile.ndncert").native();
503
504 BOOST_CHECK_EXCEPTION(config.parse(INVALID_KEY_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700505 ConfigFile::Error,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600506 bind(&validateErrorMessage, error.str(), _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700507}
508
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600509BOOST_AUTO_TEST_CASE(NoCertfileDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700510{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600511 const std::string NO_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700512 "authorizations\n"
513 "{\n"
514 " authorize\n"
515 " {\n"
516 " privileges\n"
517 " {\n"
518 " fib\n"
519 " stats\n"
520 " }\n"
521 " }\n"
522 " authorize\n"
523 " {\n"
524 " }\n"
525 "}\n";
526
527
528 ConfigFile config;
529 CommandValidator validator;
530
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600531 validator.setConfigFile(config);
532 BOOST_CHECK_EXCEPTION(config.parse(NO_CERT_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700533 ConfigFile::Error,
534 bind(&validateErrorMessage,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600535 "No certfile specified\n"
536 "No certfile specified", _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700537}
538
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600539BOOST_AUTO_TEST_CASE(MalformedCertDryRun)
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700540{
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600541 using namespace boost::filesystem;
542
543 const std::string MALFORMED_CERT_CONFIG =
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700544 "authorizations\n"
545 "{\n"
546 " authorize\n"
547 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600548 " certfile \"tests/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700549 " privileges\n"
550 " {\n"
551 " fib\n"
552 " stats\n"
553 " }\n"
554 " }\n"
555 " authorize\n"
556 " {\n"
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600557 " certfile \"tests/mgmt/malformed.ndncert\"\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700558 " }\n"
559 "}\n";
560
561
562 ConfigFile config;
563 CommandValidator validator;
564
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600565 validator.setConfigFile(config);
566
567 std::stringstream error;
568 error << "Malformed certificate file "
569 << absolute("tests/mgmt/malformed.ndncert").native() << "\n"
570 << "Malformed certificate file "
571 << absolute("tests/mgmt/malformed.ndncert").native();
572
573 BOOST_CHECK_EXCEPTION(config.parse(MALFORMED_CERT_CONFIG, true, CONFIG_PATH.native()),
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700574 ConfigFile::Error,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600575 bind(&validateErrorMessage, error.str(), _1));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700576}
577
578BOOST_AUTO_TEST_SUITE_END()
579
580} // namespace tests
581
582} // namespace nfd
583