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