A Discrete-Event Network Simulator
API
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 
25 using namespace ns3;
26 
33 class OlsrEmfTestCase : public TestCase
34 {
35  public:
37  void DoRun() override;
38 };
39 
41  : TestCase("Check Emf olsr time conversion")
42 {
43 }
44 
45 void
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 
62 class OlsrMidTestCase : public TestCase
63 {
64  public:
66  void DoRun() override;
67 };
68 
70  : TestCase("Check Mid olsr messages")
71 {
72 }
73 
74 void
76 {
77  Packet packet;
78 
79  {
80  olsr::PacketHeader hdr;
81  olsr::MessageHeader msg1;
82  olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
83  olsr::MessageHeader msg2;
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));
97  msg1.SetMessageSequenceNumber(7);
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));
110  msg2.SetMessageType(olsr::MessageHeader::MID_MESSAGE);
111  msg2.SetMessageSequenceNumber(7);
112 
113  // Build an OLSR packet header
114  hdr.SetPacketLength(hdr.GetSerializedSize() + msg1.GetSerializedSize() +
115  msg2.GetSerializedSize());
116  hdr.SetPacketSequenceNumber(123);
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  {
125  olsr::PacketHeader hdr;
126  packet.RemoveHeader(hdr);
127  NS_TEST_ASSERT_MSG_EQ(hdr.GetPacketSequenceNumber(), 123, "200");
128  uint32_t sizeLeft = hdr.GetPacketLength() - hdr.GetSerializedSize();
129  {
130  olsr::MessageHeader msg1;
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");
137  NS_TEST_ASSERT_MSG_EQ(msg1.GetMessageType(), olsr::MessageHeader::MID_MESSAGE, "204");
138  NS_TEST_ASSERT_MSG_EQ(msg1.GetMessageSequenceNumber(), 7, "205");
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
149  olsr::MessageHeader msg2;
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");
156  NS_TEST_ASSERT_MSG_EQ(msg2.GetMessageType(), olsr::MessageHeader::MID_MESSAGE, "212");
157  NS_TEST_ASSERT_MSG_EQ(msg2.GetMessageSequenceNumber(), 7, "213");
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 
176 {
177  public:
179  void DoRun() override;
180 };
181 
183  : TestCase("Check Hello olsr messages")
184 {
185 }
186 
187 void
189 {
190  Packet packet;
191  olsr::MessageHeader msgIn;
192  olsr::MessageHeader::Hello& helloIn = msgIn.GetHello();
193 
194  helloIn.SetHTime(Seconds(7));
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");
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 
246 class OlsrTcTestCase : public TestCase
247 {
248  public:
249  OlsrTcTestCase();
250  void DoRun() override;
251 };
252 
254  : TestCase("Check Tc olsr messages")
255 {
256 }
257 
258 void
260 {
261  Packet packet;
262  olsr::MessageHeader msgIn;
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 
289 class OlsrHnaTestCase : public TestCase
290 {
291  public:
292  OlsrHnaTestCase();
293  void DoRun() override;
294 };
295 
297  : TestCase("Check Hna olsr messages")
298 {
299 }
300 
301 void
303 {
304  Packet packet;
305  olsr::MessageHeader msgIn;
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 
335 class OlsrTestSuite : public TestSuite
336 {
337  public:
338  OlsrTestSuite();
339 };
340 
342  : TestSuite("routing-olsr-header", UNIT)
343 {
344  AddTestCase(new OlsrHnaTestCase(), TestCase::QUICK);
345  AddTestCase(new OlsrTcTestCase(), TestCase::QUICK);
346  AddTestCase(new OlsrHelloTestCase(), TestCase::QUICK);
347  AddTestCase(new OlsrMidTestCase(), TestCase::QUICK);
348  AddTestCase(new OlsrEmfTestCase(), TestCase::QUICK);
349 }
350 
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: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_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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
double EmfToSeconds(uint8_t olsrFormat)
Converts a number of seconds in the mantissa/exponent format to a decimal number.
Definition: olsr-header.cc:92
uint8_t SecondsToEmf(double seconds)
Converts a decimal number of seconds to the mantissa/exponent format.
Definition: olsr-header.cc:49
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