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