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