blob: 7ebe95e5da0f057e45c1b3eb50c7ccb1afbdcc0d [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>
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
Yanbiao Lidf846e52016-01-30 21:53:47 -080054class ManagerTester : public ManagerBase
55{
56public:
57 ManagerTester(Dispatcher& dispatcher,
58 const std::string& module)
59 : ManagerBase(dispatcher, module) {
60 }
61
62 virtual void
63 authorize(const Name& prefix, const Interest& interest,
64 const ndn::mgmt::ControlParameters* params,
65 ndn::mgmt::AcceptContinuation accept,
66 ndn::mgmt::RejectContinuation reject) {
67 extractRequester(interest, accept);
68 }
69};
70
Yanbiao Li698f4fe2015-08-19 16:30:16 -070071class ManagerBaseFixture : public ManagerCommonFixture
72{
73public:
74 ManagerBaseFixture()
Yanbiao Lidf846e52016-01-30 21:53:47 -080075 : m_manager(m_dispatcher, "test-module")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070076 {
77 }
78
79protected:
Yanbiao Lidf846e52016-01-30 21:53:47 -080080 ManagerTester m_manager;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070081};
82
Yanbiao Lidf846e52016-01-30 21:53:47 -080083BOOST_AUTO_TEST_SUITE(Core)
84BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070085
86BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
87{
88 bool wasCommandHandlerCalled = false;
89 auto handler = bind([&] { wasCommandHandlerCalled = true; });
90
91 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
92 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
Yanbiao Lidf846e52016-01-30 21:53:47 -080093 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070094
95 auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) {
96 wasCommandHandlerCalled = false;
97 receiveInterest(makeControlCommandRequest(commandName, ControlParameters()));
98 };
99
100 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
101 BOOST_CHECK(wasCommandHandlerCalled);
102
103 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
104 BOOST_CHECK(!wasCommandHandlerCalled);
105}
106
107BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
108{
109 bool isStatusDatasetCalled = false;
110 auto handler = bind([&] { isStatusDatasetCalled = true; });
111
112 m_manager.registerStatusDatasetHandler("test-status", handler);
Yanbiao Lidf846e52016-01-30 21:53:47 -0800113 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700114
115 receiveInterest(makeInterest("/localhost/nfd/test-module/test-status"));
116 BOOST_CHECK(isStatusDatasetCalled);
117}
118
119BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
120{
121 auto post = m_manager.registerNotificationStream("test-notification");
Yanbiao Lidf846e52016-01-30 21:53:47 -0800122 setTopPrefix("/localhost/nfd");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700123
124 post(Block("\x82\x01\x02", 3));
125 advanceClocks(time::milliseconds(1));
126
127 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
128 BOOST_CHECK_EQUAL(m_responses[0].getName(),
129 Name("/localhost/nfd/test-module/test-notification/%FE%00"));
130}
131
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700132BOOST_AUTO_TEST_CASE(ExtractRequester)
133{
134 std::string requesterName;
135 auto testAccept = [&] (const std::string& requester) { requesterName = requester; };
136
137 auto unsignedCommand = makeInterest("/test/interest/unsigned");
138 auto signedCommand = makeControlCommandRequest("/test/interest/signed", ControlParameters());
139
140 m_manager.extractRequester(*unsignedCommand, testAccept);
141 BOOST_CHECK(requesterName.empty());
142
143 requesterName = "";
144 m_manager.extractRequester(*signedCommand, testAccept);
145 auto keyLocator = m_keyChain.getDefaultCertificateNameForIdentity(m_identityName).getPrefix(-1);
146 BOOST_CHECK_EQUAL(requesterName, keyLocator.toUri());
147}
148
149BOOST_AUTO_TEST_CASE(ValidateParameters)
150{
151 ControlParameters params;
152 TestCommandVoidParameters commandVoidParams;
153 TestCommandRequireName commandRequireName;
154
155 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandVoidParams, params), true); // succeeds
156 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), false); // fails
157
158 params.setName("test-name");
159 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), true); // succeeds
160}
161
162BOOST_AUTO_TEST_CASE(MakeRelPrefix)
163{
164 auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
165 BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
166}
167
Yanbiao Lidf846e52016-01-30 21:53:47 -0800168BOOST_AUTO_TEST_SUITE_END() // TestManagerBase
169BOOST_AUTO_TEST_SUITE_END() // Core
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700170
171} // namespace tests
172} // namespace nfd