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