A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
olsr-routing-protocol-test-suite.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2004 Francisco J. Ros
4  * Copyright (c) 2007 INESC Porto
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: Francisco J. Ros <fjrm@dif.um.es>
20  * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
21  */
22 
23 #include "ns3/test.h"
24 #include "ns3/olsr-routing-protocol.h"
25 #include "ns3/ipv4-header.h"
26 
27 /********** Willingness **********/
28 
30 #define OLSR_WILL_NEVER 0
31 #define OLSR_WILL_LOW 1
33 #define OLSR_WILL_DEFAULT 3
35 #define OLSR_WILL_HIGH 6
37 #define OLSR_WILL_ALWAYS 7
39 
40 namespace ns3 {
41 namespace olsr {
42 
44 class OlsrMprTestCase : public TestCase {
45 public:
46  OlsrMprTestCase ();
47  ~OlsrMprTestCase ();
49  virtual void DoRun (void);
50  ;
51 };
52 
53 
54 OlsrMprTestCase::OlsrMprTestCase ()
55  : TestCase ("Check OLSR MPR computing mechanism")
56 {
57 }
58 OlsrMprTestCase::~OlsrMprTestCase ()
59 {
60 }
61 void
63 {
64  Ptr<RoutingProtocol> protocol = CreateObject<RoutingProtocol> ();
65  protocol->m_mainAddress = Ipv4Address ("10.0.0.1");
66  OlsrState & state = protocol->m_state;
67 
68  /*
69  * 1 -- 2
70  * | |
71  * 3 -- 4
72  *
73  * Node 1 must select only one MPR (2 or 3, doesn't matter)
74  */
75  NeighborTuple neigbor;
76  neigbor.status = NeighborTuple::STATUS_SYM;
77  neigbor.willingness = OLSR_WILL_DEFAULT;
78  neigbor.neighborMainAddr = Ipv4Address ("10.0.0.2");
79  protocol->m_state.InsertNeighborTuple (neigbor);
80  neigbor.neighborMainAddr = Ipv4Address ("10.0.0.3");
81  protocol->m_state.InsertNeighborTuple (neigbor);
82  TwoHopNeighborTuple tuple;
83  tuple.expirationTime = Seconds (3600);
84  tuple.neighborMainAddr = Ipv4Address ("10.0.0.2");
85  tuple.twoHopNeighborAddr = Ipv4Address ("10.0.0.4");
86  protocol->m_state.InsertTwoHopNeighborTuple (tuple);
87  tuple.neighborMainAddr = Ipv4Address ("10.0.0.3");
88  tuple.twoHopNeighborAddr = Ipv4Address ("10.0.0.4");
89  protocol->m_state.InsertTwoHopNeighborTuple (tuple);
90 
91  protocol->MprComputation ();
92  NS_TEST_EXPECT_MSG_EQ (state.GetMprSet ().size (), 1, "An only address must be chosen.");
93  /*
94  * 1 -- 2 -- 5
95  * | |
96  * 3 -- 4
97  *
98  * Node 1 must select node 2 as MPR.
99  */
100  tuple.neighborMainAddr = Ipv4Address ("10.0.0.2");
101  tuple.twoHopNeighborAddr = Ipv4Address ("10.0.0.5");
102  protocol->m_state.InsertTwoHopNeighborTuple (tuple);
103 
104  protocol->MprComputation ();
105  MprSet mpr = state.GetMprSet ();
106  NS_TEST_EXPECT_MSG_EQ (mpr.size (), 1, "An only address must be chosen.");
107  NS_TEST_EXPECT_MSG_EQ ((mpr.find ("10.0.0.2") != mpr.end ()), true, "Node 1 must select node 2 as MPR");
108  /*
109  * 1 -- 2 -- 5
110  * | |
111  * 3 -- 4
112  * |
113  * 6
114  *
115  * Node 1 must select nodes 2 and 3 as MPRs.
116  */
117  tuple.neighborMainAddr = Ipv4Address ("10.0.0.3");
118  tuple.twoHopNeighborAddr = Ipv4Address ("10.0.0.6");
119  protocol->m_state.InsertTwoHopNeighborTuple (tuple);
120 
121  protocol->MprComputation ();
122  mpr = state.GetMprSet ();
123  NS_TEST_EXPECT_MSG_EQ (mpr.size (), 2, "An only address must be chosen.");
124  NS_TEST_EXPECT_MSG_EQ ((mpr.find ("10.0.0.2") != mpr.end ()), true, "Node 1 must select node 2 as MPR");
125  NS_TEST_EXPECT_MSG_EQ ((mpr.find ("10.0.0.3") != mpr.end ()), true, "Node 1 must select node 3 as MPR");
126  /*
127  * 7 (OLSR_WILL_ALWAYS)
128  * |
129  * 1 -- 2 -- 5
130  * | |
131  * 3 -- 4
132  * |
133  * 6
134  *
135  * Node 1 must select nodes 2, 3 and 7 (since it is WILL_ALWAYS) as MPRs.
136  */
137  neigbor.willingness = OLSR_WILL_ALWAYS;
138  neigbor.neighborMainAddr = Ipv4Address ("10.0.0.7");
139  protocol->m_state.InsertNeighborTuple (neigbor);
140 
141  protocol->MprComputation ();
142  mpr = state.GetMprSet ();
143  NS_TEST_EXPECT_MSG_EQ (mpr.size (), 3, "An only address must be chosen.");
144  NS_TEST_EXPECT_MSG_EQ ((mpr.find ("10.0.0.7") != mpr.end ()), true, "Node 1 must select node 7 as MPR");
145  /*
146  * 7 <- WILL_ALWAYS
147  * |
148  * 9 -- 8 -- 1 -- 2 -- 5
149  * | |
150  * ^ 3 -- 4
151  * | |
152  * WILL_NEVER 6
153  *
154  * Node 1 must select nodes 2, 3 and 7 (since it is WILL_ALWAYS) as MPRs.
155  * Node 1 must NOT select node 8 as MPR since it is WILL_NEVER
156  */
157  neigbor.willingness = OLSR_WILL_NEVER;
158  neigbor.neighborMainAddr = Ipv4Address ("10.0.0.8");
159  protocol->m_state.InsertNeighborTuple (neigbor);
160  tuple.neighborMainAddr = Ipv4Address ("10.0.0.8");
161  tuple.twoHopNeighborAddr = Ipv4Address ("10.0.0.9");
162  protocol->m_state.InsertTwoHopNeighborTuple (tuple);
163 
164  protocol->MprComputation ();
165  mpr = state.GetMprSet ();
166  NS_TEST_EXPECT_MSG_EQ (mpr.size (), 3, "An only address must be chosen.");
167  NS_TEST_EXPECT_MSG_EQ ((mpr.find ("10.0.0.9") == mpr.end ()), true, "Node 1 must NOT select node 8 as MPR");
168 }
169 
170 static class OlsrProtocolTestSuite : public TestSuite
171 {
172 public:
174 } g_olsrProtocolTestSuite;
175 
176 OlsrProtocolTestSuite::OlsrProtocolTestSuite()
177  : TestSuite ("routing-olsr", UNIT)
178 {
179  AddTestCase (new OlsrMprTestCase (), TestCase::QUICK);
180 }
181 
182 }
183 } // namespace olsr, ns3
184 
185 
This class encapsulates all data structures needed for maintaining internal state of an OLSR node...
Definition: olsr-state.h:35
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
A suite of tests to run.
Definition: test.h:962
Testcase for MPR computation mechanism.
encapsulates test code
Definition: test.h:834
virtual void DoRun(void)
Run test case.
uint8_t willingness
A value between 0 and 7 specifying the node's willingness to carry traffic on behalf of other nodes...
Time expirationTime
Time at which this tuple expires and must be removed.
Ipv4Address neighborMainAddr
Main address of a neighbor node.
Ipv4Address twoHopNeighborAddr
Main address of a 2-hop neighbor with a symmetric link to nb_main_addr.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
Definition: test.cc:172
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
MprSet GetMprSet() const
Gets an MPR Set needed by tests.
Definition: olsr-state.cc:276
Ipv4Address neighborMainAddr
Main address of a neighbor.