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 * 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
14using namespace ns3;
15
16/**
17 * @ingroup olsr-test
18 * @ingroup tests
19 *
20 * Check Emf olsr time conversion
21 */
23{
24 public:
26 void DoRun() override;
27};
28
30 : TestCase("Check Emf olsr time conversion")
31{
32}
33
34void
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 */
52{
53 public:
55 void DoRun() override;
56};
57
59 : TestCase("Check Mid olsr messages")
60{
61}
62
63void
65{
66 Packet packet;
67
68 {
71 olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
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));
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));
101
102 // Build an OLSR packet header
104 msg2.GetSerializedSize());
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 {
115 packet.RemoveHeader(hdr);
117 uint32_t sizeLeft = hdr.GetPacketLength() - hdr.GetSerializedSize();
118 {
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");
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
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");
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 */
165{
166 public:
168 void DoRun() override;
169};
170
172 : TestCase("Check Hello olsr messages")
173{
174}
175
176void
178{
179 Packet packet;
181 olsr::MessageHeader::Hello& helloIn = msgIn.GetHello();
182
183 helloIn.SetHTime(Seconds(7));
184 helloIn.willingness = olsr::Willingness::HIGH;
185
186 {
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
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 */
236{
237 public:
239 void DoRun() override;
240};
241
243 : TestCase("Check Tc olsr messages")
244{
245}
246
247void
249{
250 Packet packet;
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 */
279{
280 public:
282 void DoRun() override;
283};
284
286 : TestCase("Check Hna olsr messages")
287{
288}
289
290void
292{
293 Packet packet;
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(
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 */
325{
326 public:
328};
329
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
340static 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.
a class to represent an Ipv4 address mask
network packets
Definition packet.h:228
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition packet.cc:283
void AddHeader(const Header &header)
Add header to this packet.
Definition packet.cc:257
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition packet.h:850
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
This header can store HELP, TC, MID and HNA messages.
void SetOriginatorAddress(Ipv4Address originatorAddress)
Set the originator address.
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Mid & GetMid()
Set the message type to MID and return the message content.
Tc & GetTc()
Set the message type to TC and return the message content.
Hello & GetHello()
Set the message type to HELLO and return the message content.
uint8_t GetTimeToLive() const
Get the time to live.
void SetMessageSequenceNumber(uint16_t messageSequenceNumber)
Set the message sequence number.
void SetMessageType(MessageType messageType)
Set the message type.
MessageType GetMessageType() const
Get the message type.
Hna & GetHna()
Set the message type to HNA and return the message content.
Time GetVTime() const
Get the validity time.
void SetTimeToLive(uint8_t timeToLive)
Set the time to live.
uint32_t GetSerializedSize() const override
uint16_t GetMessageSequenceNumber() const
Get the message sequence number.
void SetVTime(Time time)
Set the validity time.
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition olsr-header.h:68
void SetPacketSequenceNumber(uint16_t seqnum)
Set the packet sequence number.
Definition olsr-header.h:95
void SetPacketLength(uint16_t length)
Set the packet total length.
Definition olsr-header.h:77
uint32_t GetSerializedSize() const override
uint16_t GetPacketLength() const
Get the packet total length.
Definition olsr-header.h:86
uint16_t GetPacketSequenceNumber() const
Get the packet sequence number.
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
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.
void SetHTime(Time time)
Set the HELLO emission interval.
Willingness willingness
The willingness of a node to carry and forward traffic for other nodes.
std::vector< LinkMessage > linkMessages
Link messages container.
Time GetHTime() const
Get the HELLO emission interval.
HNA (Host Network Association) Message Format.
std::vector< Association > associations
Association container.
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
uint16_t ansn
Advertised Neighbor Sequence Number.
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.