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