blob: 4a75983717e0f1dcf348f3d28ca3149b220b7b85 [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 Pesavento1db1bb62025-01-06 01:23:41 -05003 * Copyright (c) 2014-2025, 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 Pesavento4296fe32025-01-14 23:00:41 -050032 auto id = m_keyChain.createIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY);
33 BOOST_VERIFY(id);
Junxiao Shi8a1f1702017-07-03 00:05:08 +000034}
35
36Interest
Davide Pesavento20cafa82022-07-25 01:15:03 -040037InterestSignerFixture::makeControlCommandRequest(Name commandName,
38 const ControlParameters& params,
Davide Pesavento6d6f2072022-09-12 23:08:34 -040039 ndn::security::SignedInterestFormat format,
Davide Pesavento20cafa82022-07-25 01:15:03 -040040 const Name& identity)
Junxiao Shi8a1f1702017-07-03 00:05:08 +000041{
Davide Pesavento1db1bb62025-01-06 01:23:41 -050042 commandName.append(params.wireEncode());
Davide Pesavento6d6f2072022-09-12 23:08:34 -040043
44 switch (format) {
45 case ndn::security::SignedInterestFormat::V02:
46 return m_signer.makeCommandInterest(commandName, ndn::security::signingByIdentity(identity));
47 case ndn::security::SignedInterestFormat::V03: {
48 Interest interest(commandName);
49 m_signer.makeSignedInterest(interest, ndn::security::signingByIdentity(identity));
50 return interest;
51 }
52 }
53 NDN_CXX_UNREACHABLE;
Junxiao Shi8a1f1702017-07-03 00:05:08 +000054}
55
jaczhib0657682025-01-08 23:01:45 -080056Interest
57InterestSignerFixture::makeControlCommandRequest(Name commandName,
58 const ndn::PrefixAnnouncement& prefixAnnouncement,
59 const Name& identity)
60{
61 const Block& paBlock = prefixAnnouncement.getData().value().wireEncode();
62
63 Interest interest(commandName);
64 interest.setApplicationParameters(paBlock);
65 m_signer.makeSignedInterest(interest, ndn::security::signingByIdentity(identity));
66
67 return interest;
68}
69
Yanbiao Li698f4fe2015-08-19 16:30:16 -070070void
Davide Pesavento78ddcab2019-02-28 22:00:03 -050071ManagerCommonFixture::setTopPrefix()
Yanbiao Li698f4fe2015-08-19 16:30:16 -070072{
Davide Pesavento78ddcab2019-02-28 22:00:03 -050073 m_dispatcher.addTopPrefix("/localhost/nfd");
74 advanceClocks(1_ms); // so that all filters are added
Yanbiao Li698f4fe2015-08-19 16:30:16 -070075}
76
Yanbiao Li698f4fe2015-08-19 16:30:16 -070077void
Junxiao Shi8a1f1702017-07-03 00:05:08 +000078ManagerCommonFixture::receiveInterest(const Interest& interest)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070079{
Junxiao Shi8a1f1702017-07-03 00:05:08 +000080 m_face.receive(interest);
Davide Pesavento17057442018-04-20 15:21:31 -040081 advanceClocks(1_ms);
Yanbiao Li698f4fe2015-08-19 16:30:16 -070082}
83
Yanbiao Li698f4fe2015-08-19 16:30:16 -070084ManagerCommonFixture::CheckResponseResult
85ManagerCommonFixture::checkResponse(size_t idx,
86 const Name& expectedName,
87 const ControlResponse& expectedResponse,
Davide Pesavento4296fe32025-01-14 23:00:41 -050088 int expectedContentType)
Yanbiao Li698f4fe2015-08-19 16:30:16 -070089{
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) {
Davide Pesavento4296fe32025-01-14 23:00:41 -0500100 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << std::quoted(data.getName().toUri()));
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 }
Davide Pesavento4296fe32025-01-14 23:00:41 -0500114 catch (const tlv::Error& e) {
115 BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded: " << e.what());
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()) {
Davide Pesavento4296fe32025-01-14 23:00:41 -0500125 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << std::quoted(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 }
Davide Pesavento4296fe32025-01-14 23:00:41 -0500135
136 if (!std::equal(body.value_begin(), body.value_end(), expectedBody.value_begin())) {
Junxiao Shi7d30d852017-01-22 03:29:26 +0000137 BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value");
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700138 return CheckResponseResult::WRONG_BODY_VALUE;
139 }
140
141 return CheckResponseResult::OK;
142}
143
144Block
145ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses)
146{
Davide Pesavento17057442018-04-20 15:21:31 -0400147 while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) {
Yanbiao Lidf846e52016-01-30 21:53:47 -0800148 const Name& name = m_responses.back().getName();
149 Name prefix = name.getPrefix(-1);
150 uint64_t segmentNo = name.at(-1).toSegment() + 1;
Yanbiao Li150b80d2016-03-18 15:30:10 +0800151 // request for the next segment
Junxiao Shi9d727852019-05-14 13:44:22 -0600152 receiveInterest(*makeInterest(prefix.appendSegment(segmentNo)));
Yanbiao Li150b80d2016-03-18 15:30:10 +0800153 }
154
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700155 size_t endIndex = startIndex + nResponses; // not included
156 if (nResponses == startIndex || endIndex > m_responses.size()) {
157 endIndex = m_responses.size();
158 }
159
160 ndn::EncodingBuffer encoder;
Davide Pesaventodeb54272022-03-11 18:51:05 -0500161 size_t length = 0;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700162 for (size_t i = startIndex; i < endIndex ; i ++) {
Davide Pesaventodeb54272022-03-11 18:51:05 -0500163 length += encoder.appendBytes(m_responses[i].getContent().value_bytes());
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700164 }
Davide Pesaventodeb54272022-03-11 18:51:05 -0500165 encoder.prependVarNumber(length);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700166 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) {
Davide Pesavento4296fe32025-01-14 23:00:41 -0500174 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";
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000192 }
Davide Pesavento4296fe32025-01-14 23:00:41 -0500193 return os << ndn::to_underlying(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{
Davide Pesavento21353752020-11-20 00:43:44 -0500199 saveIdentityCert(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert");
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500200
Davide Pesavento20cafa82022-07-25 01:15:03 -0400201 const std::string config = R"CONFIG(
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500202 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400220} // namespace nfd::tests