Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [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 | |
| 26 | #ifndef NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP |
| 27 | #define NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP |
| 28 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 29 | #include "tests/io-fixture.hpp" |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 30 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 31 | #include "tests/test-common.hpp" |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 34 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 36 | |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame^] | 37 | #include <boost/asio/defer.hpp> |
Davide Pesavento | cf9e1c7 | 2019-12-22 17:56:07 -0500 | [diff] [blame] | 38 | #include <boost/concept/assert.hpp> |
| 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | namespace nfd::tests { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 41 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 42 | using ndn::nfd::ControlParameters; |
| 43 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 44 | /** |
| 45 | * \brief Fixture to emulate NFD management. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 46 | */ |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 47 | class MockNfdMgmtFixture : public IoFixture, public KeyChainFixture |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 48 | { |
| 49 | protected: |
| 50 | MockNfdMgmtFixture() |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 51 | : face(m_io, m_keyChain, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 52 | {true, false, std::bind(&MockNfdMgmtFixture::processEventsOverride, this, _1)}) |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 53 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 54 | face.onSendInterest.connect([this] (const Interest& interest) { |
| 55 | if (processInterest) { |
Davide Pesavento | 5d64263 | 2023-10-03 00:36:08 -0400 | [diff] [blame^] | 56 | boost::asio::defer(m_io, [=] { processInterest(interest); }); |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 57 | } |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 58 | }); |
| 59 | } |
| 60 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 61 | protected: // ControlCommand |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 62 | /** \brief Check the Interest is a command with specified prefix. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 63 | * \retval nullopt last Interest is not the expected command |
| 64 | * \return command parameters |
| 65 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 66 | static std::optional<ControlParameters> |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 67 | parseCommand(const Interest& interest, const Name& expectedPrefix) |
| 68 | { |
| 69 | if (!expectedPrefix.isPrefixOf(interest.getName())) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 70 | return std::nullopt; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 71 | } |
| 72 | return ControlParameters(interest.getName().at(expectedPrefix.size()).blockFromValue()); |
| 73 | } |
| 74 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 75 | /** \brief Send successful response to a command Interest. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 76 | */ |
| 77 | void |
| 78 | succeedCommand(const Interest& interest, const ControlParameters& parameters) |
| 79 | { |
| 80 | this->sendCommandReply(interest, 200, "OK", parameters.wireEncode()); |
| 81 | } |
| 82 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 83 | /** \brief Send failure response to a command Interest. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 84 | */ |
| 85 | void |
| 86 | failCommand(const Interest& interest, uint32_t code, const std::string& text) |
| 87 | { |
| 88 | this->sendCommandReply(interest, {code, text}); |
| 89 | } |
| 90 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 91 | /** \brief Send failure response to a command Interest. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 92 | */ |
| 93 | void |
| 94 | failCommand(const Interest& interest, uint32_t code, const std::string& text, const ControlParameters& body) |
| 95 | { |
| 96 | this->sendCommandReply(interest, code, text, body.wireEncode()); |
| 97 | } |
| 98 | |
| 99 | protected: // StatusDataset |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 100 | /** \brief Send an empty dataset in reply to StatusDataset request. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 101 | * \param prefix dataset prefix without version and segment |
| 102 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 103 | */ |
| 104 | void |
| 105 | sendEmptyDataset(const Name& prefix) |
| 106 | { |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 107 | this->sendDatasetReply(prefix, span<uint8_t>{}); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 110 | /** \brief Send one WireEncodable in reply to StatusDataset request. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 111 | * \param prefix dataset prefix without version and segment |
| 112 | * \param payload payload block |
| 113 | * \note payload must fit in one Data |
| 114 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 115 | */ |
| 116 | template<typename T> |
| 117 | void |
| 118 | sendDataset(const Name& prefix, const T& payload) |
| 119 | { |
| 120 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>)); |
| 121 | |
| 122 | this->sendDatasetReply(prefix, payload.wireEncode()); |
| 123 | } |
| 124 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 125 | /** \brief Send two WireEncodables in reply to StatusDataset request. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 126 | * \param prefix dataset prefix without version and segment |
| 127 | * \param payload1 first vector item |
| 128 | * \param payload2 second vector item |
| 129 | * \note all payloads must fit in one Data |
| 130 | * \pre Interest for dataset has been expressed, sendDataset has not been invoked |
| 131 | */ |
| 132 | template<typename T1, typename T2> |
| 133 | void |
| 134 | sendDataset(const Name& prefix, const T1& payload1, const T2& payload2) |
| 135 | { |
| 136 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>)); |
| 137 | BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>)); |
| 138 | |
| 139 | ndn::encoding::EncodingBuffer buffer; |
| 140 | payload2.wireEncode(buffer); |
| 141 | payload1.wireEncode(buffer); |
| 142 | |
Davide Pesavento | a599d2a | 2022-02-16 18:52:43 -0500 | [diff] [blame] | 143 | this->sendDatasetReply(prefix, buffer); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
| 147 | virtual void |
| 148 | processEventsOverride(time::milliseconds timeout) |
| 149 | { |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 150 | if (timeout <= 0_ms) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 151 | // give enough time to finish execution |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 152 | timeout = 30_s; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 153 | } |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 154 | this->advanceClocks(100_ms, timeout); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | sendCommandReply(const Interest& interest, const ndn::nfd::ControlResponse& resp) |
| 159 | { |
| 160 | auto data = makeData(interest.getName()); |
| 161 | data->setContent(resp.wireEncode()); |
| 162 | face.receive(*data); |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | sendCommandReply(const Interest& interest, uint32_t code, const std::string& text, |
| 167 | const Block& body) |
| 168 | { |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 169 | this->sendCommandReply(interest, ndn::nfd::ControlResponse(code, text).setBody(body)); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 172 | /** \brief Send a payload in reply to StatusDataset request. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 173 | * \param name dataset prefix without version and segment |
| 174 | * \param contentArgs passed to Data::setContent |
| 175 | */ |
| 176 | template<typename ...ContentArgs> |
| 177 | void |
| 178 | sendDatasetReply(Name name, ContentArgs&&... contentArgs) |
| 179 | { |
| 180 | name.appendVersion().appendSegment(0); |
| 181 | |
| 182 | // These warnings assist in debugging when nfdc does not receive StatusDataset. |
| 183 | // They usually indicate a misspelled prefix or incorrect timing in the test case. |
| 184 | if (face.sentInterests.empty()) { |
| 185 | BOOST_WARN_MESSAGE(false, "no Interest expressed"); |
| 186 | } |
| 187 | else { |
| 188 | BOOST_WARN_MESSAGE(face.sentInterests.back().getName().isPrefixOf(name), |
| 189 | "last Interest " << face.sentInterests.back().getName() << |
| 190 | " cannot be satisfied by this Data " << name); |
| 191 | } |
| 192 | |
| 193 | auto data = make_shared<Data>(name); |
Junxiao Shi | 73b4980 | 2019-05-25 07:52:26 +0000 | [diff] [blame] | 194 | data->setFreshnessPeriod(1_s); |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 195 | data->setFinalBlock(name[-1]); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 196 | data->setContent(std::forward<ContentArgs>(contentArgs)...); |
| 197 | this->signDatasetReply(*data); |
| 198 | face.receive(*data); |
| 199 | } |
| 200 | |
| 201 | virtual void |
| 202 | signDatasetReply(Data& data) |
| 203 | { |
| 204 | signData(data); |
| 205 | } |
| 206 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 207 | protected: |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame] | 208 | ndn::DummyClientFace face; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 209 | std::function<void(const Interest&)> processInterest; |
| 210 | }; |
| 211 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 212 | } // namespace nfd::tests |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 213 | |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 214 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 215 | * \brief Require the command in \p interest to have the expected prefix. |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 216 | * \note This must be used in the `processInterest` lambda, and the Interest must be named `interest`. |
| 217 | * \return ControlParameters. The test case will fail if \p interest does not match \p expectedPrefix. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 218 | */ |
| 219 | #define MOCK_NFD_MGMT_REQUIRE_COMMAND_IS(expectedPrefix) \ |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 220 | [&interest] { \ |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 221 | auto params = parseCommand(interest, (expectedPrefix)); \ |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 222 | BOOST_REQUIRE_MESSAGE(params.has_value(), "Interest " << interest.getName() << \ |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 223 | " does not match command prefix " << (expectedPrefix)); \ |
| 224 | return *params; \ |
| 225 | } () |
| 226 | |
| 227 | #endif // NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP |