Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * 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 Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 27 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 28 | namespace nfd::tests { |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 30 | InterestSignerFixture::InterestSignerFixture() |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 31 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 32 | BOOST_REQUIRE(m_keyChain.createIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)); |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Interest |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 36 | InterestSignerFixture::makeControlCommandRequest(Name commandName, |
| 37 | const ControlParameters& params, |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 38 | ndn::security::SignedInterestFormat format, |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 39 | const Name& identity) |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 40 | { |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 41 | commandName.append(tlv::GenericNameComponent, params.wireEncode()); |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 42 | |
| 43 | switch (format) { |
| 44 | case ndn::security::SignedInterestFormat::V02: |
| 45 | return m_signer.makeCommandInterest(commandName, ndn::security::signingByIdentity(identity)); |
| 46 | case ndn::security::SignedInterestFormat::V03: { |
| 47 | Interest interest(commandName); |
| 48 | m_signer.makeSignedInterest(interest, ndn::security::signingByIdentity(identity)); |
| 49 | return interest; |
| 50 | } |
| 51 | } |
| 52 | NDN_CXX_UNREACHABLE; |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 55 | void |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 56 | ManagerCommonFixture::setTopPrefix() |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 57 | { |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 58 | m_dispatcher.addTopPrefix("/localhost/nfd"); |
| 59 | advanceClocks(1_ms); // so that all filters are added |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 62 | void |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 63 | ManagerCommonFixture::receiveInterest(const Interest& interest) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 65 | m_face.receive(interest); |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 66 | advanceClocks(1_ms); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ControlResponse |
| 70 | ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text, |
| 71 | const ControlParameters& parameters) |
| 72 | { |
| 73 | return ControlResponse(code, text).setBody(parameters.wireEncode()); |
| 74 | } |
| 75 | |
| 76 | ManagerCommonFixture::CheckResponseResult |
| 77 | ManagerCommonFixture::checkResponse(size_t idx, |
| 78 | const Name& expectedName, |
| 79 | const ControlResponse& expectedResponse, |
| 80 | int expectedContentType /*= -1*/) |
| 81 | { |
| 82 | Data data; |
| 83 | try { |
| 84 | data = m_responses.at(idx); |
| 85 | } |
| 86 | catch (const std::out_of_range&) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 87 | BOOST_TEST_MESSAGE("response[" << idx << "] does not exist"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 88 | return CheckResponseResult::OUT_OF_BOUNDARY; |
| 89 | } |
| 90 | |
| 91 | if (data.getName() != expectedName) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 92 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 93 | return CheckResponseResult::WRONG_NAME; |
| 94 | } |
| 95 | |
| 96 | if (expectedContentType != -1 && |
| 97 | data.getContentType() != static_cast<uint32_t>(expectedContentType)) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 98 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | return CheckResponseResult::WRONG_CONTENT_TYPE; |
| 100 | } |
| 101 | |
| 102 | ControlResponse response; |
| 103 | try { |
| 104 | response.wireDecode(data.getContent().blockFromValue()); |
| 105 | } |
| 106 | catch (const tlv::Error&) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 107 | BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 108 | return CheckResponseResult::INVALID_RESPONSE; |
| 109 | } |
| 110 | |
| 111 | if (response.getCode() != expectedResponse.getCode()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 112 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 113 | return CheckResponseResult::WRONG_CODE; |
| 114 | } |
| 115 | |
| 116 | if (response.getText() != expectedResponse.getText()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 117 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | return CheckResponseResult::WRONG_TEXT; |
| 119 | } |
| 120 | |
| 121 | const Block& body = response.getBody(); |
| 122 | const Block& expectedBody = expectedResponse.getBody(); |
| 123 | if (body.value_size() != expectedBody.value_size()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 124 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 125 | return CheckResponseResult::WRONG_BODY_SIZE; |
| 126 | } |
| 127 | if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 128 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | return CheckResponseResult::WRONG_BODY_VALUE; |
| 130 | } |
| 131 | |
| 132 | return CheckResponseResult::OK; |
| 133 | } |
| 134 | |
| 135 | Block |
| 136 | ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses) |
| 137 | { |
Davide Pesavento | 1705744 | 2018-04-20 15:21:31 -0400 | [diff] [blame] | 138 | while (m_responses.back().getName().at(-1) != m_responses.back().getFinalBlock()) { |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 139 | const Name& name = m_responses.back().getName(); |
| 140 | Name prefix = name.getPrefix(-1); |
| 141 | uint64_t segmentNo = name.at(-1).toSegment() + 1; |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 142 | // request for the next segment |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 143 | receiveInterest(*makeInterest(prefix.appendSegment(segmentNo))); |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 146 | size_t endIndex = startIndex + nResponses; // not included |
| 147 | if (nResponses == startIndex || endIndex > m_responses.size()) { |
| 148 | endIndex = m_responses.size(); |
| 149 | } |
| 150 | |
| 151 | ndn::EncodingBuffer encoder; |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 152 | size_t length = 0; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 153 | for (size_t i = startIndex; i < endIndex ; i ++) { |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 154 | length += encoder.appendBytes(m_responses[i].getContent().value_bytes()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 155 | } |
Davide Pesavento | deb5427 | 2022-03-11 18:51:05 -0500 | [diff] [blame] | 156 | encoder.prependVarNumber(length); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 157 | encoder.prependVarNumber(tlv::Content); |
| 158 | return encoder.block(); |
| 159 | } |
| 160 | |
| 161 | std::ostream& |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 162 | operator<<(std::ostream& os, ManagerCommonFixture::CheckResponseResult result) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 163 | { |
| 164 | switch (result) { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame] | 165 | case ManagerCommonFixture::CheckResponseResult::OK: |
| 166 | return os << "OK"; |
| 167 | case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY: |
| 168 | return os << "OUT_OF_BOUNDARY"; |
| 169 | case ManagerCommonFixture::CheckResponseResult::WRONG_NAME: |
| 170 | return os << "WRONG_NAME"; |
| 171 | case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE: |
| 172 | return os << "WRONG_CONTENT_TYPE"; |
| 173 | case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE: |
| 174 | return os << "INVALID_RESPONSE"; |
| 175 | case ManagerCommonFixture::CheckResponseResult::WRONG_CODE: |
| 176 | return os << "WRONG_CODE"; |
| 177 | case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT: |
| 178 | return os << "WRONG_TEXT"; |
| 179 | case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE: |
| 180 | return os << "WRONG_BODY_SIZE"; |
| 181 | case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE: |
| 182 | return os << "WRONG_BODY_VALUE"; |
| 183 | } |
| 184 | return os << static_cast<int>(result); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 187 | void |
| 188 | ManagerFixtureWithAuthenticator::setPrivilege(const std::string& privilege) |
| 189 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 190 | saveIdentityCert(DEFAULT_COMMAND_SIGNER_IDENTITY, "ManagerCommonFixture.ndncert"); |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 191 | |
Davide Pesavento | 20cafa8 | 2022-07-25 01:15:03 -0400 | [diff] [blame] | 192 | const std::string config = R"CONFIG( |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 193 | authorizations |
| 194 | { |
| 195 | authorize |
| 196 | { |
| 197 | certfile "ManagerCommonFixture.ndncert" |
| 198 | privileges |
| 199 | { |
| 200 | )CONFIG" + privilege + R"CONFIG( |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | )CONFIG"; |
| 205 | |
| 206 | ConfigFile cf; |
| 207 | m_authenticator->setConfigFile(cf); |
| 208 | cf.parse(config, false, "ManagerCommonFixture.authenticator.conf"); |
| 209 | } |
| 210 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 211 | } // namespace nfd::tests |