blob: f1171e5edc75251b19ba21ae6dba4bf6153f40f6 [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento97210d52016-10-14 15:45:48 +02003 * Copyright (c) 2014-2016, 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
32#include <boost/mpl/empty_sequence.hpp>
33#include <boost/mpl/int.hpp>
34#include <boost/mpl/joint_view.hpp>
35#include <boost/mpl/map.hpp>
36#include <boost/mpl/set.hpp>
37#include <boost/mpl/transform_view.hpp>
38
Junxiao Shi57df2882015-11-11 06:12:35 -070039namespace nfd {
40namespace face {
41namespace tests {
42
43using namespace nfd::tests;
Davide Pesavento97210d52016-10-14 15:45:48 +020044namespace mpl = boost::mpl;
Junxiao Shicde37ad2015-12-24 01:02:05 -070045
Junxiao Shi57df2882015-11-11 06:12:35 -070046class DummyTransportFixture : public BaseFixture
47{
Davide Pesavento97210d52016-10-14 15:45:48 +020048protected:
Junxiao Shi57df2882015-11-11 06:12:35 -070049 DummyTransportFixture()
50 : transport(nullptr)
51 , sentPackets(nullptr)
52 , receivedPackets(nullptr)
53 {
54 // Constructor does not initialize the fixture,
55 // so that test case may specify different parameters to DummyTransport constructor.
56 }
57
58 void
59 initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
60 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070061 this->face = make_unique<Face>(make_unique<DummyReceiveLinkService>(), std::move(transport));
Junxiao Shi57df2882015-11-11 06:12:35 -070062 this->transport = static_cast<DummyTransport*>(face->getTransport());
63 this->sentPackets = &this->transport->sentPackets;
64 this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
65 }
66
Davide Pesavento97210d52016-10-14 15:45:48 +020067protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070068 unique_ptr<Face> face;
Junxiao Shi57df2882015-11-11 06:12:35 -070069 DummyTransport* transport;
70 std::vector<Transport::Packet>* sentPackets;
71 std::vector<Transport::Packet>* receivedPackets;
72};
73
Davide Pesavento97210d52016-10-14 15:45:48 +020074BOOST_AUTO_TEST_SUITE(Face)
Junxiao Shi57df2882015-11-11 06:12:35 -070075BOOST_FIXTURE_TEST_SUITE(TestTransport, DummyTransportFixture)
76
77BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
78{
79 this->initialize();
80 checkStaticPropertiesInitialized(*transport);
81}
82
Junxiao Shi57df2882015-11-11 06:12:35 -070083/** \brief a macro to declare a TransportState as a integral constant
84 */
85#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
86
87/** \brief a map from every TransportState to a valid state transition sequence
88 * for entering this state from UP
89 */
90typedef mpl::map<
91 mpl::pair<TRANSPORT_STATE_C(UP),
92 mpl::vector<>>,
93 mpl::pair<TRANSPORT_STATE_C(DOWN),
94 mpl::vector<
95 TRANSPORT_STATE_C(DOWN)
96 >>,
97 mpl::pair<TRANSPORT_STATE_C(CLOSING),
98 mpl::vector<
99 TRANSPORT_STATE_C(CLOSING)
100 >>,
101 mpl::pair<TRANSPORT_STATE_C(FAILED),
102 mpl::vector<
103 TRANSPORT_STATE_C(FAILED)
104 >>,
105 mpl::pair<TRANSPORT_STATE_C(CLOSED),
106 mpl::vector<
107 TRANSPORT_STATE_C(CLOSING),
108 TRANSPORT_STATE_C(CLOSED)
109 >>
110> StateEntering;
111
112/** \brief a sequence of all valid TransportStates
113 */
114typedef mpl::fold<StateEntering,
115 mpl::vector<>,
116 mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
117>::type States;
118
119/** \brief a set of all valid state transitions
120 */
121typedef mpl::set<
122 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
123 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
124 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
125 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
126 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
127 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
128 mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
129 mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
130> ValidStateTransitions;
131
132/** \brief a metafunction class to generate a sequence of all state transitions
133 * from a specified state
134 */
135struct StateTransitionsFrom
136{
137 template<typename FROM>
138 struct apply
139 {
140 typedef typename mpl::fold<
141 States,
142 mpl::vector<>,
143 mpl::push_back<mpl::_1, mpl::pair<FROM, mpl::_2>>
144 >::type type;
145 };
146};
147
148/** \brief a sequence of all state transitions
149 */
150typedef mpl::fold<
151 States,
152 mpl::empty_sequence,
153 mpl::joint_view<mpl::_1, mpl::apply<StateTransitionsFrom, mpl::protect<mpl::_1>>::type>
154>::type AllStateTransitions;
155
156#undef TRANSPORT_STATE_C
157
158BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
159{
160 this->initialize();
161
162 TransportState from = static_cast<TransportState>(T::first::value);
163 TransportState to = static_cast<TransportState>(T::second::value);
164 BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
165
166 // enter from state
167 typedef typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type Steps;
168 mpl::for_each<Steps>(
169 [this] (int state) { transport->setState(static_cast<TransportState>(state)); });
170 BOOST_REQUIRE_EQUAL(transport->getState(), from);
171
172 bool hasSignal = false;
173 this->transport->afterStateChange.connect(
174 [from, to, &hasSignal] (TransportState oldState, TransportState newState) {
175 hasSignal = true;
176 BOOST_CHECK_EQUAL(oldState, from);
177 BOOST_CHECK_EQUAL(newState, to);
178 });
179
180 // do transition
181 bool isValid = from == to ||
182 mpl::has_key<ValidStateTransitions,
183 mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
184 >::value;
185 if (isValid) {
186 BOOST_REQUIRE_NO_THROW(transport->setState(to));
187 BOOST_CHECK_EQUAL(hasSignal, from != to);
188 }
189 else {
190 BOOST_CHECK_THROW(this->transport->setState(to), std::runtime_error);
191 }
192}
193
Eric Newberryc64d30a2015-12-26 11:07:27 -0700194BOOST_AUTO_TEST_CASE(NoExpirationTime)
195{
196 initialize();
197
198 BOOST_CHECK_EQUAL(transport->getExpirationTime(), time::steady_clock::TimePoint::max());
199}
200
Junxiao Shi57df2882015-11-11 06:12:35 -0700201BOOST_AUTO_TEST_CASE(Send)
202{
203 this->initialize();
204
205 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
206 transport->send(Transport::Packet(Block(pkt1)));
207
208 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
209 transport->send(Transport::Packet(Block(pkt2)));
210
211 transport->setState(TransportState::DOWN);
212 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
213 transport->send(Transport::Packet(Block(pkt3)));
214
215 transport->setState(TransportState::CLOSING);
216 Block pkt4 = ndn::encoding::makeStringBlock(303, "ut labore et dolore magna aliqua.");
217 transport->send(Transport::Packet(Block(pkt4)));
218
219 BOOST_CHECK_EQUAL(transport->getCounters().nOutPackets, 2);
220 BOOST_CHECK_EQUAL(transport->getCounters().nOutBytes, pkt1.size() + pkt2.size());
221 BOOST_REQUIRE_EQUAL(sentPackets->size(), 3);
222 BOOST_CHECK(sentPackets->at(0).packet == pkt1);
223 BOOST_CHECK(sentPackets->at(1).packet == pkt2);
224 BOOST_CHECK(sentPackets->at(2).packet == pkt3);
225}
226
227BOOST_AUTO_TEST_CASE(Receive)
228{
229 this->initialize();
230
231 Block pkt1 = ndn::encoding::makeStringBlock(300, "Lorem ipsum dolor sit amet,");
232 transport->receivePacket(pkt1);
233
234 Block pkt2 = ndn::encoding::makeStringBlock(301, "consectetur adipiscing elit,");
235 transport->receivePacket(pkt2);
236
237 transport->setState(TransportState::DOWN);
238 Block pkt3 = ndn::encoding::makeStringBlock(302, "sed do eiusmod tempor incididunt ");
239 transport->receivePacket(pkt3);
240
241 BOOST_CHECK_EQUAL(transport->getCounters().nInPackets, 3);
242 BOOST_CHECK_EQUAL(transport->getCounters().nInBytes, pkt1.size() + pkt2.size() + pkt3.size());
243 BOOST_REQUIRE_EQUAL(receivedPackets->size(), 3);
244 BOOST_CHECK(receivedPackets->at(0).packet == pkt1);
245 BOOST_CHECK(receivedPackets->at(1).packet == pkt2);
246 BOOST_CHECK(receivedPackets->at(2).packet == pkt3);
247}
248
249BOOST_AUTO_TEST_SUITE_END() // TestTransport
250BOOST_AUTO_TEST_SUITE_END() // Face
251
252} // namespace tests
253} // namespace face
254} // namespace nfd