blob: 88db7e114ef25918492b8f6a47b0b8b2fd3602c3 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li150b80d2016-03-18 15:30:10 +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
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&) {
88 return CheckResponseResult::OUT_OF_BOUNDARY;
89 }
90
91 if (data.getName() != expectedName) {
92 return CheckResponseResult::WRONG_NAME;
93 }
94
95 if (expectedContentType != -1 &&
96 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
97 return CheckResponseResult::WRONG_CONTENT_TYPE;
98 }
99
100 ControlResponse response;
101 try {
102 response.wireDecode(data.getContent().blockFromValue());
103 }
104 catch (const tlv::Error&) {
105 return CheckResponseResult::INVALID_RESPONSE;
106 }
107
108 if (response.getCode() != expectedResponse.getCode()) {
109 return CheckResponseResult::WRONG_CODE;
110 }
111
112 if (response.getText() != expectedResponse.getText()) {
113 return CheckResponseResult::WRONG_TEXT;
114 }
115
116 const Block& body = response.getBody();
117 const Block& expectedBody = expectedResponse.getBody();
118 if (body.value_size() != expectedBody.value_size()) {
119 return CheckResponseResult::WRONG_BODY_SIZE;
120 }
121 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
122 return CheckResponseResult::WRONG_BODY_VALUE;
123 }
124
125 return CheckResponseResult::OK;
126}
127
128Block
129ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
130{
Yanbiao Li150b80d2016-03-18 15:30:10 +0800131 auto isFinalSegment = [] (const Data& data) -> bool {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800132 const name::Component& lastComponent = data.getName().at(-1);
133 return !lastComponent.isSegment() || lastComponent == data.getFinalBlockId();
Yanbiao Li150b80d2016-03-18 15:30:10 +0800134 };
135
Yanbiao Lidf846e52016-01-30 21:53:47 -0800136 while (!isFinalSegment(m_responses.back())) {
137 const Name& name = m_responses.back().getName();
138 Name prefix = name.getPrefix(-1);
139 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800140 // request for the next segment
141 receiveInterest(makeInterest(prefix.appendSegment(segmentNo)));
142 }
143
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700144 size_t endIndex = startIndex + nResponses; // not included
145 if (nResponses == startIndex || endIndex > m_responses.size()) {
146 endIndex = m_responses.size();
147 }
148
149 ndn::EncodingBuffer encoder;
150 size_t valueLength = 0;
151 for (size_t i = startIndex; i < endIndex ; i ++) {
152 valueLength += encoder.appendByteArray(m_responses[i].getContent().value(),
153 m_responses[i].getContent().value_size());
154 }
155 encoder.prependVarNumber(valueLength);
156 encoder.prependVarNumber(tlv::Content);
157 return encoder.block();
158}
159
160std::ostream&
161operator<<(std::ostream &os, const ManagerCommonFixture::CheckResponseResult& result)
162{
163 switch (result) {
164 case ManagerCommonFixture::CheckResponseResult::OK:
165 os << "OK";
166 break;
167 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
168 os << "OUT_OF_BOUNDARY";
169 break;
170 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
171 os << "WRONG_NAME";
172 break;
173 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
174 os << "WRONG_CONTENT_TYPE";
175 break;
176 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
177 os << "INVALID_RESPONSE";
178 break;
179 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
180 os << "WRONG_CODE";
181 break;
182 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
183 os << "WRONG_TEXT";
184 break;
185 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
186 os << "WRONG_BODY_SIZE";
187 break;
188 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
189 os << "WRONG_BODY_VALUE";
190 break;
191 default:
192 break;
193 };
194
195 return os;
196}
197
198} // namespace tests
199} // namespace nfd