blob: 2ae3b8526c917561ee636668c36d1e51792d046c [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento78ddcab2019-02-28 22:00:03 -05002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Yanbiao Li698f4fe2015-08-19 16:30:16 -07004 * 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 Pesavento78ddcab2019-02-28 22:00:03 -050026#ifndef NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028
Davide Pesavento78ddcab2019-02-28 22:00:03 -050029#include "mgmt/manager-base.hpp"
30#include "fw/forwarder.hpp"
31
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040032#include "tests/test-common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040033#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040034#include "tests/daemon/global-io-fixture.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070035
Davide Pesavento6a699be2021-05-17 02:13:37 -040036#include <ndn-cxx/security/interest-signer.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037#include <ndn-cxx/util/dummy-client-face.hpp>
38
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039namespace nfd::tests {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070040
Davide Pesavento20cafa82022-07-25 01:15:03 -040041/**
42 * \brief A fixture that wraps an InterestSigner.
Junxiao Shi8a1f1702017-07-03 00:05:08 +000043 */
Davide Pesavento20cafa82022-07-25 01:15:03 -040044class InterestSignerFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi8a1f1702017-07-03 00:05:08 +000045{
46protected:
Davide Pesavento20cafa82022-07-25 01:15:03 -040047 InterestSignerFixture();
Junxiao Shi8a1f1702017-07-03 00:05:08 +000048
49 /** \brief sign a command Interest
50 * \param name command name include prefix and parameters
51 * \param identity signing identity
52 * \return a command Interest
53 */
54 Interest
55 makeCommandInterest(const Name& name, const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
56
57 /** \brief create a ControlCommand request
Davide Pesavento6a699be2021-05-17 02:13:37 -040058 * \param commandName command name including prefix, such as `/localhost/nfd/fib/add-nexthop`
Junxiao Shi8a1f1702017-07-03 00:05:08 +000059 * \param params command parameters
60 * \param identity signing identity
61 * \return a command Interest
62 */
63 Interest
Davide Pesavento20cafa82022-07-25 01:15:03 -040064 makeControlCommandRequest(Name commandName,
65 const ControlParameters& params = {},
Junxiao Shi8a1f1702017-07-03 00:05:08 +000066 const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
67
68protected:
Davide Pesavento20cafa82022-07-25 01:15:03 -040069 static inline const Name DEFAULT_COMMAND_SIGNER_IDENTITY{"/InterestSignerFixture-identity"};
Junxiao Shi8a1f1702017-07-03 00:05:08 +000070
71private:
Davide Pesavento20cafa82022-07-25 01:15:03 -040072 ndn::security::InterestSigner m_signer{m_keyChain};
Junxiao Shi8a1f1702017-07-03 00:05:08 +000073};
74
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050076 * @brief A collection of common functions shared by all manager's test fixtures.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070077 */
Davide Pesavento20cafa82022-07-25 01:15:03 -040078class ManagerCommonFixture : public InterestSignerFixture
Yanbiao Li698f4fe2015-08-19 16:30:16 -070079{
80public: // initialize
Yanbiao Li698f4fe2015-08-19 16:30:16 -070081 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050082 * @brief Add `/localhost/nfd` as a top prefix to the dispatcher.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070083 *
Davide Pesavento78ddcab2019-02-28 22:00:03 -050084 * Afterwards, advanceClocks() is called to ensure all added filters take effect.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070085 */
86 void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050087 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070088
89public: // test
Yanbiao Li698f4fe2015-08-19 16:30:16 -070090 /**
91 * @brief cause management to receive an Interest
92 *
93 * call DummyClientFace::receive to receive Interest and then call advanceClocks to ensure
94 * the Interest dispatched
95 *
96 * @param interest the Interest to receive
97 */
98 void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000099 receiveInterest(const Interest& interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700100
101public: // verify
Davide Pesavento20cafa82022-07-25 01:15:03 -0400102 static ControlResponse
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500103 makeResponse(uint32_t code, const std::string& text, const ControlParameters& parameters);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700104
Davide Pesavento20cafa82022-07-25 01:15:03 -0400105 enum class CheckResponseResult {
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700106 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 Pesavento35120ea2015-11-17 21:13:18 +0100115 };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700116
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
154protected:
Davide Pesavento20cafa82022-07-25 01:15:03 -0400155 ndn::util::DummyClientFace m_face{g_io, m_keyChain, {true, true}};
156 Dispatcher m_dispatcher{m_face, m_keyChain};
157 std::vector<Data>& m_responses{m_face.sentData};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700158};
159
160std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500161operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result);
162
163class ManagerFixtureWithAuthenticator : public ManagerCommonFixture
164{
165public:
166 /** \brief grant m_identityName privilege to sign commands for the management module
167 */
168 void
169 setPrivilege(const std::string& privilege);
170
171protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400172 FaceTable m_faceTable;
173 Forwarder m_forwarder{m_faceTable};
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500174 shared_ptr<CommandAuthenticator> m_authenticator = CommandAuthenticator::create();
175};
176
177class CommandSuccess
178{
179public:
180 static ControlResponse
181 getExpected()
182 {
183 return ControlResponse()
184 .setCode(200)
185 .setText("OK");
186 }
187};
188
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400189template<auto CODE>
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500190class CommandFailure
191{
192public:
193 static ControlResponse
194 getExpected()
195 {
196 return ControlResponse()
197 .setCode(CODE);
198 // error description should not be checked
199 }
200};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700201
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400202} // namespace nfd::tests
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700203
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500204#endif // NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP