blob: 70eb1b6f4edcc95da178f916fe1035eed6fd5655 [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 Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, 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 Pesavento45c1f6a2025-01-01 19:30:30 -050032class TestCommandVoidParameters : public ndn::nfd::ControlCommand<TestCommandVoidParameters>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070033{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050034 NDN_CXX_CONTROL_COMMAND("test-module", "test-void");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070035};
36
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050037const TestCommandVoidParameters::RequestFormat TestCommandVoidParameters::s_requestFormat;
38
39class TestCommandRequireName : public ndn::nfd::ControlCommand<TestCommandRequireName>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070040{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050041 NDN_CXX_CONTROL_COMMAND("test-module", "test-name-required");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042};
43
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050044const TestCommandRequireName::RequestFormat TestCommandRequireName::s_requestFormat =
45 RequestFormat()
46 .required(ndn::nfd::CONTROL_PARAMETER_NAME);
47
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040048class DummyManager final : public ManagerBase
Yanbiao Lidf846e52016-01-30 21:53:47 -080049{
50public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050051 explicit
52 DummyManager(Dispatcher& dispatcher)
53 : ManagerBase("test-module", dispatcher)
54 {
55 }
Yanbiao Lidf846e52016-01-30 21:53:47 -080056
Davide Pesavento78ddcab2019-02-28 22:00:03 -050057private:
Junxiao Shi16a3adf2017-05-26 17:38:51 +000058 ndn::mgmt::Authorization
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050059 makeAuthorization(const std::string&) final
Junxiao Shi21738402016-08-19 19:48:00 +000060 {
Davide Pesaventoac238f22017-09-12 15:19:40 -040061 return [] (const Name& prefix, const Interest& interest,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050062 const ndn::mgmt::ControlParametersBase* params,
63 const ndn::mgmt::AcceptContinuation& accept,
64 const ndn::mgmt::RejectContinuation& reject) {
Junxiao Shi21738402016-08-19 19:48:00 +000065 accept("requester");
66 };
Yanbiao Lidf846e52016-01-30 21:53:47 -080067 }
68};
69
Yanbiao Li698f4fe2015-08-19 16:30:16 -070070class ManagerBaseFixture : public ManagerCommonFixture
71{
Yanbiao Li698f4fe2015-08-19 16:30:16 -070072protected:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050073 DummyManager m_manager{m_dispatcher};
Yanbiao Li698f4fe2015-08-19 16:30:16 -070074};
75
Davide Pesavento78ddcab2019-02-28 22:00:03 -050076BOOST_AUTO_TEST_SUITE(Mgmt)
Yanbiao Lidf846e52016-01-30 21:53:47 -080077BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070078
79BOOST_AUTO_TEST_CASE(RegisterCommandHandler)
80{
Davide Pesavento20cafa82022-07-25 01:15:03 -040081 bool wasHandlerCalled = false;
82 auto handler = [&] (auto&&...) { wasHandlerCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -070083
Davide Pesavento1db1bb62025-01-06 01:23:41 -050084 m_manager.registerCommandHandler<TestCommandVoidParameters>(handler);
85 m_manager.registerCommandHandler<TestCommandRequireName>(handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050086 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070087
Davide Pesavento20cafa82022-07-25 01:15:03 -040088 auto testRegisterCommandHandler = [&] (const Name& commandName) {
89 wasHandlerCalled = false;
90 receiveInterest(makeControlCommandRequest(commandName));
Yanbiao Li698f4fe2015-08-19 16:30:16 -070091 };
92
93 testRegisterCommandHandler("/localhost/nfd/test-module/test-void");
Davide Pesavento20cafa82022-07-25 01:15:03 -040094 BOOST_CHECK(wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070095
Davide Pesavento1db1bb62025-01-06 01:23:41 -050096 testRegisterCommandHandler("/localhost/nfd/test-module/test-name-required");
Davide Pesavento20cafa82022-07-25 01:15:03 -040097 BOOST_CHECK(!wasHandlerCalled);
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050098
Davide Pesavento1db1bb62025-01-06 01:23:41 -050099 testRegisterCommandHandler(Name("/localhost/nfd/test-module/test-name-required")
100 .append(ControlParameters().setName("/foo").wireEncode()));
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500101 BOOST_CHECK(wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700102}
103
104BOOST_AUTO_TEST_CASE(RegisterStatusDataset)
105{
Davide Pesavento20cafa82022-07-25 01:15:03 -0400106 bool wasHandlerCalled = false;
107 auto handler = [&] (auto&&...) { wasHandlerCalled = true; };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700108
109 m_manager.registerStatusDatasetHandler("test-status", handler);
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500110 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700111
Junxiao Shi9d727852019-05-14 13:44:22 -0600112 receiveInterest(*makeInterest("/localhost/nfd/test-module/test-status", true));
Davide Pesavento20cafa82022-07-25 01:15:03 -0400113 BOOST_CHECK(wasHandlerCalled);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700114}
115
116BOOST_AUTO_TEST_CASE(RegisterNotificationStream)
117{
118 auto post = m_manager.registerNotificationStream("test-notification");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500119 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700120
Davide Pesaventoa599d2a2022-02-16 18:52:43 -0500121 post(Block({0x82, 0x01, 0x02}));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400122 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700123
124 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
125 BOOST_CHECK_EQUAL(m_responses[0].getName(),
Eric Newberry358414d2021-03-21 20:56:42 -0700126 Name("/localhost/nfd/test-module/test-notification").appendSequenceNumber(0));
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700127}
128
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400129BOOST_AUTO_TEST_CASE(ExtractSigner)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700130{
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400131 std::string signer = ManagerBase::extractSigner(Interest("/test/interest/unsigned"));
132 BOOST_CHECK_EQUAL(signer, ""); // missing SignatureInfo
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700133
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400134 auto signedV03 = makeControlCommandRequest("/test/interest/signed", {},
135 ndn::security::SignedInterestFormat::V03);
136 signer = ManagerBase::extractSigner(signedV03);
137 BOOST_CHECK_EQUAL(signer,
138 m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
139 .getDefaultKey().getDefaultCertificate().getName().toUri());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700140
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400141 signedV03.setSignatureInfo(ndn::SignatureInfo(tlv::SignatureSha256WithEcdsa));
142 signer = ManagerBase::extractSigner(signedV03);
143 BOOST_CHECK_EQUAL(signer, ""); // missing KeyLocator
144
145 auto signedV02 = makeControlCommandRequest("/test/interest/signed", {},
146 ndn::security::SignedInterestFormat::V02);
147 signer = ManagerBase::extractSigner(signedV02);
148 BOOST_CHECK_EQUAL(signer,
Davide Pesavento20cafa82022-07-25 01:15:03 -0400149 m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)
150 .getDefaultKey().getDefaultCertificate().getName().toUri());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700151}
152
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700153BOOST_AUTO_TEST_CASE(MakeRelPrefix)
154{
155 auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb");
156 BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb"));
157}
158
Yanbiao Lidf846e52016-01-30 21:53:47 -0800159BOOST_AUTO_TEST_SUITE_END() // TestManagerBase
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500160BOOST_AUTO_TEST_SUITE_END() // Mgmt
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700161
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400162} // namespace nfd::tests