blob: d586c20167beafcd9db4387ffdaafa85f1f9d211 [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 Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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
29#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070030#include "dummy-receive-link-service.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040031#include "dummy-transport.hpp"
Eric Newberry8717e872015-11-23 12:41:50 -070032#include "transport-test-common.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070033
Davide Pesavento45898d22018-04-17 21:59:54 -040034#include <boost/mpl/fold.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070035#include <boost/mpl/int.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040036#include <boost/mpl/lambda.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070037#include <boost/mpl/map.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040038#include <boost/mpl/pair.hpp>
39#include <boost/mpl/push_back.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070040#include <boost/mpl/set.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040041#include <boost/mpl/vector.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070042
Junxiao Shi57df2882015-11-11 06:12:35 -070043namespace nfd {
44namespace face {
45namespace tests {
46
Davide Pesavento97210d52016-10-14 15:45:48 +020047namespace mpl = boost::mpl;
Junxiao Shicde37ad2015-12-24 01:02:05 -070048
Davide Pesavento97210d52016-10-14 15:45:48 +020049BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento32065652017-01-15 01:52:21 -050050BOOST_AUTO_TEST_SUITE(TestTransport)
Junxiao Shi57df2882015-11-11 06:12:35 -070051
52BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
53{
Davide Pesavento32065652017-01-15 01:52:21 -050054 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -070055 checkStaticPropertiesInitialized(*transport);
56}
57
Davide Pesavento32065652017-01-15 01:52:21 -050058class PersistencyTestTransport : public DummyTransport
59{
60public:
61 PersistencyTestTransport()
62 : DummyTransport("dummy://", "dummy://",
63 ndn::nfd::FACE_SCOPE_NON_LOCAL,
64 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND)
65 {
66 }
67
68protected:
69 void
70 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
71 {
72 persistencyHistory.push_back(oldPersistency);
73 }
74
75public:
76 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
77};
78
79BOOST_AUTO_TEST_CASE(PersistencyChange)
80{
81 auto transport = make_unique<PersistencyTestTransport>();
82 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
83 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
84
85 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_NONE), false);
86 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(transport->getPersistency()), true);
87 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
88
89 transport->setPersistency(transport->getPersistency());
90 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
91 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
92
93 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
94 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
95 BOOST_REQUIRE_EQUAL(transport->persistencyHistory.size(), 1);
96 BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
97}
98
Junxiao Shi57df2882015-11-11 06:12:35 -070099/** \brief a macro to declare a TransportState as a integral constant
Davide Pesavento45898d22018-04-17 21:59:54 -0400100 * \note we cannot use mpl::integral_c because TransportState is not an integral type
Junxiao Shi57df2882015-11-11 06:12:35 -0700101 */
102#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
103
104/** \brief a map from every TransportState to a valid state transition sequence
105 * for entering this state from UP
106 */
107typedef mpl::map<
108 mpl::pair<TRANSPORT_STATE_C(UP),
109 mpl::vector<>>,
110 mpl::pair<TRANSPORT_STATE_C(DOWN),
111 mpl::vector<
112 TRANSPORT_STATE_C(DOWN)
113 >>,
114 mpl::pair<TRANSPORT_STATE_C(CLOSING),
115 mpl::vector<
116 TRANSPORT_STATE_C(CLOSING)
117 >>,
118 mpl::pair<TRANSPORT_STATE_C(FAILED),
119 mpl::vector<
120 TRANSPORT_STATE_C(FAILED)
121 >>,
122 mpl::pair<TRANSPORT_STATE_C(CLOSED),
123 mpl::vector<
124 TRANSPORT_STATE_C(CLOSING),
125 TRANSPORT_STATE_C(CLOSED)
126 >>
127> StateEntering;
128
129/** \brief a sequence of all valid TransportStates
130 */
131typedef mpl::fold<StateEntering,
132 mpl::vector<>,
133 mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
134>::type States;
135
136/** \brief a set of all valid state transitions
137 */
138typedef mpl::set<
139 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
140 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
141 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
142 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
143 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
144 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
145 mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
146 mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
147> ValidStateTransitions;
148
Davide Pesavento45898d22018-04-17 21:59:54 -0400149/** \brief a metafunction to generate a sequence of all state transitions
Junxiao Shi57df2882015-11-11 06:12:35 -0700150 * from a specified state
151 */
Davide Pesavento45898d22018-04-17 21:59:54 -0400152template<typename FromState, typename Result>
153struct StateTransitionsFrom : mpl::fold<
154 States,
155 Result,
156 mpl::push_back<mpl::_1, mpl::pair<FromState, mpl::_2>>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700157{
Junxiao Shi57df2882015-11-11 06:12:35 -0700158};
159
160/** \brief a sequence of all state transitions
161 */
162typedef mpl::fold<
163 States,
Davide Pesavento45898d22018-04-17 21:59:54 -0400164 mpl::vector<>,
165 mpl::lambda<StateTransitionsFrom<mpl::_2, mpl::_1>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700166>::type AllStateTransitions;
167
168#undef TRANSPORT_STATE_C
169
170BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
171{
Davide Pesavento32065652017-01-15 01:52:21 -0500172 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -0700173
174 TransportState from = static_cast<TransportState>(T::first::value);
175 TransportState to = static_cast<TransportState>(T::second::value);
176 BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
177
178 // enter from state
Davide Pesavento32065652017-01-15 01:52:21 -0500179 using Steps = typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type;
180 mpl::for_each<Steps>([&transport] (int state) {
181 transport->setState(static_cast<TransportState>(state));
182 });
Junxiao Shi57df2882015-11-11 06:12:35 -0700183 BOOST_REQUIRE_EQUAL(transport->getState(), from);
184
185 bool hasSignal = false;
Davide Pesavento32065652017-01-15 01:52:21 -0500186 transport->afterStateChange.connect(
Junxiao Shi57df2882015-11-11 06:12:35 -0700187 [from, to, &hasSignal] (TransportState oldState, TransportState newState) {
188 hasSignal = true;
189 BOOST_CHECK_EQUAL(oldState, from);
190 BOOST_CHECK_EQUAL(newState, to);
191 });
192
193 // do transition
194 bool isValid = from == to ||
195 mpl::has_key<ValidStateTransitions,
196 mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
197 >::value;
198 if (isValid) {
199 BOOST_REQUIRE_NO_THROW(transport->setState(to));
200 BOOST_CHECK_EQUAL(hasSignal, from != to);
201 }
202 else {
Davide Pesavento32065652017-01-15 01:52:21 -0500203 BOOST_CHECK_THROW(transport->setState(to), std::runtime_error);
Junxiao Shi57df2882015-11-11 06:12:35 -0700204 }
205}
206
Eric Newberryc64d30a2015-12-26 11:07:27 -0700207BOOST_AUTO_TEST_CASE(NoExpirationTime)
208{
Davide Pesavento32065652017-01-15 01:52:21 -0500209 auto transport = make_unique<DummyTransport>();
Eric Newberryc64d30a2015-12-26 11:07:27 -0700210
211 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
212}
213
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400214class DummyTransportFixture : public nfd::tests::GlobalIoFixture
Davide Pesavento32065652017-01-15 01:52:21 -0500215{
216protected:
217 DummyTransportFixture()
218 : transport(nullptr)
219 , sentPackets(nullptr)
220 , receivedPackets(nullptr)
221 {
222 // Constructor does not initialize the fixture,
223 // so that test case may specify different parameters to DummyTransport constructor.
224 }
225
226 void
227 initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
228 {
229 this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(transport));
230 this->transport = static_cast<DummyTransport*>(face->getTransport());
231 this->sentPackets = &this->transport->sentPackets;
232 this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
233 }
234
235protected:
236 unique_ptr<nfd::Face> face;
237 DummyTransport* transport;
238 std::vector<Transport::Packet>* sentPackets;
239 std::vector<Transport::Packet>* receivedPackets;
240};
241
242BOOST_FIXTURE_TEST_CASE(Send, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700243{
244 this->initialize();
245
246 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
247 transport->send(Transport::Packet(Block(pkt1)));
248
249 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
250 transport->send(Transport::Packet(Block(pkt2)));
251
252 transport->setState(TransportState::DOWN);
253 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
254 transport->send(Transport::Packet(Block(pkt3)));
255
256 transport->setState(TransportState::CLOSING);
257 Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
258 transport->send(Transport::Packet(Block(pkt4)));
259
260 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
261 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size());
262 BOOST_REQUIRE_EQUAL(sentPackets->size(), 3);
263 BOOST_CHECK(sentPackets->at(0).packet == pkt1);
264 BOOST_CHECK(sentPackets->at(1).packet == pkt2);
265 BOOST_CHECK(sentPackets->at(2).packet == pkt3);
266}
267
Davide Pesavento32065652017-01-15 01:52:21 -0500268BOOST_FIXTURE_TEST_CASE(Receive, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700269{
270 this->initialize();
271
272 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
273 transport->receivePacket(pkt1);
274
275 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
276 transport->receivePacket(pkt2);
277
278 transport->setState(TransportState::DOWN);
279 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
280 transport->receivePacket(pkt3);
281
282 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3);
283 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size());
284 BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3);
285 BOOST_CHECK(receivedPackets->at(0).packet == pkt1);
286 BOOST_CHECK(receivedPackets->at(1).packet == pkt2);
287 BOOST_CHECK(receivedPackets->at(2).packet == pkt3);
288}
289
290BOOST_AUTO_TEST_SUITE_END() // TestTransport
291BOOST_AUTO_TEST_SUITE_END() // Face
292
293} // namespace tests
294} // namespace face
295} // namespace nfd