blob: 79923a03f13e07f397045e63277973e7f30a437e [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 Pesavento6a699be2021-05-17 02:13:37 -040041/** \brief A fixture that wraps an InterestSigner.
Junxiao Shi8a1f1702017-07-03 00:05:08 +000042 */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043class CommandInterestSignerFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi8a1f1702017-07-03 00:05:08 +000044{
45protected:
46 CommandInterestSignerFixture();
47
48 /** \brief sign a command Interest
49 * \param name command name include prefix and parameters
50 * \param identity signing identity
51 * \return a command Interest
52 */
53 Interest
54 makeCommandInterest(const Name& name, const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
55
56 /** \brief create a ControlCommand request
Davide Pesavento6a699be2021-05-17 02:13:37 -040057 * \param commandName command name including prefix, such as `/localhost/nfd/fib/add-nexthop`
Junxiao Shi8a1f1702017-07-03 00:05:08 +000058 * \param params command parameters
59 * \param identity signing identity
60 * \return a command Interest
61 */
62 Interest
63 makeControlCommandRequest(Name commandName, const ControlParameters& params,
64 const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
65
66protected:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040067 static inline const Name DEFAULT_COMMAND_SIGNER_IDENTITY{"/CommandInterestSignerFixture-identity"};
Junxiao Shi8a1f1702017-07-03 00:05:08 +000068
69private:
Davide Pesavento6a699be2021-05-17 02:13:37 -040070 ndn::security::InterestSigner m_signer;
Junxiao Shi8a1f1702017-07-03 00:05:08 +000071};
72
Yanbiao Li698f4fe2015-08-19 16:30:16 -070073/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050074 * @brief A collection of common functions shared by all manager's test fixtures.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075 */
Junxiao Shi8a1f1702017-07-03 00:05:08 +000076class ManagerCommonFixture : public CommandInterestSignerFixture
Yanbiao Li698f4fe2015-08-19 16:30:16 -070077{
78public: // initialize
79 ManagerCommonFixture();
80
81 /**
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
102 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
105 enum class CheckResponseResult
106 {
107 OK,
108 OUT_OF_BOUNDARY,
109 WRONG_NAME,
110 WRONG_CONTENT_TYPE,
111 INVALID_RESPONSE,
112 WRONG_CODE,
113 WRONG_TEXT,
114 WRONG_BODY_SIZE,
115 WRONG_BODY_VALUE
Davide Pesavento35120ea2015-11-17 21:13:18 +0100116 };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700117
118 /**
119 * @brief check a specified response data with the expected ControlResponse
120 *
121 * @param idx the index of the specified Data in m_responses
122 * @param expectedDataName the expected name of this Data
123 * @param expectedResponse the expected ControlResponse
124 * @param expectedContentType the expected content type of this Data, use -1 to skip this check
125 *
126 * @retval OK the response at the specified index can be decoded from the response data,
127 * and its code, text and response body are all matched with the expected response
128 * @retval OUT_OF_BOUNDARY the specified index out of boundary
129 * @retval WRONG_NAME the name of the specified response data does not match
130 * @retval WRONG_CONTENT_TYPE the content type of the specified response data does not match
131 * @retval INVALID_RESPONSE the data name matches but it fails in decoding a ControlResponse from
132 * the content of the specified response data
133 * @retval WRONG_CODE a valid ControlResponse can be decoded but has a wrong code
134 * @retval WRONG_TEXT a valid ControlResponse can be decoded but has a wrong text
135 * @retval WRONG_BODY_SIZE the body size of decoded ControlResponse does not match
136 * @retval WRONT_BODY_VALUE the body value of decoded ControlResponse does not match
137 */
138 CheckResponseResult
139 checkResponse(size_t idx,
140 const Name& expectedName,
141 const ControlResponse& expectedResponse,
142 int expectedContentType = -1);
143
144 /**
145 * @brief concatenate specified response Data into a single block
146 *
147 * @param startIndex the start index in m_responses
148 * @param nResponses the number of response to concatenate
149 *
150 * @return the generated block
151 */
152 Block
153 concatenateResponses(size_t startIndex = 0, size_t nResponses = 0);
154
155protected:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000156 ndn::util::DummyClientFace m_face;
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500157 Dispatcher m_dispatcher;
158 std::vector<Data>& m_responses; ///< a reference to m_face.sentData
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700159};
160
161std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500162operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result);
163
164class ManagerFixtureWithAuthenticator : public ManagerCommonFixture
165{
166public:
167 /** \brief grant m_identityName privilege to sign commands for the management module
168 */
169 void
170 setPrivilege(const std::string& privilege);
171
172protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400173 FaceTable m_faceTable;
174 Forwarder m_forwarder{m_faceTable};
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500175 shared_ptr<CommandAuthenticator> m_authenticator = CommandAuthenticator::create();
176};
177
178class CommandSuccess
179{
180public:
181 static ControlResponse
182 getExpected()
183 {
184 return ControlResponse()
185 .setCode(200)
186 .setText("OK");
187 }
188};
189
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400190template<auto CODE>
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500191class CommandFailure
192{
193public:
194 static ControlResponse
195 getExpected()
196 {
197 return ControlResponse()
198 .setCode(CODE);
199 // error description should not be checked
200 }
201};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700202
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400203} // namespace nfd::tests
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700204
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500205#endif // NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP