blob: 3aeeb9eb0829b3bce393b03c5e2e7099100bf435 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoac238f22017-09-12 15:19:40 -04002/*
Davide Pesavento78ddcab2019-02-28 22:00:03 -05003 * Copyright (c) 2014-2019, 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
Davide Pesavento78ddcab2019-02-28 22:00:03 -050026#include "mgmt/manager-base.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070027
Davide Pesavento78ddcab2019-02-28 22:00:03 -050028#include "manager-common-fixture.hpp"
29
Yanbiao Li698f4fe2015-08-19 16:30:16 -070030#include <ndn-cxx/security/key-chain.hpp>
Junxiao Shi16a3adf2017-05-26 17:38:51 +000031#include <ndn-cxx/security/pib/identity.hpp>
32#include <ndn-cxx/security/pib/key.hpp>
33#include <ndn-cxx/security/pib/pib.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070034
35namespace nfd {
36namespace tests {
37
Davide Pesavento78ddcab2019-02-28 22:00:03 -050038class TestCommandVoidParameters : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070039{
40public:
41 TestCommandVoidParameters()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050042 : ControlCommand("test-module", "test-void-parameters")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070043 {
44 }
45};
46
Davide Pesavento78ddcab2019-02-28 22:00:03 -050047class TestCommandRequireName : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070048{
49public:
50 TestCommandRequireName()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050051 : ControlCommand("test-module", "test-require-name")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070052 {
53 m_requestValidator.required(ndn::nfd::CONTROL_PARAMETER_NAME);
54 }
55};
56
Davide Pesavento78ddcab2019-02-28 22:00:03 -050057class DummyManager : public ManagerBase
Yanbiao Lidf846e52016-01-30 21:53:47 -080058{
59public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050060 explicit
61 DummyManager(Dispatcher& dispatcher)
62 : ManagerBase("test-module", dispatcher)
63 {
64 }
Yanbiao Lidf846e52016-01-30 21:53:47 -080065
Davide Pesavento78ddcab2019-02-28 22:00:03 -050066private:
Junxiao Shi16a3adf2017-05-26 17:38:51 +000067 ndn::mgmt::Authorization
Junxiao Shi21738402016-08-19 19:48:00 +000068 makeAuthorization(const std::string& verb) override
69 {
Davide Pesaventoac238f22017-09-12 15:19:40 -040070 return [] (const Name& prefix, const Interest& interest,
71 const ndn::mgmt::ControlParameters* params,
72 ndn::mgmt::AcceptContinuation accept,
73 ndn::mgmt::RejectContinuation reject) {
Junxiao Shi21738402016-08-19 19:48:00 +000074 accept("requester");
75 };
Yanbiao Lidf846e52016-01-30 21:53:47 -080076 }
77};
78
Yanbiao Li698f4fe2015-08-19 16:30:16 -070079class ManagerBaseFixture : public ManagerCommonFixture
80{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070081protected:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050082 DummyManager m_manager{m_dispatcher};
Yanbiao Li698f4fe2015-08-19 16:30:16 -070083};
84
Davide Pesavento78ddcab2019-02-28 22:00:03 -050085BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Lidf846e52016-01-30 21:53:47 -080086BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070087
88BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
89{
90 bool wasCommandHandlerCalled = false;
91 auto handler = bind([&] { wasCommandHandlerCalled = true; });
92
93 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
94 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050095 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096
97 auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) {
98 wasCommandHandlerCalled = false;
99 receiveInterest(makeControlCommandRequest(commandName, ControlParameters()));
100 };
101
102 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
103 BOOST_CHECK(wasCommandHandlerCalled);
104
105 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
106 BOOST_CHECK(!wasCommandHandlerCalled);
107}
108
109BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
110{
111 bool isStatusDatasetCalled = false;
112 auto handler = bind([&] { isStatusDatasetCalled = true; });
113
114 m_manager.registerStatusDatasetHandler("test-status", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500115 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700116
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000117 receiveInterest(Interest("/localhost/nfd/test-module/test-status"));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700118 BOOST_CHECK(isStatusDatasetCalled);
119}
120
121BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
122{
123 auto post = m_manager.registerNotificationStream("test-notification");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500124 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700125
Davide Pesavento0804f5d2017-09-22 21:18:14 -0400126 const uint8_t buf[] = {0x82, 0x01, 0x02};
127 post(Block(buf, sizeof(buf)));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700128 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
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000140 m_manager.extractRequester(Interest("/test/interest/unsigned"), testAccept);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700141 BOOST_CHECK(requesterName.empty());
142
143 requesterName = "";
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000144 m_manager.extractRequester(makeControlCommandRequest("/test/interest/signed", ControlParameters()), testAccept);
145 auto keyLocator = m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY).getDefaultKey().getName();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700146 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
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500169BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700170
171} // namespace tests
172} // namespace nfd