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