blob: bc040453e7082878b169ec476f129f348c916ace [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7d30d852017-01-22 03:29:26 +00003 * Copyright (c) 2014-2017, 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
26#include "manager-common-fixture.hpp"
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000027#include <ndn-cxx/security/signing-helpers.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028
29namespace nfd {
30namespace tests {
31
32ManagerCommonFixture::ManagerCommonFixture()
Junxiao Shi221b6fe2016-07-14 18:21:56 +000033 : m_face(getGlobalIoService(), m_keyChain, {true, true})
34 , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo())
35 , m_responses(m_face.sentData)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070036 , m_identityName("/unit-test/ManagerCommonFixture/identity")
Yanbiao Li698f4fe2015-08-19 16:30:16 -070037{
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000038 BOOST_REQUIRE(this->addIdentity(m_identityName));
Yanbiao Li698f4fe2015-08-19 16:30:16 -070039}
40
41void
Yanbiao Lidf846e52016-01-30 21:53:47 -080042ManagerCommonFixture::setTopPrefix(const Name& topPrefix)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070043{
44 m_dispatcher.addTopPrefix(topPrefix); // such that all filters are added
45 advanceClocks(time::milliseconds(1));
Yanbiao Li698f4fe2015-08-19 16:30:16 -070046}
47
48shared_ptr<Interest>
49ManagerCommonFixture::makeControlCommandRequest(Name commandName,
50 const ControlParameters& parameters,
51 const InterestHandler& beforeSigning)
52{
53 shared_ptr<Interest> command = makeInterest(commandName.append(parameters.wireEncode()));
54
55 if (beforeSigning != nullptr) {
56 beforeSigning(command);
57 }
58
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000059 m_keyChain.sign(*command, ndn::security::signingByIdentity(m_identityName));
Yanbiao Li698f4fe2015-08-19 16:30:16 -070060 return command;
61}
62
63void
64ManagerCommonFixture::receiveInterest(shared_ptr<Interest> interest)
65{
Junxiao Shi221b6fe2016-07-14 18:21:56 +000066 m_face.receive(*interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070067 advanceClocks(time::milliseconds(1));
68}
69
70ControlResponse
71ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text,
72 const ControlParameters& parameters)
73{
74 return ControlResponse(code, text).setBody(parameters.wireEncode());
75}
76
77ManagerCommonFixture::CheckResponseResult
78ManagerCommonFixture::checkResponse(size_t idx,
79 const Name& expectedName,
80 const ControlResponse& expectedResponse,
81 int expectedContentType /*= -1*/)
82{
83 Data data;
84 try {
85 data = m_responses.at(idx);
86 }
87 catch (const std::out_of_range&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000088 BOOST_TEST_MESSAGE("response[" << idx << "] does not exist");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070089 return CheckResponseResult::OUT_OF_BOUNDARY;
90 }
91
92 if (data.getName() != expectedName) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000093 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName());
Yanbiao Li698f4fe2015-08-19 16:30:16 -070094 return CheckResponseResult::WRONG_NAME;
95 }
96
97 if (expectedContentType != -1 &&
98 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000099 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700100 return CheckResponseResult::WRONG_CONTENT_TYPE;
101 }
102
103 ControlResponse response;
104 try {
105 response.wireDecode(data.getContent().blockFromValue());
106 }
107 catch (const tlv::Error&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000108 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700109 return CheckResponseResult::INVALID_RESPONSE;
110 }
111
112 if (response.getCode() != expectedResponse.getCode()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000113 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700114 return CheckResponseResult::WRONG_CODE;
115 }
116
117 if (response.getText() != expectedResponse.getText()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000118 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700119 return CheckResponseResult::WRONG_TEXT;
120 }
121
122 const Block& body = response.getBody();
123 const Block& expectedBody = expectedResponse.getBody();
124 if (body.value_size() != expectedBody.value_size()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000125 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700126 return CheckResponseResult::WRONG_BODY_SIZE;
127 }
128 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000129 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700130 return CheckResponseResult::WRONG_BODY_VALUE;
131 }
132
133 return CheckResponseResult::OK;
134}
135
136Block
137ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
138{
Yanbiao Li150b80d2016-03-18 15:30:10 +0800139 auto isFinalSegment = [] (const Data& data) -> bool {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800140 const name::Component& lastComponent = data.getName().at(-1);
141 return !lastComponent.isSegment() || lastComponent == data.getFinalBlockId();
Yanbiao Li150b80d2016-03-18 15:30:10 +0800142 };
143
Yanbiao Lidf846e52016-01-30 21:53:47 -0800144 while (!isFinalSegment(m_responses.back())) {
145 const Name& name = m_responses.back().getName();
146 Name prefix = name.getPrefix(-1);
147 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800148 // request for the next segment
149 receiveInterest(makeInterest(prefix.appendSegment(segmentNo)));
150 }
151
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700152 size_t endIndex = startIndex + nResponses; // not included
153 if (nResponses == startIndex || endIndex > m_responses.size()) {
154 endIndex = m_responses.size();
155 }
156
157 ndn::EncodingBuffer encoder;
158 size_t valueLength = 0;
159 for (size_t i = startIndex; i < endIndex ; i ++) {
160 valueLength += encoder.appendByteArray(m_responses[i].getContent().value(),
161 m_responses[i].getContent().value_size());
162 }
163 encoder.prependVarNumber(valueLength);
164 encoder.prependVarNumber(tlv::Content);
165 return encoder.block();
166}
167
168std::ostream&
169operator<<(std::ostream &os, const ManagerCommonFixture::CheckResponseResult& result)
170{
171 switch (result) {
172 case ManagerCommonFixture::CheckResponseResult::OK:
173 os << "OK";
174 break;
175 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
176 os << "OUT_OF_BOUNDARY";
177 break;
178 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
179 os << "WRONG_NAME";
180 break;
181 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
182 os << "WRONG_CONTENT_TYPE";
183 break;
184 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
185 os << "INVALID_RESPONSE";
186 break;
187 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
188 os << "WRONG_CODE";
189 break;
190 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
191 os << "WRONG_TEXT";
192 break;
193 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
194 os << "WRONG_BODY_SIZE";
195 break;
196 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
197 os << "WRONG_BODY_VALUE";
198 break;
199 default:
200 break;
201 };
202
203 return os;
204}
205
206} // namespace tests
207} // namespace nfd