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