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