Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 "face/transport.hpp" |
| 27 | #include "face/lp-face.hpp" |
| 28 | #include "dummy-transport.hpp" |
| 29 | #include "dummy-receive-link-service.hpp" |
| 30 | #include "transport-properties.hpp" |
| 31 | |
| 32 | #include <boost/mpl/empty_sequence.hpp> |
| 33 | #include <boost/mpl/int.hpp> |
| 34 | #include <boost/mpl/joint_view.hpp> |
| 35 | #include <boost/mpl/map.hpp> |
| 36 | #include <boost/mpl/set.hpp> |
| 37 | #include <boost/mpl/transform_view.hpp> |
| 38 | |
| 39 | #include "tests/test-common.hpp" |
| 40 | |
| 41 | namespace nfd { |
| 42 | namespace face { |
| 43 | namespace tests { |
| 44 | |
| 45 | using namespace nfd::tests; |
| 46 | namespace ip = boost::asio::ip; |
| 47 | |
| 48 | BOOST_AUTO_TEST_SUITE(Face) |
| 49 | |
| 50 | class DummyTransportFixture : public BaseFixture |
| 51 | { |
| 52 | public: |
| 53 | DummyTransportFixture() |
| 54 | : transport(nullptr) |
| 55 | , sentPackets(nullptr) |
| 56 | , receivedPackets(nullptr) |
| 57 | { |
| 58 | // Constructor does not initialize the fixture, |
| 59 | // so that test case may specify different parameters to DummyTransport constructor. |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>()) |
| 64 | { |
| 65 | this->face = make_unique<LpFace>(make_unique<DummyReceiveLinkService>(), std::move(transport)); |
| 66 | this->transport = static_cast<DummyTransport*>(face->getTransport()); |
| 67 | this->sentPackets = &this->transport->sentPackets; |
| 68 | this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets; |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | unique_ptr<LpFace> face; |
| 73 | DummyTransport* transport; |
| 74 | std::vector<Transport::Packet>* sentPackets; |
| 75 | std::vector<Transport::Packet>* receivedPackets; |
| 76 | }; |
| 77 | |
| 78 | BOOST_FIXTURE_TEST_SUITE(TestTransport, DummyTransportFixture) |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties) |
| 81 | { |
| 82 | this->initialize(); |
| 83 | checkStaticPropertiesInitialized(*transport); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_SUITE(StateTransition) |
| 87 | |
| 88 | namespace mpl = boost::mpl; |
| 89 | |
| 90 | /** \brief a macro to declare a TransportState as a integral constant |
| 91 | */ |
| 92 | #define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)> |
| 93 | |
| 94 | /** \brief a map from every TransportState to a valid state transition sequence |
| 95 | * for entering this state from UP |
| 96 | */ |
| 97 | typedef mpl::map< |
| 98 | mpl::pair<TRANSPORT_STATE_C(UP), |
| 99 | mpl::vector<>>, |
| 100 | mpl::pair<TRANSPORT_STATE_C(DOWN), |
| 101 | mpl::vector< |
| 102 | TRANSPORT_STATE_C(DOWN) |
| 103 | >>, |
| 104 | mpl::pair<TRANSPORT_STATE_C(CLOSING), |
| 105 | mpl::vector< |
| 106 | TRANSPORT_STATE_C(CLOSING) |
| 107 | >>, |
| 108 | mpl::pair<TRANSPORT_STATE_C(FAILED), |
| 109 | mpl::vector< |
| 110 | TRANSPORT_STATE_C(FAILED) |
| 111 | >>, |
| 112 | mpl::pair<TRANSPORT_STATE_C(CLOSED), |
| 113 | mpl::vector< |
| 114 | TRANSPORT_STATE_C(CLOSING), |
| 115 | TRANSPORT_STATE_C(CLOSED) |
| 116 | >> |
| 117 | > StateEntering; |
| 118 | |
| 119 | /** \brief a sequence of all valid TransportStates |
| 120 | */ |
| 121 | typedef mpl::fold<StateEntering, |
| 122 | mpl::vector<>, |
| 123 | mpl::push_back<mpl::_1, mpl::first<mpl::_2>> |
| 124 | >::type States; |
| 125 | |
| 126 | /** \brief a set of all valid state transitions |
| 127 | */ |
| 128 | typedef mpl::set< |
| 129 | mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>, |
| 130 | mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>, |
| 131 | mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>, |
| 132 | mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>, |
| 133 | mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>, |
| 134 | mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>, |
| 135 | mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>, |
| 136 | mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)> |
| 137 | > ValidStateTransitions; |
| 138 | |
| 139 | /** \brief a metafunction class to generate a sequence of all state transitions |
| 140 | * from a specified state |
| 141 | */ |
| 142 | struct StateTransitionsFrom |
| 143 | { |
| 144 | template<typename FROM> |
| 145 | struct apply |
| 146 | { |
| 147 | typedef typename mpl::fold< |
| 148 | States, |
| 149 | mpl::vector<>, |
| 150 | mpl::push_back<mpl::_1, mpl::pair<FROM, mpl::_2>> |
| 151 | >::type type; |
| 152 | }; |
| 153 | }; |
| 154 | |
| 155 | /** \brief a sequence of all state transitions |
| 156 | */ |
| 157 | typedef mpl::fold< |
| 158 | States, |
| 159 | mpl::empty_sequence, |
| 160 | mpl::joint_view<mpl::_1, mpl::apply<StateTransitionsFrom, mpl::protect<mpl::_1>>::type> |
| 161 | >::type AllStateTransitions; |
| 162 | |
| 163 | #undef TRANSPORT_STATE_C |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions) |
| 166 | { |
| 167 | this->initialize(); |
| 168 | |
| 169 | TransportState from = static_cast<TransportState>(T::first::value); |
| 170 | TransportState to = static_cast<TransportState>(T::second::value); |
| 171 | BOOST_TEST_MESSAGE("SetState " << from << " -> " << to); |
| 172 | |
| 173 | // enter from state |
| 174 | typedef typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type Steps; |
| 175 | mpl::for_each<Steps>( |
| 176 | [this] (int state) { transport->setState(static_cast<TransportState>(state)); }); |
| 177 | BOOST_REQUIRE_EQUAL(transport->getState(), from); |
| 178 | |
| 179 | bool hasSignal = false; |
| 180 | this->transport->afterStateChange.connect( |
| 181 | [from, to, &hasSignal] (TransportState oldState, TransportState newState) { |
| 182 | hasSignal = true; |
| 183 | BOOST_CHECK_EQUAL(oldState, from); |
| 184 | BOOST_CHECK_EQUAL(newState, to); |
| 185 | }); |
| 186 | |
| 187 | // do transition |
| 188 | bool isValid = from == to || |
| 189 | mpl::has_key<ValidStateTransitions, |
| 190 | mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>> |
| 191 | >::value; |
| 192 | if (isValid) { |
| 193 | BOOST_REQUIRE_NO_THROW(transport->setState(to)); |
| 194 | BOOST_CHECK_EQUAL(hasSignal, from != to); |
| 195 | } |
| 196 | else { |
| 197 | BOOST_CHECK_THROW(this->transport->setState(to), std::runtime_error); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_SUITE_END() // StateTransition |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(Send) |
| 204 | { |
| 205 | this->initialize(); |
| 206 | |
| 207 | Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,"); |
| 208 | transport->send(Transport::Packet(Block(pkt1))); |
| 209 | |
| 210 | Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,"); |
| 211 | transport->send(Transport::Packet(Block(pkt2))); |
| 212 | |
| 213 | transport->setState(TransportState::DOWN); |
| 214 | Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt "); |
| 215 | transport->send(Transport::Packet(Block(pkt3))); |
| 216 | |
| 217 | transport->setState(TransportState::CLOSING); |
| 218 | Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua."); |
| 219 | transport->send(Transport::Packet(Block(pkt4))); |
| 220 | |
| 221 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2); |
| 222 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size()); |
| 223 | BOOST_REQUIRE_EQUAL(sentPackets->size(), 3); |
| 224 | BOOST_CHECK(sentPackets->at(0).packet == pkt1); |
| 225 | BOOST_CHECK(sentPackets->at(1).packet == pkt2); |
| 226 | BOOST_CHECK(sentPackets->at(2).packet == pkt3); |
| 227 | } |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE(Receive) |
| 230 | { |
| 231 | this->initialize(); |
| 232 | |
| 233 | Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,"); |
| 234 | transport->receivePacket(pkt1); |
| 235 | |
| 236 | Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,"); |
| 237 | transport->receivePacket(pkt2); |
| 238 | |
| 239 | transport->setState(TransportState::DOWN); |
| 240 | Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt "); |
| 241 | transport->receivePacket(pkt3); |
| 242 | |
| 243 | BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3); |
| 244 | BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size()); |
| 245 | BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3); |
| 246 | BOOST_CHECK(receivedPackets->at(0).packet == pkt1); |
| 247 | BOOST_CHECK(receivedPackets->at(1).packet == pkt2); |
| 248 | BOOST_CHECK(receivedPackets->at(2).packet == pkt3); |
| 249 | } |
| 250 | |
| 251 | BOOST_AUTO_TEST_SUITE_END() // TestTransport |
| 252 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 253 | |
| 254 | } // namespace tests |
| 255 | } // namespace face |
| 256 | } // namespace nfd |