A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
dsr-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Yufei Cheng
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: Yufei Cheng <yfcheng@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 
32 #include <vector>
33 #include "ns3/ptr.h"
34 #include "ns3/boolean.h"
35 #include "ns3/test.h"
36 #include "ns3/ipv4-route.h"
37 #include "ns3/mesh-helper.h"
38 #include "ns3/simulator.h"
39 #include "ns3/double.h"
40 #include "ns3/uinteger.h"
41 #include "ns3/string.h"
42 #include "ns3/ipv4-address-helper.h"
43 
44 #include "ns3/dsr-fs-header.h"
45 #include "ns3/dsr-option-header.h"
46 #include "ns3/dsr-rreq-table.h"
47 #include "ns3/dsr-rcache.h"
48 #include "ns3/dsr-rsendbuff.h"
49 #include "ns3/dsr-main-helper.h"
50 #include "ns3/dsr-helper.h"
51 
52 namespace ns3 {
53 namespace dsr {
54 
55 // -----------------------------------------------------------------------------
56 // / Unit test for DSR Fixed Size Header
57 class DsrFsHeaderTest : public TestCase
58 {
59 public:
60  DsrFsHeaderTest ();
62  virtual void
63  DoRun (void);
64 };
66  : TestCase ("DSR Fixed size Header")
67 {
68 }
70 {
71 }
72 void
74 {
75  dsr::DsrRoutingHeader header;
76  dsr::DsrOptionRreqHeader rreqHeader;
77  header.AddDsrOption (rreqHeader); // has an alignment of 4n+0
78 
79  NS_TEST_EXPECT_MSG_EQ (header.GetSerializedSize () % 2, 0, "length of routing header is not a multiple of 4");
80  Buffer buf;
81  buf.AddAtStart (header.GetSerializedSize ());
82  header.Serialize (buf.Begin ());
83 
84  const uint8_t* data = buf.PeekData ();
85  NS_TEST_EXPECT_MSG_EQ (*(data + 8), rreqHeader.GetType (), "expect the rreqHeader after fixed size header");
86 }
87 // -----------------------------------------------------------------------------
88 // / Unit test for RREQ
90 {
91 public:
94  virtual void
95  DoRun (void);
96 };
98  : TestCase ("DSR RREQ")
99 {
100 }
102 {
103 }
104 void
106 {
108  std::vector<Ipv4Address> nodeList;
109  nodeList.push_back (Ipv4Address ("1.1.1.0"));
110  nodeList.push_back (Ipv4Address ("1.1.1.1"));
111  nodeList.push_back (Ipv4Address ("1.1.1.2"));
112 
113  h.SetTarget (Ipv4Address ("1.1.1.3"));
114  NS_TEST_EXPECT_MSG_EQ (h.GetTarget (), Ipv4Address ("1.1.1.3"), "trivial");
115  h.SetNodesAddress (nodeList);
116  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (0), Ipv4Address ("1.1.1.0"), "trivial");
117  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (1), Ipv4Address ("1.1.1.1"), "trivial");
118  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (2), Ipv4Address ("1.1.1.2"), "trivial");
119  h.SetId (1);
120  NS_TEST_EXPECT_MSG_EQ (h.GetId (), 1, "trivial");
121 
122  Ptr<Packet> p = Create<Packet> ();
123  dsr::DsrRoutingHeader header;
124  header.AddDsrOption (h);
125  p->AddHeader (header);
126  p->RemoveAtStart (8);
128  h2.SetNumberAddress (3);
129  uint32_t bytes = p->RemoveHeader (h2);
130  NS_TEST_EXPECT_MSG_EQ (bytes, 20, "Total RREP is 20 bytes long");
131 }
132 // -----------------------------------------------------------------------------
133 // / Unit test for RREP
135 {
136 public:
139  virtual void
140  DoRun (void);
141 };
143  : TestCase ("DSR RREP")
144 {
145 }
147 {
148 }
149 void
151 {
153 
154  std::vector<Ipv4Address> nodeList;
155  nodeList.push_back (Ipv4Address ("1.1.1.0"));
156  nodeList.push_back (Ipv4Address ("1.1.1.1"));
157  nodeList.push_back (Ipv4Address ("1.1.1.2"));
158  h.SetNodesAddress (nodeList);
159  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (0), Ipv4Address ("1.1.1.0"), "trivial");
160  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (1), Ipv4Address ("1.1.1.1"), "trivial");
161  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (2), Ipv4Address ("1.1.1.2"), "trivial");
162 
163  Ptr<Packet> p = Create<Packet> ();
164  dsr::DsrRoutingHeader header;
165  header.AddDsrOption (h);
166  p->AddHeader (header);
167  p->RemoveAtStart (8);
169  h2.SetNumberAddress (3);
170  uint32_t bytes = p->RemoveHeader (h2);
171  NS_TEST_EXPECT_MSG_EQ (bytes, 16, "Total RREP is 16 bytes long");
172 }
173 // -----------------------------------------------------------------------------
174 // / Unit test for Source Route
175 class DsrSRHeaderTest : public TestCase
176 {
177 public:
178  DsrSRHeaderTest ();
179  ~DsrSRHeaderTest ();
180  virtual void
181  DoRun (void);
182 };
184  : TestCase ("DSR Source Route")
185 {
186 }
188 {
189 }
190 void
192 {
194  std::vector<Ipv4Address> nodeList;
195  nodeList.push_back (Ipv4Address ("1.1.1.0"));
196  nodeList.push_back (Ipv4Address ("1.1.1.1"));
197  nodeList.push_back (Ipv4Address ("1.1.1.2"));
198  h.SetNodesAddress (nodeList);
199  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (0), Ipv4Address ("1.1.1.0"), "trivial");
200  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (1), Ipv4Address ("1.1.1.1"), "trivial");
201  NS_TEST_EXPECT_MSG_EQ (h.GetNodeAddress (2), Ipv4Address ("1.1.1.2"), "trivial");
202 
203  h.SetSalvage (1);
204  NS_TEST_EXPECT_MSG_EQ (h.GetSalvage (), 1, "trivial");
205  h.SetSegmentsLeft (2);
206  NS_TEST_EXPECT_MSG_EQ (h.GetSegmentsLeft (), 2, "trivial");
207 
208  Ptr<Packet> p = Create<Packet> ();
209  dsr::DsrRoutingHeader header;
210  header.AddDsrOption (h);
211  p->AddHeader (header);
212  p->RemoveAtStart (8);
214  h2.SetNumberAddress (3);
215  uint32_t bytes = p->RemoveHeader (h2);
216  NS_TEST_EXPECT_MSG_EQ (bytes, 16, "Total RREP is 16 bytes long");
217 }
218 // -----------------------------------------------------------------------------
219 // / Unit test for RERR
221 {
222 public:
225  virtual void
226  DoRun (void);
227 };
229  : TestCase ("DSR RERR")
230 {
231 }
233 {
234 }
235 void
237 {
239  h.SetErrorSrc (Ipv4Address ("1.1.1.0"));
240  NS_TEST_EXPECT_MSG_EQ (h.GetErrorSrc (), Ipv4Address ("1.1.1.0"), "trivial");
241  h.SetErrorDst (Ipv4Address ("1.1.1.1"));
242  NS_TEST_EXPECT_MSG_EQ (h.GetErrorDst (), Ipv4Address ("1.1.1.1"), "trivial");
243  h.SetSalvage (1);
244  NS_TEST_EXPECT_MSG_EQ (h.GetSalvage (), 1, "trivial");
245  h.SetUnreachNode (Ipv4Address ("1.1.1.2"));
246  NS_TEST_EXPECT_MSG_EQ (h.GetUnreachNode (), Ipv4Address ("1.1.1.2"), "trivial");
247 
248  Ptr<Packet> p = Create<Packet> ();
249  dsr::DsrRoutingHeader header;
250  header.AddDsrOption (h);
251  p->AddHeader (header);
252  p->RemoveAtStart (8);
254  uint32_t bytes = p->RemoveHeader (h2);
255  NS_TEST_EXPECT_MSG_EQ (bytes, 20, "Total RREP is 20 bytes long");
256 }
257 // -----------------------------------------------------------------------------
258 // / Unit test for ACK-REQ
260 {
261 public:
264  virtual void
265  DoRun (void);
266 };
268  : TestCase ("DSR Ack Req")
269 {
270 }
272 {
273 }
274 void
276 {
278 
279  h.SetAckId (1);
280  NS_TEST_EXPECT_MSG_EQ (h.GetAckId (), 1, "trivial");
281 
282  Ptr<Packet> p = Create<Packet> ();
283  dsr::DsrRoutingHeader header;
284  header.AddDsrOption (h);
285  p->AddHeader (header);
286  p->RemoveAtStart (8);
287  p->AddHeader (header);
289  p->RemoveAtStart (8);
290  uint32_t bytes = p->RemoveHeader (h2);
291  NS_TEST_EXPECT_MSG_EQ (bytes, 4, "Total RREP is 4 bytes long");
292 }
293 // -----------------------------------------------------------------------------
294 // / Unit test for ACK
296 {
297 public:
298  DsrAckHeaderTest ();
300  virtual void
301  DoRun (void);
302 };
304  : TestCase ("DSR ACK")
305 {
306 }
308 {
309 }
310 void
312 {
314 
315  h.SetRealSrc (Ipv4Address ("1.1.1.0"));
316  NS_TEST_EXPECT_MSG_EQ (h.GetRealSrc (), Ipv4Address ("1.1.1.0"), "trivial");
317  h.SetRealDst (Ipv4Address ("1.1.1.1"));
318  NS_TEST_EXPECT_MSG_EQ (h.GetRealDst (), Ipv4Address ("1.1.1.1"), "trivial");
319  h.SetAckId (1);
320  NS_TEST_EXPECT_MSG_EQ (h.GetAckId (), 1, "trivial");
321 
322  Ptr<Packet> p = Create<Packet> ();
323  dsr::DsrRoutingHeader header;
324  header.AddDsrOption (h);
325  p->AddHeader (header);
326  p->RemoveAtStart (8);
327  p->AddHeader (header);
329  p->RemoveAtStart (8);
330  uint32_t bytes = p->RemoveHeader (h2);
331  NS_TEST_EXPECT_MSG_EQ (bytes, 12, "Total RREP is 12 bytes long");
332 }
333 // -----------------------------------------------------------------------------
334 // / Unit test for DSR route cache entry
336 {
337 public:
340  virtual void
341  DoRun (void);
342 };
344  : TestCase ("DSR ACK")
345 {
346 }
348 {
349 }
350 void
352 {
353  Ptr<dsr::RouteCache> rcache = CreateObject<dsr::RouteCache> ();
354  std::vector<Ipv4Address> ip;
355  ip.push_back (Ipv4Address ("0.0.0.0"));
356  ip.push_back (Ipv4Address ("0.0.0.1"));
357  Ipv4Address dst = Ipv4Address ("0.0.0.1");
358  dsr::RouteCacheEntry entry (ip, dst, Seconds (1));
359  NS_TEST_EXPECT_MSG_EQ (entry.GetVector ().size (), 2, "trivial");
360  NS_TEST_EXPECT_MSG_EQ (entry.GetDestination (), Ipv4Address ("0.0.0.1"), "trivial");
361  NS_TEST_EXPECT_MSG_EQ (entry.GetExpireTime (), Seconds (1), "trivial");
362 
363  entry.SetExpireTime (Seconds (3));
364  NS_TEST_EXPECT_MSG_EQ (entry.GetExpireTime (), Seconds (3), "trivial");
365  entry.SetDestination (Ipv4Address ("1.1.1.1"));
366  NS_TEST_EXPECT_MSG_EQ (entry.GetDestination (), Ipv4Address ("1.1.1.1"), "trivial");
367  ip.push_back (Ipv4Address ("0.0.0.2"));
368  entry.SetVector (ip);
369  NS_TEST_EXPECT_MSG_EQ (entry.GetVector ().size (), 3, "trivial");
370 
371  NS_TEST_EXPECT_MSG_EQ (rcache->AddRoute (entry), true, "trivial");
372 
373  std::vector<Ipv4Address> ip2;
374  ip2.push_back (Ipv4Address ("1.1.1.0"));
375  ip2.push_back (Ipv4Address ("1.1.1.1"));
376  Ipv4Address dst2 = Ipv4Address ("1.1.1.1");
377  dsr::RouteCacheEntry entry2 (ip2, dst2, Seconds (2));
378  dsr::RouteCacheEntry newEntry;
379  NS_TEST_EXPECT_MSG_EQ (rcache->AddRoute (entry2), true, "trivial");
380  NS_TEST_EXPECT_MSG_EQ (rcache->LookupRoute (dst2, newEntry), true, "trivial");
381  NS_TEST_EXPECT_MSG_EQ (rcache->DeleteRoute (Ipv4Address ("2.2.2.2")), false, "trivial");
382 
383  NS_TEST_EXPECT_MSG_EQ (rcache->DeleteRoute (Ipv4Address ("1.1.1.1")), true, "trivial");
384  NS_TEST_EXPECT_MSG_EQ (rcache->DeleteRoute (Ipv4Address ("1.1.1.1")), false, "trivial");
385 }
386 // -----------------------------------------------------------------------------
387 // / Unit test for Send Buffer
388 class DsrSendBuffTest : public TestCase
389 {
390 public:
391  DsrSendBuffTest ();
392  ~DsrSendBuffTest ();
393  virtual void
394  DoRun (void);
395  void CheckSizeLimit ();
396  void CheckTimeout ();
397 
399 };
401  : TestCase ("DSR SendBuff"),
402  q ()
403 {
404 }
406 {
407 }
408 void
410 {
411  q.SetMaxQueueLen (32);
412  NS_TEST_EXPECT_MSG_EQ (q.GetMaxQueueLen (), 32, "trivial");
414  NS_TEST_EXPECT_MSG_EQ (q.GetSendBufferTimeout (), Seconds (10), "trivial");
415 
416  Ptr<const Packet> packet = Create<Packet> ();
417  Ipv4Address dst1 = Ipv4Address ("0.0.0.1");
418  dsr::SendBuffEntry e1 (packet, dst1, Seconds (1));
419  q.Enqueue (e1);
420  q.Enqueue (e1);
421  q.Enqueue (e1);
422  NS_TEST_EXPECT_MSG_EQ (q.Find (Ipv4Address ("0.0.0.1")), true, "trivial");
423  NS_TEST_EXPECT_MSG_EQ (q.Find (Ipv4Address ("1.1.1.1")), false, "trivial");
424  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 1, "trivial");
425  q.DropPacketWithDst (Ipv4Address ("0.0.0.1"));
426  NS_TEST_EXPECT_MSG_EQ (q.Find (Ipv4Address ("0.0.0.1")), false, "trivial");
427  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 0, "trivial");
428 
429  Ipv4Address dst2 = Ipv4Address ("0.0.0.2");
430  dsr::SendBuffEntry e2 (packet, dst2, Seconds (1));
431  q.Enqueue (e1);
432  q.Enqueue (e2);
433  Ptr<Packet> packet2 = Create<Packet> ();
434  dsr::SendBuffEntry e3 (packet2, dst2, Seconds (1));
435  NS_TEST_EXPECT_MSG_EQ (q.Dequeue (Ipv4Address ("0.0.0.3"), e3), false, "trivial");
436  NS_TEST_EXPECT_MSG_EQ (q.Dequeue (Ipv4Address ("0.0.0.2"), e3), true, "trivial");
437  NS_TEST_EXPECT_MSG_EQ (q.Find (Ipv4Address ("0.0.0.2")), false, "trivial");
438  q.Enqueue (e2);
439  q.Enqueue (e3);
440  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 2, "trivial");
441  Ptr<Packet> packet4 = Create<Packet> ();
442  Ipv4Address dst4 = Ipv4Address ("0.0.0.4");
443  dsr::SendBuffEntry e4 (packet4, dst4, Seconds (20));
444  q.Enqueue (e4);
445  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 3, "trivial");
446  q.DropPacketWithDst (Ipv4Address ("0.0.0.4"));
447  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 2, "trivial");
448 
449  CheckSizeLimit ();
450 
452 
453  Simulator::Run ();
455 }
456 void
458 {
459  Ptr<Packet> packet = Create<Packet> ();
460  Ipv4Address dst;
461  dsr::SendBuffEntry e1 (packet, dst, Seconds (1));
462 
463  for (uint32_t i = 0; i < q.GetMaxQueueLen (); ++i)
464  {
465  q.Enqueue (e1);
466  }
467  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 3, "trivial");
468 
469  for (uint32_t i = 0; i < q.GetMaxQueueLen (); ++i)
470  {
471  q.Enqueue (e1);
472  }
473  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 3, "trivial");
474 }
475 void
477 {
478  NS_TEST_EXPECT_MSG_EQ (q.GetSize (), 0, "Must be empty now");
479 }
480 // -----------------------------------------------------------------------------
481 // / Unit test for DSR routing table entry
483 {
484 public:
485  DsrRreqTableTest ();
487  virtual void
488  DoRun (void);
489 };
491  : TestCase ("DSR RreqTable")
492 {
493 }
495 {
496 }
497 void
499 {
501 
502  rt.m_reqNo = 2;
503  NS_TEST_EXPECT_MSG_EQ (rt.m_reqNo, 2, "trivial");
504 }
505 // -----------------------------------------------------------------------------
506 class DsrTestSuite : public TestSuite
507 {
508 public:
509  DsrTestSuite () : TestSuite ("routing-dsr", UNIT)
510  {
520  }
522 } // namespace dsr
523 } // namespace ns3