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