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 
46 {
47 public:
49  virtual void DoRun (void);
50 };
51 
53  : TestCase ("GlobalRouteManagerImplTestCase")
54 {
55 }
56 void
58 {
59  CandidateQueue candidate;
60 
61  for (int i = 0; i < 100; ++i)
62  {
63  SPFVertex *v = new SPFVertex;
64  v->SetDistanceFromRoot (std::rand () % 100);
65  candidate.Push (v);
66  }
67 
68  for (int i = 0; i < 100; ++i)
69  {
70  SPFVertex *v = candidate.Pop ();
71  delete v;
72  v = 0;
73  }
74 
75  // Build fake link state database; four routers (0-3), 3 point-to-point
76  // links
77  //
78  // n0
79  // \ link 0
80  // \ link 2
81  // n2 -------------------------n3
82  // /
83  // / link 1
84  // n1
85  //
86  // link0: 10.1.1.1/30, 10.1.1.2/30
87  // link1: 10.1.2.1/30, 10.1.2.2/30
88  // link2: 10.1.3.1/30, 10.1.3.2/30
89  //
90  // Router 0
92  GlobalRoutingLinkRecord::PointToPoint,
93  "0.0.0.2", // router ID 0.0.0.2
94  "10.1.1.1", // local ID
95  1); // metric
96 
98  GlobalRoutingLinkRecord::StubNetwork,
99  "10.1.1.1",
100  "255.255.255.252",
101  1);
102 
103  GlobalRoutingLSA* lsa0 = new GlobalRoutingLSA ();
104  lsa0->SetLSType (GlobalRoutingLSA::RouterLSA);
105  lsa0->SetLinkStateId ("0.0.0.0");
106  lsa0->SetAdvertisingRouter ("0.0.0.0");
107  lsa0->AddLinkRecord (lr0);
108  lsa0->AddLinkRecord (lr1);
109 
110  // Router 1
112  GlobalRoutingLinkRecord::PointToPoint,
113  "0.0.0.2",
114  "10.1.2.1",
115  1);
116 
118  GlobalRoutingLinkRecord::StubNetwork,
119  "10.1.2.1",
120  "255.255.255.252",
121  1);
122 
123  GlobalRoutingLSA* lsa1 = new GlobalRoutingLSA ();
124  lsa1->SetLSType (GlobalRoutingLSA::RouterLSA);
125  lsa1->SetLinkStateId ("0.0.0.1");
126  lsa1->SetAdvertisingRouter ("0.0.0.1");
127  lsa1->AddLinkRecord (lr2);
128  lsa1->AddLinkRecord (lr3);
129 
130  // Router 2
132  GlobalRoutingLinkRecord::PointToPoint,
133  "0.0.0.0",
134  "10.1.1.2",
135  1);
136 
138  GlobalRoutingLinkRecord::StubNetwork,
139  "10.1.1.2",
140  "255.255.255.252",
141  1);
142 
144  GlobalRoutingLinkRecord::PointToPoint,
145  "0.0.0.1",
146  "10.1.2.2",
147  1);
148 
150  GlobalRoutingLinkRecord::StubNetwork,
151  "10.1.2.2",
152  "255.255.255.252",
153  1);
154 
156  GlobalRoutingLinkRecord::PointToPoint,
157  "0.0.0.3",
158  "10.1.3.2",
159  1);
160 
162  GlobalRoutingLinkRecord::StubNetwork,
163  "10.1.3.2",
164  "255.255.255.252",
165  1);
166 
167  GlobalRoutingLSA* lsa2 = new GlobalRoutingLSA ();
168  lsa2->SetLSType (GlobalRoutingLSA::RouterLSA);
169  lsa2->SetLinkStateId ("0.0.0.2");
170  lsa2->SetAdvertisingRouter ("0.0.0.2");
171  lsa2->AddLinkRecord (lr4);
172  lsa2->AddLinkRecord (lr5);
173  lsa2->AddLinkRecord (lr6);
174  lsa2->AddLinkRecord (lr7);
175  lsa2->AddLinkRecord (lr8);
176  lsa2->AddLinkRecord (lr9);
177 
178  // Router 3
180  GlobalRoutingLinkRecord::PointToPoint,
181  "0.0.0.2",
182  "10.1.2.1",
183  1);
184 
186  GlobalRoutingLinkRecord::StubNetwork,
187  "10.1.2.1",
188  "255.255.255.252",
189  1);
190 
191  GlobalRoutingLSA* lsa3 = new GlobalRoutingLSA ();
192  lsa3->SetLSType (GlobalRoutingLSA::RouterLSA);
193  lsa3->SetLinkStateId ("0.0.0.3");
194  lsa3->SetAdvertisingRouter ("0.0.0.3");
195  lsa3->AddLinkRecord (lr10);
196  lsa3->AddLinkRecord (lr11);
197 
198  // Test the database
200  srmlsdb->Insert (lsa0->GetLinkStateId (), lsa0);
201  srmlsdb->Insert (lsa1->GetLinkStateId (), lsa1);
202  srmlsdb->Insert (lsa2->GetLinkStateId (), lsa2);
203  srmlsdb->Insert (lsa3->GetLinkStateId (), lsa3);
204  NS_ASSERT (lsa2 == srmlsdb->GetLSA (lsa2->GetLinkStateId ()));
205 
206  // next, calculate routes based on the manually created LSDB
208  srm->DebugUseLsdb (srmlsdb); // manually add in an LSDB
209  // Note-- this will succeed without any nodes in the topology
210  // because the NodeList is empty
211  srm->DebugSPFCalculate (lsa0->GetLinkStateId ()); // node n0
212 
213  Simulator::Run ();
214 
216 
217  Simulator::Destroy ();
218 
219  // This delete clears the srm, which deletes the LSDB, which clears
220  // all of the LSAs, which each destroys the attached LinkRecords.
221  delete srm;
222 
224  // No testing has actually been done other than making sure that this code
225  // does not crash
226 }
227 
228 
236 {
237 public:
239 private:
240 };
241 
243  : TestSuite ("global-route-manager-impl", UNIT)
244 {
245  AddTestCase (new GlobalRouteManagerImplTestCase (), TestCase::QUICK);
246 }
247 
GlobalRoutingLSA * GetLSA(Ipv4Address addr) const
Look up the Link State Advertisement associated with the given link state ID (address).
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:1342
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:1155
Vertex used in shortest path first (SPF) computations.
void SetDistanceFromRoot(uint32_t distance)
Set the distance from the root vertex to "this" SPFVertex object.
A global router implementation.
A Candidate Queue used in routing calculations.
a Link State Advertisement (LSA) for a router, used in global routing.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
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.
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.
static GlobalRouteManagerImplTestSuite g_globalRoutingManagerImplTestSuite
Static variable for test initialization.
The Link State DataBase (LSDB) of the Global Route Manager.
Ipv4Address GetLinkStateId(void) const
Get the Link State ID as defined by the OSPF spec.
void SetLinkStateId(Ipv4Address addr)
Set the Link State ID is defined by the OSPF spec.