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