blob: dba2dd86836be9812a68496816c55c5ecaf57a2c [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 Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070036
Davide Pesavento78ddcab2019-02-28 22:00:03 -050037class TestCommandVoidParameters : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070038{
39public:
40 TestCommandVoidParameters()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041 : ControlCommand("test-module", "test-void-parameters")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042 {
43 }
44};
45
Davide Pesavento78ddcab2019-02-28 22:00:03 -050046class TestCommandRequireName : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070047{
48public:
49 TestCommandRequireName()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050050 : ControlCommand("test-module", "test-require-name")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070051 {
52 m_requestValidator.required(ndn::nfd::CONTROL_PARAMETER_NAME);
53 }
54};
55
Davide Pesavento78ddcab2019-02-28 22:00:03 -050056class DummyManager : public ManagerBase
Yanbiao Lidf846e52016-01-30 21:53:47 -080057{
58public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050059 explicit
60 DummyManager(Dispatcher& dispatcher)
61 : ManagerBase("test-module", dispatcher)
62 {
63 }
Yanbiao Lidf846e52016-01-30 21:53:47 -080064
Davide Pesavento78ddcab2019-02-28 22:00:03 -050065private:
Junxiao Shi16a3adf2017-05-26 17:38:51 +000066 ndn::mgmt::Authorization
Junxiao Shi21738402016-08-19 19:48:00 +000067 makeAuthorization(const std::string& verb) override
68 {
Davide Pesaventoac238f22017-09-12 15:19:40 -040069 return [] (const Name& prefix, const Interest& interest,
70 const ndn::mgmt::ControlParameters* params,
71 ndn::mgmt::AcceptContinuation accept,
72 ndn::mgmt::RejectContinuation reject) {
Junxiao Shi21738402016-08-19 19:48:00 +000073 accept("requester");
74 };
Yanbiao Lidf846e52016-01-30 21:53:47 -080075 }
76};
77
Yanbiao Li698f4fe2015-08-19 16:30:16 -070078class ManagerBaseFixture : public ManagerCommonFixture
79{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070080protected:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050081 DummyManager m_manager{m_dispatcher};
Yanbiao Li698f4fe2015-08-19 16:30:16 -070082};
83
Davide Pesavento78ddcab2019-02-28 22:00:03 -050084BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Lidf846e52016-01-30 21:53:47 -080085BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070086
87BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
88{
89 bool wasCommandHandlerCalled = false;
Davide Pesavento412c9822021-07-02 00:21:05 -040090 auto handler = [&] (auto&&...) { wasCommandHandlerCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -070091
92 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
93 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050094 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070095
96 auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) {
97 wasCommandHandlerCalled = false;
98 receiveInterest(makeControlCommandRequest(commandName, ControlParameters()));
99 };
100
101 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
102 BOOST_CHECK(wasCommandHandlerCalled);
103
104 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
105 BOOST_CHECK(!wasCommandHandlerCalled);
106}
107
108BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
109{
110 bool isStatusDatasetCalled = false;
Davide Pesavento412c9822021-07-02 00:21:05 -0400111 auto handler = [&] (auto&&...) { isStatusDatasetCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700112
113 m_manager.registerStatusDatasetHandler("test-status", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500114 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700115
Junxiao Shi9d727852019-05-14 13:44:22 -0600116 receiveInterest(*makeInterest("/localhost/nfd/test-module/test-status", true));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700117 BOOST_CHECK(isStatusDatasetCalled);
118}
119
120BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
121{
122 auto post = m_manager.registerNotificationStream("test-notification");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500123 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700124
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500125 post(Block({0x82, 0x01, 0x02}));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400126 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700127
128 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
129 BOOST_CHECK_EQUAL(m_responses[0].getName(),
Eric Newberry358414d2021-03-21 20:56:42 -0700130 Name("/localhost/nfd/test-module/test-notification").appendSequenceNumber(0));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700131}
132
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700133BOOST_AUTO_TEST_CASE(ExtractRequester)
134{
135 std::string requesterName;
136 auto testAccept = [&] (const std::string& requester) { requesterName = requester; };
137
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000138 m_manager.extractRequester(Interest("/test/interest/unsigned"), testAccept);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700139 BOOST_CHECK(requesterName.empty());
140
141 requesterName = "";
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000142 m_manager.extractRequester(makeControlCommandRequest("/test/interest/signed", ControlParameters()), testAccept);
Junxiao Shi4b84a2c2022-04-28 03:17:05 +0000143 BOOST_CHECK_EQUAL(requesterName,
144 m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
145 .getDefaultKey().getDefaultCertificate().getName().toUri());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700146}
147
148BOOST_AUTO_TEST_CASE(ValidateParameters)
149{
150 ControlParameters params;
151 TestCommandVoidParameters commandVoidParams;
152 TestCommandRequireName commandRequireName;
153
154 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandVoidParams, params), true); // succeeds
155 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), false); // fails
156
157 params.setName("test-name");
158 BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), true); // succeeds
159}
160
161BOOST_AUTO_TEST_CASE(MakeRelPrefix)
162{
163 auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
164 BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
165}
166
Yanbiao Lidf846e52016-01-30 21:53:47 -0800167BOOST_AUTO_TEST_SUITE_END() // TestManagerBase
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500168BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700169
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400170} // namespace nfd::tests