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 | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, 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 | #ifndef NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 29 | #include "mgmt/manager-base.hpp" |
| 30 | #include "fw/forwarder.hpp" |
| 31 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 32 | #include "tests/key-chain-fixture.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/security/command-interest-signer.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 36 | |
| 37 | namespace nfd { |
| 38 | namespace tests { |
| 39 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 40 | /** \brief A fixture that provides a CommandInterestSigner. |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 41 | */ |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 42 | class CommandInterestSignerFixture : public UnitTestTimeFixture, public KeyChainFixture |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 43 | { |
| 44 | protected: |
| 45 | CommandInterestSignerFixture(); |
| 46 | |
| 47 | /** \brief sign a command Interest |
| 48 | * \param name command name include prefix and parameters |
| 49 | * \param identity signing identity |
| 50 | * \return a command Interest |
| 51 | */ |
| 52 | Interest |
| 53 | makeCommandInterest(const Name& name, const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY); |
| 54 | |
| 55 | /** \brief create a ControlCommand request |
| 56 | * \param commandName command name including prefix, such as "/localhost/nfd/fib/add-nexthop" |
| 57 | * \param params command parameters |
| 58 | * \param identity signing identity |
| 59 | * \return a command Interest |
| 60 | */ |
| 61 | Interest |
| 62 | makeControlCommandRequest(Name commandName, const ControlParameters& params, |
| 63 | const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY); |
| 64 | |
| 65 | protected: |
| 66 | static const Name DEFAULT_COMMAND_SIGNER_IDENTITY; |
| 67 | |
| 68 | private: |
| 69 | ndn::security::CommandInterestSigner m_commandInterestSigner; |
| 70 | }; |
| 71 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 72 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 73 | * @brief A collection of common functions shared by all manager's test fixtures. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 74 | */ |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 75 | class ManagerCommonFixture : public CommandInterestSignerFixture |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 76 | { |
| 77 | public: // initialize |
| 78 | ManagerCommonFixture(); |
| 79 | |
| 80 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 81 | * @brief Add `/localhost/nfd` as a top prefix to the dispatcher. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 82 | * |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 83 | * Afterwards, advanceClocks() is called to ensure all added filters take effect. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 84 | */ |
| 85 | void |
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 | |
| 88 | public: // test |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 89 | /** |
| 90 | * @brief cause management to receive an Interest |
| 91 | * |
| 92 | * call DummyClientFace::receive to receive Interest and then call advanceClocks to ensure |
| 93 | * the Interest dispatched |
| 94 | * |
| 95 | * @param interest the Interest to receive |
| 96 | */ |
| 97 | void |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 98 | receiveInterest(const Interest& interest); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | |
| 100 | public: // verify |
| 101 | ControlResponse |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 102 | makeResponse(uint32_t code, const std::string& text, const ControlParameters& parameters); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 103 | |
| 104 | enum class CheckResponseResult |
| 105 | { |
| 106 | OK, |
| 107 | OUT_OF_BOUNDARY, |
| 108 | WRONG_NAME, |
| 109 | WRONG_CONTENT_TYPE, |
| 110 | INVALID_RESPONSE, |
| 111 | WRONG_CODE, |
| 112 | WRONG_TEXT, |
| 113 | WRONG_BODY_SIZE, |
| 114 | WRONG_BODY_VALUE |
Davide Pesavento | 35120ea | 2015-11-17 21:13:18 +0100 | [diff] [blame] | 115 | }; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * @brief check a specified response data with the expected ControlResponse |
| 119 | * |
| 120 | * @param idx the index of the specified Data in m_responses |
| 121 | * @param expectedDataName the expected name of this Data |
| 122 | * @param expectedResponse the expected ControlResponse |
| 123 | * @param expectedContentType the expected content type of this Data, use -1 to skip this check |
| 124 | * |
| 125 | * @retval OK the response at the specified index can be decoded from the response data, |
| 126 | * and its code, text and response body are all matched with the expected response |
| 127 | * @retval OUT_OF_BOUNDARY the specified index out of boundary |
| 128 | * @retval WRONG_NAME the name of the specified response data does not match |
| 129 | * @retval WRONG_CONTENT_TYPE the content type of the specified response data does not match |
| 130 | * @retval INVALID_RESPONSE the data name matches but it fails in decoding a ControlResponse from |
| 131 | * the content of the specified response data |
| 132 | * @retval WRONG_CODE a valid ControlResponse can be decoded but has a wrong code |
| 133 | * @retval WRONG_TEXT a valid ControlResponse can be decoded but has a wrong text |
| 134 | * @retval WRONG_BODY_SIZE the body size of decoded ControlResponse does not match |
| 135 | * @retval WRONT_BODY_VALUE the body value of decoded ControlResponse does not match |
| 136 | */ |
| 137 | CheckResponseResult |
| 138 | checkResponse(size_t idx, |
| 139 | const Name& expectedName, |
| 140 | const ControlResponse& expectedResponse, |
| 141 | int expectedContentType = -1); |
| 142 | |
| 143 | /** |
| 144 | * @brief concatenate specified response Data into a single block |
| 145 | * |
| 146 | * @param startIndex the start index in m_responses |
| 147 | * @param nResponses the number of response to concatenate |
| 148 | * |
| 149 | * @return the generated block |
| 150 | */ |
| 151 | Block |
| 152 | concatenateResponses(size_t startIndex = 0, size_t nResponses = 0); |
| 153 | |
| 154 | protected: |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 155 | ndn::util::DummyClientFace m_face; |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 156 | Dispatcher m_dispatcher; |
| 157 | std::vector<Data>& m_responses; ///< a reference to m_face.sentData |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | std::ostream& |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 161 | operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result); |
| 162 | |
| 163 | class ManagerFixtureWithAuthenticator : public ManagerCommonFixture |
| 164 | { |
| 165 | public: |
| 166 | /** \brief grant m_identityName privilege to sign commands for the management module |
| 167 | */ |
| 168 | void |
| 169 | setPrivilege(const std::string& privilege); |
| 170 | |
| 171 | protected: |
| 172 | Forwarder m_forwarder; |
| 173 | shared_ptr<CommandAuthenticator> m_authenticator = CommandAuthenticator::create(); |
| 174 | }; |
| 175 | |
| 176 | class CommandSuccess |
| 177 | { |
| 178 | public: |
| 179 | static ControlResponse |
| 180 | getExpected() |
| 181 | { |
| 182 | return ControlResponse() |
| 183 | .setCode(200) |
| 184 | .setText("OK"); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | template<int CODE> |
| 189 | class CommandFailure |
| 190 | { |
| 191 | public: |
| 192 | static ControlResponse |
| 193 | getExpected() |
| 194 | { |
| 195 | return ControlResponse() |
| 196 | .setCode(CODE); |
| 197 | // error description should not be checked |
| 198 | } |
| 199 | }; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 200 | |
| 201 | } // namespace tests |
| 202 | } // namespace nfd |
| 203 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 204 | #endif // NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP |