blob: cf249895cdad8e837ade7d7016cd122349f5f31a [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 Pesavento0a05f7a2023-10-16 20:28:06 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::tests {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070031
Davide Pesavento78ddcab2019-02-28 22:00:03 -050032class TestCommandVoidParameters : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070033{
34public:
35 TestCommandVoidParameters()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050036 : ControlCommand("test-module", "test-void-parameters")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037 {
38 }
39};
40
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041class TestCommandRequireName : public ControlCommand
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042{
43public:
44 TestCommandRequireName()
Davide Pesavento78ddcab2019-02-28 22:00:03 -050045 : ControlCommand("test-module", "test-require-name")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070046 {
47 m_requestValidator.required(ndn::nfd::CONTROL_PARAMETER_NAME);
48 }
49};
50
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040051class DummyManager final : public ManagerBase
Yanbiao Lidf846e52016-01-30 21:53:47 -080052{
53public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050054 explicit
55 DummyManager(Dispatcher& dispatcher)
56 : ManagerBase("test-module", dispatcher)
57 {
58 }
Yanbiao Lidf846e52016-01-30 21:53:47 -080059
Davide Pesavento78ddcab2019-02-28 22:00:03 -050060private:
Junxiao Shi16a3adf2017-05-26 17:38:51 +000061 ndn::mgmt::Authorization
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040062 makeAuthorization(const std::string& verb) final
Junxiao Shi21738402016-08-19 19:48:00 +000063 {
Davide Pesaventoac238f22017-09-12 15:19:40 -040064 return [] (const Name& prefix, const Interest& interest,
65 const ndn::mgmt::ControlParameters* params,
66 ndn::mgmt::AcceptContinuation accept,
67 ndn::mgmt::RejectContinuation reject) {
Junxiao Shi21738402016-08-19 19:48:00 +000068 accept("requester");
69 };
Yanbiao Lidf846e52016-01-30 21:53:47 -080070 }
71};
72
Yanbiao Li698f4fe2015-08-19 16:30:16 -070073class ManagerBaseFixture : public ManagerCommonFixture
74{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075protected:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050076 DummyManager m_manager{m_dispatcher};
Yanbiao Li698f4fe2015-08-19 16:30:16 -070077};
78
Davide Pesavento78ddcab2019-02-28 22:00:03 -050079BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Lidf846e52016-01-30 21:53:47 -080080BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070081
82BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
83{
Davide Pesavento20cafa82022-07-25 01:15:03 -040084 bool wasHandlerCalled = false;
85 auto handler = [&] (auto&&...) { wasHandlerCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -070086
87 m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler);
88 m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050089 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070090
Davide Pesavento20cafa82022-07-25 01:15:03 -040091 auto testRegisterCommandHandler = [&] (const Name& commandName) {
92 wasHandlerCalled = false;
93 receiveInterest(makeControlCommandRequest(commandName));
Yanbiao Li698f4fe2015-08-19 16:30:16 -070094 };
95
96 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
Davide Pesavento20cafa82022-07-25 01:15:03 -040097 BOOST_CHECK(wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070098
99 testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name");
Davide Pesavento20cafa82022-07-25 01:15:03 -0400100 BOOST_CHECK(!wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101}
102
103BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
104{
Davide Pesavento20cafa82022-07-25 01:15:03 -0400105 bool wasHandlerCalled = false;
106 auto handler = [&] (auto&&...) { wasHandlerCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700107
108 m_manager.registerStatusDatasetHandler("test-status", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500109 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700110
Junxiao Shi9d727852019-05-14 13:44:22 -0600111 receiveInterest(*makeInterest("/localhost/nfd/test-module/test-status", true));
Davide Pesavento20cafa82022-07-25 01:15:03 -0400112 BOOST_CHECK(wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700113}
114
115BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
116{
117 auto post = m_manager.registerNotificationStream("test-notification");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500118 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700119
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500120 post(Block({0x82, 0x01, 0x02}));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400121 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700122
123 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
124 BOOST_CHECK_EQUAL(m_responses[0].getName(),
Eric Newberry358414d2021-03-21 20:56:42 -0700125 Name("/localhost/nfd/test-module/test-notification").appendSequenceNumber(0));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700126}
127
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400128BOOST_AUTO_TEST_CASE(ExtractSigner)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700129{
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400130 std::string signer = ManagerBase::extractSigner(Interest("/test/interest/unsigned"));
131 BOOST_CHECK_EQUAL(signer, ""); // missing SignatureInfo
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700132
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400133 auto signedV03 = makeControlCommandRequest("/test/interest/signed", {},
134 ndn::security::SignedInterestFormat::V03);
135 signer = ManagerBase::extractSigner(signedV03);
136 BOOST_CHECK_EQUAL(signer,
137 m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
138 .getDefaultKey().getDefaultCertificate().getName().toUri());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700139
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400140 signedV03.setSignatureInfo(ndn::SignatureInfo(tlv::SignatureSha256WithEcdsa));
141 signer = ManagerBase::extractSigner(signedV03);
142 BOOST_CHECK_EQUAL(signer, ""); // missing KeyLocator
143
144 auto signedV02 = makeControlCommandRequest("/test/interest/signed", {},
145 ndn::security::SignedInterestFormat::V02);
146 signer = ManagerBase::extractSigner(signedV02);
147 BOOST_CHECK_EQUAL(signer,
Davide Pesavento20cafa82022-07-25 01:15:03 -0400148 m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
149 .getDefaultKey().getDefaultCertificate().getName().toUri());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700150}
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
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500172BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700173
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400174} // namespace nfd::tests