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 using namespace ns3;
26 
27 class OlsrEmfTestCase : public TestCase
28 {
29 public:
30  OlsrEmfTestCase ();
31  virtual void DoRun (void);
32 };
33 
35  : TestCase ("Check Emf olsr time conversion")
36 {
37 }
38 void
40 {
41  for (int time = 1; time <= 30; time++)
42  {
43  uint8_t emf = olsr::SecondsToEmf (time);
44  double seconds = olsr::EmfToSeconds (emf);
45  NS_TEST_ASSERT_MSG_EQ ((seconds < 0 || std::fabs (seconds - time) > 0.1), false,
46  "100");
47  }
48 }
49 
50 
51 class OlsrMidTestCase : public TestCase
52 {
53 public:
54  OlsrMidTestCase ();
55  virtual void DoRun (void);
56 };
57 
59  : TestCase ("Check Mid olsr messages")
60 {
61 }
62 void
64 {
65  Packet packet;
66 
67  {
68  olsr::PacketHeader hdr;
69  olsr::MessageHeader msg1;
70  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
71  olsr::MessageHeader msg2;
72  olsr::MessageHeader::Mid &mid2 = msg2.GetMid ();
73 
74  // MID message #1
75  {
76  std::vector<Ipv4Address> &addresses = mid1.interfaceAddresses;
77  addresses.clear ();
78  addresses.push_back (Ipv4Address ("1.2.3.4"));
79  addresses.push_back (Ipv4Address ("1.2.3.5"));
80  }
81 
82  msg1.SetTimeToLive (255);
83  msg1.SetOriginatorAddress (Ipv4Address ("11.22.33.44"));
84  msg1.SetVTime (Seconds (9));
85  msg1.SetMessageSequenceNumber (7);
86 
87  // MID message #2
88  {
89  std::vector<Ipv4Address> &addresses = mid2.interfaceAddresses;
90  addresses.clear ();
91  addresses.push_back (Ipv4Address ("2.2.3.4"));
92  addresses.push_back (Ipv4Address ("2.2.3.5"));
93  }
94 
95  msg2.SetTimeToLive (254);
96  msg2.SetOriginatorAddress (Ipv4Address ("12.22.33.44"));
97  msg2.SetVTime (Seconds (10));
98  msg2.SetMessageType (olsr::MessageHeader::MID_MESSAGE);
99  msg2.SetMessageSequenceNumber (7);
100 
101  // Build an OLSR packet header
102  hdr.SetPacketLength (hdr.GetSerializedSize () + msg1.GetSerializedSize () + msg2.GetSerializedSize ());
103  hdr.SetPacketSequenceNumber (123);
104 
105 
106  // Now add all the headers in the correct order
107  packet.AddHeader (msg2);
108  packet.AddHeader (msg1);
109  packet.AddHeader (hdr);
110  }
111 
112  {
113  olsr::PacketHeader hdr;
114  packet.RemoveHeader (hdr);
115  NS_TEST_ASSERT_MSG_EQ (hdr.GetPacketSequenceNumber (), 123, "200");
116  uint32_t sizeLeft = hdr.GetPacketLength () - hdr.GetSerializedSize ();
117  {
118  olsr::MessageHeader msg1;
119 
120  packet.RemoveHeader (msg1);
121 
122  NS_TEST_ASSERT_MSG_EQ (msg1.GetTimeToLive (), 255, "201");
123  NS_TEST_ASSERT_MSG_EQ (msg1.GetOriginatorAddress (), Ipv4Address ("11.22.33.44"), "202");
124  NS_TEST_ASSERT_MSG_EQ (msg1.GetVTime (), Seconds (9), "203");
125  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "204");
126  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageSequenceNumber (), 7, "205");
127 
128  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
129  NS_TEST_ASSERT_MSG_EQ (mid1.interfaceAddresses.size (), 2, "206");
130  NS_TEST_ASSERT_MSG_EQ (*mid1.interfaceAddresses.begin (), Ipv4Address ("1.2.3.4"), "207");
131 
132  sizeLeft -= msg1.GetSerializedSize ();
133  NS_TEST_ASSERT_MSG_EQ ((sizeLeft > 0), true, "208");
134  }
135  {
136  // now read the second message
137  olsr::MessageHeader msg2;
138 
139  packet.RemoveHeader (msg2);
140 
141  NS_TEST_ASSERT_MSG_EQ (msg2.GetTimeToLive (), 254, "209");
142  NS_TEST_ASSERT_MSG_EQ (msg2.GetOriginatorAddress (), Ipv4Address ("12.22.33.44"), "210");
143  NS_TEST_ASSERT_MSG_EQ (msg2.GetVTime (), Seconds (10), "211");
144  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "212");
145  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageSequenceNumber (), 7, "213");
146 
147  olsr::MessageHeader::Mid mid2 = msg2.GetMid ();
148  NS_TEST_ASSERT_MSG_EQ (mid2.interfaceAddresses.size (), 2, "214");
149  NS_TEST_ASSERT_MSG_EQ (*mid2.interfaceAddresses.begin (), Ipv4Address ("2.2.3.4"), "215");
150 
151  sizeLeft -= msg2.GetSerializedSize ();
152  NS_TEST_ASSERT_MSG_EQ (sizeLeft, 0, "216");
153  }
154  }
155 }
156 
157 
159 {
160 public:
162  virtual void DoRun (void);
163 };
164 
166  : TestCase ("Check Hello olsr messages")
167 {
168 }
169 void
171 {
172  Packet packet;
173  olsr::MessageHeader msgIn;
174  olsr::MessageHeader::Hello &helloIn = msgIn.GetHello ();
175 
176  helloIn.SetHTime (Seconds (7));
177  helloIn.willingness = 66;
178 
179  {
181  lm1.linkCode = 2;
182  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.4"));
183  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.5"));
184  helloIn.linkMessages.push_back (lm1);
185 
187  lm2.linkCode = 3;
188  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.4"));
189  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.5"));
190  helloIn.linkMessages.push_back (lm2);
191  }
192 
193  packet.AddHeader (msgIn);
194 
195  olsr::MessageHeader msgOut;
196  packet.RemoveHeader (msgOut);
197  olsr::MessageHeader::Hello &helloOut = msgOut.GetHello ();
198 
199  NS_TEST_ASSERT_MSG_EQ (helloOut.GetHTime (), Seconds (7), "300");
200  NS_TEST_ASSERT_MSG_EQ (helloOut.willingness, 66, "301");
201  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages.size (), 2, "302");
202 
203  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].linkCode, 2, "303");
204  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[0],
205  Ipv4Address ("1.2.3.4"), "304");
206  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[1],
207  Ipv4Address ("1.2.3.5"), "305");
208 
209  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].linkCode, 3, "306");
210  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[0],
211  Ipv4Address ("2.2.3.4"), "307");
212  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[1],
213  Ipv4Address ("2.2.3.5"), "308");
214 
215  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
216 
217 }
218 
219 class OlsrTcTestCase : public TestCase
220 {
221 public:
222  OlsrTcTestCase ();
223  virtual void DoRun (void);
224 };
225 
227  : TestCase ("Check Tc olsr messages")
228 {
229 }
230 void
232 {
233  Packet packet;
234  olsr::MessageHeader msgIn;
235  olsr::MessageHeader::Tc &tcIn = msgIn.GetTc ();
236 
237  tcIn.ansn = 0x1234;
238  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.4"));
239  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.5"));
240  packet.AddHeader (msgIn);
241 
242  olsr::MessageHeader msgOut;
243  packet.RemoveHeader (msgOut);
244  olsr::MessageHeader::Tc &tcOut = msgOut.GetTc ();
245 
246  NS_TEST_ASSERT_MSG_EQ (tcOut.ansn, 0x1234, "400");
247  NS_TEST_ASSERT_MSG_EQ (tcOut.neighborAddresses.size (), 2, "401");
248 
250  Ipv4Address ("1.2.3.4"), "402");
252  Ipv4Address ("1.2.3.5"), "403");
253 
254  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "404");
255 
256 }
257 
258 class OlsrHnaTestCase : public TestCase
259 {
260 public:
261  OlsrHnaTestCase ();
262  virtual void DoRun (void);
263 };
264 
266  : TestCase ("Check Hna olsr messages")
267 {
268 }
269 
270 void
272 {
273  Packet packet;
274  olsr::MessageHeader msgIn;
275  olsr::MessageHeader::Hna &hnaIn = msgIn.GetHna ();
276 
278  { Ipv4Address ("1.2.3.4"), Ipv4Mask ("255.255.255.0")});
280  { Ipv4Address ("1.2.3.5"), Ipv4Mask ("255.255.0.0")});
281  packet.AddHeader (msgIn);
282 
283  olsr::MessageHeader msgOut;
284  packet.RemoveHeader (msgOut);
285  olsr::MessageHeader::Hna &hnaOut = msgOut.GetHna ();
286 
287  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations.size (), 2, "500");
288 
289  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[0].address,
290  Ipv4Address ("1.2.3.4"), "501");
291  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[0].mask,
292  Ipv4Mask ("255.255.255.0"), "502");
293 
294  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].address,
295  Ipv4Address ("1.2.3.5"), "503");
296  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].mask,
297  Ipv4Mask ("255.255.0.0"), "504");
298 
299  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
300 
301 }
302 
303 
304 static class OlsrTestSuite : public TestSuite
305 {
306 public:
307  OlsrTestSuite ();
309 
311  : TestSuite ("routing-olsr-header", UNIT)
312 {
313  AddTestCase (new OlsrHnaTestCase (), TestCase::QUICK);
314  AddTestCase (new OlsrTcTestCase (), TestCase::QUICK);
315  AddTestCase (new OlsrHelloTestCase (), TestCase::QUICK);
316  AddTestCase (new OlsrMidTestCase (), TestCase::QUICK);
317  AddTestCase (new OlsrEmfTestCase (), TestCase::QUICK);
318 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual void DoRun(void)
Implementation to actually run this TestCase.
TC Message Format.
Definition: olsr-header.h:458
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
A suite of tests to run.
Definition: test.h:1333
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
void SetHTime(Time time)
Set the HELLO emission interval.
Definition: olsr-header.h:393
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:408
encapsulates test code
Definition: test.h:1147
uint16_t ansn
Advertised Neighbor Sequence Number.
Definition: olsr-header.h:461
network packets
Definition: packet.h:227
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
Definition: olsr-header.h:315
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:460
Association item structure.
Definition: olsr-header.h:519
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
virtual void DoRun(void)
Implementation to actually run this TestCase.
#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:161
Time GetHTime() const
Get the HELLO emission interval.
Definition: olsr-header.h:402
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t willingness
The willingness of a node to carry and forward traffic for other nodes.
Definition: olsr-header.h:407
std::vector< Association > associations
Association container.
Definition: olsr-header.h:525
HNA (Host Network Association) Message Format.
Definition: olsr-header.h:514
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
HELLO Message Format.
Definition: olsr-header.h:376
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
MID Message Format.
Definition: olsr-header.h:313
uint8_t SecondsToEmf(double seconds)
Converts a decimal number of seconds to the mantissa/exponent format.
Definition: olsr-header.cc:49
virtual void DoRun(void)
Implementation to actually run this TestCase.
double EmfToSeconds(uint8_t olsrFormat)
Converts a number of seconds in the mantissa/exponent format to a decimal number. ...
Definition: olsr-header.cc:89
OlsrTestSuite g_olsrTestSuite
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257