A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
39 using namespace ns3;
40 
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 
73 void
75 {
76  NodeContainer n;
77  n.Create(2);
78 
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;
101  ApplicationContainer apps = server.Install(n.Get(1));
102  apps.Start(Seconds(1.0));
103  apps.Stop(Seconds(10.0));
104 
105  uint32_t MaxPacketSize = 1024;
106  Time interPacketInterval = Seconds(1.);
107  uint32_t maxPacketCount = 10;
109  client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
110  client.SetAttribute("Interval", TimeValue(interPacketInterval));
111  client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
112  apps = client.Install(n.Get(0));
113  apps.Start(Seconds(2.0));
114  apps.Stop(Seconds(10.0));
115 
116  Simulator::Run();
117  Simulator::Destroy();
118 
119  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetLost(), 0, "Packets were lost !");
120  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetReceived(),
121  8,
122  "Did not receive expected number of packets !");
123 }
124 
131 {
132  public:
135 
136  private:
137  void DoRun() override;
138 };
139 
141  : TestCase("Test that all the udp packets generated by an udpTraceClient application are "
142  "correctly received by an udpServer application")
143 {
144 }
145 
147 {
148 }
149 
150 void
152 {
153  NodeContainer n;
154  n.Create(2);
155 
157  internet.Install(n);
158 
159  // link the two nodes
160  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
161  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
162  n.Get(0)->AddDevice(txDev);
163  n.Get(1)->AddDevice(rxDev);
164  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
165  rxDev->SetChannel(channel1);
166  txDev->SetChannel(channel1);
168  d.Add(txDev);
169  d.Add(rxDev);
170 
172  ipv4.SetBase("10.1.1.0", "255.255.255.0");
173  Ipv4InterfaceContainer i = ipv4.Assign(d);
174 
175  uint16_t port = 4000;
177  ApplicationContainer apps = server.Install(n.Get(1));
178  apps.Start(Seconds(1.0));
179  apps.Stop(Seconds(10.0));
180 
181  uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
183  client.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
184  apps = client.Install(n.Get(0));
185  apps.Start(Seconds(2.0));
186  apps.Stop(Seconds(10.0));
187 
188  Simulator::Run();
189  Simulator::Destroy();
190 
191  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetLost(), 0, "Packets were lost !");
192  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetReceived(),
193  247,
194  "Did not receive expected number of packets !");
195 }
196 
202 {
203  public:
205  ~PacketLossCounterTestCase() override;
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 
220 void
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 
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 
297 void
299 {
301  nodes.Create(2);
302 
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");
321 
322  uint16_t port = 5000;
325  serverApps.Start(Seconds(1.0));
326  serverApps.Stop(Seconds(10.0));
328  echoClient.SetAttribute("MaxPackets", UintegerValue(1));
329  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
330  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
331 
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 
345  Simulator::Run();
346  Simulator::Destroy();
347 }
348 
356 {
357  public:
359 };
360 
362  : TestSuite("udp-client-server", UNIT)
363 {
364  AddTestCase(new UdpTraceClientServerTestCase, TestCase::QUICK);
365  AddTestCase(new UdpClientServerTestCase, TestCase::QUICK);
366  AddTestCase(new PacketLossCounterTestCase, TestCase::QUICK);
367  AddTestCase(new UdpEchoClientSetFillTestCase, TestCase::QUICK);
368 }
369 
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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:138
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
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
echoClient
Definition: first.py:59
serverApps
Definition: first.py:54
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.