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