blob: 59619cadb9a188bff542d8cba99d36ba98d8a69a [file] [log] [blame]
Yingdi Yu17bc3012014-02-10 17:37:12 -08001/* -*- 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 <boost/test/unit_test.hpp>
8
9#include "helper/command-interest-generator.hpp"
10#include "helper/command-interest-validator.hpp"
11#include "util/random.hpp"
12
13namespace ndn {
14
15BOOST_AUTO_TEST_SUITE(TestCommandInterest)
16
17class TestCore
18{
19public:
20 TestCore()
21 : m_validity(false)
22 {}
23
24 void
25 validated(const shared_ptr<const Interest>& interest)
26 { m_validity = true; }
27
28 void
29 validationFailed(const shared_ptr<const Interest>& interest)
30 { m_validity = false; }
31
32 void
33 reset()
34 { m_validity = false; }
35
36 bool m_validity;
37};
38
39BOOST_AUTO_TEST_CASE (Validation)
40{
41 KeyChain keyChain;
42 Name identity("/TestCommandInterest/Validation");
43 Name certName;
44 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
45
46 TestCore core;
47 CommandInterestGenerator generator;
48 CommandInterestValidator validator;
49
50 validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName));
51
52 //Test a legitimate command
53 shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1");
54 generator.generateWithIdentity(*commandInterest1, identity);
55 validator.validate(*commandInterest1,
56 bind(&TestCore::validated, &core, _1),
57 bind(&TestCore::validationFailed, &core, _1));
58
59 BOOST_CHECK_EQUAL(core.m_validity, true);
60
61 //Test an outdated command
62 core.reset();
63 shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
64 int64_t timestamp = time::now() / 1000000;
65 timestamp -= 5000;
66 commandInterest2->getName().append(name::Component::fromNumber(timestamp)).append(name::Component::fromNumber(random::generateWord64()));
67 keyChain.signByIdentity(*commandInterest2, identity);
68 validator.validate(*commandInterest2,
69 bind(&TestCore::validated, &core, _1),
70 bind(&TestCore::validationFailed, &core, _1));
71
72 BOOST_CHECK_EQUAL(core.m_validity, false);
73
74 //Test an unauthorized command
75 Name identity2("/TestCommandInterest/Validation2");
76 Name certName2;
77 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
78
79 shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
80 generator.generateWithIdentity(*commandInterest3, identity2);
81 validator.validate(*commandInterest3,
82 bind(&TestCore::validated, &core, _1),
83 bind(&TestCore::validationFailed, &core, _1));
84
85 BOOST_CHECK_EQUAL(core.m_validity, false);
86
87 //Test another unauthorized command
88 shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command");
89 generator.generateWithIdentity(*commandInterest4, identity);
90 validator.validate(*commandInterest4,
91 bind(&TestCore::validated, &core, _1),
92 bind(&TestCore::validationFailed, &core, _1));
93
94 BOOST_CHECK_EQUAL(core.m_validity, false);
95
96 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
97 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
98}
99
100BOOST_AUTO_TEST_SUITE_END()
101}