blob: a29c441981301a4a328dfa0b031b6824bef8942f [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, 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
Yanbiao Lidf846e52016-01-30 21:53:47 -080026#ifndef NFD_TESTS_MANAGER_COMMON_FIXTURE_HPP
27#define NFD_TESTS_MANAGER_COMMON_FIXTURE_HPP
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028
29#include "tests/test-common.hpp"
30#include "tests/identity-management-fixture.hpp"
Yanbiao Lidf846e52016-01-30 21:53:47 -080031#include "core/manager-base.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070032
33#include <ndn-cxx/mgmt/dispatcher.hpp>
34#include <ndn-cxx/util/dummy-client-face.hpp>
35
36namespace nfd {
37namespace tests {
38
39/**
40 * @brief a collection of common functions shared by all manager's testing fixtures.
41 */
42class ManagerCommonFixture : public UnitTestTimeFixture
43 , public IdentityManagementFixture
44{
45public: // initialize
46 ManagerCommonFixture();
47
48 /**
Yanbiao Lidf846e52016-01-30 21:53:47 -080049 * @brief set topPrefix to the dispatcher.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070050 *
51 * after setting @param topPrefix, call advanceClocks to ensure all added filters take effects.
52 *
53 * @param topPrefix top prefix for the dispatcher
Yanbiao Li698f4fe2015-08-19 16:30:16 -070054 */
55 void
Yanbiao Lidf846e52016-01-30 21:53:47 -080056 setTopPrefix(const Name& topPrefix);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070057
58public: // test
59 typedef std::function<void(shared_ptr<Interest> interest)> InterestHandler;
60
61 /**
62 * @brief create a ControlCommand request
63 *
64 * step1: append the @param parameters to the @param commandName and make an Interest.
65 * step2: call @param beforeSinging to do something with the Interest.
66 * step3: sign the generated command
67 *
68 * @param commandName the command name
69 * @param parameters the ControlParameters
70 * @param beforeSigning a callback that can modify the Interest before it's signed
71 *
72 * @return the signed ControlCommand request
73 */
74 shared_ptr<Interest>
75 makeControlCommandRequest(Name commandName,
76 const ControlParameters& parameters,
77 const InterestHandler& beforeSigning = nullptr);
78
79 /**
80 * @brief cause management to receive an Interest
81 *
82 * call DummyClientFace::receive to receive Interest and then call advanceClocks to ensure
83 * the Interest dispatched
84 *
85 * @param interest the Interest to receive
86 */
87 void
88 receiveInterest(shared_ptr<Interest> interest);
89
90public: // verify
91 ControlResponse
92 makeResponse(uint32_t code, const std::string& text,
93 const ControlParameters& parameters);
94
95 enum class CheckResponseResult
96 {
97 OK,
98 OUT_OF_BOUNDARY,
99 WRONG_NAME,
100 WRONG_CONTENT_TYPE,
101 INVALID_RESPONSE,
102 WRONG_CODE,
103 WRONG_TEXT,
104 WRONG_BODY_SIZE,
105 WRONG_BODY_VALUE
Davide Pesavento35120ea2015-11-17 21:13:18 +0100106 };
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700107
108 /**
109 * @brief check a specified response data with the expected ControlResponse
110 *
111 * @param idx the index of the specified Data in m_responses
112 * @param expectedDataName the expected name of this Data
113 * @param expectedResponse the expected ControlResponse
114 * @param expectedContentType the expected content type of this Data, use -1 to skip this check
115 *
116 * @retval OK the response at the specified index can be decoded from the response data,
117 * and its code, text and response body are all matched with the expected response
118 * @retval OUT_OF_BOUNDARY the specified index out of boundary
119 * @retval WRONG_NAME the name of the specified response data does not match
120 * @retval WRONG_CONTENT_TYPE the content type of the specified response data does not match
121 * @retval INVALID_RESPONSE the data name matches but it fails in decoding a ControlResponse from
122 * the content of the specified response data
123 * @retval WRONG_CODE a valid ControlResponse can be decoded but has a wrong code
124 * @retval WRONG_TEXT a valid ControlResponse can be decoded but has a wrong text
125 * @retval WRONG_BODY_SIZE the body size of decoded ControlResponse does not match
126 * @retval WRONT_BODY_VALUE the body value of decoded ControlResponse does not match
127 */
128 CheckResponseResult
129 checkResponse(size_t idx,
130 const Name& expectedName,
131 const ControlResponse& expectedResponse,
132 int expectedContentType = -1);
133
134 /**
135 * @brief concatenate specified response Data into a single block
136 *
137 * @param startIndex the start index in m_responses
138 * @param nResponses the number of response to concatenate
139 *
140 * @return the generated block
141 */
142 Block
143 concatenateResponses(size_t startIndex = 0, size_t nResponses = 0);
144
145protected:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000146 ndn::util::DummyClientFace m_face;
147 ndn::mgmt::Dispatcher m_dispatcher;
148 std::vector<Data>& m_responses; ///< a reference of m_face->sentData
149 Name m_identityName; ///< the identity used to sign request
150 shared_ptr<ndn::IdentityCertificate> m_certificate; ///< the certificate used to sign request
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700151};
152
153std::ostream&
154operator<<(std::ostream &os, const ManagerCommonFixture::CheckResponseResult& result);
155
156} // namespace tests
157} // namespace nfd
158
Yanbiao Lidf846e52016-01-30 21:53:47 -0800159#endif // NFD_TESTS_MANAGER_COMMON_FIXTURE_HPP