blob: 35576276f1013c2c248edb42c73364be930ad03b [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/*
3 * Copyright (c) 2014-2019, 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
Junxiao Shi8a1f1702017-07-03 00:05:08 +000036#include <ndn-cxx/security/command-interest-signer.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037#include <ndn-cxx/util/dummy-client-face.hpp>
38
39namespace nfd {
40namespace tests {
41
Davide Pesavento78ddcab2019-02-28 22:00:03 -050042/** \brief A fixture that provides a CommandInterestSigner.
Junxiao Shi8a1f1702017-07-03 00:05:08 +000043 */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040044class CommandInterestSignerFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi8a1f1702017-07-03 00:05:08 +000045{
46protected:
47 CommandInterestSignerFixture();
48
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
58 * \param commandName command name including prefix, such as "/localhost/nfd/fib/add-nexthop"
59 * \param params command parameters
60 * \param identity signing identity
61 * \return a command Interest
62 */
63 Interest
64 makeControlCommandRequest(Name commandName, const ControlParameters& params,
65 const Name& identity = DEFAULT_COMMAND_SIGNER_IDENTITY);
66
67protected:
68 static const Name DEFAULT_COMMAND_SIGNER_IDENTITY;
69
70private:
71 ndn::security::CommandInterestSigner m_commandInterestSigner;
72};
73
Yanbiao Li698f4fe2015-08-19 16:30:16 -070074/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050075 * @brief A collection of common functions shared by all manager's test fixtures.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070076 */
Junxiao Shi8a1f1702017-07-03 00:05:08 +000077class ManagerCommonFixture : public CommandInterestSignerFixture
Yanbiao Li698f4fe2015-08-19 16:30:16 -070078{
79public: // initialize
80 ManagerCommonFixture();
81
82 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050083 * @brief Add `/localhost/nfd` as a top prefix to the dispatcher.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070084 *
Davide Pesavento78ddcab2019-02-28 22:00:03 -050085 * Afterwards, advanceClocks() is called to ensure all added filters take effect.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070086 */
87 void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050088 setTopPrefix();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070089
90public: // test
Yanbiao Li698f4fe2015-08-19 16:30:16 -070091 /**
92 * @brief cause management to receive an Interest
93 *
94 * call DummyClientFace::receive to receive Interest and then call advanceClocks to ensure
95 * the Interest dispatched
96 *
97 * @param interest the Interest to receive
98 */
99 void
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000100 receiveInterest(const Interest& interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101
102public: // verify
103 ControlResponse
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500104 makeResponse(uint32_t code, const std::string& text, const ControlParameters& parameters);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700105
106 enum class CheckResponseResult
107 {
108 OK,
109 OUT_OF_BOUNDARY,
110 WRONG_NAME,
111 WRONG_CONTENT_TYPE,
112 INVALID_RESPONSE,
113 WRONG_CODE,
114 WRONG_TEXT,
115 WRONG_BODY_SIZE,
116 WRONG_BODY_VALUE
Davide Pesavento35120ea2015-11-17 21:13:18 +0100117 };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700118
119 /**
120 * @brief check a specified response data with the expected ControlResponse
121 *
122 * @param idx the index of the specified Data in m_responses
123 * @param expectedDataName the expected name of this Data
124 * @param expectedResponse the expected ControlResponse
125 * @param expectedContentType the expected content type of this Data, use -1 to skip this check
126 *
127 * @retval OK the response at the specified index can be decoded from the response data,
128 * and its code, text and response body are all matched with the expected response
129 * @retval OUT_OF_BOUNDARY the specified index out of boundary
130 * @retval WRONG_NAME the name of the specified response data does not match
131 * @retval WRONG_CONTENT_TYPE the content type of the specified response data does not match
132 * @retval INVALID_RESPONSE the data name matches but it fails in decoding a ControlResponse from
133 * the content of the specified response data
134 * @retval WRONG_CODE a valid ControlResponse can be decoded but has a wrong code
135 * @retval WRONG_TEXT a valid ControlResponse can be decoded but has a wrong text
136 * @retval WRONG_BODY_SIZE the body size of decoded ControlResponse does not match
137 * @retval WRONT_BODY_VALUE the body value of decoded ControlResponse does not match
138 */
139 CheckResponseResult
140 checkResponse(size_t idx,
141 const Name& expectedName,
142 const ControlResponse& expectedResponse,
143 int expectedContentType = -1);
144
145 /**
146 * @brief concatenate specified response Data into a single block
147 *
148 * @param startIndex the start index in m_responses
149 * @param nResponses the number of response to concatenate
150 *
151 * @return the generated block
152 */
153 Block
154 concatenateResponses(size_t startIndex = 0, size_t nResponses = 0);
155
156protected:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000157 ndn::util::DummyClientFace m_face;
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500158 Dispatcher m_dispatcher;
159 std::vector<Data>& m_responses; ///< a reference to m_face.sentData
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700160};
161
162std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500163operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result);
164
165class ManagerFixtureWithAuthenticator : public ManagerCommonFixture
166{
167public:
168 /** \brief grant m_identityName privilege to sign commands for the management module
169 */
170 void
171 setPrivilege(const std::string& privilege);
172
173protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400174 FaceTable m_faceTable;
175 Forwarder m_forwarder{m_faceTable};
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500176 shared_ptr<CommandAuthenticator> m_authenticator = CommandAuthenticator::create();
177};
178
179class CommandSuccess
180{
181public:
182 static ControlResponse
183 getExpected()
184 {
185 return ControlResponse()
186 .setCode(200)
187 .setText("OK");
188 }
189};
190
191template<int CODE>
192class CommandFailure
193{
194public:
195 static ControlResponse
196 getExpected()
197 {
198 return ControlResponse()
199 .setCode(CODE);
200 // error description should not be checked
201 }
202};
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700203
204} // namespace tests
205} // namespace nfd
206
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500207#endif // NFD_TESTS_DAEMON_MGMT_MANAGER_COMMON_FIXTURE_HPP