blob: a28611a5f87694c95ab8774b351641a15ca2345d [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 Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, 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
Davide Pesavento6d6f2072022-09-12 23:08:34 -040049 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040050 * \brief Create a ControlCommand request.
Davide Pesavento6d6f2072022-09-12 23:08:34 -040051 * \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 Shi8a1f1702017-07-03 00:05:08 +000055 */
56 Interest
Davide Pesavento20cafa82022-07-25 01:15:03 -040057 makeControlCommandRequest(Name commandName,
58 const ControlParameters& params = {},
Davide Pesavento6d6f2072022-09-12 23:08:34 -040059 ndn::security::SignedInterestFormat format = ndn::security::SignedInterestFormat::V03,
Junxiao Shi8a1f1702017-07-03 00:05:08 +000060 const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
61
jaczhib0657682025-01-08 23:01:45 -080062 /**
63 * \brief Create a ControlCommand request for a Prefix Announcement.
64 * \param commandName Command name including prefix, such as `/localhost/nfd/rib/announce`
65 * \param prefixAnnouncement Prefix Announcement object
66 * \param identity Signing identity
67 *
68 * Per specification, Prefix Announcements use Signed Interest v0.3 only.
69 */
70 Interest
71 makeControlCommandRequest(Name commandName,
72 const ndn::PrefixAnnouncement& prefixAnnouncement,
73 const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
74
Junxiao Shi8a1f1702017-07-03 00:05:08 +000075protected:
Davide Pesavento20cafa82022-07-25 01:15:03 -040076 static inline const Name DEFAULT_COMMAND_SIGNER_IDENTITY{"/InterestSignerFixture-identity"};
Junxiao Shi8a1f1702017-07-03 00:05:08 +000077
78private:
Davide Pesavento20cafa82022-07-25 01:15:03 -040079 ndn::security::InterestSigner m_signer{m_keyChain};
Junxiao Shi8a1f1702017-07-03 00:05:08 +000080};
81
Yanbiao Li698f4fe2015-08-19 16:30:16 -070082/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050083 * @brief A collection of common functions shared by all manager's test fixtures.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070084 */
Davide Pesavento20cafa82022-07-25 01:15:03 -040085class ManagerCommonFixture : public InterestSignerFixture
Yanbiao Li698f4fe2015-08-19 16:30:16 -070086{
Davide Pesavento4296fe32025-01-14 23:00:41 -050087public:
Yanbiao Li698f4fe2015-08-19 16:30:16 -070088 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050089 * @brief Add `/localhost/nfd` as a top prefix to the dispatcher.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070090 *
Davide Pesavento78ddcab2019-02-28 22:00:03 -050091 * Afterwards, advanceClocks() is called to ensure all added filters take effect.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070092 */
93 void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050094 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070095
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040097 * @brief Cause management to receive an Interest.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070098 *
Davide Pesavento4296fe32025-01-14 23:00:41 -050099 * Calls DummyClientFace::receive() to receive the Interest and then calls advanceClocks()
100 * to ensure the Interest is dispatched.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101 */
102 void
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000103 receiveInterest(const Interest& interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700104
Davide Pesavento20cafa82022-07-25 01:15:03 -0400105 static ControlResponse
Davide Pesavento4296fe32025-01-14 23:00:41 -0500106 makeResponse(uint32_t code, const std::string& text, const ControlParameters& parameters)
107 {
108 return ControlResponse(code, text).setBody(parameters.wireEncode());
109 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700110
Davide Pesavento20cafa82022-07-25 01:15:03 -0400111 enum class CheckResponseResult {
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700112 OK,
113 OUT_OF_BOUNDARY,
114 WRONG_NAME,
115 WRONG_CONTENT_TYPE,
116 INVALID_RESPONSE,
117 WRONG_CODE,
118 WRONG_TEXT,
119 WRONG_BODY_SIZE,
Davide Pesavento4296fe32025-01-14 23:00:41 -0500120 WRONG_BODY_VALUE,
Davide Pesavento35120ea2015-11-17 21:13:18 +0100121 };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700122
123 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400124 * @brief Check a specified response data with the expected ControlResponse.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700125 *
126 * @param idx the index of the specified Data in m_responses
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500127 * @param expectedName the expected name of this Data
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700128 * @param expectedResponse the expected ControlResponse
129 * @param expectedContentType the expected content type of this Data, use -1 to skip this check
130 *
131 * @retval OK the response at the specified index can be decoded from the response data,
132 * and its code, text and response body are all matched with the expected response
133 * @retval OUT_OF_BOUNDARY the specified index out of boundary
134 * @retval WRONG_NAME the name of the specified response data does not match
135 * @retval WRONG_CONTENT_TYPE the content type of the specified response data does not match
136 * @retval INVALID_RESPONSE the data name matches but it fails in decoding a ControlResponse from
137 * the content of the specified response data
138 * @retval WRONG_CODE a valid ControlResponse can be decoded but has a wrong code
139 * @retval WRONG_TEXT a valid ControlResponse can be decoded but has a wrong text
140 * @retval WRONG_BODY_SIZE the body size of decoded ControlResponse does not match
141 * @retval WRONT_BODY_VALUE the body value of decoded ControlResponse does not match
142 */
143 CheckResponseResult
144 checkResponse(size_t idx,
145 const Name& expectedName,
146 const ControlResponse& expectedResponse,
147 int expectedContentType = -1);
148
149 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400150 * @brief Concatenate specified response Data into a single block.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700151 *
152 * @param startIndex the start index in m_responses
153 * @param nResponses the number of response to concatenate
154 *
155 * @return the generated block
156 */
157 Block
158 concatenateResponses(size_t startIndex = 0, size_t nResponses = 0);
159
160protected:
Davide Pesaventoae430302023-05-11 01:42:46 -0400161 ndn::DummyClientFace m_face{g_io, m_keyChain, {true, true}};
Davide Pesavento20cafa82022-07-25 01:15:03 -0400162 Dispatcher m_dispatcher{m_face, m_keyChain};
163 std::vector<Data>& m_responses{m_face.sentData};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700164};
165
166std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500167operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result);
168
169class ManagerFixtureWithAuthenticator : public ManagerCommonFixture
170{
171public:
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500172 /**
173 * \brief Grant m_identityName privilege to sign commands for the management module.
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500174 */
175 void
176 setPrivilege(const std::string& privilege);
177
178protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400179 FaceTable m_faceTable;
180 Forwarder m_forwarder{m_faceTable};
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500181 shared_ptr<CommandAuthenticator> m_authenticator = CommandAuthenticator::create();
182};
183
184class CommandSuccess
185{
186public:
187 static ControlResponse
188 getExpected()
189 {
190 return ControlResponse()
191 .setCode(200)
192 .setText("OK");
193 }
194};
195
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400196template<auto CODE>
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500197class CommandFailure
198{
199public:
200 static ControlResponse
201 getExpected()
202 {
203 return ControlResponse()
204 .setCode(CODE);
205 // error description should not be checked
206 }
207};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700208
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209} // namespace nfd::tests
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700210
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500211#endif // NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP