A Discrete-Event Network Simulator
API
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/uinteger.h"
35 
36 #include <fstream>
37 
38 using namespace ns3;
39 
53 {
54  public:
56  ~UdpClientServerTestCase() override;
57 
58  private:
59  void DoRun() override;
60 };
61 
63  : TestCase("Test that all the udp packets generated by an udpClient application are correctly "
64  "received by an udpServer application")
65 {
66 }
67 
69 {
70 }
71 
72 void
74 {
75  NodeContainer n;
76  n.Create(2);
77 
78  InternetStackHelper internet;
79  internet.Install(n);
80 
81  // link the two nodes
82  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
83  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
84  n.Get(0)->AddDevice(txDev);
85  n.Get(1)->AddDevice(rxDev);
86  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
87  rxDev->SetChannel(channel1);
88  txDev->SetChannel(channel1);
90  d.Add(txDev);
91  d.Add(rxDev);
92 
93  Ipv4AddressHelper ipv4;
94 
95  ipv4.SetBase("10.1.1.0", "255.255.255.0");
96  Ipv4InterfaceContainer i = ipv4.Assign(d);
97 
98  uint16_t port = 4000;
99  UdpServerHelper server(port);
100  ApplicationContainer apps = server.Install(n.Get(1));
101  apps.Start(Seconds(1.0));
102  apps.Stop(Seconds(10.0));
103 
104  uint32_t MaxPacketSize = 1024;
105  Time interPacketInterval = Seconds(1.);
106  uint32_t maxPacketCount = 10;
107  UdpClientHelper client(i.GetAddress(1), port);
108  client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
109  client.SetAttribute("Interval", TimeValue(interPacketInterval));
110  client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
111  apps = client.Install(n.Get(0));
112  apps.Start(Seconds(2.0));
113  apps.Stop(Seconds(10.0));
114 
115  Simulator::Run();
116  Simulator::Destroy();
117 
118  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetLost(), 0, "Packets were lost !");
119  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetReceived(),
120  8,
121  "Did not receive expected number of packets !");
122 }
123 
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 
149 void
151 {
152  NodeContainer n;
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 
170  Ipv4AddressHelper ipv4;
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 server(port);
176  ApplicationContainer apps = server.Install(n.Get(1));
177  apps.Start(Seconds(1.0));
178  apps.Stop(Seconds(10.0));
179 
180  uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
181  UdpTraceClientHelper client(i.GetAddress(1), port, "");
182  client.SetAttribute("MaxPacketSize", UintegerValue(MaxPacketSize));
183  apps = client.Install(n.Get(0));
184  apps.Start(Seconds(2.0));
185  apps.Stop(Seconds(10.0));
186 
187  Simulator::Run();
188  Simulator::Destroy();
189 
190  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetLost(), 0, "Packets were lost !");
191  NS_TEST_ASSERT_MSG_EQ(server.GetServer()->GetReceived(),
192  247,
193  "Did not receive expected number of packets !");
194 }
195 
201 {
202  public:
204  ~PacketLossCounterTestCase() override;
205 
206  private:
207  void DoRun() override;
208 };
209 
211  : TestCase("Test that all the PacketLossCounter class checks loss correctly in different cases")
212 {
213 }
214 
216 {
217 }
218 
219 void
221 {
222  PacketLossCounter lossCounter(32);
223  lossCounter.NotifyReceived(32); // out of order
224  for (uint32_t i = 0; i < 64; i++)
225  {
226  lossCounter.NotifyReceived(i);
227  }
228 
229  NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 0, "Check that 0 packets are lost");
230 
231  for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
232  {
233  lossCounter.NotifyReceived(i);
234  }
235  NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 1, "Check that 1 packet is lost");
236 
237  for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
238  {
239  lossCounter.NotifyReceived(i);
240  }
241  NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 7, "Check that 7 (6+1) packets are lost");
242 
243  // reordering without loss
244  lossCounter.NotifyReceived(205);
245  lossCounter.NotifyReceived(206);
246  lossCounter.NotifyReceived(207);
247  lossCounter.NotifyReceived(200);
248  lossCounter.NotifyReceived(201);
249  lossCounter.NotifyReceived(202);
250  lossCounter.NotifyReceived(203);
251  lossCounter.NotifyReceived(204);
252  for (uint32_t i = 205; i < 250; i++)
253  {
254  lossCounter.NotifyReceived(i);
255  }
256  NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(),
257  7,
258  "Check that 7 (6+1) packets are lost even when reordering happens");
259 
260  // reordering with loss
261  lossCounter.NotifyReceived(255);
262  // drop (2) seqNum 250, 251
263  lossCounter.NotifyReceived(252);
264  lossCounter.NotifyReceived(253);
265  lossCounter.NotifyReceived(254);
266  for (uint32_t i = 256; i < 300; i++)
267  {
268  lossCounter.NotifyReceived(i);
269  }
270  NS_TEST_ASSERT_MSG_EQ(lossCounter.GetLost(), 9, "Check that 9 (6+1+2) packet are lost");
271 }
272 
278 {
279  public:
282 
283  private:
284  void DoRun() override;
285 };
286 
288  : TestCase("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
289 {
290 }
291 
293 {
294 }
295 
296 void
298 {
300  nodes.Create(2);
301 
302  InternetStackHelper internet;
303  internet.Install(nodes);
304 
305  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice>();
306  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice>();
307  nodes.Get(0)->AddDevice(txDev);
308  nodes.Get(1)->AddDevice(rxDev);
309  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel>();
310  rxDev->SetChannel(channel1);
311  txDev->SetChannel(channel1);
313  d.Add(txDev);
314  d.Add(rxDev);
315 
316  Ipv4AddressHelper ipv4;
317 
318  ipv4.SetBase("10.1.1.0", "255.255.255.0");
320 
321  uint16_t port = 5000;
324  serverApps.Start(Seconds(1.0));
325  serverApps.Stop(Seconds(10.0));
327  echoClient.SetAttribute("MaxPackets", UintegerValue(1));
328  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
329  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
330 
332 
333  uint8_t array[64];
334  uint8_t i;
335  for (i = 0; i < 64; i++)
336  {
337  array[i] = i;
338  }
339  echoClient.SetFill(clientApps.Get(0), &(array[0]), (uint32_t)64, (uint32_t)64);
340 
341  clientApps.Start(Seconds(2.0));
342  clientApps.Stop(Seconds(10.0));
343 
344  Simulator::Run();
345  Simulator::Destroy();
346 }
347 
355 {
356  public:
358 };
359 
361  : TestSuite("udp-client-server", UNIT)
362 {
363  AddTestCase(new UdpTraceClientServerTestCase, TestCase::QUICK);
364  AddTestCase(new UdpClientServerTestCase, TestCase::QUICK);
365  AddTestCase(new PacketLossCounterTestCase, TestCase::QUICK);
366  AddTestCase(new UdpEchoClientSetFillTestCase, TestCase::QUICK);
367 }
368 
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.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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:78
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:305
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1423
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
ApplicationContainer Install(NodeContainer c)
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...
Ptr< UdpServer > GetServer()
Return the last created server.
ApplicationContainer Install(NodeContainer c)
Create one UDP server application on each of the Nodes in the NodeContainer.
Create UdpTraceClient application which sends UDP packets based on a trace file of an MPEG4 stream.
ApplicationContainer Install(NodeContainer c)
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:45
#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:1336
NodeContainer nodes
echoClient
Definition: first.py:53
serverApps
Definition: first.py:48
echoServer
Definition: first.py:46
clientApps
Definition: first.py:58
interfaces
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static UdpClientServerTestSuite udpClientServerTestSuite
Static variable for test initialization.