blob: fb447d96ceabfbaf32f080b9cfbd164ef7bd6829 [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;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080066 Name commandName = commandInterest2->getName();
67 commandName
68 .append(name::Component::fromNumber(timestamp))
69 .append(name::Component::fromNumber(random::generateWord64()));
70 commandInterest2->setName(commandName);
71
Yingdi Yu17bc3012014-02-10 17:37:12 -080072 keyChain.signByIdentity(*commandInterest2, identity);
73 validator.validate(*commandInterest2,
74 bind(&TestCore::validated, &core, _1),
75 bind(&TestCore::validationFailed, &core, _1));
76
77 BOOST_CHECK_EQUAL(core.m_validity, false);
78
79 //Test an unauthorized command
80 Name identity2("/TestCommandInterest/Validation2");
81 Name certName2;
82 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
83
84 shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3");
85 generator.generateWithIdentity(*commandInterest3, identity2);
86 validator.validate(*commandInterest3,
87 bind(&TestCore::validated, &core, _1),
88 bind(&TestCore::validationFailed, &core, _1));
89
90 BOOST_CHECK_EQUAL(core.m_validity, false);
91
92 //Test another unauthorized command
93 shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command");
94 generator.generateWithIdentity(*commandInterest4, identity);
95 validator.validate(*commandInterest4,
96 bind(&TestCore::validated, &core, _1),
97 bind(&TestCore::validationFailed, &core, _1));
98
99 BOOST_CHECK_EQUAL(core.m_validity, false);
100
101 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
102 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2));
103}
104
105BOOST_AUTO_TEST_SUITE_END()
106}