blob: 67ac46b5c5d2a486f9ae6b938837cf5e0b1603e8 [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 Pesavento21353752020-11-20 00:43:44 -05003 * Copyright (c) 2014-2020, 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
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028namespace nfd {
29namespace tests {
30
Junxiao Shi8a1f1702017-07-03 00:05:08 +000031const Name CommandInterestSignerFixture::DEFAULT_COMMAND_SIGNER_IDENTITY("/CommandInterestSignerFixture-identity");
32
33CommandInterestSignerFixture::CommandInterestSignerFixture()
34 : m_commandInterestSigner(m_keyChain)
35{
Davide Pesavento21353752020-11-20 00:43:44 -050036 BOOST_REQUIRE(m_keyChain.createIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY));
Junxiao Shi8a1f1702017-07-03 00:05:08 +000037}
38
39Interest
40CommandInterestSignerFixture::makeCommandInterest(const Name& name, const Name& identity)
41{
42 return m_commandInterestSigner.makeCommandInterest(name, ndn::security::signingByIdentity(identity));
43}
44
45Interest
46CommandInterestSignerFixture::makeControlCommandRequest(Name commandName,
47 const ControlParameters& params,
48 const Name& identity)
49{
50 commandName.append(params.wireEncode());
51 return this->makeCommandInterest(commandName, identity);
52}
53
Yanbiao Li698f4fe2015-08-19 16:30:16 -070054ManagerCommonFixture::ManagerCommonFixture()
Davide Pesavento3dade002019-03-19 11:29:56 -060055 : m_face(g_io, m_keyChain, {true, true})
Junxiao Shi221b6fe2016-07-14 18:21:56 +000056 , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo())
57 , m_responses(m_face.sentData)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070058{
59}
60
61void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050062ManagerCommonFixture::setTopPrefix()
Yanbiao Li698f4fe2015-08-19 16:30:16 -070063{
Davide Pesavento78ddcab2019-02-28 22:00:03 -050064 m_dispatcher.addTopPrefix("/localhost/nfd");
65 advanceClocks(1_ms); // so that all filters are added
Yanbiao Li698f4fe2015-08-19 16:30:16 -070066}
67
Yanbiao Li698f4fe2015-08-19 16:30:16 -070068void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000069ManagerCommonFixture::receiveInterest(const Interest& interest)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070070{
Junxiao Shi8a1f1702017-07-03 00:05:08 +000071 m_face.receive(interest);
Davide Pesavento17057442018-04-20 15:21:31 -040072 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070073}
74
75ControlResponse
76ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text,
77 const ControlParameters& parameters)
78{
79 return ControlResponse(code, text).setBody(parameters.wireEncode());
80}
81
82ManagerCommonFixture::CheckResponseResult
83ManagerCommonFixture::checkResponse(size_t idx,
84 const Name& expectedName,
85 const ControlResponse& expectedResponse,
86 int expectedContentType /*= -1*/)
87{
88 Data data;
89 try {
90 data = m_responses.at(idx);
91 }
92 catch (const std::out_of_range&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000093 BOOST_TEST_MESSAGE("response[" << idx << "] does not exist");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070094 return CheckResponseResult::OUT_OF_BOUNDARY;
95 }
96
97 if (data.getName() != expectedName) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000098 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName());
Yanbiao Li698f4fe2015-08-19 16:30:16 -070099 return CheckResponseResult::WRONG_NAME;
100 }
101
102 if (expectedContentType != -1 &&
103 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000104 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700105 return CheckResponseResult::WRONG_CONTENT_TYPE;
106 }
107
108 ControlResponse response;
109 try {
110 response.wireDecode(data.getContent().blockFromValue());
111 }
112 catch (const tlv::Error&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000113 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700114 return CheckResponseResult::INVALID_RESPONSE;
115 }
116
117 if (response.getCode() != expectedResponse.getCode()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000118 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700119 return CheckResponseResult::WRONG_CODE;
120 }
121
122 if (response.getText() != expectedResponse.getText()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000123 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700124 return CheckResponseResult::WRONG_TEXT;
125 }
126
127 const Block& body = response.getBody();
128 const Block& expectedBody = expectedResponse.getBody();
129 if (body.value_size() != expectedBody.value_size()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000130 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700131 return CheckResponseResult::WRONG_BODY_SIZE;
132 }
133 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000134 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700135 return CheckResponseResult::WRONG_BODY_VALUE;
136 }
137
138 return CheckResponseResult::OK;
139}
140
141Block
142ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
143{
Davide Pesavento17057442018-04-20 15:21:31 -0400144 while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800145 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
Junxiao Shi9d727852019-05-14 13:44:22 -0600149 receiveInterest(*makeInterest(prefix.appendSegment(segmentNo)));
Yanbiao Li150b80d2016-03-18 15:30:10 +0800150 }
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&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500169operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700170{
171 switch (result) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000172 case ManagerCommonFixture::CheckResponseResult::OK:
173 return os << "OK";
174 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
175 return os << "OUT_OF_BOUNDARY";
176 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
177 return os << "WRONG_NAME";
178 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
179 return os << "WRONG_CONTENT_TYPE";
180 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
181 return os << "INVALID_RESPONSE";
182 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
183 return os << "WRONG_CODE";
184 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
185 return os << "WRONG_TEXT";
186 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
187 return os << "WRONG_BODY_SIZE";
188 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
189 return os << "WRONG_BODY_VALUE";
190 }
191 return os << static_cast<int>(result);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700192}
193
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500194void
195ManagerFixtureWithAuthenticator::setPrivilege(const std::string& privilege)
196{
Davide Pesavento21353752020-11-20 00:43:44 -0500197 saveIdentityCert(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500198
199 const std::string& config = R"CONFIG(
200 authorizations
201 {
202 authorize
203 {
204 certfile "ManagerCommonFixture.ndncert"
205 privileges
206 {
207 )CONFIG" + privilege + R"CONFIG(
208 }
209 }
210 }
211 )CONFIG";
212
213 ConfigFile cf;
214 m_authenticator->setConfigFile(cf);
215 cf.parse(config, false, "ManagerCommonFixture.authenticator.conf");
216}
217
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700218} // namespace tests
219} // namespace nfd