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