A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-client-server-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18 * <amine.ismail@udcast.com>
19 */
20
21#include "ns3/abort.h"
22#include "ns3/config.h"
23#include "ns3/inet-socket-address.h"
24#include "ns3/internet-stack-helper.h"
25#include "ns3/ipv4-address-helper.h"
26#include "ns3/log.h"
27#include "ns3/simple-channel.h"
28#include "ns3/simple-net-device.h"
29#include "ns3/simulator.h"
30#include "ns3/string.h"
31#include "ns3/test.h"
32#include "ns3/udp-client-server-helper.h"
33#include "ns3/udp-echo-helper.h"
34#include "ns3/udp-server.h"
35#include "ns3/uinteger.h"
36
37#include <fstream>
38
39using namespace ns3;
40
41/**
42 * \ingroup applications
43 * \defgroup applications-test applications module tests
44 */
45
46/**
47 * \ingroup applications-test
48 * \ingroup tests
49 *
50 * Test that all the UDP packets generated by an UdpClient application are
51 * correctly received by an UdpServer application
52 */
54{
55 public:
57 ~UdpClientServerTestCase() override;
58
59 private:
60 void DoRun() override;
61};
62
64 : TestCase("Test that all the udp packets generated by an udpClient application are correctly "
65 "received by an udpServer application")
66{
67}
68
70{
71}
72
73void
75{
77 n.Create(2);
78
79 InternetStackHelper internet;
80 internet.Install(n);
81
82 // link the two nodes
83 Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
84 Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
85 n.Get(0)->AddDevice(txDev);
86 n.Get(1)->AddDevice(rxDev);
87 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
88 rxDev->SetChannel(channel1);
89 txDev->SetChannel(channel1);
91 d.Add(txDev);
92 d.Add(rxDev);
93
95
96 ipv4.SetBase("10.1.1.0", "255.255.255.0");
97 Ipv4InterfaceContainer i = ipv4.Assign(d);
98
99 uint16_t port = 4000;
100 UdpServerHelper serverHelper(port);
101 auto serverApp = serverHelper.Install(n.Get(1));
102 serverApp.Start(Seconds(1.0));
103 serverApp.Stop(Seconds(10.0));
104
105 uint32_t MaxPacketSize = 1024;
106 Time interPacketInterval = Seconds(1.);
107 uint32_t maxPacketCount = 10;
108 UdpClientHelper clientHelper(i.GetAddress(1), port);
109 clientHelper.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
110 clientHelper.SetAttribute("Interval", TimeValue(interPacketInterval));
111 clientHelper.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
112 auto clientApp = clientHelper.Install(n.Get(0));
113 clientApp.Start(Seconds(2.0));
114 clientApp.Stop(Seconds(10.0));
115
118
119 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
120 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
121 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(), 8, "Did not receive expected number of packets !");
122}
123
124/**
125 * Test that all the udp packets generated by an udpTraceClient application are
126 * correctly received by an udpServer application
127 */
128
130{
131 public:
134
135 private:
136 void DoRun() override;
137};
138
140 : TestCase("Test that all the udp packets generated by an udpTraceClient application are "
141 "correctly received by an udpServer application")
142{
143}
144
146{
147}
148
149void
151{
153 n.Create(2);
154
155 InternetStackHelper internet;
156 internet.Install(n);
157
158 // link the two nodes
159 Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
160 Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
161 n.Get(0)->AddDevice(txDev);
162 n.Get(1)->AddDevice(rxDev);
163 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
164 rxDev->SetChannel(channel1);
165 txDev->SetChannel(channel1);
167 d.Add(txDev);
168 d.Add(rxDev);
169
171 ipv4.SetBase("10.1.1.0", "255.255.255.0");
172 Ipv4InterfaceContainer i = ipv4.Assign(d);
173
174 uint16_t port = 4000;
175 UdpServerHelper serverHelper(port);
176 auto serverApp = serverHelper.Install(n.Get(1));
177 serverApp.Start(Seconds(1.0));
178 serverApp.Stop(Seconds(10.0));
179
180 uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
181 UdpTraceClientHelper clientHelper(i.GetAddress(1), port);
182 clientHelper.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
183 auto clientApp = clientHelper.Install(n.Get(0));
184 clientApp.Start(Seconds(2.0));
185 clientApp.Stop(Seconds(10.0));
186
189
190 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
191 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
192 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(),
193 247,
194 "Did not receive expected number of packets !");
195}
196
197/**
198 * Test that all the PacketLossCounter class checks loss correctly in different cases
199 */
200
202{
203 public:
206
207 private:
208 void DoRun() override;
209};
210
212 : TestCase("Test that all the PacketLossCounter class checks loss correctly in different cases")
213{
214}
215
217{
218}
219
220void
222{
223 PacketLossCounter lossCounter(32);
224 lossCounter.NotifyReceived(32); // out of order
225 for (uint32_t i = 0; i < 64; i++)
226 {
227 lossCounter.NotifyReceived(i);
228 }
229
230 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 0, "Check that 0 packets are lost");
231
232 for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
233 {
234 lossCounter.NotifyReceived(i);
235 }
236 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 1, "Check that 1 packet is lost");
237
238 for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
239 {
240 lossCounter.NotifyReceived(i);
241 }
242 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 7, "Check that 7 (6+1) packets are lost");
243
244 // reordering without loss
245 lossCounter.NotifyReceived(205);
246 lossCounter.NotifyReceived(206);
247 lossCounter.NotifyReceived(207);
248 lossCounter.NotifyReceived(200);
249 lossCounter.NotifyReceived(201);
250 lossCounter.NotifyReceived(202);
251 lossCounter.NotifyReceived(203);
252 lossCounter.NotifyReceived(204);
253 for (uint32_t i = 205; i < 250; i++)
254 {
255 lossCounter.NotifyReceived(i);
256 }
257 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(),
258 7,
259 "Check that 7 (6+1) packets are lost even when reordering happens");
260
261 // reordering with loss
262 lossCounter.NotifyReceived(255);
263 // drop (2) seqNum 250, 251
264 lossCounter.NotifyReceived(252);
265 lossCounter.NotifyReceived(253);
266 lossCounter.NotifyReceived(254);
267 for (uint32_t i = 256; i < 300; i++)
268 {
269 lossCounter.NotifyReceived(i);
270 }
271 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 9, "Check that 9 (6+1+2) packet are lost");
272}
273
274/**
275 * Test fix for \bugid{1378}
276 */
277
279{
280 public:
283
284 private:
285 void DoRun() override;
286};
287
289 : TestCase("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
290{
291}
292
294{
295}
296
297void
299{
301 nodes.Create(2);
302
303 InternetStackHelper internet;
304 internet.Install(nodes);
305
306 Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
307 Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
308 nodes.Get(0)->AddDevice(txDev);
309 nodes.Get(1)->AddDevice(rxDev);
310 Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
311 rxDev->SetChannel(channel1);
312 txDev->SetChannel(channel1);
314 d.Add(txDev);
315 d.Add(rxDev);
316
318
319 ipv4.SetBase("10.1.1.0", "255.255.255.0");
320 Ipv4InterfaceContainer interfaces = ipv4.Assign(d);
321
322 uint16_t port = 5000;
323 UdpEchoServerHelper echoServer(port);
324 ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
325 serverApps.Start(Seconds(1.0));
326 serverApps.Stop(Seconds(10.0));
327 UdpEchoClientHelper echoClient(interfaces.GetAddress(1), port);
328 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
329 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
330 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
331
332 ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
333
334 uint8_t array[64];
335 uint8_t i;
336 for (i = 0; i < 64; i++)
337 {
338 array[i] = i;
339 }
340 echoClient.SetFill(clientApps.Get(0), &(array[0]), (uint32_t)64, (uint32_t)64);
341
342 clientApps.Start(Seconds(2.0));
343 clientApps.Stop(Seconds(10.0));
344
347}
348
349/**
350 * \ingroup applications-test
351 * \ingroup tests
352 *
353 * \brief UDP client and server TestSuite
354 */
356{
357 public:
359};
360
362 : TestSuite("applications-udp-client-server", Type::UNIT)
363{
364 AddTestCase(new UdpTraceClientServerTestCase, TestCase::Duration::QUICK);
365 AddTestCase(new UdpClientServerTestCase, TestCase::Duration::QUICK);
366 AddTestCase(new PacketLossCounterTestCase, TestCase::Duration::QUICK);
367 AddTestCase(new UdpEchoClientSetFillTestCase, TestCase::Duration::QUICK);
368}
369
371 udpClientServerTestSuite; //!< Static variable for test initialization
Test that all the PacketLossCounter class checks loss correctly in different cases.
void DoRun() override
Implementation to actually run this TestCase.
Test that all the UDP packets generated by an UdpClient application are correctly received by an UdpS...
void DoRun() override
Implementation to actually run this TestCase.
UDP client and server TestSuite.
void DoRun() override
Implementation to actually run this TestCase.
Test that all the udp packets generated by an udpTraceClient application are correctly received by an...
void DoRun() override
Implementation to actually run this TestCase.
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:135
A class to count the number of lost packets.
void NotifyReceived(uint32_t seq)
Record a successfully received packet.
uint32_t GetLost() const
Get the number of lost packets.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1406
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Create a server application which waits for input UDP packets and uses the information carried into t...
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream.
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.