blob: 81898599d8befb6fa926800f04496c536b99a3cc [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"
27#include "face/lp-face.hpp"
28#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
48class DummyTransportFixture : public BaseFixture
49{
50public:
51 DummyTransportFixture()
52 : transport(nullptr)
53 , sentPackets(nullptr)
54 , receivedPackets(nullptr)
55 {
56 // Constructor does not initialize the fixture,
57 // so that test case may specify different parameters to DummyTransport constructor.
58 }
59
60 void
61 initialize(unique_ptr<DummyTransport> transport = make_unique<DummyTransport>())
62 {
63 this->face = make_unique<LpFace>(make_unique<DummyReceiveLinkService>(), std::move(transport));
64 this->transport = static_cast<DummyTransport*>(face->getTransport());
65 this->sentPackets = &this->transport->sentPackets;
66 this->receivedPackets = &static_cast<DummyReceiveLinkService*>(face->getLinkService())->receivedPackets;
67 }
68
69public:
70 unique_ptr<LpFace> face;
71 DummyTransport* transport;
72 std::vector<Transport::Packet>* sentPackets;
73 std::vector<Transport::Packet>* receivedPackets;
74};
75
76BOOST_FIXTURE_TEST_SUITE(TestTransport, DummyTransportFixture)
77
78BOOST_AUTO_TEST_CASE(DummyTransportStaticProperties)
79{
80 this->initialize();
81 checkStaticPropertiesInitialized(*transport);
82}
83
84BOOST_AUTO_TEST_SUITE(StateTransition)
85
86namespace mpl = boost::mpl;
87
88/** \brief a macro to declare a TransportState as a integral constant
89 */
90#define TRANSPORT_STATE_C(X) mpl::int_<static_cast<int>(TransportState::X)>
91
92/** \brief a map from every TransportState to a valid state transition sequence
93 * for entering this state from UP
94 */
95typedef mpl::map<
96 mpl::pair<TRANSPORT_STATE_C(UP),
97 mpl::vector<>>,
98 mpl::pair<TRANSPORT_STATE_C(DOWN),
99 mpl::vector<
100 TRANSPORT_STATE_C(DOWN)
101 >>,
102 mpl::pair<TRANSPORT_STATE_C(CLOSING),
103 mpl::vector<
104 TRANSPORT_STATE_C(CLOSING)
105 >>,
106 mpl::pair<TRANSPORT_STATE_C(FAILED),
107 mpl::vector<
108 TRANSPORT_STATE_C(FAILED)
109 >>,
110 mpl::pair<TRANSPORT_STATE_C(CLOSED),
111 mpl::vector<
112 TRANSPORT_STATE_C(CLOSING),
113 TRANSPORT_STATE_C(CLOSED)
114 >>
115> StateEntering;
116
117/** \brief a sequence of all valid TransportStates
118 */
119typedef mpl::fold<StateEntering,
120 mpl::vector<>,
121 mpl::push_back<mpl::_1, mpl::first<mpl::_2>>
122>::type States;
123
124/** \brief a set of all valid state transitions
125 */
126typedef mpl::set<
127 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(DOWN)>,
128 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(UP)>,
129 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(CLOSING)>,
130 mpl::pair<TRANSPORT_STATE_C(UP), TRANSPORT_STATE_C(FAILED)>,
131 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(CLOSING)>,
132 mpl::pair<TRANSPORT_STATE_C(DOWN), TRANSPORT_STATE_C(FAILED)>,
133 mpl::pair<TRANSPORT_STATE_C(CLOSING), TRANSPORT_STATE_C(CLOSED)>,
134 mpl::pair<TRANSPORT_STATE_C(FAILED), TRANSPORT_STATE_C(CLOSED)>
135> ValidStateTransitions;
136
137/** \brief a metafunction class to generate a sequence of all state transitions
138 * from a specified state
139 */
140struct StateTransitionsFrom
141{
142 template<typename FROM>
143 struct apply
144 {
145 typedef typename mpl::fold<
146 States,
147 mpl::vector<>,
148 mpl::push_back<mpl::_1, mpl::pair<FROM, mpl::_2>>
149 >::type type;
150 };
151};
152
153/** \brief a sequence of all state transitions
154 */
155typedef mpl::fold<
156 States,
157 mpl::empty_sequence,
158 mpl::joint_view<mpl::_1, mpl::apply<StateTransitionsFrom, mpl::protect<mpl::_1>>::type>
159>::type AllStateTransitions;
160
161#undef TRANSPORT_STATE_C
162
163BOOST_AUTO_TEST_CASE_TEMPLATE(SetState, T, AllStateTransitions)
164{
165 this->initialize();
166
167 TransportState from = static_cast<TransportState>(T::first::value);
168 TransportState to = static_cast<TransportState>(T::second::value);
169 BOOST_TEST_MESSAGE("SetState " << from << " -> " << to);
170
171 // enter from state
172 typedef typename mpl::at<StateEntering, mpl::int_<T::first::value>>::type Steps;
173 mpl::for_each<Steps>(
174 [this] (int state) { transport->setState(static_cast<TransportState>(state)); });
175 BOOST_REQUIRE_EQUAL(transport->getState(), from);
176
177 bool hasSignal = false;
178 this->transport->afterStateChange.connect(
179 [from, to, &hasSignal] (TransportState oldState, TransportState newState) {
180 hasSignal = true;
181 BOOST_CHECK_EQUAL(oldState, from);
182 BOOST_CHECK_EQUAL(newState, to);
183 });
184
185 // do transition
186 bool isValid = from == to ||
187 mpl::has_key<ValidStateTransitions,
188 mpl::pair<mpl::int_<T::first::value>, mpl::int_<T::second::value>>
189 >::value;
190 if (isValid) {
191 BOOST_REQUIRE_NO_THROW(transport->setState(to));
192 BOOST_CHECK_EQUAL(hasSignal, from != to);
193 }
194 else {
195 BOOST_CHECK_THROW(this->transport->setState(to), std::runtime_error);
196 }
197}
198
199BOOST_AUTO_TEST_SUITE_END() // StateTransition
200
201BOOST_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