blob: d06195e9421cea2b03ea93445234c44c5bfa7fd9 [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/*
3 * Copyright (c) 2014-2018, 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"
Junxiao Shi57df2882015-11-11 06:12:35 -070028#include "dummy-transport.hpp"
29#include "dummy-receive-link-service.hpp"
Eric Newberry8717e872015-11-23 12:41:50 -070030#include "transport-test-common.hpp"
Junxiao Shi57df2882015-11-11 06:12:35 -070031
Davide Pesavento45898d22018-04-17 21:59:54 -040032#include <boost/mpl/fold.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070033#include <boost/mpl/int.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040034#include <boost/mpl/lambda.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070035#include <boost/mpl/map.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040036#include <boost/mpl/pair.hpp>
37#include <boost/mpl/push_back.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070038#include <boost/mpl/set.hpp>
Davide Pesavento45898d22018-04-17 21:59:54 -040039#include <boost/mpl/vector.hpp>
Junxiao Shi57df2882015-11-11 06:12:35 -070040
Junxiao Shi57df2882015-11-11 06:12:35 -070041namespace nfd {
42namespace face {
43namespace tests {
44
Davide Pesavento97210d52016-10-14 15:45:48 +020045namespace mpl = boost::mpl;
Junxiao Shicde37ad2015-12-24 01:02:05 -070046
Davide Pesavento97210d52016-10-14 15:45:48 +020047BOOST_AUTO_TEST_SUITE(Face)
Davide Pesavento32065652017-01-15 01:52:21 -050048BOOST_AUTO_TEST_SUITE(TestTransport)
Junxiao Shi57df2882015-11-11 06:12:35 -070049
50BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
51{
Davide Pesavento32065652017-01-15 01:52:21 -050052 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -070053 checkStaticPropertiesInitialized(*transport);
54}
55
Davide Pesavento32065652017-01-15 01:52:21 -050056class PersistencyTestTransport : public DummyTransport
57{
58public:
59 PersistencyTestTransport()
60 : DummyTransport("dummy://", "dummy://",
61 ndn::nfd::FACE_SCOPE_NON_LOCAL,
62 ndn::nfd::FACE_PERSISTENCY_ON_DEMAND)
63 {
64 }
65
66protected:
67 void
68 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency) final
69 {
70 persistencyHistory.push_back(oldPersistency);
71 }
72
73public:
74 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
75};
76
77BOOST_AUTO_TEST_CASE(PersistencyChange)
78{
79 auto transport = make_unique<PersistencyTestTransport>();
80 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
81 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
82
83 BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_NONE), false);
84 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(transport->getPersistency()), true);
85 BOOST_REQUIRE_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true);
86
87 transport->setPersistency(transport->getPersistency());
88 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
89 BOOST_CHECK_EQUAL(transport->persistencyHistory.size(), 0);
90
91 transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
92 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
93 BOOST_REQUIRE_EQUAL(transport->persistencyHistory.size(), 1);
94 BOOST_CHECK_EQUAL(transport->persistencyHistory.back(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
95}
96
Junxiao Shi57df2882015-11-11 06:12:35 -070097/** \brief a macro to declare a TransportState as a integral constant
Davide Pesavento45898d22018-04-17 21:59:54 -040098 * \note we cannot use mpl::integral_c because TransportState is not an integral type
Junxiao Shi57df2882015-11-11 06:12:35 -070099 */
100#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
101
102/** \brief a map from every TransportState to a valid state transition sequence
103 * for entering this state from UP
104 */
105typedef mpl::map<
106 mpl::pair<TRANSPORT_STATE_C(UP),
107 mpl::vector<>>,
108 mpl::pair<TRANSPORT_STATE_C(DOWN),
109 mpl::vector<
110 TRANSPORT_STATE_C(DOWN)
111 >>,
112 mpl::pair<TRANSPORT_STATE_C(CLOSING),
113 mpl::vector<
114 TRANSPORT_STATE_C(CLOSING)
115 >>,
116 mpl::pair<TRANSPORT_STATE_C(FAILED),
117 mpl::vector<
118 TRANSPORT_STATE_C(FAILED)
119 >>,
120 mpl::pair<TRANSPORT_STATE_C(CLOSED),
121 mpl::vector<
122 TRANSPORT_STATE_C(CLOSING),
123 TRANSPORT_STATE_C(CLOSED)
124 >>
125> StateEntering;
126
127/** \brief a sequence of all valid TransportStates
128 */
129typedef mpl::fold<StateEntering,
130 mpl::vector<>,
131 mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
132>::type States;
133
134/** \brief a set of all valid state transitions
135 */
136typedef mpl::set<
137 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
138 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
139 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
140 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
141 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
142 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
143 mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
144 mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
145> ValidStateTransitions;
146
Davide Pesavento45898d22018-04-17 21:59:54 -0400147/** \brief a metafunction to generate a sequence of all state transitions
Junxiao Shi57df2882015-11-11 06:12:35 -0700148 * from a specified state
149 */
Davide Pesavento45898d22018-04-17 21:59:54 -0400150template<typename FromState, typename Result>
151struct StateTransitionsFrom : mpl::fold<
152 States,
153 Result,
154 mpl::push_back<mpl::_1, mpl::pair<FromState, mpl::_2>>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700155{
Junxiao Shi57df2882015-11-11 06:12:35 -0700156};
157
158/** \brief a sequence of all state transitions
159 */
160typedef mpl::fold<
161 States,
Davide Pesavento45898d22018-04-17 21:59:54 -0400162 mpl::vector<>,
163 mpl::lambda<StateTransitionsFrom<mpl::_2, mpl::_1>>
Junxiao Shi57df2882015-11-11 06:12:35 -0700164>::type AllStateTransitions;
165
166#undef TRANSPORT_STATE_C
167
168BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
169{
Davide Pesavento32065652017-01-15 01:52:21 -0500170 auto transport = make_unique<DummyTransport>();
Junxiao Shi57df2882015-11-11 06:12:35 -0700171
172 TransportState from = static_cast<TransportState>(T::first::value);
173 TransportState to = static_cast<TransportState>(T::second::value);
174 BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
175
176 // enter from state
Davide Pesavento32065652017-01-15 01:52:21 -0500177 using Steps = typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type;
178 mpl::for_each<Steps>([&transport] (int state) {
179 transport->setState(static_cast<TransportState>(state));
180 });
Junxiao Shi57df2882015-11-11 06:12:35 -0700181 BOOST_REQUIRE_EQUAL(transport->getState(), from);
182
183 bool hasSignal = false;
Davide Pesavento32065652017-01-15 01:52:21 -0500184 transport->afterStateChange.connect(
Junxiao Shi57df2882015-11-11 06:12:35 -0700185 [from, to, &hasSignal] (TransportState oldState, TransportState newState) {
186 hasSignal = true;
187 BOOST_CHECK_EQUAL(oldState, from);
188 BOOST_CHECK_EQUAL(newState, to);
189 });
190
191 // do transition
192 bool isValid = from == to ||
193 mpl::has_key<ValidStateTransitions,
194 mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
195 >::value;
196 if (isValid) {
197 BOOST_REQUIRE_NO_THROW(transport->setState(to));
198 BOOST_CHECK_EQUAL(hasSignal, from != to);
199 }
200 else {
Davide Pesavento32065652017-01-15 01:52:21 -0500201 BOOST_CHECK_THROW(transport->setState(to), std::runtime_error);
Junxiao Shi57df2882015-11-11 06:12:35 -0700202 }
203}
204
Eric Newberryc64d30a2015-12-26 11:07:27 -0700205BOOST_AUTO_TEST_CASE(NoExpirationTime)
206{
Davide Pesavento32065652017-01-15 01:52:21 -0500207 auto transport = make_unique<DummyTransport>();
Eric Newberryc64d30a2015-12-26 11:07:27 -0700208
209 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
210}
211
Davide Pesavento32065652017-01-15 01:52:21 -0500212class DummyTransportFixture : public nfd::tests::BaseFixture
213{
214protected:
215 DummyTransportFixture()
216 : transport(nullptr)
217 , sentPackets(nullptr)
218 , receivedPackets(nullptr)
219 {
220 // Constructor does not initialize the fixture,
221 // so that test case may specify different parameters to DummyTransport constructor.
222 }
223
224 void
225 initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
226 {
227 this->face = make_unique<nfd::Face>(make_unique<DummyReceiveLinkService>(), std::move(transport));
228 this->transport = static_cast<DummyTransport*>(face->getTransport());
229 this->sentPackets = &this->transport->sentPackets;
230 this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
231 }
232
233protected:
234 unique_ptr<nfd::Face> face;
235 DummyTransport* transport;
236 std::vector<Transport::Packet>* sentPackets;
237 std::vector<Transport::Packet>* receivedPackets;
238};
239
240BOOST_FIXTURE_TEST_CASE(Send, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700241{
242 this->initialize();
243
244 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
245 transport->send(Transport::Packet(Block(pkt1)));
246
247 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
248 transport->send(Transport::Packet(Block(pkt2)));
249
250 transport->setState(TransportState::DOWN);
251 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
252 transport->send(Transport::Packet(Block(pkt3)));
253
254 transport->setState(TransportState::CLOSING);
255 Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
256 transport->send(Transport::Packet(Block(pkt4)));
257
258 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
259 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size());
260 BOOST_REQUIRE_EQUAL(sentPackets->size(), 3);
261 BOOST_CHECK(sentPackets->at(0).packet == pkt1);
262 BOOST_CHECK(sentPackets->at(1).packet == pkt2);
263 BOOST_CHECK(sentPackets->at(2).packet == pkt3);
264}
265
Davide Pesavento32065652017-01-15 01:52:21 -0500266BOOST_FIXTURE_TEST_CASE(Receive, DummyTransportFixture)
Junxiao Shi57df2882015-11-11 06:12:35 -0700267{
268 this->initialize();
269
270 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
271 transport->receivePacket(pkt1);
272
273 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
274 transport->receivePacket(pkt2);
275
276 transport->setState(TransportState::DOWN);
277 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
278 transport->receivePacket(pkt3);
279
280 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3);
281 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size());
282 BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3);
283 BOOST_CHECK(receivedPackets->at(0).packet == pkt1);
284 BOOST_CHECK(receivedPackets->at(1).packet == pkt2);
285 BOOST_CHECK(receivedPackets->at(2).packet == pkt3);
286}
287
288BOOST_AUTO_TEST_SUITE_END() // TestTransport
289BOOST_AUTO_TEST_SUITE_END() // Face
290
291} // namespace tests
292} // namespace face
293} // namespace nfd