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