A Discrete-Event Network Simulator
API
olsr-routing-protocol-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 Francisco J. Ros
3  * Copyright (c) 2007 INESC Porto
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: Francisco J. Ros <fjrm@dif.um.es>
19  * Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
20  */
21 
22 #include "ns3/ipv4-header.h"
23 #include "ns3/olsr-routing-protocol.h"
24 #include "ns3/test.h"
25 
31 /********** Willingness **********/
32 
34 #define OLSR_WILL_NEVER 0
36 #define OLSR_WILL_LOW 1
38 #define OLSR_WILL_DEFAULT 3
40 #define OLSR_WILL_HIGH 6
42 #define OLSR_WILL_ALWAYS 7
43 
44 using namespace ns3;
45 using namespace olsr;
46 
53 class OlsrMprTestCase : public TestCase
54 {
55  public:
57  ~OlsrMprTestCase() override;
58  void DoRun() override;
59 };
60 
62  : TestCase("Check OLSR MPR computing mechanism")
63 {
64 }
65 
67 {
68 }
69 
70 void
72 {
73  Ptr<RoutingProtocol> protocol = CreateObject<RoutingProtocol>();
74  protocol->m_mainAddress = Ipv4Address("10.0.0.1");
75  OlsrState& state = protocol->m_state;
76 
77  /*
78  * 1 -- 2
79  * | |
80  * 3 -- 4
81  *
82  * Node 1 must select only one MPR (2 or 3, doesn't matter)
83  */
84  NeighborTuple neighbor;
85  neighbor.status = NeighborTuple::STATUS_SYM;
86  neighbor.willingness = OLSR_WILL_DEFAULT;
87  neighbor.neighborMainAddr = Ipv4Address("10.0.0.2");
88  protocol->m_state.InsertNeighborTuple(neighbor);
89  neighbor.neighborMainAddr = Ipv4Address("10.0.0.3");
90  protocol->m_state.InsertNeighborTuple(neighbor);
91  TwoHopNeighborTuple tuple;
92  tuple.expirationTime = Seconds(3600);
93  tuple.neighborMainAddr = Ipv4Address("10.0.0.2");
94  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.4");
95  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
96  tuple.neighborMainAddr = Ipv4Address("10.0.0.3");
97  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.4");
98  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
99 
100  protocol->MprComputation();
101  NS_TEST_EXPECT_MSG_EQ(state.GetMprSet().size(), 1, "An only address must be chosen.");
102  /*
103  * 1 -- 2 -- 5
104  * | |
105  * 3 -- 4
106  *
107  * Node 1 must select node 2 as MPR.
108  */
109  tuple.neighborMainAddr = Ipv4Address("10.0.0.2");
110  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.5");
111  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
112 
113  protocol->MprComputation();
114  MprSet mpr = state.GetMprSet();
115  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 1, "An only address must be chosen.");
116  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.2") != mpr.end()),
117  true,
118  "Node 1 must select node 2 as MPR");
119  /*
120  * 1 -- 2 -- 5
121  * | |
122  * 3 -- 4
123  * |
124  * 6
125  *
126  * Node 1 must select nodes 2 and 3 as MPRs.
127  */
128  tuple.neighborMainAddr = Ipv4Address("10.0.0.3");
129  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.6");
130  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
131 
132  protocol->MprComputation();
133  mpr = state.GetMprSet();
134  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 2, "An only address must be chosen.");
135  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.2") != mpr.end()),
136  true,
137  "Node 1 must select node 2 as MPR");
138  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.3") != mpr.end()),
139  true,
140  "Node 1 must select node 3 as MPR");
141  /*
142  * 7 (OLSR_WILL_ALWAYS)
143  * |
144  * 1 -- 2 -- 5
145  * | |
146  * 3 -- 4
147  * |
148  * 6
149  *
150  * Node 1 must select nodes 2, 3 and 7 (since it is WILL_ALWAYS) as MPRs.
151  */
152  neighbor.willingness = OLSR_WILL_ALWAYS;
153  neighbor.neighborMainAddr = Ipv4Address("10.0.0.7");
154  protocol->m_state.InsertNeighborTuple(neighbor);
155 
156  protocol->MprComputation();
157  mpr = state.GetMprSet();
158  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 3, "An only address must be chosen.");
159  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.7") != mpr.end()),
160  true,
161  "Node 1 must select node 7 as MPR");
162  /*
163  * 7 <- WILL_ALWAYS
164  * |
165  * 9 -- 8 -- 1 -- 2 -- 5
166  * | |
167  * ^ 3 -- 4
168  * | |
169  * WILL_NEVER 6
170  *
171  * Node 1 must select nodes 2, 3 and 7 (since it is WILL_ALWAYS) as MPRs.
172  * Node 1 must NOT select node 8 as MPR since it is WILL_NEVER
173  */
174  neighbor.willingness = OLSR_WILL_NEVER;
175  neighbor.neighborMainAddr = Ipv4Address("10.0.0.8");
176  protocol->m_state.InsertNeighborTuple(neighbor);
177  tuple.neighborMainAddr = Ipv4Address("10.0.0.8");
178  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.9");
179  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
180 
181  protocol->MprComputation();
182  mpr = state.GetMprSet();
183  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 3, "An only address must be chosen.");
184  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.9") == mpr.end()),
185  true,
186  "Node 1 must NOT select node 8 as MPR");
187 }
188 
196 {
197  public:
199 };
200 
202  : TestSuite("routing-olsr", UNIT)
203 {
204  AddTestCase(new OlsrMprTestCase(), TestCase::QUICK);
205 }
206 
Testcase for MPR computation mechanism.
void DoRun() override
Implementation to actually run this TestCase.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
std::set< Ipv4Address > MprSet
MPR Set type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Definition: olsr.py:1
#define OLSR_WILL_DEFAULT
Willingness for forwarding packets from other nodes: medium.
#define OLSR_WILL_NEVER
Willingness for forwarding packets from other nodes: never.
#define OLSR_WILL_ALWAYS
Willingness for forwarding packets from other nodes: always.
static OlsrProtocolTestSuite g_olsrProtocolTestSuite
Static variable for test initialization.