Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 26 | #include "core/manager-base.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 27 | #include "manager-common-fixture.hpp" |
| 28 | |
| 29 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | class TestCommandVoidParameters : public ndn::nfd::ControlCommand |
| 36 | { |
| 37 | public: |
| 38 | TestCommandVoidParameters() |
| 39 | : ndn::nfd::ControlCommand("test-module", "test-void-parameters") |
| 40 | { |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class TestCommandRequireName : public ndn::nfd::ControlCommand |
| 45 | { |
| 46 | public: |
| 47 | TestCommandRequireName() |
| 48 | : ndn::nfd::ControlCommand("test-module", "test-require-name") |
| 49 | { |
| 50 | m_requestValidator.required(ndn::nfd::CONTROL_PARAMETER_NAME); |
| 51 | } |
| 52 | }; |
| 53 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 54 | class ManagerTester : public ManagerBase |
| 55 | { |
| 56 | public: |
| 57 | ManagerTester(Dispatcher& dispatcher, |
| 58 | const std::string& module) |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 59 | : ManagerBase(dispatcher, module) |
| 60 | { |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 63 | virtual ndn::mgmt::Authorization |
| 64 | makeAuthorization(const std::string& verb) override |
| 65 | { |
| 66 | return [this] (const Name& prefix, const Interest& interest, |
| 67 | const ndn::mgmt::ControlParameters* params, |
| 68 | ndn::mgmt::AcceptContinuation accept, |
| 69 | ndn::mgmt::RejectContinuation reject) { |
| 70 | accept("requester"); |
| 71 | }; |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 72 | } |
| 73 | }; |
| 74 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 75 | class ManagerBaseFixture : public ManagerCommonFixture |
| 76 | { |
| 77 | public: |
| 78 | ManagerBaseFixture() |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 79 | : m_manager(m_dispatcher, "test-module") |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 80 | { |
| 81 | } |
| 82 | |
| 83 | protected: |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 84 | ManagerTester m_manager; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 87 | BOOST_AUTO_TEST_SUITE(Core) |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestManagerBase, ManagerBaseFixture) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 89 | |
| 90 | BOOST_AUTO_TEST_CASE(RegisterCommandHandler) |
| 91 | { |
| 92 | bool wasCommandHandlerCalled = false; |
| 93 | auto handler = bind([&] { wasCommandHandlerCalled = true; }); |
| 94 | |
| 95 | m_manager.registerCommandHandler<TestCommandVoidParameters>("test-void", handler); |
| 96 | m_manager.registerCommandHandler<TestCommandRequireName>("test-require-name", handler); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 97 | setTopPrefix("/localhost/nfd"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 98 | |
| 99 | auto testRegisterCommandHandler = [&wasCommandHandlerCalled, this] (const Name& commandName) { |
| 100 | wasCommandHandlerCalled = false; |
| 101 | receiveInterest(makeControlCommandRequest(commandName, ControlParameters())); |
| 102 | }; |
| 103 | |
| 104 | testRegisterCommandHandler("/localhost/nfd/test-module/test-void"); |
| 105 | BOOST_CHECK(wasCommandHandlerCalled); |
| 106 | |
| 107 | testRegisterCommandHandler("/localhost/nfd/test-module/test-require-name"); |
| 108 | BOOST_CHECK(!wasCommandHandlerCalled); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE(RegisterStatusDataset) |
| 112 | { |
| 113 | bool isStatusDatasetCalled = false; |
| 114 | auto handler = bind([&] { isStatusDatasetCalled = true; }); |
| 115 | |
| 116 | m_manager.registerStatusDatasetHandler("test-status", handler); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 117 | setTopPrefix("/localhost/nfd"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | |
| 119 | receiveInterest(makeInterest("/localhost/nfd/test-module/test-status")); |
| 120 | BOOST_CHECK(isStatusDatasetCalled); |
| 121 | } |
| 122 | |
| 123 | BOOST_AUTO_TEST_CASE(RegisterNotificationStream) |
| 124 | { |
| 125 | auto post = m_manager.registerNotificationStream("test-notification"); |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 126 | setTopPrefix("/localhost/nfd"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 127 | |
| 128 | post(Block("\x82\x01\x02", 3)); |
| 129 | advanceClocks(time::milliseconds(1)); |
| 130 | |
| 131 | BOOST_REQUIRE_EQUAL(m_responses.size(), 1); |
| 132 | BOOST_CHECK_EQUAL(m_responses[0].getName(), |
| 133 | Name("/localhost/nfd/test-module/test-notification/%FE%00")); |
| 134 | } |
| 135 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | BOOST_AUTO_TEST_CASE(ExtractRequester) |
| 137 | { |
| 138 | std::string requesterName; |
| 139 | auto testAccept = [&] (const std::string& requester) { requesterName = requester; }; |
| 140 | |
| 141 | auto unsignedCommand = makeInterest("/test/interest/unsigned"); |
| 142 | auto signedCommand = makeControlCommandRequest("/test/interest/signed", ControlParameters()); |
| 143 | |
| 144 | m_manager.extractRequester(*unsignedCommand, testAccept); |
| 145 | BOOST_CHECK(requesterName.empty()); |
| 146 | |
| 147 | requesterName = ""; |
| 148 | m_manager.extractRequester(*signedCommand, testAccept); |
| 149 | auto keyLocator = m_keyChain.getDefaultCertificateNameForIdentity(m_identityName).getPrefix(-1); |
| 150 | BOOST_CHECK_EQUAL(requesterName, keyLocator.toUri()); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(ValidateParameters) |
| 154 | { |
| 155 | ControlParameters params; |
| 156 | TestCommandVoidParameters commandVoidParams; |
| 157 | TestCommandRequireName commandRequireName; |
| 158 | |
| 159 | BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandVoidParams, params), true); // succeeds |
| 160 | BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), false); // fails |
| 161 | |
| 162 | params.setName("test-name"); |
| 163 | BOOST_CHECK_EQUAL(ManagerBase::validateParameters(commandRequireName, params), true); // succeeds |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(MakeRelPrefix) |
| 167 | { |
| 168 | auto generatedRelPrefix = m_manager.makeRelPrefix("test-verb"); |
| 169 | BOOST_CHECK_EQUAL(generatedRelPrefix, PartialName("/test-module/test-verb")); |
| 170 | } |
| 171 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 172 | BOOST_AUTO_TEST_SUITE_END() // TestManagerBase |
| 173 | BOOST_AUTO_TEST_SUITE_END() // Core |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 174 | |
| 175 | } // namespace tests |
| 176 | } // namespace nfd |