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 
33 /**
34  * \ingroup internet
35  * \defgroup internet-test internet module tests
36  */
37 
38 
39 /**
40  * \ingroup internet-test
41  * \ingroup tests
42  *
43  * \brief Global Route Manager Test
44  */
45 class GlobalRouteManagerImplTestCase : public TestCase
46 {
47 public:
48   GlobalRouteManagerImplTestCase();
49   virtual void DoRun (void);
50 };
51 
GlobalRouteManagerImplTestCase()52 GlobalRouteManagerImplTestCase::GlobalRouteManagerImplTestCase()
53   : TestCase ("GlobalRouteManagerImplTestCase")
54 {
55 }
56 void
DoRun(void)57 GlobalRouteManagerImplTestCase::DoRun (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
91   GlobalRoutingLinkRecord* lr0 = new GlobalRoutingLinkRecord (
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 
97   GlobalRoutingLinkRecord* lr1 = new GlobalRoutingLinkRecord (
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
111   GlobalRoutingLinkRecord* lr2 = new GlobalRoutingLinkRecord (
112       GlobalRoutingLinkRecord::PointToPoint,
113       "0.0.0.2",
114       "10.1.2.1",
115       1);
116 
117   GlobalRoutingLinkRecord* lr3 = new GlobalRoutingLinkRecord (
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
131   GlobalRoutingLinkRecord* lr4 = new GlobalRoutingLinkRecord (
132       GlobalRoutingLinkRecord::PointToPoint,
133       "0.0.0.0",
134       "10.1.1.2",
135       1);
136 
137   GlobalRoutingLinkRecord* lr5 = new GlobalRoutingLinkRecord (
138       GlobalRoutingLinkRecord::StubNetwork,
139       "10.1.1.2",
140       "255.255.255.252",
141       1);
142 
143   GlobalRoutingLinkRecord* lr6 = new GlobalRoutingLinkRecord (
144       GlobalRoutingLinkRecord::PointToPoint,
145       "0.0.0.1",
146       "10.1.2.2",
147       1);
148 
149   GlobalRoutingLinkRecord* lr7 = new GlobalRoutingLinkRecord (
150       GlobalRoutingLinkRecord::StubNetwork,
151       "10.1.2.2",
152       "255.255.255.252",
153       1);
154 
155   GlobalRoutingLinkRecord* lr8 = new GlobalRoutingLinkRecord (
156       GlobalRoutingLinkRecord::PointToPoint,
157       "0.0.0.3",
158       "10.1.3.2",
159       1);
160 
161   GlobalRoutingLinkRecord* lr9 = new GlobalRoutingLinkRecord (
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
179   GlobalRoutingLinkRecord* lr10 = new GlobalRoutingLinkRecord (
180       GlobalRoutingLinkRecord::PointToPoint,
181       "0.0.0.2",
182       "10.1.2.1",
183       1);
184 
185   GlobalRoutingLinkRecord* lr11 = new GlobalRoutingLinkRecord (
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
199   GlobalRouteManagerLSDB* srmlsdb = new GlobalRouteManagerLSDB ();
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
207   GlobalRouteManagerImpl* srm = new GlobalRouteManagerImpl ();
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 
215 /// \todo here we should do some verification of the routes built
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 
223   /// \todo Testing
224   // No testing has actually been done other than making sure that this code
225   // does not crash
226 }
227 
228 
229 /**
230  * \ingroup internet-test
231  * \ingroup tests
232  *
233  * \brief Global Route Manager TestSuite
234  */
235 class GlobalRouteManagerImplTestSuite : public TestSuite
236 {
237 public:
238   GlobalRouteManagerImplTestSuite ();
239 private:
240 };
241 
GlobalRouteManagerImplTestSuite()242 GlobalRouteManagerImplTestSuite::GlobalRouteManagerImplTestSuite ()
243   : TestSuite ("global-route-manager-impl", UNIT)
244 {
245   AddTestCase (new GlobalRouteManagerImplTestCase (), TestCase::QUICK);
246 }
247 
248 static GlobalRouteManagerImplTestSuite g_globalRoutingManagerImplTestSuite; //!< Static variable for test initialization
249