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-repositories.h"
24 #include "ns3/olsr-routing-protocol.h"
25 #include "ns3/test.h"
26 
32 using namespace ns3;
33 using namespace olsr;
34 
41 class OlsrMprTestCase : public TestCase
42 {
43  public:
45  ~OlsrMprTestCase() override;
46  void DoRun() override;
47 };
48 
50  : TestCase("Check OLSR MPR computing mechanism")
51 {
52 }
53 
55 {
56 }
57 
58 void
60 {
61  Ptr<RoutingProtocol> protocol = CreateObject<RoutingProtocol>();
62  protocol->m_mainAddress = Ipv4Address("10.0.0.1");
63  OlsrState& state = protocol->m_state;
64 
65  /*
66  * 1 -- 2
67  * | |
68  * 3 -- 4
69  *
70  * Node 1 must select only one MPR (2 or 3, doesn't matter)
71  */
72  NeighborTuple neighbor;
73  neighbor.status = NeighborTuple::STATUS_SYM;
74  neighbor.willingness = Willingness::DEFAULT;
75  neighbor.neighborMainAddr = Ipv4Address("10.0.0.2");
76  protocol->m_state.InsertNeighborTuple(neighbor);
77  neighbor.neighborMainAddr = Ipv4Address("10.0.0.3");
78  protocol->m_state.InsertNeighborTuple(neighbor);
79  TwoHopNeighborTuple tuple;
80  tuple.expirationTime = Seconds(3600);
81  tuple.neighborMainAddr = Ipv4Address("10.0.0.2");
82  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.4");
83  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
84  tuple.neighborMainAddr = Ipv4Address("10.0.0.3");
85  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.4");
86  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
87 
88  protocol->MprComputation();
89  NS_TEST_EXPECT_MSG_EQ(state.GetMprSet().size(), 1, "An only address must be chosen.");
90  /*
91  * 1 -- 2 -- 5
92  * | |
93  * 3 -- 4
94  *
95  * Node 1 must select node 2 as MPR.
96  */
97  tuple.neighborMainAddr = Ipv4Address("10.0.0.2");
98  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.5");
99  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
100 
101  protocol->MprComputation();
102  MprSet mpr = state.GetMprSet();
103  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 1, "An only address must be chosen.");
104  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.2") != mpr.end()),
105  true,
106  "Node 1 must select node 2 as MPR");
107  /*
108  * 1 -- 2 -- 5
109  * | |
110  * 3 -- 4
111  * |
112  * 6
113  *
114  * Node 1 must select nodes 2 and 3 as MPRs.
115  */
116  tuple.neighborMainAddr = Ipv4Address("10.0.0.3");
117  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.6");
118  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
119 
120  protocol->MprComputation();
121  mpr = state.GetMprSet();
122  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 2, "An only address must be chosen.");
123  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.2") != mpr.end()),
124  true,
125  "Node 1 must select node 2 as MPR");
126  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.3") != mpr.end()),
127  true,
128  "Node 1 must select node 3 as MPR");
129  /*
130  * 7 (Willingness::ALWAYS)
131  * |
132  * 1 -- 2 -- 5
133  * | |
134  * 3 -- 4
135  * |
136  * 6
137  *
138  * Node 1 must select nodes 2, 3 and 7 (since it is Willingness::ALWAYS) as MPRs.
139  */
140  neighbor.willingness = olsr::Willingness::ALWAYS;
141  neighbor.neighborMainAddr = Ipv4Address("10.0.0.7");
142  protocol->m_state.InsertNeighborTuple(neighbor);
143 
144  protocol->MprComputation();
145  mpr = state.GetMprSet();
146  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 3, "An only address must be chosen.");
147  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.7") != mpr.end()),
148  true,
149  "Node 1 must select node 7 as MPR");
150  /*
151  * 7 <- Willingness::ALWAYS
152  * |
153  * 9 -- 8 -- 1 -- 2 -- 5
154  * | |
155  * ^ 3 -- 4
156  * | |
157  * Willingness::NEVER 6
158  *
159  * Node 1 must select nodes 2, 3 and 7 (since it is Willingness::ALWAYS) as MPRs.
160  * Node 1 must NOT select node 8 as MPR since it is Willingness::NEVER
161  */
162  neighbor.willingness = Willingness::NEVER;
163  neighbor.neighborMainAddr = Ipv4Address("10.0.0.8");
164  protocol->m_state.InsertNeighborTuple(neighbor);
165  tuple.neighborMainAddr = Ipv4Address("10.0.0.8");
166  tuple.twoHopNeighborAddr = Ipv4Address("10.0.0.9");
167  protocol->m_state.InsertTwoHopNeighborTuple(tuple);
168 
169  protocol->MprComputation();
170  mpr = state.GetMprSet();
171  NS_TEST_EXPECT_MSG_EQ(mpr.size(), 3, "An only address must be chosen.");
172  NS_TEST_EXPECT_MSG_EQ((mpr.find("10.0.0.9") == mpr.end()),
173  true,
174  "Node 1 must NOT select node 8 as MPR");
175 }
176 
184 {
185  public:
187 };
188 
190  : TestSuite("routing-olsr", UNIT)
191 {
192  AddTestCase(new OlsrMprTestCase(), TestCase::QUICK);
193 }
194 
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:42
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:301
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:1326
std::set< Ipv4Address > MprSet
MPR Set type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Definition: olsr.py:1
static OlsrProtocolTestSuite g_olsrProtocolTestSuite
Static variable for test initialization.