A Discrete-Event Network Simulator
API
global-route-manager-impl-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
4  * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Tom Henderson (tomhend@u.washington.edu)
20  *
21  * Kunihiro Ishigura, Toshiaki Takada (GNU Zebra) are attributed authors
22  * of the quagga 0.99.7/src/ospfd/ospf_spf.c code which was ported here
23  */
24 
25 #include "ns3/test.h"
26 #include "ns3/global-route-manager-impl.h"
27 #include "ns3/candidate-queue.h"
28 #include "ns3/simulator.h"
29 #include <cstdlib> // for rand()
30 
31 using namespace ns3;
32 
34 {
35 public:
37  virtual void DoRun (void);
38 };
39 
41  : TestCase ("GlobalRouteManagerImplTestCase")
42 {
43 }
44 void
46 {
47  CandidateQueue candidate;
48 
49  for (int i = 0; i < 100; ++i)
50  {
51  SPFVertex *v = new SPFVertex;
52  v->SetDistanceFromRoot (std::rand () % 100);
53  candidate.Push (v);
54  }
55 
56  for (int i = 0; i < 100; ++i)
57  {
58  SPFVertex *v = candidate.Pop ();
59  delete v;
60  v = 0;
61  }
62 
63  // Build fake link state database; four routers (0-3), 3 point-to-point
64  // links
65  //
66  // n0
67  // \ link 0
68  // \ link 2
69  // n2 -------------------------n3
70  // /
71  // / link 1
72  // n1
73  //
74  // link0: 10.1.1.1/30, 10.1.1.2/30
75  // link1: 10.1.2.1/30, 10.1.2.2/30
76  // link2: 10.1.3.1/30, 10.1.3.2/30
77  //
78  // Router 0
80  GlobalRoutingLinkRecord::PointToPoint,
81  "0.0.0.2", // router ID 0.0.0.2
82  "10.1.1.1", // local ID
83  1); // metric
84 
86  GlobalRoutingLinkRecord::StubNetwork,
87  "10.1.1.1",
88  "255.255.255.252",
89  1);
90 
91  GlobalRoutingLSA* lsa0 = new GlobalRoutingLSA ();
92  lsa0->SetLSType (GlobalRoutingLSA::RouterLSA);
93  lsa0->SetLinkStateId ("0.0.0.0");
94  lsa0->SetAdvertisingRouter ("0.0.0.0");
95  lsa0->AddLinkRecord (lr0);
96  lsa0->AddLinkRecord (lr1);
97 
98  // Router 1
100  GlobalRoutingLinkRecord::PointToPoint,
101  "0.0.0.2",
102  "10.1.2.1",
103  1);
104 
106  GlobalRoutingLinkRecord::StubNetwork,
107  "10.1.2.1",
108  "255.255.255.252",
109  1);
110 
111  GlobalRoutingLSA* lsa1 = new GlobalRoutingLSA ();
112  lsa1->SetLSType (GlobalRoutingLSA::RouterLSA);
113  lsa1->SetLinkStateId ("0.0.0.1");
114  lsa1->SetAdvertisingRouter ("0.0.0.1");
115  lsa1->AddLinkRecord (lr2);
116  lsa1->AddLinkRecord (lr3);
117 
118  // Router 2
120  GlobalRoutingLinkRecord::PointToPoint,
121  "0.0.0.0",
122  "10.1.1.2",
123  1);
124 
126  GlobalRoutingLinkRecord::StubNetwork,
127  "10.1.1.2",
128  "255.255.255.252",
129  1);
130 
132  GlobalRoutingLinkRecord::PointToPoint,
133  "0.0.0.1",
134  "10.1.2.2",
135  1);
136 
138  GlobalRoutingLinkRecord::StubNetwork,
139  "10.1.2.2",
140  "255.255.255.252",
141  1);
142 
144  GlobalRoutingLinkRecord::PointToPoint,
145  "0.0.0.3",
146  "10.1.3.2",
147  1);
148 
150  GlobalRoutingLinkRecord::StubNetwork,
151  "10.1.3.2",
152  "255.255.255.252",
153  1);
154 
155  GlobalRoutingLSA* lsa2 = new GlobalRoutingLSA ();
156  lsa2->SetLSType (GlobalRoutingLSA::RouterLSA);
157  lsa2->SetLinkStateId ("0.0.0.2");
158  lsa2->SetAdvertisingRouter ("0.0.0.2");
159  lsa2->AddLinkRecord (lr4);
160  lsa2->AddLinkRecord (lr5);
161  lsa2->AddLinkRecord (lr6);
162  lsa2->AddLinkRecord (lr7);
163  lsa2->AddLinkRecord (lr8);
164  lsa2->AddLinkRecord (lr9);
165 
166  // Router 3
168  GlobalRoutingLinkRecord::PointToPoint,
169  "0.0.0.2",
170  "10.1.2.1",
171  1);
172 
174  GlobalRoutingLinkRecord::StubNetwork,
175  "10.1.2.1",
176  "255.255.255.252",
177  1);
178 
179  GlobalRoutingLSA* lsa3 = new GlobalRoutingLSA ();
180  lsa3->SetLSType (GlobalRoutingLSA::RouterLSA);
181  lsa3->SetLinkStateId ("0.0.0.3");
182  lsa3->SetAdvertisingRouter ("0.0.0.3");
183  lsa3->AddLinkRecord (lr10);
184  lsa3->AddLinkRecord (lr11);
185 
186  // Test the database
188  srmlsdb->Insert (lsa0->GetLinkStateId (), lsa0);
189  srmlsdb->Insert (lsa1->GetLinkStateId (), lsa1);
190  srmlsdb->Insert (lsa2->GetLinkStateId (), lsa2);
191  srmlsdb->Insert (lsa3->GetLinkStateId (), lsa3);
192  NS_ASSERT (lsa2 == srmlsdb->GetLSA (lsa2->GetLinkStateId ()));
193 
194  // next, calculate routes based on the manually created LSDB
196  srm->DebugUseLsdb (srmlsdb); // manually add in an LSDB
197  // Note-- this will succeed without any nodes in the topology
198  // because the NodeList is empty
199  srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0
200 
201  Simulator::Run ();
202 
204 
205  Simulator::Destroy ();
206 
207  // This delete clears the srm, which deletes the LSDB, which clears
208  // all of the LSAs, which each destroys the attached LinkRecords.
209  delete srm;
210 
212  // No testing has actually been done other than making sure that this code
213  // does not crash
214 }
215 
216 
218 {
219 public:
221  : TestSuite ("global-route-manager-impl", UNIT)
222  {
223  AddTestCase (new GlobalRouteManagerImplTestCase (), TestCase::QUICK);
224  }
SPFVertex * Pop(void)
Pop the Shortest Path First Vertex pointer at the top of the queue.
A suite of tests to run.
Definition: test.h:1333
void SetAdvertisingRouter(Ipv4Address rtr)
Set the Advertising Router as defined by the OSPF spec.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
uint32_t AddLinkRecord(GlobalRoutingLinkRecord *lr)
Add a given Global Routing Link Record to the LSA.
encapsulates test code
Definition: test.h:1147
Vertex used in shortest path first (SPF) computations.
This test suite implements a Unit Test.
Definition: test.h:1343
void SetDistanceFromRoot(uint32_t distance)
Set the distance from the root vertex to "this" SPFVertex object.
A global router implementation.
GlobalRouteManagerImplTestSuite g_globalRoutingManagerImplTestSuite
A Candidate Queue used in routing calculations.
a Link State Advertisement (LSA) for a router, used in global routing.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
void DebugSPFCalculate(Ipv4Address root)
Debugging routine; call the core SPF from the unit tests.
void SetLSType(LSType typ)
Set the LS type field of the LSA.
void Push(SPFVertex *vNew)
Push a Shortest Path First Vertex pointer onto the queue according to the priority scheme...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void DebugUseLsdb(GlobalRouteManagerLSDB *)
Debugging routine; allow client code to supply a pre-built LSDB.
GlobalRoutingLSA * GetLSA(Ipv4Address addr) const
Look up the Link State Advertisement associated with the given link state ID (address).
void Insert(Ipv4Address addr, GlobalRoutingLSA *lsa)
Insert an IP address / Link State Advertisement pair into the Link State Database.
virtual void DoRun(void)
Implementation to actually run this TestCase.
The Link State DataBase (LSDB) of the Global Route Manager.
void SetLinkStateId(Ipv4Address addr)
Set the Link State ID is defined by the OSPF spec.
Ipv4Address GetLinkStateId(void) const
Get the Link State ID as defined by the OSPF spec.