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 | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 34 | #include <boost/mp11/algorithm.hpp> |
| 35 | #include <boost/mp11/bind.hpp> |
| 36 | #include <boost/mp11/map.hpp> |
| 37 | #include <boost/mp11/set.hpp> |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 39 | namespace nfd::tests { |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 41 | using namespace boost::mp11; |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 42 | using namespace nfd::face; |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 97210d5 | 2016-10-14 15:45:48 +0200 | [diff] [blame] | 44 | BOOST_AUTO_TEST_SUITE(Face) |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 45 | BOOST_AUTO_TEST_SUITE(TestTransport) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE(PersistencyChange) |
| 48 | { |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 49 | auto transport = make_unique<DummyTransport>(); |
| 50 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0); |
| 52 | |
| 53 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_NONE), false); |
| 54 | BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(transport->getPersistency()), true); |
| 55 | BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true); |
| 56 | |
| 57 | transport->setPersistency(transport->getPersistency()); |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0); |
| 60 | |
| 61 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 62 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 63 | BOOST_REQUIRE_EQUAL(transport->persistencyHistory.size(), 1); |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 67 | // Map from every TransportState to a valid state transition sequence |
| 68 | // for entering this state from UP. |
| 69 | using StateInitSequence = mp_list< |
| 70 | mp_list_c<TransportState, TransportState::UP /* nothing to do, state is already UP */>, |
| 71 | mp_list_c<TransportState, TransportState::DOWN, TransportState::DOWN>, |
| 72 | mp_list_c<TransportState, TransportState::CLOSING, TransportState::CLOSING>, |
| 73 | mp_list_c<TransportState, TransportState::FAILED, TransportState::FAILED>, |
| 74 | mp_list_c<TransportState, TransportState::CLOSED, TransportState::CLOSING, TransportState::CLOSED> |
| 75 | >; |
| 76 | static_assert(mp_is_map<StateInitSequence>()); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 78 | using TransportStates = mp_map_keys<StateInitSequence>; |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 79 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 80 | // The set of all state transitions (cartesian product of TransportStates) |
| 81 | using AllStateTransitions = mp_product<mp_list, TransportStates, TransportStates>; |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 83 | // The set of *valid* state transitions |
| 84 | using ValidStateTransitions = mp_list< |
| 85 | mp_list_c<TransportState, TransportState::UP, TransportState::DOWN>, |
| 86 | mp_list_c<TransportState, TransportState::UP, TransportState::CLOSING>, |
| 87 | mp_list_c<TransportState, TransportState::UP, TransportState::FAILED>, |
| 88 | mp_list_c<TransportState, TransportState::DOWN, TransportState::UP>, |
| 89 | mp_list_c<TransportState, TransportState::DOWN, TransportState::CLOSING>, |
| 90 | mp_list_c<TransportState, TransportState::DOWN, TransportState::FAILED>, |
| 91 | mp_list_c<TransportState, TransportState::CLOSING, TransportState::CLOSED>, |
| 92 | mp_list_c<TransportState, TransportState::FAILED, TransportState::CLOSED> |
| 93 | >; |
| 94 | // Sanity check that there are no duplicates |
| 95 | static_assert(mp_is_set<ValidStateTransitions>()); |
| 96 | // Sanity check that ValidStateTransitions is a proper subset of AllStateTransitions |
| 97 | static_assert(mp_all_of_q<ValidStateTransitions, mp_bind_front<mp_set_contains, AllStateTransitions>>()); |
| 98 | static_assert(mp_size<ValidStateTransitions>() < mp_size<AllStateTransitions>()); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 99 | |
| 100 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions) |
| 101 | { |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 102 | constexpr TransportState from = mp_first<T>::value; |
| 103 | constexpr TransportState to = mp_second<T>::value; |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 104 | BOOST_TEST_INFO_SCOPE(from << " -> " << to); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 105 | |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 106 | auto transport = make_unique<DummyTransport>(); |
| 107 | // initialize transport to the 'from' state |
| 108 | using Steps = mp_rest<mp_map_find<StateInitSequence, mp_first<T>>>; |
| 109 | mp_for_each<Steps>([&transport] (auto state) { transport->setState(state); }); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 110 | BOOST_REQUIRE_EQUAL(transport->getState(), from); |
| 111 | |
| 112 | bool hasSignal = false; |
Davide Pesavento | d7083a5 | 2023-10-19 17:51:16 -0400 | [diff] [blame] | 113 | transport->afterStateChange.connect([&] (TransportState oldState, TransportState newState) { |
| 114 | hasSignal = true; |
| 115 | BOOST_CHECK_EQUAL(oldState, from); |
| 116 | BOOST_CHECK_EQUAL(newState, to); |
| 117 | }); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 118 | |
| 119 | // do transition |
Davide Pesavento | f56cf63 | 2024-01-27 22:22:06 -0500 | [diff] [blame] | 120 | constexpr bool isValid = (from == to) || mp_set_contains<ValidStateTransitions, T>(); |
| 121 | if constexpr (isValid) { |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 122 | BOOST_REQUIRE_NO_THROW(transport->setState(to)); |
| 123 | BOOST_CHECK_EQUAL(hasSignal, from != to); |
| 124 | } |
| 125 | else { |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 126 | BOOST_CHECK_THROW(transport->setState(to), std::runtime_error); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 130 | class DummyTransportFixture : public GlobalIoFixture |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 131 | { |
| 132 | protected: |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 133 | void |
Davide Pesavento | 16916ae | 2019-03-29 23:53:26 -0400 | [diff] [blame] | 134 | initialize(unique_ptr<DummyTransport> t = make_unique<DummyTransport>()) |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 135 | { |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 136 | this->face = make_unique<nfd::Face>(make_unique<DummyLinkService>(), std::move(t)); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 137 | this->transport = static_cast<DummyTransport*>(face->getTransport()); |
| 138 | this->sentPackets = &this->transport->sentPackets; |
Davide Pesavento | a809858 | 2019-03-31 15:48:02 -0400 | [diff] [blame] | 139 | this->receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets; |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | protected: |
| 143 | unique_ptr<nfd::Face> face; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 144 | DummyTransport* transport = nullptr; |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 145 | const std::vector<Block>* sentPackets = nullptr; |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 146 | const std::vector<RxPacket>* receivedPackets = nullptr; |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | BOOST_FIXTURE_TEST_CASE(Send, DummyTransportFixture) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 150 | { |
| 151 | this->initialize(); |
| 152 | |
| 153 | Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 154 | transport->send(pkt1); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 155 | |
| 156 | Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,"); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 157 | transport->send(pkt2); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 158 | |
| 159 | transport->setState(TransportState::DOWN); |
| 160 | Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt "); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 161 | transport->send(pkt3); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 162 | |
| 163 | transport->setState(TransportState::CLOSING); |
| 164 | Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua."); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 165 | transport->send(pkt4); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 166 | |
| 167 | BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2); |
| 168 | BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size()); |
| 169 | BOOST_REQUIRE_EQUAL(sentPackets->size(), 3); |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 170 | BOOST_CHECK(sentPackets->at(0) == pkt1); |
| 171 | BOOST_CHECK(sentPackets->at(1) == pkt2); |
| 172 | BOOST_CHECK(sentPackets->at(2) == pkt3); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 175 | BOOST_FIXTURE_TEST_CASE(Receive, DummyTransportFixture) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 176 | { |
| 177 | this->initialize(); |
| 178 | |
| 179 | Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,"); |
| 180 | transport->receivePacket(pkt1); |
| 181 | |
| 182 | Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,"); |
| 183 | transport->receivePacket(pkt2); |
| 184 | |
| 185 | transport->setState(TransportState::DOWN); |
| 186 | Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt "); |
| 187 | transport->receivePacket(pkt3); |
| 188 | |
| 189 | BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3); |
| 190 | BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size()); |
| 191 | BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3); |
| 192 | BOOST_CHECK(receivedPackets->at(0).packet == pkt1); |
| 193 | BOOST_CHECK(receivedPackets->at(1).packet == pkt2); |
| 194 | BOOST_CHECK(receivedPackets->at(2).packet == pkt3); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() // TestTransport |
| 198 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 199 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 200 | } // namespace nfd::tests |