blob: 4eeab41672fe2ef6faf9b99862cdb785c90ec38b [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, 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_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP
27#define NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP
28
29#include <ndn-cxx/util/dummy-client-face.hpp>
30
31#include "tests/test-common.hpp"
32#include "tests/identity-management-fixture.hpp"
33
34namespace nfd {
35namespace tools {
36namespace tests {
37
38using namespace nfd::tests;
39using ndn::nfd::ControlParameters;
40
41/** \brief fixture to emulate NFD management
42 */
43class MockNfdMgmtFixture : public IdentityManagementTimeFixture
44{
45protected:
46 MockNfdMgmtFixture()
47 : face(g_io, m_keyChain,
48 {true, false, bind(&MockNfdMgmtFixture::processEventsOverride, this, _1)})
49 {
50 face.onSendInterest.connect([=] (const Interest& interest) {
51 g_io.post([=] {
52 if (processInterest != nullptr) {
53 processInterest(interest);
54 }
55 });
56 });
57 }
58
59protected: // ControlCommand
60 /** \brief check the Interest is a command with specified prefix
61 * \retval nullopt last Interest is not the expected command
62 * \return command parameters
63 */
64 static ndn::optional<ControlParameters>
65 parseCommand(const Interest& interest, const Name& expectedPrefix)
66 {
67 if (!expectedPrefix.isPrefixOf(interest.getName())) {
68 return ndn::nullopt;
69 }
70 return ControlParameters(interest.getName().at(expectedPrefix.size()).blockFromValue());
71 }
72
73 /** \brief send successful response to a command Interest
74 */
75 void
76 succeedCommand(const Interest& interest, const ControlParameters& parameters)
77 {
78 this->sendCommandReply(interest, 200, "OK", parameters.wireEncode());
79 }
80
81 /** \brief send failure response to a command Interest
82 */
83 void
84 failCommand(const Interest& interest, uint32_t code, const std::string& text)
85 {
86 this->sendCommandReply(interest, {code, text});
87 }
88
89 /** \brief send failure response to a command Interest
90 */
91 void
92 failCommand(const Interest& interest, uint32_t code, const std::string& text, const ControlParameters& body)
93 {
94 this->sendCommandReply(interest, code, text, body.wireEncode());
95 }
96
97protected: // StatusDataset
98 /** \brief send an empty dataset in reply to StatusDataset request
99 * \param prefix dataset prefix without version and segment
100 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
101 */
102 void
103 sendEmptyDataset(const Name& prefix)
104 {
105 this->sendDatasetReply(prefix, nullptr, 0);
106 }
107
108 /** \brief send one WireEncodable in reply to StatusDataset request
109 * \param prefix dataset prefix without version and segment
110 * \param payload payload block
111 * \note payload must fit in one Data
112 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
113 */
114 template<typename T>
115 void
116 sendDataset(const Name& prefix, const T& payload)
117 {
118 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T>));
119
120 this->sendDatasetReply(prefix, payload.wireEncode());
121 }
122
123 /** \brief send two WireEncodables in reply to StatusDataset request
124 * \param prefix dataset prefix without version and segment
125 * \param payload1 first vector item
126 * \param payload2 second vector item
127 * \note all payloads must fit in one Data
128 * \pre Interest for dataset has been expressed, sendDataset has not been invoked
129 */
130 template<typename T1, typename T2>
131 void
132 sendDataset(const Name& prefix, const T1& payload1, const T2& payload2)
133 {
134 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T1>));
135 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<T2>));
136
137 ndn::encoding::EncodingBuffer buffer;
138 payload2.wireEncode(buffer);
139 payload1.wireEncode(buffer);
140
141 this->sendDatasetReply(prefix, buffer.buf(), buffer.size());
142 }
143
144private:
145 virtual void
146 processEventsOverride(time::milliseconds timeout)
147 {
148 if (timeout <= time::milliseconds::zero()) {
149 // give enough time to finish execution
150 timeout = time::seconds(30);
151 }
152 this->advanceClocks(time::milliseconds(100), timeout);
153 }
154
155 void
156 sendCommandReply(const Interest& interest, const ndn::nfd::ControlResponse& resp)
157 {
158 auto data = makeData(interest.getName());
159 data->setContent(resp.wireEncode());
160 face.receive(*data);
161 }
162
163 void
164 sendCommandReply(const Interest& interest, uint32_t code, const std::string& text,
165 const Block& body)
166 {
167 this->sendCommandReply(interest,
168 ndn::nfd::ControlResponse(code, text).setBody(body));
169 }
170
171 /** \brief send a payload in reply to StatusDataset request
172 * \param name dataset prefix without version and segment
173 * \param contentArgs passed to Data::setContent
174 */
175 template<typename ...ContentArgs>
176 void
177 sendDatasetReply(Name name, ContentArgs&&... contentArgs)
178 {
179 name.appendVersion().appendSegment(0);
180
181 // These warnings assist in debugging when nfdc does not receive StatusDataset.
182 // They usually indicate a misspelled prefix or incorrect timing in the test case.
183 if (face.sentInterests.empty()) {
184 BOOST_WARN_MESSAGE(false, "no Interest expressed");
185 }
186 else {
187 BOOST_WARN_MESSAGE(face.sentInterests.back().getName().isPrefixOf(name),
188 "last Interest " << face.sentInterests.back().getName() <<
189 " cannot be satisfied by this Data " << name);
190 }
191
192 auto data = make_shared<Data>(name);
193 data->setFinalBlockId(name[-1]);
194 data->setContent(std::forward<ContentArgs>(contentArgs)...);
195 this->signDatasetReply(*data);
196 face.receive(*data);
197 }
198
199 virtual void
200 signDatasetReply(Data& data)
201 {
202 signData(data);
203 }
204
205protected:
206 ndn::util::DummyClientFace face;
207 std::function<void(const Interest&)> processInterest;
208};
209
210} // namespace tests
211} // namespace tools
212} // namespace nfd
213
214/** \brief require the command in \p interest has expected prefix
215 * \note This must be used in processInterest lambda, and the Interest must be named 'interest'.
216 * \return ControlParameters, or nullopt if \p interest does match \p expectedPrefix
217 */
218#define MOCK_NFD_MGMT_REQUIRE_COMMAND_IS(expectedPrefix) \
219 [interest] { \
220 auto params = parseCommand(interest, (expectedPrefix)); \
221 BOOST_REQUIRE_MESSAGE(params, "Interest " << interest.getName() << \
222 " does not match command prefix " << (expectedPrefix)); \
223 return *params; \
224 } ()
225
226#endif // NFD_TESTS_TOOLS_MOCK_NFD_MGMT_FIXTURE_HPP