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