blob: ff2f0d43b44ee8374f57944864693757857b4422 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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 * 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/manager-base.hpp"
27#include "manager-common-fixture.hpp"
28
29#include <ndn-cxx/security/key-chain.hpp>
30#include <ndn-cxx/management/nfd-control-command.hpp>
31
32namespace nfd {
33namespace tests {
34
Yanbiao Li698f4fe2015-08-19 16:30:16 -070035class TestCommandVoidParameters : public ndn::nfd::ControlCommand
36{
37public:
38 TestCommandVoidParameters()
39 : ndn::nfd::ControlCommand("test-module", "test-void-parameters")
40 {
41 }
42};
43
44class TestCommandRequireName : public ndn::nfd::ControlCommand
45{
46public:
47 TestCommandRequireName()
48 : ndn::nfd::ControlCommand("test-module", "test-require-name")
49 {
50 m_requestValidator.required(ndn::nfd::CONTROL_PARAMETER_NAME);
51 }
52};
53
54class ManagerBaseFixture : public ManagerCommonFixture
55{
56public:
57 ManagerBaseFixture()
58 : m_manager(m_dispatcher, m_validator, "test-module")
59 {
60 }
61
62protected:
63 ManagerBase m_manager;
64};
65
66BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseFixture)
67
68BOOST_AUTO_TEST_CASE(AddSupportedPrivilegeInConstructor)
69{
70 BOOST_CHECK_NO_THROW(m_validator.addSupportedPrivilege("other-module"));
71 // test-module has already been added by the constructor of ManagerBase
72 BOOST_CHECK_THROW(m_validator.addSupportedPrivilege("test-module"), CommandValidator::Error);
73}
74
75BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
76{
77 bool wasCommandHandlerCalled = false;
78 auto handler = bind([&] { wasCommandHandlerCalled = true; });
79
80 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
81 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
82 setTopPrefixAndPrivilege("/localhost/nfd", "test-module");
83
84 auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) {
85 wasCommandHandlerCalled = false;
86 receiveInterest(makeControlCommandRequest(commandName, ControlParameters()));
87 };
88
89 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
90 BOOST_CHECK(wasCommandHandlerCalled);
91
92 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
93 BOOST_CHECK(!wasCommandHandlerCalled);
94}
95
96BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
97{
98 bool isStatusDatasetCalled = false;
99 auto handler = bind([&] { isStatusDatasetCalled = true; });
100
101 m_manager.registerStatusDatasetHandler("test-status", handler);
102 m_dispatcher.addTopPrefix("/localhost/nfd");
103 advanceClocks(time::milliseconds(1));
104
105 receiveInterest(makeInterest("/localhost/nfd/test-module/test-status"));
106 BOOST_CHECK(isStatusDatasetCalled);
107}
108
109BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
110{
111 auto post = m_manager.registerNotificationStream("test-notification");
112 m_dispatcher.addTopPrefix("/localhost/nfd");
113 advanceClocks(time::milliseconds(1));
114
115 post(Block("\x82\x01\x02", 3));
116 advanceClocks(time::milliseconds(1));
117
118 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
119 BOOST_CHECK_EQUAL(m_responses[0].getName(),
120 Name("/localhost/nfd/test-module/test-notification/%FE%00"));
121}
122
123BOOST_AUTO_TEST_CASE(CommandAuthorization)
124{
125 bool didAcceptCallbackFire = false;
126 bool didRejectCallbackFire = false;
127 auto testAuthorization = [&] {
128 didAcceptCallbackFire = false;
129 didRejectCallbackFire = false;
130
131 auto command = makeControlCommandRequest("/localhost/nfd/test-module/test-verb",
132 ControlParameters());
133 ndn::nfd::ControlParameters params;
134 m_manager.authorize("/top/prefix", *command, &params,
135 bind([&] { didAcceptCallbackFire = true; }),
136 bind([&] { didRejectCallbackFire = true; }));
137 };
138
139 testAuthorization();
140 BOOST_CHECK(!didAcceptCallbackFire);
141 BOOST_CHECK(didRejectCallbackFire);
142
143 m_validator.addInterestRule("^<localhost><nfd><test-module>", *m_certificate);
144 testAuthorization();
145 BOOST_CHECK(didAcceptCallbackFire);
146 BOOST_CHECK(!didRejectCallbackFire);
147}
148
149BOOST_AUTO_TEST_CASE(ExtractRequester)
150{
151 std::string requesterName;
152 auto testAccept = [&] (const std::string& requester) { requesterName = requester; };
153
154 auto unsignedCommand = makeInterest("/test/interest/unsigned");
155 auto signedCommand = makeControlCommandRequest("/test/interest/signed", ControlParameters());
156
157 m_manager.extractRequester(*unsignedCommand, testAccept);
158 BOOST_CHECK(requesterName.empty());
159
160 requesterName = "";
161 m_manager.extractRequester(*signedCommand, testAccept);
162 auto keyLocator = m_keyChain.getDefaultCertificateNameForIdentity(m_identityName).getPrefix(-1);
163 BOOST_CHECK_EQUAL(requesterName, keyLocator.toUri());
164}
165
166BOOST_AUTO_TEST_CASE(ValidateParameters)
167{
168 ControlParameters params;
169 TestCommandVoidParameters commandVoidParams;
170 TestCommandRequireName commandRequireName;
171
172 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandVoidParams, params), true); // succeeds
173 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), false); // fails
174
175 params.setName("test-name");
176 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), true); // succeeds
177}
178
179BOOST_AUTO_TEST_CASE(MakeRelPrefix)
180{
181 auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
182 BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
183}
184
185BOOST_AUTO_TEST_SUITE_END()
186
187} // namespace tests
188} // namespace nfd