A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
udp-client-server-test.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 
22 #include <fstream>
23 #include "ns3/log.h"
24 #include "ns3/abort.h"
25 #include "ns3/config.h"
26 #include "ns3/string.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/inet-socket-address.h"
29 #include "ns3/internet-stack-helper.h"
30 #include "ns3/ipv4-address-helper.h"
31 #include "ns3/udp-client-server-helper.h"
32 #include "ns3/udp-echo-helper.h"
33 #include "ns3/simple-net-device.h"
34 #include "ns3/simple-channel.h"
35 #include "ns3/test.h"
36 #include "ns3/simulator.h"
37 
38 using namespace ns3;
39 
46 {
47 public:
49  virtual ~UdpClientServerTestCase ();
50 
51 private:
52  virtual void DoRun (void);
53 
54 };
55 
56 UdpClientServerTestCase::UdpClientServerTestCase ()
57  : TestCase ("Test that all the udp packets generated by an udpClient application are correctly received by an udpServer application")
58 {
59 }
60 
61 UdpClientServerTestCase::~UdpClientServerTestCase ()
62 {
63 }
64 
66 {
67  NodeContainer n;
68  n.Create (2);
69 
70  InternetStackHelper internet;
71  internet.Install (n);
72 
73  // link the two nodes
74  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
75  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
76  n.Get (0)->AddDevice (txDev);
77  n.Get (1)->AddDevice (rxDev);
78  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
79  rxDev->SetChannel (channel1);
80  txDev->SetChannel (channel1);
82  d.Add (txDev);
83  d.Add (rxDev);
84 
85  Ipv4AddressHelper ipv4;
86 
87  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
88  Ipv4InterfaceContainer i = ipv4.Assign (d);
89 
90  uint16_t port = 4000;
91  UdpServerHelper server (port);
92  ApplicationContainer apps = server.Install (n.Get (1));
93  apps.Start (Seconds (1.0));
94  apps.Stop (Seconds (10.0));
95 
96  uint32_t MaxPacketSize = 1024;
97  Time interPacketInterval = Seconds (1.);
98  uint32_t maxPacketCount = 10;
99  UdpClientHelper client (i.GetAddress (1), port);
100  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
101  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
102  client.SetAttribute ("PacketSize", UintegerValue (MaxPacketSize));
103  apps = client.Install (n.Get (0));
104  apps.Start (Seconds (2.0));
105  apps.Stop (Seconds (10.0));
106 
107  Simulator::Run ();
108  Simulator::Destroy ();
109 
110  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetLost (), 0, "Packets were lost !");
111  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetReceived (), 8, "Did not receive expected number of packets !");
112 }
113 
120 {
121 public:
123  virtual ~UdpTraceClientServerTestCase ();
124 
125 private:
126  virtual void DoRun (void);
127 
128 };
129 
130 UdpTraceClientServerTestCase::UdpTraceClientServerTestCase ()
131  : TestCase ("Test that all the udp packets generated by an udpTraceClient application are correctly received by an udpServer application")
132 {
133 }
134 
135 UdpTraceClientServerTestCase::~UdpTraceClientServerTestCase ()
136 {
137 }
138 
140 {
141  NodeContainer n;
142  n.Create (2);
143 
144  InternetStackHelper internet;
145  internet.Install (n);
146 
147  // link the two nodes
148  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
149  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
150  n.Get (0)->AddDevice (txDev);
151  n.Get (1)->AddDevice (rxDev);
152  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
153  rxDev->SetChannel (channel1);
154  txDev->SetChannel (channel1);
156  d.Add (txDev);
157  d.Add (rxDev);
158 
159  Ipv4AddressHelper ipv4;
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 server (port);
165  ApplicationContainer apps = server.Install (n.Get (1));
166  apps.Start (Seconds (1.0));
167  apps.Stop (Seconds (10.0));
168 
169  uint32_t MaxPacketSize = 1400 - 28; // ip/udp header
170  UdpTraceClientHelper client (i.GetAddress (1), port,"");
171  client.SetAttribute ("MaxPacketSize", UintegerValue (MaxPacketSize));
172  apps = client.Install (n.Get (0));
173  apps.Start (Seconds (2.0));
174  apps.Stop (Seconds (10.0));
175 
176  Simulator::Run ();
177  Simulator::Destroy ();
178 
179  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetLost (), 0, "Packets were lost !");
180  NS_TEST_ASSERT_MSG_EQ (server.GetServer ()->GetReceived (), 247, "Did not receive expected number of packets !");
181 }
182 
183 
189 {
190 public:
192  virtual ~PacketLossCounterTestCase ();
193 
194 private:
195  virtual void DoRun (void);
196 
197 };
198 
199 PacketLossCounterTestCase::PacketLossCounterTestCase ()
200  : TestCase ("Test that all the PacketLossCounter class checks loss correctly in different cases")
201 {
202 }
203 
204 PacketLossCounterTestCase::~PacketLossCounterTestCase ()
205 {
206 }
207 
209 {
210  PacketLossCounter lossCounter (32);
211  lossCounter.NotifyReceived (32); //out of order
212  for (uint32_t i = 0; i < 64; i++)
213  {
214  lossCounter.NotifyReceived (i);
215  }
216 
217  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 0, "Check that 0 packets are lost");
218 
219  for (uint32_t i = 65; i < 128; i++) // drop (1) seqNum 64
220  {
221  lossCounter.NotifyReceived (i);
222  }
223  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 1, "Check that 1 packet is lost");
224 
225  for (uint32_t i = 134; i < 200; i++) // drop seqNum 128,129,130,131,132,133
226  {
227  lossCounter.NotifyReceived (i);
228  }
229  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 7, "Check that 7 (6+1) packets are lost");
230 
231  // reordering without loss
232  lossCounter.NotifyReceived (205);
233  lossCounter.NotifyReceived (206);
234  lossCounter.NotifyReceived (207);
235  lossCounter.NotifyReceived (200);
236  lossCounter.NotifyReceived (201);
237  lossCounter.NotifyReceived (202);
238  lossCounter.NotifyReceived (203);
239  lossCounter.NotifyReceived (204);
240  for (uint32_t i = 205; i < 250; i++)
241  {
242  lossCounter.NotifyReceived (i);
243  }
244  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 7, "Check that 7 (6+1) packets are lost even when reordering happens");
245 
246  // reordering with loss
247  lossCounter.NotifyReceived (255);
248  // drop (2) seqNum 250, 251
249  lossCounter.NotifyReceived (252);
250  lossCounter.NotifyReceived (253);
251  lossCounter.NotifyReceived (254);
252  for (uint32_t i = 256; i < 300; i++)
253  {
254  lossCounter.NotifyReceived (i);
255  }
256  NS_TEST_ASSERT_MSG_EQ (lossCounter.GetLost (), 9, "Check that 9 (6+1+2) packet are lost");
257 }
258 
264 {
265 public:
267  virtual ~UdpEchoClientSetFillTestCase ();
268 
269 private:
270  virtual void DoRun (void);
271 
272 };
273 
274 UdpEchoClientSetFillTestCase::UdpEchoClientSetFillTestCase ()
275  : TestCase ("Test that the UdpEchoClient::SetFill class sets packet size (bug 1378)")
276 {
277 }
278 
279 UdpEchoClientSetFillTestCase::~UdpEchoClientSetFillTestCase ()
280 {
281 }
282 
284 {
285  NodeContainer nodes;
286  nodes.Create (2);
287 
288  InternetStackHelper internet;
289  internet.Install (nodes);
290 
291  Ptr<SimpleNetDevice> txDev = CreateObject<SimpleNetDevice> ();
292  Ptr<SimpleNetDevice> rxDev = CreateObject<SimpleNetDevice> ();
293  nodes.Get (0)->AddDevice (txDev);
294  nodes.Get (1)->AddDevice (rxDev);
295  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
296  rxDev->SetChannel (channel1);
297  txDev->SetChannel (channel1);
299  d.Add (txDev);
300  d.Add (rxDev);
301 
302  Ipv4AddressHelper ipv4;
303 
304  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
305  Ipv4InterfaceContainer interfaces = ipv4.Assign (d);
306 
307  uint16_t port = 5000;
308  UdpEchoServerHelper echoServer (port);
309  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
310  serverApps.Start (Seconds (1.0));
311  serverApps.Stop (Seconds (10.0));
312  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), port);
313  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
314  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
315  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
316 
317  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
318 
319  uint8_t arry[64];
320  uint8_t i;
321  for (i = 0; i < 64; i++)
322  {
323  arry[i] = i;
324  }
325  echoClient.SetFill (clientApps.Get (0), &(arry[0]), (uint32_t)64, (uint32_t)64);
326 
327  clientApps.Start (Seconds (2.0));
328  clientApps.Stop (Seconds (10.0));
329 
330  Simulator::Run ();
331  Simulator::Destroy ();
332 }
333 
335 {
336 public:
338 };
339 
340 UdpClientServerTestSuite::UdpClientServerTestSuite ()
341  : TestSuite ("udp-client-server", UNIT)
342 {
343  AddTestCase (new UdpTraceClientServerTestCase, TestCase::QUICK);
344  AddTestCase (new UdpClientServerTestCase, TestCase::QUICK);
345  AddTestCase (new PacketLossCounterTestCase, TestCase::QUICK);
346  AddTestCase (new UdpEchoClientSetFillTestCase, TestCase::QUICK);
347 }
348 
349 static UdpClientServerTestSuite udpClientServerTestSuite;
holds a vector of ns3::Application pointers.
keep track of time unit.
Definition: nstime.h:149
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetAttribute(std::string name, const AttributeValue &value)
A suite of tests to run.
Definition: test.h:962
create an application which sends a udp packet and waits for an echo of this packet ...
virtual void DoRun(void)
Implementation to actually run this test case.
aggregate IP/TCP/UDP functionality to existing Nodes.
virtual void DoRun(void)
Implementation to actually run this test case.
encapsulates test code
Definition: test.h:834
ApplicationContainer Install(Ptr< Node > node) const
Create a server application which waits for input udp packets and sends them back to the original sen...
Create a client application which sends udp packets carrying a 32bit sequence number and a 64 bit tim...
hold objects of type ns3::Time
Definition: nstime.h:700
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Hold an unsigned integer type.
Definition: uinteger.h:46
virtual void DoRun(void)
Implementation to actually run this test case.
holds a vector of ns3::NetDevice pointers
Create a server application which waits for input udp packets and uses the information carried into t...
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void SetAttribute(std::string name, const AttributeValue &value)
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Install(std::string nodeName) const
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
ApplicationContainer Install(NodeContainer c)
uint32_t AddDevice(Ptr< NetDevice > device)
Definition: node.cc:119
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
virtual void DoRun(void)
Implementation to actually run this test case.