blob: b427a3ea099fd6de1b8fe211a246fa389deb50a8 [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 Pesaventodeb54272022-03-11 18:51:05 -05003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040028namespace nfd::tests {
Yanbiao Li698f4fe2015-08-19 16:30:16 -070029
Davide Pesavento20cafa82022-07-25 01:15:03 -040030InterestSignerFixture::InterestSignerFixture()
Junxiao Shi8a1f1702017-07-03 00:05:08 +000031{
Davide Pesavento21353752020-11-20 00:43:44 -050032 BOOST_REQUIRE(m_keyChain.createIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY));
Junxiao Shi8a1f1702017-07-03 00:05:08 +000033}
34
35Interest
Davide Pesavento20cafa82022-07-25 01:15:03 -040036InterestSignerFixture::makeCommandInterest(const Name& name, const Name& identity)
Junxiao Shi8a1f1702017-07-03 00:05:08 +000037{
Davide Pesavento6a699be2021-05-17 02:13:37 -040038 return m_signer.makeCommandInterest(name, ndn::security::signingByIdentity(identity));
Junxiao Shi8a1f1702017-07-03 00:05:08 +000039}
40
41Interest
Davide Pesavento20cafa82022-07-25 01:15:03 -040042InterestSignerFixture::makeControlCommandRequest(Name commandName,
43 const ControlParameters& params,
44 const Name& identity)
Junxiao Shi8a1f1702017-07-03 00:05:08 +000045{
Davide Pesaventodeb54272022-03-11 18:51:05 -050046 commandName.append(tlv::GenericNameComponent, params.wireEncode());
Junxiao Shi8a1f1702017-07-03 00:05:08 +000047 return this->makeCommandInterest(commandName, identity);
48}
49
Yanbiao Li698f4fe2015-08-19 16:30:16 -070050void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050051ManagerCommonFixture::setTopPrefix()
Yanbiao Li698f4fe2015-08-19 16:30:16 -070052{
Davide Pesavento78ddcab2019-02-28 22:00:03 -050053 m_dispatcher.addTopPrefix("/localhost/nfd");
54 advanceClocks(1_ms); // so that all filters are added
Yanbiao Li698f4fe2015-08-19 16:30:16 -070055}
56
Yanbiao Li698f4fe2015-08-19 16:30:16 -070057void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000058ManagerCommonFixture::receiveInterest(const Interest& interest)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070059{
Junxiao Shi8a1f1702017-07-03 00:05:08 +000060 m_face.receive(interest);
Davide Pesavento17057442018-04-20 15:21:31 -040061 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070062}
63
64ControlResponse
65ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text,
66 const ControlParameters& parameters)
67{
68 return ControlResponse(code, text).setBody(parameters.wireEncode());
69}
70
71ManagerCommonFixture::CheckResponseResult
72ManagerCommonFixture::checkResponse(size_t idx,
73 const Name& expectedName,
74 const ControlResponse& expectedResponse,
75 int expectedContentType /*= -1*/)
76{
77 Data data;
78 try {
79 data = m_responses.at(idx);
80 }
81 catch (const std::out_of_range&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000082 BOOST_TEST_MESSAGE("response[" << idx << "] does not exist");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070083 return CheckResponseResult::OUT_OF_BOUNDARY;
84 }
85
86 if (data.getName() != expectedName) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000087 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName());
Yanbiao Li698f4fe2015-08-19 16:30:16 -070088 return CheckResponseResult::WRONG_NAME;
89 }
90
91 if (expectedContentType != -1 &&
92 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000093 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType());
Yanbiao Li698f4fe2015-08-19 16:30:16 -070094 return CheckResponseResult::WRONG_CONTENT_TYPE;
95 }
96
97 ControlResponse response;
98 try {
99 response.wireDecode(data.getContent().blockFromValue());
100 }
101 catch (const tlv::Error&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000102 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700103 return CheckResponseResult::INVALID_RESPONSE;
104 }
105
106 if (response.getCode() != expectedResponse.getCode()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000107 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700108 return CheckResponseResult::WRONG_CODE;
109 }
110
111 if (response.getText() != expectedResponse.getText()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000112 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700113 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()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000119 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700120 return CheckResponseResult::WRONG_BODY_SIZE;
121 }
122 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000123 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700124 return CheckResponseResult::WRONG_BODY_VALUE;
125 }
126
127 return CheckResponseResult::OK;
128}
129
130Block
131ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
132{
Davide Pesavento17057442018-04-20 15:21:31 -0400133 while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800134 const Name& name = m_responses.back().getName();
135 Name prefix = name.getPrefix(-1);
136 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800137 // request for the next segment
Junxiao Shi9d727852019-05-14 13:44:22 -0600138 receiveInterest(*makeInterest(prefix.appendSegment(segmentNo)));
Yanbiao Li150b80d2016-03-18 15:30:10 +0800139 }
140
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700141 size_t endIndex = startIndex + nResponses; // not included
142 if (nResponses == startIndex || endIndex > m_responses.size()) {
143 endIndex = m_responses.size();
144 }
145
146 ndn::EncodingBuffer encoder;
Davide Pesaventodeb54272022-03-11 18:51:05 -0500147 size_t length = 0;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700148 for (size_t i = startIndex; i < endIndex ; i ++) {
Davide Pesaventodeb54272022-03-11 18:51:05 -0500149 length += encoder.appendBytes(m_responses[i].getContent().value_bytes());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700150 }
Davide Pesaventodeb54272022-03-11 18:51:05 -0500151 encoder.prependVarNumber(length);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700152 encoder.prependVarNumber(tlv::Content);
153 return encoder.block();
154}
155
156std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500157operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700158{
159 switch (result) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000160 case ManagerCommonFixture::CheckResponseResult::OK:
161 return os << "OK";
162 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
163 return os << "OUT_OF_BOUNDARY";
164 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
165 return os << "WRONG_NAME";
166 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
167 return os << "WRONG_CONTENT_TYPE";
168 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
169 return os << "INVALID_RESPONSE";
170 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
171 return os << "WRONG_CODE";
172 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
173 return os << "WRONG_TEXT";
174 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
175 return os << "WRONG_BODY_SIZE";
176 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
177 return os << "WRONG_BODY_VALUE";
178 }
179 return os << static_cast<int>(result);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700180}
181
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500182void
183ManagerFixtureWithAuthenticator::setPrivilege(const std::string& privilege)
184{
Davide Pesavento21353752020-11-20 00:43:44 -0500185 saveIdentityCert(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500186
Davide Pesavento20cafa82022-07-25 01:15:03 -0400187 const std::string config = R"CONFIG(
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500188 authorizations
189 {
190 authorize
191 {
192 certfile "ManagerCommonFixture.ndncert"
193 privileges
194 {
195 )CONFIG" + privilege + R"CONFIG(
196 }
197 }
198 }
199 )CONFIG";
200
201 ConfigFile cf;
202 m_authenticator->setConfigFile(cf);
203 cf.parse(config, false, "ManagerCommonFixture.authenticator.conf");
204}
205
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400206} // namespace nfd::tests