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