blob: b03449ecb992ec1d867267e8591b381049d55fe9 [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#include "manager-common-fixture.hpp"
27
28namespace nfd {
29namespace tests {
30
31ManagerCommonFixture::ManagerCommonFixture()
32 : m_face(ndn::util::makeDummyClientFace(UnitTestTimeFixture::g_io, {true, true}))
33 , m_dispatcher(*m_face, m_keyChain, ndn::security::SigningInfo())
34 , m_responses(m_face->sentDatas)
35 , m_identityName("/unit-test/ManagerCommonFixture/identity")
36 , m_certificate(m_keyChain.getCertificate(m_keyChain.createIdentity(m_identityName)))
37{
38}
39
40void
41ManagerCommonFixture::setTopPrefixAndPrivilege(const Name& topPrefix,
42 const std::string& privilege)
43{
44 m_dispatcher.addTopPrefix(topPrefix); // such that all filters are added
45 advanceClocks(time::milliseconds(1));
46
47 std::string regex("^");
48 for (auto component : topPrefix) {
49 regex += "<" + component.toUri() + ">";
50 }
51 m_validator.addInterestRule(regex + "<" + privilege + ">", *m_certificate);
52}
53
54shared_ptr<Interest>
55ManagerCommonFixture::makeControlCommandRequest(Name commandName,
56 const ControlParameters& parameters,
57 const InterestHandler& beforeSigning)
58{
59 shared_ptr<Interest> command = makeInterest(commandName.append(parameters.wireEncode()));
60
61 if (beforeSigning != nullptr) {
62 beforeSigning(command);
63 }
64
65 m_keyChain.sign(*command, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
66 m_identityName));
67 return command;
68}
69
70void
71ManagerCommonFixture::receiveInterest(shared_ptr<Interest> interest)
72{
73 m_face->receive<Interest>(*interest);
74 advanceClocks(time::milliseconds(1));
75}
76
77ControlResponse
78ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text,
79 const ControlParameters& parameters)
80{
81 return ControlResponse(code, text).setBody(parameters.wireEncode());
82}
83
84ManagerCommonFixture::CheckResponseResult
85ManagerCommonFixture::checkResponse(size_t idx,
86 const Name& expectedName,
87 const ControlResponse& expectedResponse,
88 int expectedContentType /*= -1*/)
89{
90 Data data;
91 try {
92 data = m_responses.at(idx);
93 }
94 catch (const std::out_of_range&) {
95 return CheckResponseResult::OUT_OF_BOUNDARY;
96 }
97
98 if (data.getName() != expectedName) {
99 return CheckResponseResult::WRONG_NAME;
100 }
101
102 if (expectedContentType != -1 &&
103 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
104 return CheckResponseResult::WRONG_CONTENT_TYPE;
105 }
106
107 ControlResponse response;
108 try {
109 response.wireDecode(data.getContent().blockFromValue());
110 }
111 catch (const tlv::Error&) {
112 return CheckResponseResult::INVALID_RESPONSE;
113 }
114
115 if (response.getCode() != expectedResponse.getCode()) {
116 return CheckResponseResult::WRONG_CODE;
117 }
118
119 if (response.getText() != expectedResponse.getText()) {
120 return CheckResponseResult::WRONG_TEXT;
121 }
122
123 const Block& body = response.getBody();
124 const Block& expectedBody = expectedResponse.getBody();
125 if (body.value_size() != expectedBody.value_size()) {
126 return CheckResponseResult::WRONG_BODY_SIZE;
127 }
128 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
129 return CheckResponseResult::WRONG_BODY_VALUE;
130 }
131
132 return CheckResponseResult::OK;
133}
134
135Block
136ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
137{
138 size_t endIndex = startIndex + nResponses; // not included
139 if (nResponses == startIndex || endIndex > m_responses.size()) {
140 endIndex = m_responses.size();
141 }
142
143 ndn::EncodingBuffer encoder;
144 size_t valueLength = 0;
145 for (size_t i = startIndex; i < endIndex ; i ++) {
146 valueLength += encoder.appendByteArray(m_responses[i].getContent().value(),
147 m_responses[i].getContent().value_size());
148 }
149 encoder.prependVarNumber(valueLength);
150 encoder.prependVarNumber(tlv::Content);
151 return encoder.block();
152}
153
154std::ostream&
155operator<<(std::ostream &os, const ManagerCommonFixture::CheckResponseResult& result)
156{
157 switch (result) {
158 case ManagerCommonFixture::CheckResponseResult::OK:
159 os << "OK";
160 break;
161 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
162 os << "OUT_OF_BOUNDARY";
163 break;
164 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
165 os << "WRONG_NAME";
166 break;
167 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
168 os << "WRONG_CONTENT_TYPE";
169 break;
170 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
171 os << "INVALID_RESPONSE";
172 break;
173 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
174 os << "WRONG_CODE";
175 break;
176 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
177 os << "WRONG_TEXT";
178 break;
179 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
180 os << "WRONG_BODY_SIZE";
181 break;
182 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
183 os << "WRONG_BODY_VALUE";
184 break;
185 default:
186 break;
187 };
188
189 return os;
190}
191
192} // namespace tests
193} // namespace nfd