Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, Regents of the University of California, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * 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 Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 26 | #include "mgmt/manager-base.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 28 | #include "manager-common-fixture.hpp" |
| 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::tests { |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 32 | class TestCommandVoidParameters : public ndn::nfd::ControlCommand<TestCommandVoidParameters> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | { |
Davide Pesavento | 1db1bb6 | 2025-01-06 01:23:41 -0500 | [diff] [blame^] | 34 | NDN_CXX_CONTROL_COMMAND("test-module", "test-void"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 37 | const TestCommandVoidParameters::RequestFormat TestCommandVoidParameters::s_requestFormat; |
| 38 | |
| 39 | class TestCommandRequireName : public ndn::nfd::ControlCommand<TestCommandRequireName> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 1db1bb6 | 2025-01-06 01:23:41 -0500 | [diff] [blame^] | 41 | NDN_CXX_CONTROL_COMMAND("test-module", "test-name-required"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 44 | const TestCommandRequireName::RequestFormat TestCommandRequireName::s_requestFormat = |
| 45 | RequestFormat() |
| 46 | .required(ndn::nfd::CONTROL_PARAMETER_NAME); |
| 47 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 48 | class DummyManager final : public ManagerBase |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 49 | { |
| 50 | public: |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 51 | explicit |
| 52 | DummyManager(Dispatcher& dispatcher) |
| 53 | : ManagerBase("test-module", dispatcher) |
| 54 | { |
| 55 | } |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 56 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 57 | private: |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 58 | ndn::mgmt::Authorization |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 59 | makeAuthorization(const std::string&) final |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 60 | { |
Davide Pesavento | ac238f2 | 2017-09-12 15:19:40 -0400 | [diff] [blame] | 61 | return [] (const Name& prefix, const Interest& interest, |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 62 | const ndn::mgmt::ControlParametersBase* params, |
| 63 | const ndn::mgmt::AcceptContinuation& accept, |
| 64 | const ndn::mgmt::RejectContinuation& reject) { |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 65 | accept("requester"); |
| 66 | }; |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 67 | } |
| 68 | }; |
| 69 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 70 | class ManagerBaseFixture : public ManagerCommonFixture |
| 71 | { |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 72 | protected: |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 73 | DummyManager m_manager{m_dispatcher}; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 76 | BOOST_AUTO_TEST_SUITE(Mgmt) |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 77 | BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 78 | |
| 79 | BOOST_AUTO_TEST_CASE(RegisterCommandHandler) |
| 80 | { |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 81 | bool wasHandlerCalled = false; |
| 82 | auto handler = [&] (auto&&...) { wasHandlerCalled = true; }; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 83 | |
Davide Pesavento | 1db1bb6 | 2025-01-06 01:23:41 -0500 | [diff] [blame^] | 84 | m_manager.registerCommandHandler<TestCommandVoidParameters>(handler); |
| 85 | m_manager.registerCommandHandler<TestCommandRequireName>(handler); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 86 | setTopPrefix(); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 87 | |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 88 | auto testRegisterCommandHandler = [&] (const Name& commandName) { |
| 89 | wasHandlerCalled = false; |
| 90 | receiveInterest(makeControlCommandRequest(commandName)); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | testRegisterCommandHandler("/localhost/nfd/test-module/test-void"); |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 94 | BOOST_CHECK(wasHandlerCalled); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 95 | |
Davide Pesavento | 1db1bb6 | 2025-01-06 01:23:41 -0500 | [diff] [blame^] | 96 | testRegisterCommandHandler("/localhost/nfd/test-module/test-name-required"); |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 97 | BOOST_CHECK(!wasHandlerCalled); |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 98 | |
Davide Pesavento | 1db1bb6 | 2025-01-06 01:23:41 -0500 | [diff] [blame^] | 99 | testRegisterCommandHandler(Name("/localhost/nfd/test-module/test-name-required") |
| 100 | .append(ControlParameters().setName("/foo").wireEncode())); |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 101 | BOOST_CHECK(wasHandlerCalled); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(RegisterStatusDataset) |
| 105 | { |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 106 | bool wasHandlerCalled = false; |
| 107 | auto handler = [&] (auto&&...) { wasHandlerCalled = true; }; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 108 | |
| 109 | m_manager.registerStatusDatasetHandler("test-status", handler); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 110 | setTopPrefix(); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 112 | receiveInterest(*makeInterest("/localhost/nfd/test-module/test-status", true)); |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 113 | BOOST_CHECK(wasHandlerCalled); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(RegisterNotificationStream) |
| 117 | { |
| 118 | auto post = m_manager.registerNotificationStream("test-notification"); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 119 | setTopPrefix(); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 120 | |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 121 | post(Block({0x82, 0x01, 0x02})); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 122 | advanceClocks(1_ms); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 123 | |
| 124 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 125 | BOOST_CHECK_EQUAL(m_responses[0].getName(), |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 126 | Name("/localhost/nfd/test-module/test-notification").appendSequenceNumber(0)); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(ExtractSigner) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 130 | { |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 131 | std::string signer = ManagerBase::extractSigner(Interest("/test/interest/unsigned")); |
| 132 | BOOST_CHECK_EQUAL(signer, ""); // missing SignatureInfo |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 133 | |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 134 | 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 Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 140 | |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 141 | 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 Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 149 | m_keyChain.getPib().getIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY) |
| 150 | .getDefaultKey().getDefaultCertificate().getName().toUri()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 153 | BOOST_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 Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 159 | BOOST_AUTO_TEST_SUITE_END() // TestManagerBase |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 160 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 161 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 162 | } // namespace nfd::tests |