blob: 2137a018169d755895a4d996fa206f8efcaa2985 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, Regents of the University of California,
Yanbiao Li698f4fe2015-08-19 16:30:16 -07004 * 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
Yanbiao Lidf846e52016-01-30 21:53:47 -080026#include "core/manager-base.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070027#include "manager-common-fixture.hpp"
28
29#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000030#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070031
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
Yanbiao Lidf846e52016-01-30 21:53:47 -080054class ManagerTester : public ManagerBase
55{
56public:
57 ManagerTester(Dispatcher& dispatcher,
58 const std::string& module)
Junxiao Shi21738402016-08-19 19:48:00 +000059 : ManagerBase(dispatcher, module)
60 {
Yanbiao Lidf846e52016-01-30 21:53:47 -080061 }
62
Junxiao Shi21738402016-08-19 19:48:00 +000063 virtual ndn::mgmt::Authorization
64 makeAuthorization(const std::string& verb) override
65 {
66 return [this] (const Name& prefix, const Interest& interest,
67 const ndn::mgmt::ControlParameters* params,
68 ndn::mgmt::AcceptContinuation accept,
69 ndn::mgmt::RejectContinuation reject) {
70 accept("requester");
71 };
Yanbiao Lidf846e52016-01-30 21:53:47 -080072 }
73};
74
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075class ManagerBaseFixture : public ManagerCommonFixture
76{
77public:
78 ManagerBaseFixture()
Yanbiao Lidf846e52016-01-30 21:53:47 -080079 : m_manager(m_dispatcher, "test-module")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070080 {
81 }
82
83protected:
Yanbiao Lidf846e52016-01-30 21:53:47 -080084 ManagerTester m_manager;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070085};
86
Yanbiao Lidf846e52016-01-30 21:53:47 -080087BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070088
89BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
90{
91 bool wasCommandHandlerCalled = false;
92 auto handler = bind([&] { wasCommandHandlerCalled = true; });
93
94 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
95 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
Yanbiao Lidf846e52016-01-30 21:53:47 -080096 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070097
98 auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) {
99 wasCommandHandlerCalled = false;
100 receiveInterest(makeControlCommandRequest(commandName, ControlParameters()));
101 };
102
103 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
104 BOOST_CHECK(wasCommandHandlerCalled);
105
106 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
107 BOOST_CHECK(!wasCommandHandlerCalled);
108}
109
110BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
111{
112 bool isStatusDatasetCalled = false;
113 auto handler = bind([&] { isStatusDatasetCalled = true; });
114
115 m_manager.registerStatusDatasetHandler("test-status", handler);
Yanbiao Lidf846e52016-01-30 21:53:47 -0800116 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700117
118 receiveInterest(makeInterest("/localhost/nfd/test-module/test-status"));
119 BOOST_CHECK(isStatusDatasetCalled);
120}
121
122BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
123{
124 auto post = m_manager.registerNotificationStream("test-notification");
Yanbiao Lidf846e52016-01-30 21:53:47 -0800125 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700126
127 post(Block("\x82\x01\x02", 3));
128 advanceClocks(time::milliseconds(1));
129
130 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
131 BOOST_CHECK_EQUAL(m_responses[0].getName(),
132 Name("/localhost/nfd/test-module/test-notification/%FE%00"));
133}
134
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700135BOOST_AUTO_TEST_CASE(ExtractRequester)
136{
137 std::string requesterName;
138 auto testAccept = [&] (const std::string& requester) { requesterName = requester; };
139
140 auto unsignedCommand = makeInterest("/test/interest/unsigned");
141 auto signedCommand = makeControlCommandRequest("/test/interest/signed", ControlParameters());
142
143 m_manager.extractRequester(*unsignedCommand, testAccept);
144 BOOST_CHECK(requesterName.empty());
145
146 requesterName = "";
147 m_manager.extractRequester(*signedCommand, testAccept);
148 auto keyLocator = m_keyChain.getDefaultCertificateNameForIdentity(m_identityName).getPrefix(-1);
149 BOOST_CHECK_EQUAL(requesterName, keyLocator.toUri());
150}
151
152BOOST_AUTO_TEST_CASE(ValidateParameters)
153{
154 ControlParameters params;
155 TestCommandVoidParameters commandVoidParams;
156 TestCommandRequireName commandRequireName;
157
158 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandVoidParams, params), true); // succeeds
159 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), false); // fails
160
161 params.setName("test-name");
162 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), true); // succeeds
163}
164
165BOOST_AUTO_TEST_CASE(MakeRelPrefix)
166{
167 auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
168 BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
169}
170
Yanbiao Lidf846e52016-01-30 21:53:47 -0800171BOOST_AUTO_TEST_SUITE_END() // TestManagerBase
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700172
173} // namespace tests
174} // namespace nfd