Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 27 | #include <ndn-cxx/security/signing-helpers.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace tests { |
| 31 | |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 32 | const Name CommandInterestSignerFixture::DEFAULT_COMMAND_SIGNER_IDENTITY("/CommandInterestSignerFixture-identity"); |
| 33 | |
| 34 | CommandInterestSignerFixture::CommandInterestSignerFixture() |
| 35 | : m_commandInterestSigner(m_keyChain) |
| 36 | { |
| 37 | BOOST_REQUIRE(this->addIdentity(DEFAULT_COMMAND_SIGNER_IDENTITY)); |
| 38 | } |
| 39 | |
| 40 | Interest |
| 41 | CommandInterestSignerFixture::makeCommandInterest(const Name& name, const Name& identity) |
| 42 | { |
| 43 | return m_commandInterestSigner.makeCommandInterest(name, ndn::security::signingByIdentity(identity)); |
| 44 | } |
| 45 | |
| 46 | Interest |
| 47 | CommandInterestSignerFixture::makeControlCommandRequest(Name commandName, |
| 48 | const ControlParameters& params, |
| 49 | const Name& identity) |
| 50 | { |
| 51 | commandName.append(params.wireEncode()); |
| 52 | return this->makeCommandInterest(commandName, identity); |
| 53 | } |
| 54 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 55 | ManagerCommonFixture::ManagerCommonFixture() |
Junxiao Shi | 221b6fe | 2016-07-14 18:21:56 +0000 | [diff] [blame] | 56 | : m_face(getGlobalIoService(), m_keyChain, {true, true}) |
| 57 | , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo()) |
| 58 | , m_responses(m_face.sentData) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | void |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 63 | ManagerCommonFixture::setTopPrefix(const Name& topPrefix) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | { |
| 65 | m_dispatcher.addTopPrefix(topPrefix); // such that all filters are added |
| 66 | advanceClocks(time::milliseconds(1)); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 69 | void |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 70 | ManagerCommonFixture::receiveInterest(const Interest& interest) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 71 | { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 72 | m_face.receive(interest); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 73 | advanceClocks(time::milliseconds(1)); |
| 74 | } |
| 75 | |
| 76 | ControlResponse |
| 77 | ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text, |
| 78 | const ControlParameters& parameters) |
| 79 | { |
| 80 | return ControlResponse(code, text).setBody(parameters.wireEncode()); |
| 81 | } |
| 82 | |
| 83 | ManagerCommonFixture::CheckResponseResult |
| 84 | ManagerCommonFixture::checkResponse(size_t idx, |
| 85 | const Name& expectedName, |
| 86 | const ControlResponse& expectedResponse, |
| 87 | int expectedContentType /*= -1*/) |
| 88 | { |
| 89 | Data data; |
| 90 | try { |
| 91 | data = m_responses.at(idx); |
| 92 | } |
| 93 | catch (const std::out_of_range&) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 94 | BOOST_TEST_MESSAGE("response[" << idx << "] does not exist"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 95 | return CheckResponseResult::OUT_OF_BOUNDARY; |
| 96 | } |
| 97 | |
| 98 | if (data.getName() != expectedName) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 99 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 100 | return CheckResponseResult::WRONG_NAME; |
| 101 | } |
| 102 | |
| 103 | if (expectedContentType != -1 && |
| 104 | data.getContentType() != static_cast<uint32_t>(expectedContentType)) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 105 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 106 | return CheckResponseResult::WRONG_CONTENT_TYPE; |
| 107 | } |
| 108 | |
| 109 | ControlResponse response; |
| 110 | try { |
| 111 | response.wireDecode(data.getContent().blockFromValue()); |
| 112 | } |
| 113 | catch (const tlv::Error&) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 114 | BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 115 | return CheckResponseResult::INVALID_RESPONSE; |
| 116 | } |
| 117 | |
| 118 | if (response.getCode() != expectedResponse.getCode()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 119 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 120 | return CheckResponseResult::WRONG_CODE; |
| 121 | } |
| 122 | |
| 123 | if (response.getText() != expectedResponse.getText()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 124 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 125 | return CheckResponseResult::WRONG_TEXT; |
| 126 | } |
| 127 | |
| 128 | const Block& body = response.getBody(); |
| 129 | const Block& expectedBody = expectedResponse.getBody(); |
| 130 | if (body.value_size() != expectedBody.value_size()) { |
Junxiao Shi | 7d30d85 | 2017-01-22 03:29:26 +0000 | [diff] [blame] | 131 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size()); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 132 | return CheckResponseResult::WRONG_BODY_SIZE; |
| 133 | } |
| 134 | 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] | 135 | BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value"); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 136 | return CheckResponseResult::WRONG_BODY_VALUE; |
| 137 | } |
| 138 | |
| 139 | return CheckResponseResult::OK; |
| 140 | } |
| 141 | |
| 142 | Block |
| 143 | ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses) |
| 144 | { |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 145 | auto isFinalSegment = [] (const Data& data) -> bool { |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 146 | const name::Component& lastComponent = data.getName().at(-1); |
| 147 | return !lastComponent.isSegment() || lastComponent == data.getFinalBlockId(); |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 150 | while (!isFinalSegment(m_responses.back())) { |
| 151 | const Name& name = m_responses.back().getName(); |
| 152 | Name prefix = name.getPrefix(-1); |
| 153 | uint64_t segmentNo = name.at(-1).toSegment() + 1; |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 154 | // request for the next segment |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 155 | receiveInterest(Interest(prefix.appendSegment(segmentNo))); |
Yanbiao Li | 150b80d | 2016-03-18 15:30:10 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 158 | size_t endIndex = startIndex + nResponses; // not included |
| 159 | if (nResponses == startIndex || endIndex > m_responses.size()) { |
| 160 | endIndex = m_responses.size(); |
| 161 | } |
| 162 | |
| 163 | ndn::EncodingBuffer encoder; |
| 164 | size_t valueLength = 0; |
| 165 | for (size_t i = startIndex; i < endIndex ; i ++) { |
| 166 | valueLength += encoder.appendByteArray(m_responses[i].getContent().value(), |
| 167 | m_responses[i].getContent().value_size()); |
| 168 | } |
| 169 | encoder.prependVarNumber(valueLength); |
| 170 | encoder.prependVarNumber(tlv::Content); |
| 171 | return encoder.block(); |
| 172 | } |
| 173 | |
| 174 | std::ostream& |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 175 | operator<<(std::ostream& os, const ManagerCommonFixture::CheckResponseResult& result) |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 176 | { |
| 177 | switch (result) { |
Junxiao Shi | 8a1f170 | 2017-07-03 00:05:08 +0000 | [diff] [blame^] | 178 | case ManagerCommonFixture::CheckResponseResult::OK: |
| 179 | return os << "OK"; |
| 180 | case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY: |
| 181 | return os << "OUT_OF_BOUNDARY"; |
| 182 | case ManagerCommonFixture::CheckResponseResult::WRONG_NAME: |
| 183 | return os << "WRONG_NAME"; |
| 184 | case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE: |
| 185 | return os << "WRONG_CONTENT_TYPE"; |
| 186 | case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE: |
| 187 | return os << "INVALID_RESPONSE"; |
| 188 | case ManagerCommonFixture::CheckResponseResult::WRONG_CODE: |
| 189 | return os << "WRONG_CODE"; |
| 190 | case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT: |
| 191 | return os << "WRONG_TEXT"; |
| 192 | case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE: |
| 193 | return os << "WRONG_BODY_SIZE"; |
| 194 | case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE: |
| 195 | return os << "WRONG_BODY_VALUE"; |
| 196 | } |
| 197 | return os << static_cast<int>(result); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | } // namespace tests |
| 201 | } // namespace nfd |