A Discrete-Event Network Simulator
API
global-route-manager-impl-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2007 University of Washington
3  * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
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  * Authors: Tom Henderson (tomhend@u.washington.edu)
19  *
20  * Kunihiro Ishigura, Toshiaki Takada (GNU Zebra) are attributed authors
21  * of the quagga 0.99.7/src/ospfd/ospf_spf.c code which was ported here
22  */
23 
24 #include "ns3/candidate-queue.h"
25 #include "ns3/global-route-manager-impl.h"
26 #include "ns3/simulator.h"
27 #include "ns3/test.h"
28 
29 #include <cstdlib> // for rand()
30 
31 using namespace ns3;
32 
45 {
46  public:
48  void DoRun() override;
49 };
50 
52  : TestCase("GlobalRouteManagerImplTestCase")
53 {
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 = nullptr;
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  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
93  "0.0.0.2", // router ID 0.0.0.2
94  "10.1.1.1", // local ID
95  1); // metric
96 
97  GlobalRoutingLinkRecord* lr1 = new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
98  "10.1.1.1",
99  "255.255.255.252",
100  1);
101 
102  GlobalRoutingLSA* lsa0 = new GlobalRoutingLSA();
103  lsa0->SetLSType(GlobalRoutingLSA::RouterLSA);
104  lsa0->SetLinkStateId("0.0.0.0");
105  lsa0->SetAdvertisingRouter("0.0.0.0");
106  lsa0->AddLinkRecord(lr0);
107  lsa0->AddLinkRecord(lr1);
108 
109  // Router 1
111  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
112  "0.0.0.2",
113  "10.1.2.1",
114  1);
115 
116  GlobalRoutingLinkRecord* lr3 = new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
117  "10.1.2.1",
118  "255.255.255.252",
119  1);
120 
121  GlobalRoutingLSA* lsa1 = new GlobalRoutingLSA();
122  lsa1->SetLSType(GlobalRoutingLSA::RouterLSA);
123  lsa1->SetLinkStateId("0.0.0.1");
124  lsa1->SetAdvertisingRouter("0.0.0.1");
125  lsa1->AddLinkRecord(lr2);
126  lsa1->AddLinkRecord(lr3);
127 
128  // Router 2
130  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
131  "0.0.0.0",
132  "10.1.1.2",
133  1);
134 
135  GlobalRoutingLinkRecord* lr5 = new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
136  "10.1.1.2",
137  "255.255.255.252",
138  1);
139 
141  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
142  "0.0.0.1",
143  "10.1.2.2",
144  1);
145 
146  GlobalRoutingLinkRecord* lr7 = new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
147  "10.1.2.2",
148  "255.255.255.252",
149  1);
150 
152  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
153  "0.0.0.3",
154  "10.1.3.2",
155  1);
156 
157  GlobalRoutingLinkRecord* lr9 = new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
158  "10.1.3.2",
159  "255.255.255.252",
160  1);
161 
162  GlobalRoutingLSA* lsa2 = new GlobalRoutingLSA();
163  lsa2->SetLSType(GlobalRoutingLSA::RouterLSA);
164  lsa2->SetLinkStateId("0.0.0.2");
165  lsa2->SetAdvertisingRouter("0.0.0.2");
166  lsa2->AddLinkRecord(lr4);
167  lsa2->AddLinkRecord(lr5);
168  lsa2->AddLinkRecord(lr6);
169  lsa2->AddLinkRecord(lr7);
170  lsa2->AddLinkRecord(lr8);
171  lsa2->AddLinkRecord(lr9);
172 
173  // Router 3
175  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::PointToPoint,
176  "0.0.0.2",
177  "10.1.2.1",
178  1);
179 
181  new GlobalRoutingLinkRecord(GlobalRoutingLinkRecord::StubNetwork,
182  "10.1.2.1",
183  "255.255.255.252",
184  1);
185 
186  GlobalRoutingLSA* lsa3 = new GlobalRoutingLSA();
187  lsa3->SetLSType(GlobalRoutingLSA::RouterLSA);
188  lsa3->SetLinkStateId("0.0.0.3");
189  lsa3->SetAdvertisingRouter("0.0.0.3");
190  lsa3->AddLinkRecord(lr10);
191  lsa3->AddLinkRecord(lr11);
192 
193  // Test the database
195  srmlsdb->Insert(lsa0->GetLinkStateId(), lsa0);
196  srmlsdb->Insert(lsa1->GetLinkStateId(), lsa1);
197  srmlsdb->Insert(lsa2->GetLinkStateId(), lsa2);
198  srmlsdb->Insert(lsa3->GetLinkStateId(), lsa3);
200  srmlsdb->GetLSA(lsa2->GetLinkStateId()),
201  "The Ipv4Address is not stored as the link state ID");
202 
203  // next, calculate routes based on the manually created LSDB
205  srm->DebugUseLsdb(srmlsdb); // manually add in an LSDB
206  // Note-- this will succeed without any nodes in the topology
207  // because the NodeList is empty
208  srm->DebugSPFCalculate(lsa0->GetLinkStateId()); // node n0
209 
210  Simulator::Run();
211 
213 
214  Simulator::Destroy();
215 
216  // This delete clears the srm, which deletes the LSDB, which clears
217  // all of the LSAs, which each destroys the attached LinkRecords.
218  delete srm;
219 
221  // No testing has actually been done other than making sure that this code
222  // does not crash
223 }
224 
231 {
232  public:
234 
235  private:
236 };
237 
239  : TestSuite("global-route-manager-impl", UNIT)
240 {
241  AddTestCase(new GlobalRouteManagerImplTestCase(), TestCase::QUICK);
242 }
243 
void DoRun() override
Implementation to actually run this TestCase.
A Candidate Queue used in routing calculations.
SPFVertex * Pop()
Pop the Shortest Path First Vertex pointer at the top of the queue.
void Push(SPFVertex *vNew)
Push a Shortest Path First Vertex pointer onto the queue according to the priority scheme.
A global router implementation.
void DebugUseLsdb(GlobalRouteManagerLSDB *lsdb)
Debugging routine; allow client code to supply a pre-built LSDB.
void DebugSPFCalculate(Ipv4Address root)
Debugging routine; call the core SPF from the unit tests.
The Link State DataBase (LSDB) of the Global Route Manager.
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.
a Link State Advertisement (LSA) for a router, used in global routing.
uint32_t AddLinkRecord(GlobalRoutingLinkRecord *lr)
Add a given Global Routing Link Record to the LSA.
void SetLSType(LSType typ)
Set the LS type field of the LSA.
void SetAdvertisingRouter(Ipv4Address rtr)
Set the Advertising Router as defined by the OSPF spec.
void SetLinkStateId(Ipv4Address addr)
Set the Link State ID is defined by the OSPF spec.
Ipv4Address GetLinkStateId() const
Get the Link State ID as defined by the OSPF spec.
Vertex used in shortest path first (SPF) computations.
void SetDistanceFromRoot(uint32_t distance)
Set the distance from the root vertex to "this" SPFVertex object.
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
static GlobalRouteManagerImplTestSuite g_globalRoutingManagerImplTestSuite
Static variable for test initialization.
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.