/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /** * Copyright (c) 2014-2017, Regents of the University of California, * Arizona Board of Regents, * Colorado State University, * University Pierre & Marie Curie, Sorbonne University, * Washington University in St. Louis, * Beijing Institute of Technology, * The University of Memphis. * * This file is part of NFD (Named Data Networking Forwarding Daemon). * See AUTHORS.md for complete list of NFD authors and contributors. * * NFD is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. * * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with * NFD, e.g., in COPYING.md file. If not, see . */ #include "manager-common-fixture.hpp" #include namespace nfd { namespace tests { ManagerCommonFixture::ManagerCommonFixture() : m_face(getGlobalIoService(), m_keyChain, {true, true}) , m_dispatcher(m_face, m_keyChain, ndn::security::SigningInfo()) , m_responses(m_face.sentData) , m_identityName("/unit-test/ManagerCommonFixture/identity") { BOOST_REQUIRE(this->addIdentity(m_identityName)); } void ManagerCommonFixture::setTopPrefix(const Name& topPrefix) { m_dispatcher.addTopPrefix(topPrefix); // such that all filters are added advanceClocks(time::milliseconds(1)); } shared_ptr ManagerCommonFixture::makeControlCommandRequest(Name commandName, const ControlParameters& parameters, const InterestHandler& beforeSigning) { shared_ptr command = makeInterest(commandName.append(parameters.wireEncode())); if (beforeSigning != nullptr) { beforeSigning(command); } m_keyChain.sign(*command, ndn::security::signingByIdentity(m_identityName)); return command; } void ManagerCommonFixture::receiveInterest(shared_ptr interest) { m_face.receive(*interest); advanceClocks(time::milliseconds(1)); } ControlResponse ManagerCommonFixture::makeResponse(uint32_t code, const std::string& text, const ControlParameters& parameters) { return ControlResponse(code, text).setBody(parameters.wireEncode()); } ManagerCommonFixture::CheckResponseResult ManagerCommonFixture::checkResponse(size_t idx, const Name& expectedName, const ControlResponse& expectedResponse, int expectedContentType /*= -1*/) { Data data; try { data = m_responses.at(idx); } catch (const std::out_of_range&) { BOOST_TEST_MESSAGE("response[" << idx << "] does not exist"); return CheckResponseResult::OUT_OF_BOUNDARY; } if (data.getName() != expectedName) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong name " << data.getName()); return CheckResponseResult::WRONG_NAME; } if (expectedContentType != -1 && data.getContentType() != static_cast(expectedContentType)) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong ContentType " << data.getContentType()); return CheckResponseResult::WRONG_CONTENT_TYPE; } ControlResponse response; try { response.wireDecode(data.getContent().blockFromValue()); } catch (const tlv::Error&) { BOOST_TEST_MESSAGE("response[" << idx << "] cannot be decoded"); return CheckResponseResult::INVALID_RESPONSE; } if (response.getCode() != expectedResponse.getCode()) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusCode " << response.getCode()); return CheckResponseResult::WRONG_CODE; } if (response.getText() != expectedResponse.getText()) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong StatusText " << response.getText()); return CheckResponseResult::WRONG_TEXT; } const Block& body = response.getBody(); const Block& expectedBody = expectedResponse.getBody(); if (body.value_size() != expectedBody.value_size()) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body size " << body.value_size()); return CheckResponseResult::WRONG_BODY_SIZE; } if (body.value_size() > 0 && memcmp(body.value(), expectedBody.value(), body.value_size()) != 0) { BOOST_TEST_MESSAGE("response[" << idx << "] has wrong body value"); return CheckResponseResult::WRONG_BODY_VALUE; } return CheckResponseResult::OK; } Block ManagerCommonFixture::concatenateResponses(size_t startIndex, size_t nResponses) { auto isFinalSegment = [] (const Data& data) -> bool { const name::Component& lastComponent = data.getName().at(-1); return !lastComponent.isSegment() || lastComponent == data.getFinalBlockId(); }; while (!isFinalSegment(m_responses.back())) { const Name& name = m_responses.back().getName(); Name prefix = name.getPrefix(-1); uint64_t segmentNo = name.at(-1).toSegment() + 1; // request for the next segment receiveInterest(makeInterest(prefix.appendSegment(segmentNo))); } size_t endIndex = startIndex + nResponses; // not included if (nResponses == startIndex || endIndex > m_responses.size()) { endIndex = m_responses.size(); } ndn::EncodingBuffer encoder; size_t valueLength = 0; for (size_t i = startIndex; i < endIndex ; i ++) { valueLength += encoder.appendByteArray(m_responses[i].getContent().value(), m_responses[i].getContent().value_size()); } encoder.prependVarNumber(valueLength); encoder.prependVarNumber(tlv::Content); return encoder.block(); } std::ostream& operator<<(std::ostream &os, const ManagerCommonFixture::CheckResponseResult& result) { switch (result) { case ManagerCommonFixture::CheckResponseResult::OK: os << "OK"; break; case ManagerCommonFixture::CheckResponseResult::OUT_OF_BOUNDARY: os << "OUT_OF_BOUNDARY"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_NAME: os << "WRONG_NAME"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_CONTENT_TYPE: os << "WRONG_CONTENT_TYPE"; break; case ManagerCommonFixture::CheckResponseResult::INVALID_RESPONSE: os << "INVALID_RESPONSE"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_CODE: os << "WRONG_CODE"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_TEXT: os << "WRONG_TEXT"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_SIZE: os << "WRONG_BODY_SIZE"; break; case ManagerCommonFixture::CheckResponseResult::WRONG_BODY_VALUE: os << "WRONG_BODY_VALUE"; break; default: break; }; return os; } } // namespace tests } // namespace nfd