A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
7
*/
8
9
#include "ns3/olsr-header.h"
10
#include "ns3/olsr-repositories.h"
11
#include "ns3/packet.h"
12
#include "ns3/test.h"
13
14
using namespace
ns3
;
15
16
/**
17
* @ingroup olsr-test
18
* @ingroup tests
19
*
20
* Check Emf olsr time conversion
21
*/
22
class
OlsrEmfTestCase
:
public
TestCase
23
{
24
public
:
25
OlsrEmfTestCase
();
26
void
DoRun
()
override
;
27
};
28
29
OlsrEmfTestCase::OlsrEmfTestCase
()
30
:
TestCase
(
"Check Emf olsr time conversion"
)
31
{
32
}
33
34
void
35
OlsrEmfTestCase::DoRun
()
36
{
37
for
(
int
time = 1; time <= 30; time++)
38
{
39
uint8_t
emf
= olsr::SecondsToEmf(time);
40
double
seconds
= olsr::EmfToSeconds(
emf
);
41
NS_TEST_ASSERT_MSG_EQ
((
seconds
< 0 || std::fabs(
seconds
- time) > 0.1),
false
,
"100"
);
42
}
43
}
44
45
/**
46
* @ingroup olsr-test
47
* @ingroup tests
48
*
49
* Check Mid olsr messages
50
*/
51
class
OlsrMidTestCase
:
public
TestCase
52
{
53
public
:
54
OlsrMidTestCase
();
55
void
DoRun
()
override
;
56
};
57
58
OlsrMidTestCase::OlsrMidTestCase
()
59
:
TestCase
(
"Check Mid olsr messages"
)
60
{
61
}
62
63
void
64
OlsrMidTestCase::DoRun
()
65
{
66
Packet
packet;
67
68
{
69
olsr::PacketHeader
hdr
;
70
olsr::MessageHeader
msg1
;
71
olsr::MessageHeader::Mid
&
mid1
=
msg1
.GetMid();
72
olsr::MessageHeader
msg2
;
73
olsr::MessageHeader::Mid
&
mid2
=
msg2
.GetMid();
74
75
// MID message #1
76
{
77
std::vector<Ipv4Address>&
addresses
=
mid1
.
interfaceAddresses
;
78
addresses
.clear();
79
addresses
.emplace_back(
"1.2.3.4"
);
80
addresses
.emplace_back(
"1.2.3.5"
);
81
}
82
83
msg1
.SetTimeToLive(255);
84
msg1
.SetOriginatorAddress(
Ipv4Address
(
"11.22.33.44"
));
85
msg1
.SetVTime(
Seconds
(9));
86
msg1
.SetMessageSequenceNumber(7);
87
88
// MID message #2
89
{
90
std::vector<Ipv4Address>&
addresses
=
mid2
.interfaceAddresses;
91
addresses
.clear();
92
addresses
.emplace_back(
"2.2.3.4"
);
93
addresses
.emplace_back(
"2.2.3.5"
);
94
}
95
96
msg2
.SetTimeToLive(254);
97
msg2
.SetOriginatorAddress(
Ipv4Address
(
"12.22.33.44"
));
98
msg2
.SetVTime(
Seconds
(10));
99
msg2
.SetMessageType(
olsr::MessageHeader::MID_MESSAGE
);
100
msg2
.SetMessageSequenceNumber(7);
101
102
// Build an OLSR packet header
103
hdr
.SetPacketLength(
hdr
.GetSerializedSize() +
msg1
.GetSerializedSize() +
104
msg2
.GetSerializedSize());
105
hdr
.SetPacketSequenceNumber(123);
106
107
// Now add all the headers in the correct order
108
packet.
AddHeader
(
msg2
);
109
packet.
AddHeader
(
msg1
);
110
packet.
AddHeader
(
hdr
);
111
}
112
113
{
114
olsr::PacketHeader
hdr
;
115
packet.
RemoveHeader
(
hdr
);
116
NS_TEST_ASSERT_MSG_EQ
(
hdr
.GetPacketSequenceNumber(), 123,
"200"
);
117
uint32_t
sizeLeft
=
hdr
.GetPacketLength() -
hdr
.GetSerializedSize();
118
{
119
olsr::MessageHeader
msg1
;
120
121
packet.
RemoveHeader
(
msg1
);
122
123
NS_TEST_ASSERT_MSG_EQ
(
msg1
.GetTimeToLive(), 255,
"201"
);
124
NS_TEST_ASSERT_MSG_EQ
(
msg1
.GetOriginatorAddress(),
Ipv4Address
(
"11.22.33.44"
),
"202"
);
125
NS_TEST_ASSERT_MSG_EQ
(
msg1
.GetVTime(),
Seconds
(9),
"203"
);
126
NS_TEST_ASSERT_MSG_EQ
(
msg1
.GetMessageType(),
olsr::MessageHeader::MID_MESSAGE
,
"204"
);
127
NS_TEST_ASSERT_MSG_EQ
(
msg1
.GetMessageSequenceNumber(), 7,
"205"
);
128
129
olsr::MessageHeader::Mid
&
mid1
=
msg1
.GetMid();
130
NS_TEST_ASSERT_MSG_EQ
(
mid1
.interfaceAddresses.size(), 2,
"206"
);
131
NS_TEST_ASSERT_MSG_EQ
(*
mid1
.interfaceAddresses.begin(),
Ipv4Address
(
"1.2.3.4"
),
"207"
);
132
133
sizeLeft
-=
msg1
.GetSerializedSize();
134
NS_TEST_ASSERT_MSG_EQ
((
sizeLeft
> 0),
true
,
"208"
);
135
}
136
{
137
// now read the second message
138
olsr::MessageHeader
msg2
;
139
140
packet.
RemoveHeader
(
msg2
);
141
142
NS_TEST_ASSERT_MSG_EQ
(
msg2
.GetTimeToLive(), 254,
"209"
);
143
NS_TEST_ASSERT_MSG_EQ
(
msg2
.GetOriginatorAddress(),
Ipv4Address
(
"12.22.33.44"
),
"210"
);
144
NS_TEST_ASSERT_MSG_EQ
(
msg2
.GetVTime(),
Seconds
(10),
"211"
);
145
NS_TEST_ASSERT_MSG_EQ
(
msg2
.GetMessageType(),
olsr::MessageHeader::MID_MESSAGE
,
"212"
);
146
NS_TEST_ASSERT_MSG_EQ
(
msg2
.GetMessageSequenceNumber(), 7,
"213"
);
147
148
olsr::MessageHeader::Mid
mid2
=
msg2
.GetMid();
149
NS_TEST_ASSERT_MSG_EQ
(
mid2
.interfaceAddresses.size(), 2,
"214"
);
150
NS_TEST_ASSERT_MSG_EQ
(*
mid2
.interfaceAddresses.begin(),
Ipv4Address
(
"2.2.3.4"
),
"215"
);
151
152
sizeLeft
-=
msg2
.GetSerializedSize();
153
NS_TEST_ASSERT_MSG_EQ
(
sizeLeft
, 0,
"216"
);
154
}
155
}
156
}
157
158
/**
159
* @ingroup olsr-test
160
* @ingroup tests
161
*
162
* Check Hello olsr messages
163
*/
164
class
OlsrHelloTestCase
:
public
TestCase
165
{
166
public
:
167
OlsrHelloTestCase
();
168
void
DoRun
()
override
;
169
};
170
171
OlsrHelloTestCase::OlsrHelloTestCase
()
172
:
TestCase
(
"Check Hello olsr messages"
)
173
{
174
}
175
176
void
177
OlsrHelloTestCase::DoRun
()
178
{
179
Packet
packet;
180
olsr::MessageHeader
msgIn
;
181
olsr::MessageHeader::Hello
&
helloIn
=
msgIn
.GetHello();
182
183
helloIn
.
SetHTime
(
Seconds
(7));
184
helloIn
.willingness = olsr::Willingness::HIGH;
185
186
{
187
olsr::MessageHeader::Hello::LinkMessage
lm1
;
188
lm1
.
linkCode
= 2;
189
lm1
.neighborInterfaceAddresses.emplace_back(
"1.2.3.4"
);
190
lm1
.neighborInterfaceAddresses.emplace_back(
"1.2.3.5"
);
191
helloIn
.linkMessages.push_back(
lm1
);
192
193
olsr::MessageHeader::Hello::LinkMessage
lm2
;
194
lm2
.
linkCode
= 3;
195
lm2
.neighborInterfaceAddresses.emplace_back(
"2.2.3.4"
);
196
lm2
.neighborInterfaceAddresses.emplace_back(
"2.2.3.5"
);
197
helloIn
.linkMessages.push_back(
lm2
);
198
}
199
200
packet.
AddHeader
(
msgIn
);
201
202
olsr::MessageHeader
msgOut
;
203
packet.
RemoveHeader
(
msgOut
);
204
olsr::MessageHeader::Hello
&
helloOut
=
msgOut
.GetHello();
205
206
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.GetHTime(),
Seconds
(7),
"300"
);
207
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.willingness, olsr::Willingness::HIGH,
"301"
);
208
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages.size(), 2,
"302"
);
209
210
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[0].linkCode, 2,
"303"
);
211
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[0].neighborInterfaceAddresses[0],
212
Ipv4Address
(
"1.2.3.4"
),
213
"304"
);
214
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[0].neighborInterfaceAddresses[1],
215
Ipv4Address
(
"1.2.3.5"
),
216
"305"
);
217
218
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[1].linkCode, 3,
"306"
);
219
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[1].neighborInterfaceAddresses[0],
220
Ipv4Address
(
"2.2.3.4"
),
221
"307"
);
222
NS_TEST_ASSERT_MSG_EQ
(
helloOut
.linkMessages[1].neighborInterfaceAddresses[1],
223
Ipv4Address
(
"2.2.3.5"
),
224
"308"
);
225
226
NS_TEST_ASSERT_MSG_EQ
(packet.
GetSize
(), 0,
"All bytes in packet were not read"
);
227
}
228
229
/**
230
* @ingroup olsr-test
231
* @ingroup tests
232
*
233
* Check Tc olsr messages
234
*/
235
class
OlsrTcTestCase
:
public
TestCase
236
{
237
public
:
238
OlsrTcTestCase
();
239
void
DoRun
()
override
;
240
};
241
242
OlsrTcTestCase::OlsrTcTestCase
()
243
:
TestCase
(
"Check Tc olsr messages"
)
244
{
245
}
246
247
void
248
OlsrTcTestCase::DoRun
()
249
{
250
Packet
packet;
251
olsr::MessageHeader
msgIn
;
252
olsr::MessageHeader::Tc
&
tcIn
=
msgIn
.GetTc();
253
254
tcIn
.
ansn
= 0x1234;
255
tcIn
.neighborAddresses.emplace_back(
"1.2.3.4"
);
256
tcIn
.neighborAddresses.emplace_back(
"1.2.3.5"
);
257
packet.
AddHeader
(
msgIn
);
258
259
olsr::MessageHeader
msgOut
;
260
packet.
RemoveHeader
(
msgOut
);
261
olsr::MessageHeader::Tc
&
tcOut
=
msgOut
.GetTc();
262
263
NS_TEST_ASSERT_MSG_EQ
(
tcOut
.ansn, 0x1234,
"400"
);
264
NS_TEST_ASSERT_MSG_EQ
(
tcOut
.neighborAddresses.size(), 2,
"401"
);
265
266
NS_TEST_ASSERT_MSG_EQ
(
tcOut
.neighborAddresses[0],
Ipv4Address
(
"1.2.3.4"
),
"402"
);
267
NS_TEST_ASSERT_MSG_EQ
(
tcOut
.neighborAddresses[1],
Ipv4Address
(
"1.2.3.5"
),
"403"
);
268
269
NS_TEST_ASSERT_MSG_EQ
(packet.
GetSize
(), 0,
"404"
);
270
}
271
272
/**
273
* @ingroup olsr-test
274
* @ingroup tests
275
*
276
* Check Hna olsr messages
277
*/
278
class
OlsrHnaTestCase
:
public
TestCase
279
{
280
public
:
281
OlsrHnaTestCase
();
282
void
DoRun
()
override
;
283
};
284
285
OlsrHnaTestCase::OlsrHnaTestCase
()
286
:
TestCase
(
"Check Hna olsr messages"
)
287
{
288
}
289
290
void
291
OlsrHnaTestCase::DoRun
()
292
{
293
Packet
packet;
294
olsr::MessageHeader
msgIn
;
295
olsr::MessageHeader::Hna
&
hnaIn
=
msgIn
.GetHna();
296
297
hnaIn
.
associations
.push_back(
298
olsr::MessageHeader::Hna::Association
{
Ipv4Address
(
"1.2.3.4"
),
Ipv4Mask
(
"255.255.255.0"
)});
299
hnaIn
.associations.push_back(
300
olsr::MessageHeader::Hna::Association
{
Ipv4Address
(
"1.2.3.5"
),
Ipv4Mask
(
"255.255.0.0"
)});
301
packet.
AddHeader
(
msgIn
);
302
303
olsr::MessageHeader
msgOut
;
304
packet.
RemoveHeader
(
msgOut
);
305
olsr::MessageHeader::Hna
&
hnaOut
=
msgOut
.GetHna();
306
307
NS_TEST_ASSERT_MSG_EQ
(
hnaOut
.associations.size(), 2,
"500"
);
308
309
NS_TEST_ASSERT_MSG_EQ
(
hnaOut
.associations[0].address,
Ipv4Address
(
"1.2.3.4"
),
"501"
);
310
NS_TEST_ASSERT_MSG_EQ
(
hnaOut
.associations[0].mask,
Ipv4Mask
(
"255.255.255.0"
),
"502"
);
311
312
NS_TEST_ASSERT_MSG_EQ
(
hnaOut
.associations[1].address,
Ipv4Address
(
"1.2.3.5"
),
"503"
);
313
NS_TEST_ASSERT_MSG_EQ
(
hnaOut
.associations[1].mask,
Ipv4Mask
(
"255.255.0.0"
),
"504"
);
314
315
NS_TEST_ASSERT_MSG_EQ
(packet.
GetSize
(), 0,
"All bytes in packet were not read"
);
316
}
317
318
/**
319
* @ingroup olsr-test
320
* @ingroup tests
321
*
322
* Check olsr header messages
323
*/
324
class
OlsrTestSuite
:
public
TestSuite
325
{
326
public
:
327
OlsrTestSuite
();
328
};
329
330
OlsrTestSuite::OlsrTestSuite
()
331
:
TestSuite
(
"routing-olsr-header"
,
Type
::UNIT)
332
{
333
AddTestCase
(
new
OlsrHnaTestCase
(), TestCase::Duration::QUICK);
334
AddTestCase
(
new
OlsrTcTestCase
(), TestCase::Duration::QUICK);
335
AddTestCase
(
new
OlsrHelloTestCase
(), TestCase::Duration::QUICK);
336
AddTestCase
(
new
OlsrMidTestCase
(), TestCase::Duration::QUICK);
337
AddTestCase
(
new
OlsrEmfTestCase
(), TestCase::Duration::QUICK);
338
}
339
340
static
OlsrTestSuite
g_olsrTestSuite
;
//!< Static variable for test initialization
OlsrEmfTestCase
Check Emf olsr time conversion.
Definition
olsr-header-test-suite.cc:23
OlsrEmfTestCase::OlsrEmfTestCase
OlsrEmfTestCase()
Definition
olsr-header-test-suite.cc:29
OlsrEmfTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
olsr-header-test-suite.cc:35
OlsrHelloTestCase
Check Hello olsr messages.
Definition
olsr-header-test-suite.cc:165
OlsrHelloTestCase::OlsrHelloTestCase
OlsrHelloTestCase()
Definition
olsr-header-test-suite.cc:171
OlsrHelloTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
olsr-header-test-suite.cc:177
OlsrHnaTestCase
Check Hna olsr messages.
Definition
olsr-header-test-suite.cc:279
OlsrHnaTestCase::OlsrHnaTestCase
OlsrHnaTestCase()
Definition
olsr-header-test-suite.cc:285
OlsrHnaTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
olsr-header-test-suite.cc:291
OlsrMidTestCase
Check Mid olsr messages.
Definition
olsr-header-test-suite.cc:52
OlsrMidTestCase::OlsrMidTestCase
OlsrMidTestCase()
Definition
olsr-header-test-suite.cc:58
OlsrMidTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
olsr-header-test-suite.cc:64
OlsrTcTestCase
Check Tc olsr messages.
Definition
olsr-header-test-suite.cc:236
OlsrTcTestCase::OlsrTcTestCase
OlsrTcTestCase()
Definition
olsr-header-test-suite.cc:242
OlsrTcTestCase::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
olsr-header-test-suite.cc:248
OlsrTestSuite
Check olsr header messages.
Definition
olsr-header-test-suite.cc:325
OlsrTestSuite::OlsrTestSuite
OlsrTestSuite()
Definition
olsr-header-test-suite.cc:330
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4Mask
a class to represent an Ipv4 address mask
Definition
ipv4-address.h:246
ns3::Packet
network packets
Definition
packet.h:228
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition
packet.cc:283
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition
packet.cc:257
ns3::Packet::GetSize
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition
packet.h:850
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::olsr::MessageHeader
This header can store HELP, TC, MID and HNA messages.
Definition
olsr-header.h:150
ns3::olsr::MessageHeader::MID_MESSAGE
@ MID_MESSAGE
Definition
olsr-header.h:159
ns3::olsr::PacketHeader
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition
olsr-header.h:68
uint32_t
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
NS_TEST_ASSERT_MSG_EQ
#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:134
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
g_olsrTestSuite
static OlsrTestSuite g_olsrTestSuite
Static variable for test initialization.
Definition
olsr-header-test-suite.cc:340
ns3::olsr::MessageHeader::Hello::LinkMessage
Link message item.
Definition
olsr-header.h:380
ns3::olsr::MessageHeader::Hello::LinkMessage::linkCode
uint8_t linkCode
Link code.
Definition
olsr-header.h:381
ns3::olsr::MessageHeader::Hello
HELLO Message Format.
Definition
olsr-header.h:375
ns3::olsr::MessageHeader::Hello::SetHTime
void SetHTime(Time time)
Set the HELLO emission interval.
Definition
olsr-header.h:392
ns3::olsr::MessageHeader::Hna::Association
Association item structure.
Definition
olsr-header.h:519
ns3::olsr::MessageHeader::Hna
HNA (Host Network Association) Message Format.
Definition
olsr-header.h:514
ns3::olsr::MessageHeader::Hna::associations
std::vector< Association > associations
Association container.
Definition
olsr-header.h:524
ns3::olsr::MessageHeader::Mid
MID Message Format.
Definition
olsr-header.h:312
ns3::olsr::MessageHeader::Mid::interfaceAddresses
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
Definition
olsr-header.h:313
ns3::olsr::MessageHeader::Tc
TC Message Format.
Definition
olsr-header.h:459
ns3::olsr::MessageHeader::Tc::ansn
uint16_t ansn
Advertised Neighbor Sequence Number.
Definition
olsr-header.h:461
src
olsr
test
olsr-header-test-suite.cc
Generated on Mon Dec 15 2025 15:22:02 for ns-3 by
1.9.8