blob: ae6a9c1a6416032b5ccb7afdeb21880535e90699 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2015, 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 <http://www.gnu.org/licenses/>.
*/
#include "face/transport.hpp"
#include "face/lp-face.hpp"
#include "dummy-transport.hpp"
#include "dummy-receive-link-service.hpp"
#include "transport-properties.hpp"
#include <boost/mpl/empty_sequence.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/joint_view.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/set.hpp>
#include <boost/mpl/transform_view.hpp>
#include "tests/test-common.hpp"
namespace nfd {
namespace face {
namespace tests {
using namespace nfd::tests;
namespace ip = boost::asio::ip;
BOOST_AUTO_TEST_SUITE(Face)
class DummyTransportFixture : public BaseFixture
{
public:
DummyTransportFixture()
: transport(nullptr)
, sentPackets(nullptr)
, receivedPackets(nullptr)
{
// Constructor does not initialize the fixture,
// so that test case may specify different parameters to DummyTransport constructor.
}
void
initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
{
this->face = make_unique<LpFace>(make_unique<DummyReceiveLinkService>(), std::move(transport));
this->transport = static_cast<DummyTransport*>(face->getTransport());
this->sentPackets = &this->transport->sentPackets;
this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
}
public:
unique_ptr<LpFace> face;
DummyTransport* transport;
std::vector<Transport::Packet>* sentPackets;
std::vector<Transport::Packet>* receivedPackets;
};
BOOST_FIXTURE_TEST_SUITE(TestTransport, DummyTransportFixture)
BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
{
this->initialize();
checkStaticPropertiesInitialized(*transport);
}
BOOST_AUTO_TEST_SUITE(StateTransition)
namespace mpl = boost::mpl;
/** \brief a macro to declare a TransportState as a integral constant
*/
#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
/** \brief a map from every TransportState to a valid state transition sequence
* for entering this state from UP
*/
typedef mpl::map<
mpl::pair<TRANSPORT_STATE_C(UP),
mpl::vector<>>,
mpl::pair<TRANSPORT_STATE_C(DOWN),
mpl::vector<
TRANSPORT_STATE_C(DOWN)
>>,
mpl::pair<TRANSPORT_STATE_C(CLOSING),
mpl::vector<
TRANSPORT_STATE_C(CLOSING)
>>,
mpl::pair<TRANSPORT_STATE_C(FAILED),
mpl::vector<
TRANSPORT_STATE_C(FAILED)
>>,
mpl::pair<TRANSPORT_STATE_C(CLOSED),
mpl::vector<
TRANSPORT_STATE_C(CLOSING),
TRANSPORT_STATE_C(CLOSED)
>>
> StateEntering;
/** \brief a sequence of all valid TransportStates
*/
typedef mpl::fold<StateEntering,
mpl::vector<>,
mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
>::type States;
/** \brief a set of all valid state transitions
*/
typedef mpl::set<
mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
> ValidStateTransitions;
/** \brief a metafunction class to generate a sequence of all state transitions
* from a specified state
*/
struct StateTransitionsFrom
{
template<typename FROM>
struct apply
{
typedef typename mpl::fold<
States,
mpl::vector<>,
mpl::push_back<mpl::_1, mpl::pair<FROM, mpl::_2>>
>::type type;
};
};
/** \brief a sequence of all state transitions
*/
typedef mpl::fold<
States,
mpl::empty_sequence,
mpl::joint_view<mpl::_1, mpl::apply<StateTransitionsFrom, mpl::protect<mpl::_1>>::type>
>::type AllStateTransitions;
#undef TRANSPORT_STATE_C
BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
{
this->initialize();
TransportState from = static_cast<TransportState>(T::first::value);
TransportState to = static_cast<TransportState>(T::second::value);
BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
// enter from state
typedef typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type Steps;
mpl::for_each<Steps>(
[this] (int state) { transport->setState(static_cast<TransportState>(state)); });
BOOST_REQUIRE_EQUAL(transport->getState(), from);
bool hasSignal = false;
this->transport->afterStateChange.connect(
[from, to, &hasSignal] (TransportState oldState, TransportState newState) {
hasSignal = true;
BOOST_CHECK_EQUAL(oldState, from);
BOOST_CHECK_EQUAL(newState, to);
});
// do transition
bool isValid = from == to ||
mpl::has_key<ValidStateTransitions,
mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
>::value;
if (isValid) {
BOOST_REQUIRE_NO_THROW(transport->setState(to));
BOOST_CHECK_EQUAL(hasSignal, from != to);
}
else {
BOOST_CHECK_THROW(this->transport->setState(to), std::runtime_error);
}
}
BOOST_AUTO_TEST_SUITE_END() // StateTransition
BOOST_AUTO_TEST_CASE(Send)
{
this->initialize();
Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
transport->send(Transport::Packet(Block(pkt1)));
Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
transport->send(Transport::Packet(Block(pkt2)));
transport->setState(TransportState::DOWN);
Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
transport->send(Transport::Packet(Block(pkt3)));
transport->setState(TransportState::CLOSING);
Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
transport->send(Transport::Packet(Block(pkt4)));
BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size());
BOOST_REQUIRE_EQUAL(sentPackets->size(), 3);
BOOST_CHECK(sentPackets->at(0).packet == pkt1);
BOOST_CHECK(sentPackets->at(1).packet == pkt2);
BOOST_CHECK(sentPackets->at(2).packet == pkt3);
}
BOOST_AUTO_TEST_CASE(Receive)
{
this->initialize();
Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
transport->receivePacket(pkt1);
Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
transport->receivePacket(pkt2);
transport->setState(TransportState::DOWN);
Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
transport->receivePacket(pkt3);
BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3);
BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size());
BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3);
BOOST_CHECK(receivedPackets->at(0).packet == pkt1);
BOOST_CHECK(receivedPackets->at(1).packet == pkt2);
BOOST_CHECK(receivedPackets->at(2).packet == pkt3);
}
BOOST_AUTO_TEST_SUITE_END() // TestTransport
BOOST_AUTO_TEST_SUITE_END() // Face
} // namespace tests
} // namespace face
} // namespace nfd