A Discrete-Event Network Simulator
API
olsr-header-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
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  * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
19  */
20 
21 #include "ns3/test.h"
22 #include "ns3/olsr-header.h"
23 #include "ns3/packet.h"
24 
25 
26 using namespace ns3;
27 
34 class OlsrEmfTestCase : public TestCase
35 {
36 public:
37  OlsrEmfTestCase ();
38  virtual void DoRun (void);
39 };
40 
42  : TestCase ("Check Emf olsr time conversion")
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,
53  "100");
54  }
55 }
56 
57 
64 class OlsrMidTestCase : public TestCase
65 {
66 public:
67  OlsrMidTestCase ();
68  virtual void DoRun (void);
69 };
70 
72  : TestCase ("Check Mid olsr messages")
73 {
74 }
75 void
77 {
78  Packet packet;
79 
80  {
81  olsr::PacketHeader hdr;
82  olsr::MessageHeader msg1;
83  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
84  olsr::MessageHeader msg2;
85  olsr::MessageHeader::Mid &mid2 = msg2.GetMid ();
86 
87  // MID message #1
88  {
89  std::vector<Ipv4Address> &addresses = mid1.interfaceAddresses;
90  addresses.clear ();
91  addresses.push_back (Ipv4Address ("1.2.3.4"));
92  addresses.push_back (Ipv4Address ("1.2.3.5"));
93  }
94 
95  msg1.SetTimeToLive (255);
96  msg1.SetOriginatorAddress (Ipv4Address ("11.22.33.44"));
97  msg1.SetVTime (Seconds (9));
98  msg1.SetMessageSequenceNumber (7);
99 
100  // MID message #2
101  {
102  std::vector<Ipv4Address> &addresses = mid2.interfaceAddresses;
103  addresses.clear ();
104  addresses.push_back (Ipv4Address ("2.2.3.4"));
105  addresses.push_back (Ipv4Address ("2.2.3.5"));
106  }
107 
108  msg2.SetTimeToLive (254);
109  msg2.SetOriginatorAddress (Ipv4Address ("12.22.33.44"));
110  msg2.SetVTime (Seconds (10));
111  msg2.SetMessageType (olsr::MessageHeader::MID_MESSAGE);
112  msg2.SetMessageSequenceNumber (7);
113 
114  // Build an OLSR packet header
115  hdr.SetPacketLength (hdr.GetSerializedSize () + msg1.GetSerializedSize () + msg2.GetSerializedSize ());
116  hdr.SetPacketSequenceNumber (123);
117 
118 
119  // Now add all the headers in the correct order
120  packet.AddHeader (msg2);
121  packet.AddHeader (msg1);
122  packet.AddHeader (hdr);
123  }
124 
125  {
126  olsr::PacketHeader hdr;
127  packet.RemoveHeader (hdr);
128  NS_TEST_ASSERT_MSG_EQ (hdr.GetPacketSequenceNumber (), 123, "200");
129  uint32_t sizeLeft = hdr.GetPacketLength () - hdr.GetSerializedSize ();
130  {
131  olsr::MessageHeader msg1;
132 
133  packet.RemoveHeader (msg1);
134 
135  NS_TEST_ASSERT_MSG_EQ (msg1.GetTimeToLive (), 255, "201");
136  NS_TEST_ASSERT_MSG_EQ (msg1.GetOriginatorAddress (), Ipv4Address ("11.22.33.44"), "202");
137  NS_TEST_ASSERT_MSG_EQ (msg1.GetVTime (), Seconds (9), "203");
138  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "204");
139  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageSequenceNumber (), 7, "205");
140 
141  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
142  NS_TEST_ASSERT_MSG_EQ (mid1.interfaceAddresses.size (), 2, "206");
143  NS_TEST_ASSERT_MSG_EQ (*mid1.interfaceAddresses.begin (), Ipv4Address ("1.2.3.4"), "207");
144 
145  sizeLeft -= msg1.GetSerializedSize ();
146  NS_TEST_ASSERT_MSG_EQ ((sizeLeft > 0), true, "208");
147  }
148  {
149  // now read the second message
150  olsr::MessageHeader msg2;
151 
152  packet.RemoveHeader (msg2);
153 
154  NS_TEST_ASSERT_MSG_EQ (msg2.GetTimeToLive (), 254, "209");
155  NS_TEST_ASSERT_MSG_EQ (msg2.GetOriginatorAddress (), Ipv4Address ("12.22.33.44"), "210");
156  NS_TEST_ASSERT_MSG_EQ (msg2.GetVTime (), Seconds (10), "211");
157  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "212");
158  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageSequenceNumber (), 7, "213");
159 
160  olsr::MessageHeader::Mid mid2 = msg2.GetMid ();
161  NS_TEST_ASSERT_MSG_EQ (mid2.interfaceAddresses.size (), 2, "214");
162  NS_TEST_ASSERT_MSG_EQ (*mid2.interfaceAddresses.begin (), Ipv4Address ("2.2.3.4"), "215");
163 
164  sizeLeft -= msg2.GetSerializedSize ();
165  NS_TEST_ASSERT_MSG_EQ (sizeLeft, 0, "216");
166  }
167  }
168 }
169 
170 
178 {
179 public:
181  virtual void DoRun (void);
182 };
183 
185  : TestCase ("Check Hello olsr messages")
186 {
187 }
188 void
190 {
191  Packet packet;
192  olsr::MessageHeader msgIn;
193  olsr::MessageHeader::Hello &helloIn = msgIn.GetHello ();
194 
195  helloIn.SetHTime (Seconds (7));
196  helloIn.willingness = 66;
197 
198  {
200  lm1.linkCode = 2;
201  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.4"));
202  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.5"));
203  helloIn.linkMessages.push_back (lm1);
204 
206  lm2.linkCode = 3;
207  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.4"));
208  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.5"));
209  helloIn.linkMessages.push_back (lm2);
210  }
211 
212  packet.AddHeader (msgIn);
213 
214  olsr::MessageHeader msgOut;
215  packet.RemoveHeader (msgOut);
216  olsr::MessageHeader::Hello &helloOut = msgOut.GetHello ();
217 
218  NS_TEST_ASSERT_MSG_EQ (helloOut.GetHTime (), Seconds (7), "300");
219  NS_TEST_ASSERT_MSG_EQ (helloOut.willingness, 66, "301");
220  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages.size (), 2, "302");
221 
222  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].linkCode, 2, "303");
223  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[0],
224  Ipv4Address ("1.2.3.4"), "304");
225  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[1],
226  Ipv4Address ("1.2.3.5"), "305");
227 
228  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].linkCode, 3, "306");
229  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[0],
230  Ipv4Address ("2.2.3.4"), "307");
231  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[1],
232  Ipv4Address ("2.2.3.5"), "308");
233 
234  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
235 
236 }
237 
244 class OlsrTcTestCase : public TestCase
245 {
246 public:
247  OlsrTcTestCase ();
248  virtual void DoRun (void);
249 };
250 
252  : TestCase ("Check Tc olsr messages")
253 {
254 }
255 void
257 {
258  Packet packet;
259  olsr::MessageHeader msgIn;
260  olsr::MessageHeader::Tc &tcIn = msgIn.GetTc ();
261 
262  tcIn.ansn = 0x1234;
263  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.4"));
264  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.5"));
265  packet.AddHeader (msgIn);
266 
267  olsr::MessageHeader msgOut;
268  packet.RemoveHeader (msgOut);
269  olsr::MessageHeader::Tc &tcOut = msgOut.GetTc ();
270 
271  NS_TEST_ASSERT_MSG_EQ (tcOut.ansn, 0x1234, "400");
272  NS_TEST_ASSERT_MSG_EQ (tcOut.neighborAddresses.size (), 2, "401");
273 
275  Ipv4Address ("1.2.3.4"), "402");
277  Ipv4Address ("1.2.3.5"), "403");
278 
279  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "404");
280 
281 }
282 
289 class OlsrHnaTestCase : public TestCase
290 {
291 public:
292  OlsrHnaTestCase ();
293  virtual void DoRun (void);
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 
309  { Ipv4Address ("1.2.3.4"), Ipv4Mask ("255.255.255.0")});
311  { 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,
321  Ipv4Address ("1.2.3.4"), "501");
322  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[0].mask,
323  Ipv4Mask ("255.255.255.0"), "502");
324 
325  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].address,
326  Ipv4Address ("1.2.3.5"), "503");
327  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].mask,
328  Ipv4Mask ("255.255.0.0"), "504");
329 
330  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
331 
332 }
333 
334 
341 class OlsrTestSuite : public TestSuite
342 {
343 public:
344  OlsrTestSuite ();
345 };
346 
348  : TestSuite ("routing-olsr-header", UNIT)
349 {
350  AddTestCase (new OlsrHnaTestCase (), TestCase::QUICK);
351  AddTestCase (new OlsrTcTestCase (), TestCase::QUICK);
352  AddTestCase (new OlsrHelloTestCase (), TestCase::QUICK);
353  AddTestCase (new OlsrMidTestCase (), TestCase::QUICK);
354  AddTestCase (new OlsrEmfTestCase (), TestCase::QUICK);
355 }
356 
358 
359 
Check Emf olsr time conversion.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check Hello olsr messages.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check Hna olsr messages.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check Mid olsr messages.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check Tc olsr messages.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Check olsr header messages.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
network packets
Definition: packet.h:232
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
double EmfToSeconds(uint8_t olsrFormat)
Converts a number of seconds in the mantissa/exponent format to a decimal number.
Definition: olsr-header.cc:91
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:377
void SetHTime(Time time)
Set the HELLO emission interval.
Definition: olsr-header.h:393
uint8_t willingness
The willingness of a node to carry and forward traffic for other nodes.
Definition: olsr-header.h:407
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:408
Time GetHTime() const
Get the HELLO emission interval.
Definition: olsr-header.h:402
HNA (Host Network Association) Message Format.
Definition: olsr-header.h:515
std::vector< Association > associations
Association container.
Definition: olsr-header.h:525
MID Message Format.
Definition: olsr-header.h:314
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
Definition: olsr-header.h:315
TC Message Format.
Definition: olsr-header.h:459
uint16_t ansn
Advertised Neighbor Sequence Number.
Definition: olsr-header.h:461
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:460