A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
olsr-header-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INESC Porto
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
18 */
19
20#include "ns3/olsr-header.h"
21#include "ns3/olsr-repositories.h"
22#include "ns3/packet.h"
23#include "ns3/test.h"
24
25using namespace ns3;
26
27/**
28 * \ingroup olsr-test
29 * \ingroup tests
30 *
31 * Check Emf olsr time conversion
32 */
34{
35 public:
37 void DoRun() override;
38};
39
41 : TestCase("Check Emf olsr time conversion")
42{
43}
44
45void
47{
48 for (int time = 1; time <= 30; time++)
49 {
50 uint8_t emf = olsr::SecondsToEmf(time);
51 double seconds = olsr::EmfToSeconds(emf);
52 NS_TEST_ASSERT_MSG_EQ((seconds < 0 || std::fabs(seconds - time) > 0.1), false, "100");
53 }
54}
55
56/**
57 * \ingroup olsr-test
58 * \ingroup tests
59 *
60 * Check Mid olsr messages
61 */
63{
64 public:
66 void DoRun() override;
67};
68
70 : TestCase("Check Mid olsr messages")
71{
72}
73
74void
76{
77 Packet packet;
78
79 {
82 olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
84 olsr::MessageHeader::Mid& mid2 = msg2.GetMid();
85
86 // MID message #1
87 {
88 std::vector<Ipv4Address>& addresses = mid1.interfaceAddresses;
89 addresses.clear();
90 addresses.emplace_back("1.2.3.4");
91 addresses.emplace_back("1.2.3.5");
92 }
93
94 msg1.SetTimeToLive(255);
95 msg1.SetOriginatorAddress(Ipv4Address("11.22.33.44"));
96 msg1.SetVTime(Seconds(9));
98
99 // MID message #2
100 {
101 std::vector<Ipv4Address>& addresses = mid2.interfaceAddresses;
102 addresses.clear();
103 addresses.emplace_back("2.2.3.4");
104 addresses.emplace_back("2.2.3.5");
105 }
106
107 msg2.SetTimeToLive(254);
108 msg2.SetOriginatorAddress(Ipv4Address("12.22.33.44"));
109 msg2.SetVTime(Seconds(10));
112
113 // Build an OLSR packet header
115 msg2.GetSerializedSize());
117
118 // Now add all the headers in the correct order
119 packet.AddHeader(msg2);
120 packet.AddHeader(msg1);
121 packet.AddHeader(hdr);
122 }
123
124 {
126 packet.RemoveHeader(hdr);
128 uint32_t sizeLeft = hdr.GetPacketLength() - hdr.GetSerializedSize();
129 {
131
132 packet.RemoveHeader(msg1);
133
134 NS_TEST_ASSERT_MSG_EQ(msg1.GetTimeToLive(), 255, "201");
135 NS_TEST_ASSERT_MSG_EQ(msg1.GetOriginatorAddress(), Ipv4Address("11.22.33.44"), "202");
136 NS_TEST_ASSERT_MSG_EQ(msg1.GetVTime(), Seconds(9), "203");
139
140 olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
141 NS_TEST_ASSERT_MSG_EQ(mid1.interfaceAddresses.size(), 2, "206");
142 NS_TEST_ASSERT_MSG_EQ(*mid1.interfaceAddresses.begin(), Ipv4Address("1.2.3.4"), "207");
143
144 sizeLeft -= msg1.GetSerializedSize();
145 NS_TEST_ASSERT_MSG_EQ((sizeLeft > 0), true, "208");
146 }
147 {
148 // now read the second message
150
151 packet.RemoveHeader(msg2);
152
153 NS_TEST_ASSERT_MSG_EQ(msg2.GetTimeToLive(), 254, "209");
154 NS_TEST_ASSERT_MSG_EQ(msg2.GetOriginatorAddress(), Ipv4Address("12.22.33.44"), "210");
155 NS_TEST_ASSERT_MSG_EQ(msg2.GetVTime(), Seconds(10), "211");
158
159 olsr::MessageHeader::Mid mid2 = msg2.GetMid();
160 NS_TEST_ASSERT_MSG_EQ(mid2.interfaceAddresses.size(), 2, "214");
161 NS_TEST_ASSERT_MSG_EQ(*mid2.interfaceAddresses.begin(), Ipv4Address("2.2.3.4"), "215");
162
163 sizeLeft -= msg2.GetSerializedSize();
164 NS_TEST_ASSERT_MSG_EQ(sizeLeft, 0, "216");
165 }
166 }
167}
168
169/**
170 * \ingroup olsr-test
171 * \ingroup tests
172 *
173 * Check Hello olsr messages
174 */
176{
177 public:
179 void DoRun() override;
180};
181
183 : TestCase("Check Hello olsr messages")
184{
185}
186
187void
189{
190 Packet packet;
192 olsr::MessageHeader::Hello& helloIn = msgIn.GetHello();
193
194 helloIn.SetHTime(Seconds(7));
195 helloIn.willingness = olsr::Willingness::HIGH;
196
197 {
199 lm1.linkCode = 2;
200 lm1.neighborInterfaceAddresses.emplace_back("1.2.3.4");
201 lm1.neighborInterfaceAddresses.emplace_back("1.2.3.5");
202 helloIn.linkMessages.push_back(lm1);
203
205 lm2.linkCode = 3;
206 lm2.neighborInterfaceAddresses.emplace_back("2.2.3.4");
207 lm2.neighborInterfaceAddresses.emplace_back("2.2.3.5");
208 helloIn.linkMessages.push_back(lm2);
209 }
210
211 packet.AddHeader(msgIn);
212
213 olsr::MessageHeader msgOut;
214 packet.RemoveHeader(msgOut);
215 olsr::MessageHeader::Hello& helloOut = msgOut.GetHello();
216
217 NS_TEST_ASSERT_MSG_EQ(helloOut.GetHTime(), Seconds(7), "300");
218 NS_TEST_ASSERT_MSG_EQ(helloOut.willingness, olsr::Willingness::HIGH, "301");
219 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages.size(), 2, "302");
220
221 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].linkCode, 2, "303");
222 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].neighborInterfaceAddresses[0],
223 Ipv4Address("1.2.3.4"),
224 "304");
225 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].neighborInterfaceAddresses[1],
226 Ipv4Address("1.2.3.5"),
227 "305");
228
229 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[1].linkCode, 3, "306");
230 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[1].neighborInterfaceAddresses[0],
231 Ipv4Address("2.2.3.4"),
232 "307");
233 NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[1].neighborInterfaceAddresses[1],
234 Ipv4Address("2.2.3.5"),
235 "308");
236
237 NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "All bytes in packet were not read");
238}
239
240/**
241 * \ingroup olsr-test
242 * \ingroup tests
243 *
244 * Check Tc olsr messages
245 */
247{
248 public:
250 void DoRun() override;
251};
252
254 : TestCase("Check Tc olsr messages")
255{
256}
257
258void
260{
261 Packet packet;
263 olsr::MessageHeader::Tc& tcIn = msgIn.GetTc();
264
265 tcIn.ansn = 0x1234;
266 tcIn.neighborAddresses.emplace_back("1.2.3.4");
267 tcIn.neighborAddresses.emplace_back("1.2.3.5");
268 packet.AddHeader(msgIn);
269
270 olsr::MessageHeader msgOut;
271 packet.RemoveHeader(msgOut);
272 olsr::MessageHeader::Tc& tcOut = msgOut.GetTc();
273
274 NS_TEST_ASSERT_MSG_EQ(tcOut.ansn, 0x1234, "400");
275 NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses.size(), 2, "401");
276
277 NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses[0], Ipv4Address("1.2.3.4"), "402");
278 NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses[1], Ipv4Address("1.2.3.5"), "403");
279
280 NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "404");
281}
282
283/**
284 * \ingroup olsr-test
285 * \ingroup tests
286 *
287 * Check Hna olsr messages
288 */
290{
291 public:
293 void DoRun() override;
294};
295
297 : TestCase("Check Hna olsr messages")
298{
299}
300
301void
303{
304 Packet packet;
306 olsr::MessageHeader::Hna& hnaIn = msgIn.GetHna();
307
308 hnaIn.associations.push_back(
309 (olsr::MessageHeader::Hna::Association){Ipv4Address("1.2.3.4"), Ipv4Mask("255.255.255.0")});
310 hnaIn.associations.push_back(
311 (olsr::MessageHeader::Hna::Association){Ipv4Address("1.2.3.5"), Ipv4Mask("255.255.0.0")});
312 packet.AddHeader(msgIn);
313
314 olsr::MessageHeader msgOut;
315 packet.RemoveHeader(msgOut);
316 olsr::MessageHeader::Hna& hnaOut = msgOut.GetHna();
317
318 NS_TEST_ASSERT_MSG_EQ(hnaOut.associations.size(), 2, "500");
319
320 NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[0].address, Ipv4Address("1.2.3.4"), "501");
321 NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[0].mask, Ipv4Mask("255.255.255.0"), "502");
322
323 NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[1].address, Ipv4Address("1.2.3.5"), "503");
324 NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[1].mask, Ipv4Mask("255.255.0.0"), "504");
325
326 NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "All bytes in packet were not read");
327}
328
329/**
330 * \ingroup olsr-test
331 * \ingroup tests
332 *
333 * Check olsr header messages
334 */
336{
337 public:
339};
340
342 : TestSuite("routing-olsr-header", Type::UNIT)
343{
344 AddTestCase(new OlsrHnaTestCase(), TestCase::Duration::QUICK);
345 AddTestCase(new OlsrTcTestCase(), TestCase::Duration::QUICK);
346 AddTestCase(new OlsrHelloTestCase(), TestCase::Duration::QUICK);
347 AddTestCase(new OlsrMidTestCase(), TestCase::Duration::QUICK);
348 AddTestCase(new OlsrEmfTestCase(), TestCase::Duration::QUICK);
349}
350
351static OlsrTestSuite g_olsrTestSuite; //!< Static variable for test initialization
Check Emf olsr time conversion.
void DoRun() override
Implementation to actually run this TestCase.
Check Hello olsr messages.
void DoRun() override
Implementation to actually run this TestCase.
Check Hna olsr messages.
void DoRun() override
Implementation to actually run this TestCase.
Check Mid olsr messages.
void DoRun() override
Implementation to actually run this TestCase.
Check Tc olsr messages.
void DoRun() override
Implementation to actually run this TestCase.
Check olsr header messages.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
network packets
Definition: packet.h:239
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:161
void SetOriginatorAddress(Ipv4Address originatorAddress)
Set the originator address.
Definition: olsr-header.h:217
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Definition: olsr-header.h:226
Mid & GetMid()
Set the message type to MID and return the message content.
Definition: olsr-header.h:584
Tc & GetTc()
Set the message type to TC and return the message content.
Definition: olsr-header.h:618
Hello & GetHello()
Set the message type to HELLO and return the message content.
Definition: olsr-header.h:601
uint8_t GetTimeToLive() const
Get the time to live.
Definition: olsr-header.h:244
void SetMessageSequenceNumber(uint16_t messageSequenceNumber)
Set the message sequence number.
Definition: olsr-header.h:271
void SetMessageType(MessageType messageType)
Set the message type.
Definition: olsr-header.h:181
MessageType GetMessageType() const
Get the message type.
Definition: olsr-header.h:190
Hna & GetHna()
Set the message type to HNA and return the message content.
Definition: olsr-header.h:635
Time GetVTime() const
Get the validity time.
Definition: olsr-header.h:208
void SetTimeToLive(uint8_t timeToLive)
Set the time to live.
Definition: olsr-header.h:235
uint32_t GetSerializedSize() const override
Definition: olsr-header.cc:187
uint16_t GetMessageSequenceNumber() const
Get the message sequence number.
Definition: olsr-header.h:280
void SetVTime(Time time)
Set the validity time.
Definition: olsr-header.h:199
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition: olsr-header.h:79
void SetPacketSequenceNumber(uint16_t seqnum)
Set the packet sequence number.
Definition: olsr-header.h:106
void SetPacketLength(uint16_t length)
Set the packet total length.
Definition: olsr-header.h:88
uint32_t GetSerializedSize() const override
Definition: olsr-header.cc:129
uint16_t GetPacketLength() const
Get the packet total length.
Definition: olsr-header.h:97
uint16_t GetPacketSequenceNumber() const
Get the packet sequence number.
Definition: olsr-header.h:115
#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:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static OlsrTestSuite g_olsrTestSuite
Static variable for test initialization.
HELLO Message Format.
Definition: olsr-header.h:386
void SetHTime(Time time)
Set the HELLO emission interval.
Definition: olsr-header.h:403
Willingness willingness
The willingness of a node to carry and forward traffic for other nodes.
Definition: olsr-header.h:417
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:419
Time GetHTime() const
Get the HELLO emission interval.
Definition: olsr-header.h:412
HNA (Host Network Association) Message Format.
Definition: olsr-header.h:525
std::vector< Association > associations
Association container.
Definition: olsr-header.h:535
MID Message Format.
Definition: olsr-header.h:323
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
Definition: olsr-header.h:324
TC Message Format.
Definition: olsr-header.h:470
uint16_t ansn
Advertised Neighbor Sequence Number.
Definition: olsr-header.h:472
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:471