blob: 8be600c884ba1b84eb845c9c8d9e35aea1b1cc84 [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
Junxiao Shi8a1f1702017-07-03 00:05:08 +000030CommandInterestSignerFixture::CommandInterestSignerFixture()
Davide Pesavento6a699be2021-05-17 02:13:37 -040031 : m_signer(m_keyChain)
Junxiao Shi8a1f1702017-07-03 00:05:08 +000032{
Davide Pesavento21353752020-11-20 00:43:44 -050033 BOOST_REQUIRE(m_keyChain.createIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY));
Junxiao Shi8a1f1702017-07-03 00:05:08 +000034}
35
36Interest
37CommandInterestSignerFixture::makeCommandInterest(const Name& name, const Name& identity)
38{
Davide Pesavento6a699be2021-05-17 02:13:37 -040039 return m_signer.makeCommandInterest(name, ndn::security::signingByIdentity(identity));
Junxiao Shi8a1f1702017-07-03 00:05:08 +000040}
41
42Interest
43CommandInterestSignerFixture::makeControlCommandRequest(Name commandName,
44 const ControlParameters& params,
45 const Name& identity)
46{
Davide Pesaventodeb54272022-03-11 18:51:05 -050047 commandName.append(tlv::GenericNameComponent, params.wireEncode());
Junxiao Shi8a1f1702017-07-03 00:05:08 +000048 return this->makeCommandInterest(commandName, identity);
49}
50
Yanbiao Li698f4fe2015-08-19 16:30:16 -070051ManagerCommonFixture::ManagerCommonFixture()
Davide Pesavento3dade002019-03-19 11:29:56 -060052 : m_face(g_io, m_keyChain, {true, true})
Junxiao Shi221b6fe2016-07-14 18:21:56 +000053 , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo())
54 , m_responses(m_face.sentData)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070055{
56}
57
58void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050059ManagerCommonFixture::setTopPrefix()
Yanbiao Li698f4fe2015-08-19 16:30:16 -070060{
Davide Pesavento78ddcab2019-02-28 22:00:03 -050061 m_dispatcher.addTopPrefix("/localhost/nfd");
62 advanceClocks(1_ms); // so that all filters are added
Yanbiao Li698f4fe2015-08-19 16:30:16 -070063}
64
Yanbiao Li698f4fe2015-08-19 16:30:16 -070065void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000066ManagerCommonFixture::receiveInterest(const Interest& interest)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070067{
Junxiao Shi8a1f1702017-07-03 00:05:08 +000068 m_face.receive(interest);
Davide Pesavento17057442018-04-20 15:21:31 -040069 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070070}
71
72ControlResponse
73ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text,
74 const ControlParameters& parameters)
75{
76 return ControlResponse(code, text).setBody(parameters.wireEncode());
77}
78
79ManagerCommonFixture::CheckResponseResult
80ManagerCommonFixture::checkResponse(size_t idx,
81 const Name& expectedName,
82 const ControlResponse& expectedResponse,
83 int expectedContentType /*= -1*/)
84{
85 Data data;
86 try {
87 data = m_responses.at(idx);
88 }
89 catch (const std::out_of_range&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000090 BOOST_TEST_MESSAGE("response[" << idx << "] does not exist");
Yanbiao Li698f4fe2015-08-19 16:30:16 -070091 return CheckResponseResult::OUT_OF_BOUNDARY;
92 }
93
94 if (data.getName() != expectedName) {
Junxiao Shi7d30d852017-01-22 03:29:26 +000095 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName());
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096 return CheckResponseResult::WRONG_NAME;
97 }
98
99 if (expectedContentType != -1 &&
100 data.getContentType() != static_cast<uint32_t>(expectedContentType)) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000101 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700102 return CheckResponseResult::WRONG_CONTENT_TYPE;
103 }
104
105 ControlResponse response;
106 try {
107 response.wireDecode(data.getContent().blockFromValue());
108 }
109 catch (const tlv::Error&) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000110 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700111 return CheckResponseResult::INVALID_RESPONSE;
112 }
113
114 if (response.getCode() != expectedResponse.getCode()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000115 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700116 return CheckResponseResult::WRONG_CODE;
117 }
118
119 if (response.getText() != expectedResponse.getText()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000120 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700121 return CheckResponseResult::WRONG_TEXT;
122 }
123
124 const Block& body = response.getBody();
125 const Block& expectedBody = expectedResponse.getBody();
126 if (body.value_size() != expectedBody.value_size()) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000127 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700128 return CheckResponseResult::WRONG_BODY_SIZE;
129 }
130 if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000131 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700132 return CheckResponseResult::WRONG_BODY_VALUE;
133 }
134
135 return CheckResponseResult::OK;
136}
137
138Block
139ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
140{
Davide Pesavento17057442018-04-20 15:21:31 -0400141 while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800142 const Name& name = m_responses.back().getName();
143 Name prefix = name.getPrefix(-1);
144 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800145 // request for the next segment
Junxiao Shi9d727852019-05-14 13:44:22 -0600146 receiveInterest(*makeInterest(prefix.appendSegment(segmentNo)));
Yanbiao Li150b80d2016-03-18 15:30:10 +0800147 }
148
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700149 size_t endIndex = startIndex + nResponses; // not included
150 if (nResponses == startIndex || endIndex > m_responses.size()) {
151 endIndex = m_responses.size();
152 }
153
154 ndn::EncodingBuffer encoder;
Davide Pesaventodeb54272022-03-11 18:51:05 -0500155 size_t length = 0;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700156 for (size_t i = startIndex; i < endIndex ; i ++) {
Davide Pesaventodeb54272022-03-11 18:51:05 -0500157 length += encoder.appendBytes(m_responses[i].getContent().value_bytes());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700158 }
Davide Pesaventodeb54272022-03-11 18:51:05 -0500159 encoder.prependVarNumber(length);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700160 encoder.prependVarNumber(tlv::Content);
161 return encoder.block();
162}
163
164std::ostream&
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500165operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result)
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700166{
167 switch (result) {
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000168 case ManagerCommonFixture::CheckResponseResult::OK:
169 return os << "OK";
170 case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY:
171 return os << "OUT_OF_BOUNDARY";
172 case ManagerCommonFixture::CheckResponseResult::WRONG_NAME:
173 return os << "WRONG_NAME";
174 case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE:
175 return os << "WRONG_CONTENT_TYPE";
176 case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE:
177 return os << "INVALID_RESPONSE";
178 case ManagerCommonFixture::CheckResponseResult::WRONG_CODE:
179 return os << "WRONG_CODE";
180 case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT:
181 return os << "WRONG_TEXT";
182 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE:
183 return os << "WRONG_BODY_SIZE";
184 case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE:
185 return os << "WRONG_BODY_VALUE";
186 }
187 return os << static_cast<int>(result);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700188}
189
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500190void
191ManagerFixtureWithAuthenticator::setPrivilege(const std::string& privilege)
192{
Davide Pesavento21353752020-11-20 00:43:44 -0500193 saveIdentityCert(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500194
195 const std::string& config = R"CONFIG(
196 authorizations
197 {
198 authorize
199 {
200 certfile "ManagerCommonFixture.ndncert"
201 privileges
202 {
203 )CONFIG" + privilege + R"CONFIG(
204 }
205 }
206 }
207 )CONFIG";
208
209 ConfigFile cf;
210 m_authenticator->setConfigFile(cf);
211 cf.parse(config, false, "ManagerCommonFixture.authenticator.conf");
212}
213
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400214} // namespace nfd::tests