blob: 800dde9c9e59da507a684f8697d83b2da1557f5d [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento45898d22018-04-17 21:59:54 -04002/*
Davide Pesaventof56cf632024-01-27 22:22:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi57df2882015-11-11 06:12:35 -07004 * 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 Shicde37ad2015-12-24 01:02:05 -070027#include "face/face.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040028
Davide Pesavento16916ae2019-03-29 23:53:26 -040029#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Davide Pesaventoa8098582019-03-31 15:48:02 -040031#include "tests/daemon/face/dummy-link-service.hpp"
32#include "tests/daemon/face/dummy-transport.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070033
Davide Pesaventof56cf632024-01-27 22:22:06 -050034#include <boost/mp11/algorithm.hpp>
35#include <boost/mp11/bind.hpp>
36#include <boost/mp11/map.hpp>
37#include <boost/mp11/set.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070038
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039namespace nfd::tests {
Junxiao Shi57df2882015-11-11 06:12:35 -070040
Davide Pesaventof56cf632024-01-27 22:22:06 -050041using namespace boost::mp11;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042using namespace nfd::face;
Junxiao Shicde37ad2015-12-24 01:02:05 -070043
Davide Pesavento97210d52016-10-14 15:45:48 +020044BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento32065652017-01-15 01:52:21 -050045BOOST_AUTO_TEST_SUITE(TestTransport)
Junxiao Shi57df2882015-11-11 06:12:35 -070046
Davide Pesavento32065652017-01-15 01:52:21 -050047BOOST_AUTO_TEST_CASE(PersistencyChange)
48{
Davide Pesavento16916ae2019-03-29 23:53:26 -040049 auto transport = make_unique<DummyTransport>();
50 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento32065652017-01-15 01:52:21 -050051 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 Pesavento16916ae2019-03-29 23:53:26 -040058 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento32065652017-01-15 01:52:21 -050059 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 Pesavento16916ae2019-03-29 23:53:26 -040064 BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
Davide Pesavento32065652017-01-15 01:52:21 -050065}
66
Davide Pesaventof56cf632024-01-27 22:22:06 -050067// Map from every TransportState to a valid state transition sequence
68// for entering this state from UP.
69using 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>;
76static_assert(mp_is_map<StateInitSequence>());
Junxiao Shi57df2882015-11-11 06:12:35 -070077
Davide Pesaventof56cf632024-01-27 22:22:06 -050078using TransportStates = mp_map_keys<StateInitSequence>;
Junxiao Shi57df2882015-11-11 06:12:35 -070079
Davide Pesaventof56cf632024-01-27 22:22:06 -050080// The set of all state transitions (cartesian product of TransportStates)
81using AllStateTransitions = mp_product<mp_list, TransportStates, TransportStates>;
Junxiao Shi57df2882015-11-11 06:12:35 -070082
Davide Pesaventof56cf632024-01-27 22:22:06 -050083// The set of *valid* state transitions
84using 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
95static_assert(mp_is_set<ValidStateTransitions>());
96// Sanity check that ValidStateTransitions is a proper subset of AllStateTransitions
97static_assert(mp_all_of_q<ValidStateTransitions, mp_bind_front<mp_set_contains, AllStateTransitions>>());
98static_assert(mp_size<ValidStateTransitions>() < mp_size<AllStateTransitions>());
Junxiao Shi57df2882015-11-11 06:12:35 -070099
100BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
101{
Davide Pesaventof56cf632024-01-27 22:22:06 -0500102 constexpr TransportState from = mp_first<T>::value;
103 constexpr TransportState to = mp_second<T>::value;
Davide Pesaventod7083a52023-10-19 17:51:16 -0400104 BOOST_TEST_INFO_SCOPE(from << " -> " << to);
Junxiao Shi57df2882015-11-11 06:12:35 -0700105
Davide Pesaventof56cf632024-01-27 22:22:06 -0500106 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 Shi57df2882015-11-11 06:12:35 -0700110 BOOST_REQUIRE_EQUAL(transport->getState(), from);
111
112 bool hasSignal = false;
Davide Pesaventod7083a52023-10-19 17:51:16 -0400113 transport->afterStateChange.connect([&] (TransportState oldState, TransportState newState) {
114 hasSignal = true;
115 BOOST_CHECK_EQUAL(oldState, from);
116 BOOST_CHECK_EQUAL(newState, to);
117 });
Junxiao Shi57df2882015-11-11 06:12:35 -0700118
119 // do transition
Davide Pesaventof56cf632024-01-27 22:22:06 -0500120 constexpr bool isValid = (from == to) || mp_set_contains<ValidStateTransitions, T>();
121 if constexpr (isValid) {
Junxiao Shi57df2882015-11-11 06:12:35 -0700122 BOOST_REQUIRE_NO_THROW(transport->setState(to));
123 BOOST_CHECK_EQUAL(hasSignal, from != to);
124 }
125 else {
Davide Pesavento32065652017-01-15 01:52:21 -0500126 BOOST_CHECK_THROW(transport->setState(to), std::runtime_error);
Junxiao Shi57df2882015-11-11 06:12:35 -0700127 }
128}
129
Davide Pesavento14e71f02019-03-28 17:35:25 -0400130class DummyTransportFixture : public GlobalIoFixture
Davide Pesavento32065652017-01-15 01:52:21 -0500131{
132protected:
Davide Pesavento32065652017-01-15 01:52:21 -0500133 void
Davide Pesavento16916ae2019-03-29 23:53:26 -0400134 initialize(unique_ptr<DummyTransport> t = make_unique<DummyTransport>())
Davide Pesavento32065652017-01-15 01:52:21 -0500135 {
Davide Pesaventoa8098582019-03-31 15:48:02 -0400136 this->face = make_unique<nfd::Face>(make_unique<DummyLinkService>(), std::move(t));
Davide Pesavento32065652017-01-15 01:52:21 -0500137 this->transport = static_cast<DummyTransport*>(face->getTransport());
138 this->sentPackets = &this->transport->sentPackets;
Davide Pesaventoa8098582019-03-31 15:48:02 -0400139 this->receivedPackets = &static_cast<DummyLinkService*>(face->getLinkService())->receivedPackets;
Davide Pesavento32065652017-01-15 01:52:21 -0500140 }
141
142protected:
143 unique_ptr<nfd::Face> face;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400144 DummyTransport* transport = nullptr;
Teng Liang13d582a2020-07-21 20:23:11 -0700145 const std::vector<Block>* sentPackets = nullptr;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400146 const std::vector<RxPacket>* receivedPackets = nullptr;
Davide Pesavento32065652017-01-15 01:52:21 -0500147};
148
149BOOST_FIXTURE_TEST_CASE(Send, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700150{
151 this->initialize();
152
153 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400154 transport->send(pkt1);
Junxiao Shi57df2882015-11-11 06:12:35 -0700155
156 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400157 transport->send(pkt2);
Junxiao Shi57df2882015-11-11 06:12:35 -0700158
159 transport->setState(TransportState::DOWN);
160 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400161 transport->send(pkt3);
Junxiao Shi57df2882015-11-11 06:12:35 -0700162
163 transport->setState(TransportState::CLOSING);
164 Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400165 transport->send(pkt4);
Junxiao Shi57df2882015-11-11 06:12:35 -0700166
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 Liang13d582a2020-07-21 20:23:11 -0700170 BOOST_CHECK(sentPackets->at(0) == pkt1);
171 BOOST_CHECK(sentPackets->at(1) == pkt2);
172 BOOST_CHECK(sentPackets->at(2) == pkt3);
Junxiao Shi57df2882015-11-11 06:12:35 -0700173}
174
Davide Pesavento32065652017-01-15 01:52:21 -0500175BOOST_FIXTURE_TEST_CASE(Receive, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700176{
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
197BOOST_AUTO_TEST_SUITE_END() // TestTransport
198BOOST_AUTO_TEST_SUITE_END() // Face
199
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400200} // namespace nfd::tests