blob: 747622e287e239d5f6c67a3e7f3c22819a155273 [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;
Davide Pesavento14e71f02019-03-28 17:35:25 -040048using namespace nfd::tests;
Junxiao Shicde37ad2015-12-24 01:02:05 -070049
Davide Pesavento97210d52016-10-14 15:45:48 +020050BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento32065652017-01-15 01:52:21 -050051BOOST_AUTO_TEST_SUITE(TestTransport)
Junxiao Shi57df2882015-11-11 06:12:35 -070052
53BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
54{
Davide Pesavento32065652017-01-15 01:52:21 -050055 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -070056 checkStaticPropertiesInitialized(*transport);
57}
58
Davide Pesavento32065652017-01-15 01:52:21 -050059class PersistencyTestTransport : public DummyTransport
60{
61public:
62 PersistencyTestTransport()
63 : DummyTransport("dummy://", "dummy://",
64 ndn::nfd::FACE_SCOPE_NON_LOCAL,
65 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND)
66 {
67 }
68
69protected:
70 void
71 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
72 {
73 persistencyHistory.push_back(oldPersistency);
74 }
75
76public:
77 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
78};
79
80BOOST_AUTO_TEST_CASE(PersistencyChange)
81{
82 auto transport = make_unique<PersistencyTestTransport>();
83 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
84 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
85
86 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_NONE), false);
87 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(transport->getPersistency()), true);
88 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
89
90 transport->setPersistency(transport->getPersistency());
91 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
92 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
93
94 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
95 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
96 BOOST_REQUIRE_EQUAL(transport->persistencyHistory.size(), 1);
97 BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
98}
99
Junxiao Shi57df2882015-11-11 06:12:35 -0700100/** \brief a macro to declare a TransportState as a integral constant
Davide Pesavento45898d22018-04-17 21:59:54 -0400101 * \note we cannot use mpl::integral_c because TransportState is not an integral type
Junxiao Shi57df2882015-11-11 06:12:35 -0700102 */
103#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
104
105/** \brief a map from every TransportState to a valid state transition sequence
106 * for entering this state from UP
107 */
108typedef mpl::map<
109 mpl::pair<TRANSPORT_STATE_C(UP),
110 mpl::vector<>>,
111 mpl::pair<TRANSPORT_STATE_C(DOWN),
112 mpl::vector<
113 TRANSPORT_STATE_C(DOWN)
114 >>,
115 mpl::pair<TRANSPORT_STATE_C(CLOSING),
116 mpl::vector<
117 TRANSPORT_STATE_C(CLOSING)
118 >>,
119 mpl::pair<TRANSPORT_STATE_C(FAILED),
120 mpl::vector<
121 TRANSPORT_STATE_C(FAILED)
122 >>,
123 mpl::pair<TRANSPORT_STATE_C(CLOSED),
124 mpl::vector<
125 TRANSPORT_STATE_C(CLOSING),
126 TRANSPORT_STATE_C(CLOSED)
127 >>
128> StateEntering;
129
130/** \brief a sequence of all valid TransportStates
131 */
132typedef mpl::fold<StateEntering,
133 mpl::vector<>,
134 mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
135>::type States;
136
137/** \brief a set of all valid state transitions
138 */
139typedef mpl::set<
140 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
141 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
142 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
143 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
144 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
145 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
146 mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
147 mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
148> ValidStateTransitions;
149
Davide Pesavento45898d22018-04-17 21:59:54 -0400150/** \brief a metafunction to generate a sequence of all state transitions
Junxiao Shi57df2882015-11-11 06:12:35 -0700151 * from a specified state
152 */
Davide Pesavento45898d22018-04-17 21:59:54 -0400153template<typename FromState, typename Result>
154struct StateTransitionsFrom : mpl::fold<
155 States,
156 Result,
157 mpl::push_back<mpl::_1, mpl::pair<FromState, mpl::_2>>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700158{
Junxiao Shi57df2882015-11-11 06:12:35 -0700159};
160
161/** \brief a sequence of all state transitions
162 */
163typedef mpl::fold<
164 States,
Davide Pesavento45898d22018-04-17 21:59:54 -0400165 mpl::vector<>,
166 mpl::lambda<StateTransitionsFrom<mpl::_2, mpl::_1>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700167>::type AllStateTransitions;
168
169#undef TRANSPORT_STATE_C
170
171BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
172{
Davide Pesavento32065652017-01-15 01:52:21 -0500173 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -0700174
175 TransportState from = static_cast<TransportState>(T::first::value);
176 TransportState to = static_cast<TransportState>(T::second::value);
177 BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
178
179 // enter from state
Davide Pesavento32065652017-01-15 01:52:21 -0500180 using Steps = typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type;
181 mpl::for_each<Steps>([&transport] (int state) {
182 transport->setState(static_cast<TransportState>(state));
183 });
Junxiao Shi57df2882015-11-11 06:12:35 -0700184 BOOST_REQUIRE_EQUAL(transport->getState(), from);
185
186 bool hasSignal = false;
Davide Pesavento32065652017-01-15 01:52:21 -0500187 transport->afterStateChange.connect(
Junxiao Shi57df2882015-11-11 06:12:35 -0700188 [from, to, &hasSignal] (TransportState oldState, TransportState newState) {
189 hasSignal = true;
190 BOOST_CHECK_EQUAL(oldState, from);
191 BOOST_CHECK_EQUAL(newState, to);
192 });
193
194 // do transition
195 bool isValid = from == to ||
196 mpl::has_key<ValidStateTransitions,
197 mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
198 >::value;
199 if (isValid) {
200 BOOST_REQUIRE_NO_THROW(transport->setState(to));
201 BOOST_CHECK_EQUAL(hasSignal, from != to);
202 }
203 else {
Davide Pesavento32065652017-01-15 01:52:21 -0500204 BOOST_CHECK_THROW(transport->setState(to), std::runtime_error);
Junxiao Shi57df2882015-11-11 06:12:35 -0700205 }
206}
207
Eric Newberryc64d30a2015-12-26 11:07:27 -0700208BOOST_AUTO_TEST_CASE(NoExpirationTime)
209{
Davide Pesavento32065652017-01-15 01:52:21 -0500210 auto transport = make_unique<DummyTransport>();
Eric Newberryc64d30a2015-12-26 11:07:27 -0700211
212 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
213}
214
Davide Pesavento14e71f02019-03-28 17:35:25 -0400215class DummyTransportFixture : public GlobalIoFixture
Davide Pesavento32065652017-01-15 01:52:21 -0500216{
217protected:
218 DummyTransportFixture()
219 : transport(nullptr)
220 , sentPackets(nullptr)
221 , receivedPackets(nullptr)
222 {
223 // Constructor does not initialize the fixture,
224 // so that test case may specify different parameters to DummyTransport constructor.
225 }
226
227 void
228 initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
229 {
230 this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(transport));
231 this->transport = static_cast<DummyTransport*>(face->getTransport());
232 this->sentPackets = &this->transport->sentPackets;
233 this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
234 }
235
236protected:
237 unique_ptr<nfd::Face> face;
238 DummyTransport* transport;
239 std::vector<Transport::Packet>* sentPackets;
240 std::vector<Transport::Packet>* receivedPackets;
241};
242
243BOOST_FIXTURE_TEST_CASE(Send, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700244{
245 this->initialize();
246
247 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
248 transport->send(Transport::Packet(Block(pkt1)));
249
250 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
251 transport->send(Transport::Packet(Block(pkt2)));
252
253 transport->setState(TransportState::DOWN);
254 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
255 transport->send(Transport::Packet(Block(pkt3)));
256
257 transport->setState(TransportState::CLOSING);
258 Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
259 transport->send(Transport::Packet(Block(pkt4)));
260
261 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
262 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size());
263 BOOST_REQUIRE_EQUAL(sentPackets->size(), 3);
264 BOOST_CHECK(sentPackets->at(0).packet == pkt1);
265 BOOST_CHECK(sentPackets->at(1).packet == pkt2);
266 BOOST_CHECK(sentPackets->at(2).packet == pkt3);
267}
268
Davide Pesavento32065652017-01-15 01:52:21 -0500269BOOST_FIXTURE_TEST_CASE(Receive, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700270{
271 this->initialize();
272
273 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
274 transport->receivePacket(pkt1);
275
276 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
277 transport->receivePacket(pkt2);
278
279 transport->setState(TransportState::DOWN);
280 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
281 transport->receivePacket(pkt3);
282
283 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3);
284 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size());
285 BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3);
286 BOOST_CHECK(receivedPackets->at(0).packet == pkt1);
287 BOOST_CHECK(receivedPackets->at(1).packet == pkt2);
288 BOOST_CHECK(receivedPackets->at(2).packet == pkt3);
289}
290
291BOOST_AUTO_TEST_SUITE_END() // TestTransport
292BOOST_AUTO_TEST_SUITE_END() // Face
293
294} // namespace tests
295} // namespace face
296} // namespace nfd