blob: ed4344ee2c748a787dfc75af4e716c4a02a7cde7 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento17057442018-04-20 15:21:31 -04002/*
Davide Pesavento78ddcab2019-02-28 22:00:03 -05003 * Copyright (c) 2014-2019, 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"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050027
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000028#include <ndn-cxx/security/signing-helpers.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070029
30namespace nfd {
31namespace tests {
32
Junxiao Shi8a1f1702017-07-03 00:05:08 +000033const Name CommandInterestSignerFixture::DEFAULT_COMMAND_SIGNER_IDENTITY("/CommandInterestSignerFixture-identity");
34
35CommandInterestSignerFixture::CommandInterestSignerFixture()
36 : m_commandInterestSigner(m_keyChain)
37{
38 BOOST_REQUIRE(this->addIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY));
39}
40
41Interest
42CommandInterestSignerFixture::makeCommandInterest(const Name& name, const Name& identity)
43{
44 return m_commandInterestSigner.makeCommandInterest(name, ndn::security::signingByIdentity(identity));
45}
46
47Interest
48CommandInterestSignerFixture::makeControlCommandRequest(Name commandName,
49 const ControlParameters& params,
50 const Name& identity)
51{
52 commandName.append(params.wireEncode());
53 return this->makeCommandInterest(commandName, identity);
54}
55
Yanbiao Li698f4fe2015-08-19 16:30:16 -070056ManagerCommonFixture::ManagerCommonFixture()
Davide Pesavento3dade002019-03-19 11:29:56 -060057 : m_face(g_io, m_keyChain, {true, true})
Junxiao Shi221b6fe2016-07-14 18:21:56 +000058 , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo())
59 , m_responses(m_face.sentData)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070060{
61}
62
63void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050064ManagerCommonFixture::setTopPrefix()
Yanbiao Li698f4fe2015-08-19 16:30:16 -070065{
Davide Pesavento78ddcab2019-02-28 22:00:03 -050066 m_dispatcher.addTopPrefix("/localhost/nfd");
67 advanceClocks(1_ms); // so that all filters are added
Yanbiao Li698f4fe2015-08-19 16:30:16 -070068}
69
Yanbiao Li698f4fe2015-08-19 16:30:16 -070070void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000071ManagerCommonFixture::receiveInterest(const Interest& interest)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070072{
Junxiao Shi8a1f1702017-07-03 00:05:08 +000073 m_face.receive(interest);
Davide Pesavento17057442018-04-20 15:21:31 -040074 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075}
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&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000095 BOOST_TEST_MESSAGE("response[" << idx << "] does not exist");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096 return CheckResponseResult::OUT_OF_BOUNDARY;
97 }
98
99 if (data.getName() != expectedName) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000100 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101 return CheckResponseResult::WRONG_NAME;
102 }
103
104 if (expectedContentType != -1 &&
105 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000106 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700107 return CheckResponseResult::WRONG_CONTENT_TYPE;
108 }
109
110 ControlResponse response;
111 try {
112 response.wireDecode(data.getContent().blockFromValue());
113 }
114 catch (const tlv::Error&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000115 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700116 return CheckResponseResult::INVALID_RESPONSE;
117 }
118
119 if (response.getCode() != expectedResponse.getCode()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000120 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700121 return CheckResponseResult::WRONG_CODE;
122 }
123
124 if (response.getText() != expectedResponse.getText()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000125 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700126 return CheckResponseResult::WRONG_TEXT;
127 }
128
129 const Block& body = response.getBody();
130 const Block& expectedBody = expectedResponse.getBody();
131 if (body.value_size() != expectedBody.value_size()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000132 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700133 return CheckResponseResult::WRONG_BODY_SIZE;
134 }
135 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000136 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700137 return CheckResponseResult::WRONG_BODY_VALUE;
138 }
139
140 return CheckResponseResult::OK;
141}
142
143Block
144ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
145{
Davide Pesavento17057442018-04-20 15:21:31 -0400146 while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800147 const Name& name = m_responses.back().getName();
148 Name prefix = name.getPrefix(-1);
149 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800150 // request for the next segment
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000151 receiveInterest(Interest(prefix.appendSegment(segmentNo)));
Yanbiao Li150b80d2016-03-18 15:30:10 +0800152 }
153
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700154 size_t endIndex = startIndex + nResponses; // not included
155 if (nResponses == startIndex || endIndex > m_responses.size()) {
156 endIndex = m_responses.size();
157 }
158
159 ndn::EncodingBuffer encoder;
160 size_t valueLength = 0;
161 for (size_t i = startIndex; i < endIndex ; i ++) {
162 valueLength += encoder.appendByteArray(m_responses[i].getContent().value(),
163 m_responses[i].getContent().value_size());
164 }
165 encoder.prependVarNumber(valueLength);
166 encoder.prependVarNumber(tlv::Content);
167 return encoder.block();
168}
169
170std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500171operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700172{
173 switch (result) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000174 case ManagerCommonFixture::CheckResponseResult::OK:
175 return os << "OK";
176 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
177 return os << "OUT_OF_BOUNDARY";
178 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
179 return os << "WRONG_NAME";
180 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
181 return os << "WRONG_CONTENT_TYPE";
182 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
183 return os << "INVALID_RESPONSE";
184 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
185 return os << "WRONG_CODE";
186 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
187 return os << "WRONG_TEXT";
188 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
189 return os << "WRONG_BODY_SIZE";
190 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
191 return os << "WRONG_BODY_VALUE";
192 }
193 return os << static_cast<int>(result);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700194}
195
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500196void
197ManagerFixtureWithAuthenticator::setPrivilege(const std::string& privilege)
198{
199 saveIdentityCertificate(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert");
200
201 const std::string& config = R"CONFIG(
202 authorizations
203 {
204 authorize
205 {
206 certfile "ManagerCommonFixture.ndncert"
207 privileges
208 {
209 )CONFIG" + privilege + R"CONFIG(
210 }
211 }
212 }
213 )CONFIG";
214
215 ConfigFile cf;
216 m_authenticator->setConfigFile(cf);
217 cf.parse(config, false, "ManagerCommonFixture.authenticator.conf");
218}
219
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700220} // namespace tests
221} // namespace nfd