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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9
10#include "ns3/abort.h"
11#include "ns3/config.h"
12#include "ns3/inet-socket-address.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-address-helper.h"
15#include "ns3/log.h"
16#include "ns3/simple-channel.h"
17#include "ns3/simple-net-device.h"
18#include "ns3/simulator.h"
19#include "ns3/string.h"
20#include "ns3/test.h"
21#include "ns3/udp-client-server-helper.h"
22#include "ns3/udp-echo-helper.h"
23#include "ns3/udp-server.h"
24#include "ns3/uinteger.h"
25
26#include <fstream>
27
28using namespace ns3;
29
30/**
31 * \ingroup applications
32 * \defgroup applications-test applications module tests
33 */
34
35/**
36 * \ingroup applications-test
37 * \ingroup tests
38 *
39 * Test that all the UDP packets generated by an UdpClient application are
40 * correctly received by an UdpServer application
41 */
43{
44 public:
46 ~UdpClientServerTestCase() override;
47
48 private:
49 void DoRun() override;
50};
51
53 : TestCase("Test that all the udp packets generated by an udpClient application are correctly "
54 "received by an udpServer application")
55{
56}
57
61
62void
64{
66 n.Create(2);
67
68 InternetStackHelper internet;
69 internet.Install(n);
70
71 // link the two nodes
74 n.Get(0)->AddDevice(txDev);
75 n.Get(1)->AddDevice(rxDev);
77 rxDev->SetChannel(channel1);
78 txDev->SetChannel(channel1);
80 d.Add(txDev);
81 d.Add(rxDev);
82
84
85 ipv4.SetBase("10.1.1.0", "255.255.255.0");
86 Ipv4InterfaceContainer i = ipv4.Assign(d);
87
88 uint16_t port = 4000;
89 UdpServerHelper serverHelper(port);
90 auto serverApp = serverHelper.Install(n.Get(1));
91 serverApp.Start(Seconds(1.0));
92 serverApp.Stop(Seconds(10.0));
93
94 uint32_t MaxPacketSize = 1024;
95 Time interPacketInterval = Seconds(1.);
96 uint32_t maxPacketCount = 10;
97 UdpClientHelper clientHelper(i.GetAddress(1), port);
98 clientHelper.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
99 clientHelper.SetAttribute("Interval", TimeValue(interPacketInterval));
100 clientHelper.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
101 auto clientApp = clientHelper.Install(n.Get(0));
102 clientApp.Start(Seconds(2.0));
103 clientApp.Stop(Seconds(10.0));
104
107
108 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
109 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
110 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(), 8, "Did not receive expected number of packets !");
111}
112
113/**
114 * Test that all the udp packets generated by an udpTraceClient application are
115 * correctly received by an udpServer application
116 */
117
119{
120 public:
123
124 private:
125 void DoRun() override;
126};
127
129 : TestCase("Test that all the udp packets generated by an udpTraceClient application are "
130 "correctly received by an udpServer application")
131{
132}
133
137
138void
140{
142 n.Create(2);
143
144 InternetStackHelper internet;
145 internet.Install(n);
146
147 // link the two nodes
150 n.Get(0)->AddDevice(txDev);
151 n.Get(1)->AddDevice(rxDev);
153 rxDev->SetChannel(channel1);
154 txDev->SetChannel(channel1);
156 d.Add(txDev);
157 d.Add(rxDev);
158
160 ipv4.SetBase("10.1.1.0", "255.255.255.0");
161 Ipv4InterfaceContainer i = ipv4.Assign(d);
162
163 uint16_t port = 4000;
164 UdpServerHelper serverHelper(port);
165 auto serverApp = serverHelper.Install(n.Get(1));
166 serverApp.Start(Seconds(1.0));
167 serverApp.Stop(Seconds(10.0));
168
169 uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
170 UdpTraceClientHelper clientHelper(i.GetAddress(1), port);
171 clientHelper.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
172 auto clientApp = clientHelper.Install(n.Get(0));
173 clientApp.Start(Seconds(2.0));
174 clientApp.Stop(Seconds(10.0));
175
178
179 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
180 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
181 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(),
182 247,
183 "Did not receive expected number of packets !");
184}
185
186/**
187 * Test that all the PacketLossCounter class checks loss correctly in different cases
188 */
189
191{
192 public:
195
196 private:
197 void DoRun() override;
198};
199
201 : TestCase("Test that all the PacketLossCounter class checks loss correctly in different cases")
202{
203}
204
208
209void
211{
212 PacketLossCounter lossCounter(32);
213 lossCounter.NotifyReceived(32); // out of order
214 for (uint32_t i = 0; i < 64; i++)
215 {
216 lossCounter.NotifyReceived(i);
217 }
218
219 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 0, "Check that 0 packets are lost");
220
221 for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
222 {
223 lossCounter.NotifyReceived(i);
224 }
225 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 1, "Check that 1 packet is lost");
226
227 for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
228 {
229 lossCounter.NotifyReceived(i);
230 }
231 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 7, "Check that 7 (6+1) packets are lost");
232
233 // reordering without loss
234 lossCounter.NotifyReceived(205);
235 lossCounter.NotifyReceived(206);
236 lossCounter.NotifyReceived(207);
237 lossCounter.NotifyReceived(200);
238 lossCounter.NotifyReceived(201);
239 lossCounter.NotifyReceived(202);
240 lossCounter.NotifyReceived(203);
241 lossCounter.NotifyReceived(204);
242 for (uint32_t i = 205; i < 250; i++)
243 {
244 lossCounter.NotifyReceived(i);
245 }
246 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(),
247 7,
248 "Check that 7 (6+1) packets are lost even when reordering happens");
249
250 // reordering with loss
251 lossCounter.NotifyReceived(255);
252 // drop (2) seqNum 250, 251
253 lossCounter.NotifyReceived(252);
254 lossCounter.NotifyReceived(253);
255 lossCounter.NotifyReceived(254);
256 for (uint32_t i = 256; i < 300; i++)
257 {
258 lossCounter.NotifyReceived(i);
259 }
260 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 9, "Check that 9 (6+1+2) packet are lost");
261}
262
263/**
264 * Test fix for \bugid{1378}
265 */
266
268{
269 public:
272
273 private:
274 void DoRun() override;
275};
276
278 : TestCase("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
279{
280}
281
285
286void
288{
290 nodes.Create(2);
291
292 InternetStackHelper internet;
293 internet.Install(nodes);
294
297 nodes.Get(0)->AddDevice(txDev);
298 nodes.Get(1)->AddDevice(rxDev);
300 rxDev->SetChannel(channel1);
301 txDev->SetChannel(channel1);
303 d.Add(txDev);
304 d.Add(rxDev);
305
307
308 ipv4.SetBase("10.1.1.0", "255.255.255.0");
309 Ipv4InterfaceContainer interfaces = ipv4.Assign(d);
310
311 uint16_t port = 5000;
312 UdpEchoServerHelper echoServer(port);
313 ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
314 serverApps.Start(Seconds(1.0));
315 serverApps.Stop(Seconds(10.0));
316 UdpEchoClientHelper echoClient(interfaces.GetAddress(1), port);
317 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
318 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
319 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
320
321 ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
322
323 uint8_t array[64];
324 uint8_t i;
325 for (i = 0; i < 64; i++)
326 {
327 array[i] = i;
328 }
329 echoClient.SetFill(clientApps.Get(0), &(array[0]), (uint32_t)64, (uint32_t)64);
330
331 clientApps.Start(Seconds(2.0));
332 clientApps.Stop(Seconds(10.0));
333
336}
337
338/**
339 * \ingroup applications-test
340 * \ingroup tests
341 *
342 * \brief UDP client and server TestSuite
343 */
345{
346 public:
348};
349
351 : TestSuite("applications-udp-client-server", Type::UNIT)
352{
353 AddTestCase(new UdpTraceClientServerTestCase, TestCase::Duration::QUICK);
354 AddTestCase(new UdpClientServerTestCase, TestCase::Duration::QUICK);
355 AddTestCase(new PacketLossCounterTestCase, TestCase::Duration::QUICK);
356 AddTestCase(new UdpEchoClientSetFillTestCase, TestCase::Duration::QUICK);
357}
358
360 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:124
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.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
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:34
uint16_t port
Definition dsdv-manet.cc:33
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.