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));
92 serverApp.Stop(Seconds(10));
93
94 uint32_t MaxPacketSize = 1024;
95 Time interPacketInterval = Seconds(1.);
96 uint32_t maxPacketCount = 10;
97 // This voluntarily invokes the c'tor not doing conversion
99 clientHelper.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
100 clientHelper.SetAttribute("Interval", TimeValue(interPacketInterval));
101 clientHelper.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
102 auto clientApp = clientHelper.Install(n.Get(0));
103 clientApp.Start(Seconds(2));
104 clientApp.Stop(Seconds(10));
105
108
109 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
110 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
111 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(), 8, "Did not receive expected number of packets !");
112}
113
114/**
115 * Test that all the udp packets generated by an udpTraceClient application are
116 * correctly received by an udpServer application
117 */
118
120{
121 public:
124
125 private:
126 void DoRun() override;
127};
128
130 : TestCase("Test that all the udp packets generated by an udpTraceClient application are "
131 "correctly received by an udpServer application")
132{
133}
134
138
139void
141{
143 n.Create(2);
144
145 InternetStackHelper internet;
146 internet.Install(n);
147
148 // link the two nodes
151 n.Get(0)->AddDevice(txDev);
152 n.Get(1)->AddDevice(rxDev);
154 rxDev->SetChannel(channel1);
155 txDev->SetChannel(channel1);
157 d.Add(txDev);
158 d.Add(rxDev);
159
161 ipv4.SetBase("10.1.1.0", "255.255.255.0");
162 Ipv4InterfaceContainer i = ipv4.Assign(d);
163
164 uint16_t port = 4000;
166 auto serverApp = serverHelper.Install(n.Get(1));
167 serverApp.Start(Seconds(1));
168 serverApp.Stop(Seconds(10));
169
170 uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
172 clientHelper.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
173 auto clientApp = clientHelper.Install(n.Get(0));
174 clientApp.Start(Seconds(2));
175 clientApp.Stop(Seconds(10));
176
179
180 auto server = DynamicCast<UdpServer>(serverApp.Get(0));
181 NS_TEST_ASSERT_MSG_EQ(server->GetLost(), 0, "Packets were lost !");
182 NS_TEST_ASSERT_MSG_EQ(server->GetReceived(),
183 247,
184 "Did not receive expected number of packets !");
185}
186
187/**
188 * Test that all the PacketLossCounter class checks loss correctly in different cases
189 */
190
192{
193 public:
196
197 private:
198 void DoRun() override;
199};
200
202 : TestCase("Test that all the PacketLossCounter class checks loss correctly in different cases")
203{
204}
205
209
210void
212{
213 PacketLossCounter lossCounter(32);
214 lossCounter.NotifyReceived(32); // out of order
215 for (uint32_t i = 0; i < 64; i++)
216 {
217 lossCounter.NotifyReceived(i);
218 }
219
220 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 0, "Check that 0 packets are lost");
221
222 for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
223 {
224 lossCounter.NotifyReceived(i);
225 }
226 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 1, "Check that 1 packet is lost");
227
228 for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
229 {
230 lossCounter.NotifyReceived(i);
231 }
232 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 7, "Check that 7 (6+1) packets are lost");
233
234 // reordering without loss
235 lossCounter.NotifyReceived(205);
236 lossCounter.NotifyReceived(206);
237 lossCounter.NotifyReceived(207);
238 lossCounter.NotifyReceived(200);
239 lossCounter.NotifyReceived(201);
240 lossCounter.NotifyReceived(202);
241 lossCounter.NotifyReceived(203);
242 lossCounter.NotifyReceived(204);
243 for (uint32_t i = 205; i < 250; i++)
244 {
245 lossCounter.NotifyReceived(i);
246 }
247 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(),
248 7,
249 "Check that 7 (6+1) packets are lost even when reordering happens");
250
251 // reordering with loss
252 lossCounter.NotifyReceived(255);
253 // drop (2) seqNum 250, 251
254 lossCounter.NotifyReceived(252);
255 lossCounter.NotifyReceived(253);
256 lossCounter.NotifyReceived(254);
257 for (uint32_t i = 256; i < 300; i++)
258 {
259 lossCounter.NotifyReceived(i);
260 }
261 NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 9, "Check that 9 (6+1+2) packet are lost");
262}
263
264/**
265 * Test fix for \bugid{1378}
266 */
267
269{
270 public:
273
274 private:
275 void DoRun() override;
276};
277
279 : TestCase("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
280{
281}
282
286
287void
289{
291 nodes.Create(2);
292
293 InternetStackHelper internet;
294 internet.Install(nodes);
295
298 nodes.Get(0)->AddDevice(txDev);
299 nodes.Get(1)->AddDevice(rxDev);
301 rxDev->SetChannel(channel1);
302 txDev->SetChannel(channel1);
304 d.Add(txDev);
305 d.Add(rxDev);
306
308
309 ipv4.SetBase("10.1.1.0", "255.255.255.0");
310 Ipv4InterfaceContainer interfaces = ipv4.Assign(d);
311
312 uint16_t port = 5000;
314 ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
315 serverApps.Start(Seconds(1));
316 serverApps.Stop(Seconds(10));
317 UdpEchoClientHelper echoClient(InetSocketAddress(interfaces.GetAddress(1), port));
318 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
319 echoClient.SetAttribute("Interval", TimeValue(Seconds(1)));
320 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
321
322 ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
323
324 uint8_t array[64];
325 uint8_t i;
326 for (i = 0; i < 64; i++)
327 {
328 array[i] = i;
329 }
330 echoClient.SetFill(clientApps.Get(0), &(array[0]), (uint32_t)64, (uint32_t)64);
331
332 clientApps.Start(Seconds(2));
333 clientApps.Stop(Seconds(10));
334
337}
338
339/**
340 * @ingroup applications-test
341 * @ingroup tests
342 *
343 * @brief UDP client and server TestSuite
344 */
346{
347 public:
349};
350
352 : TestSuite("applications-udp-client-server", Type::UNIT)
353{
354 AddTestCase(new UdpTraceClientServerTestCase, TestCase::Duration::QUICK);
355 AddTestCase(new UdpClientServerTestCase, TestCase::Duration::QUICK);
356 AddTestCase(new PacketLossCounterTestCase, TestCase::Duration::QUICK);
357 AddTestCase(new UdpEchoClientSetFillTestCase, TestCase::Duration::QUICK);
358}
359
361 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.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
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
AttributeValue implementation for Time.
Definition nstime.h:1431
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:1344
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.