A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 public:
29  OlsrEmfTestCase ();
30  virtual void DoRun (void);
31 };
32 
34  : TestCase ("Check Emf olsr time conversion")
35 {
36 }
37 void
39 {
40  for (int time = 1; time <= 30; time++)
41  {
42  uint8_t emf = olsr::SecondsToEmf (time);
43  double seconds = olsr::EmfToSeconds (emf);
44  NS_TEST_ASSERT_MSG_EQ ((seconds < 0 || std::fabs (seconds - time) > 0.1), false,
45  "100");
46  }
47 }
48 
49 
50 class OlsrMidTestCase : public TestCase {
51 public:
52  OlsrMidTestCase ();
53  virtual void DoRun (void);
54 };
55 
57  : TestCase ("Check Mid olsr messages")
58 {
59 }
60 void
62 {
63  Packet packet;
64 
65  {
66  olsr::PacketHeader hdr;
67  olsr::MessageHeader msg1;
68  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
69  olsr::MessageHeader msg2;
70  olsr::MessageHeader::Mid &mid2 = msg2.GetMid ();
71 
72  // MID message #1
73  {
74  std::vector<Ipv4Address> &addresses = mid1.interfaceAddresses;
75  addresses.clear ();
76  addresses.push_back (Ipv4Address ("1.2.3.4"));
77  addresses.push_back (Ipv4Address ("1.2.3.5"));
78  }
79 
80  msg1.SetTimeToLive (255);
81  msg1.SetOriginatorAddress (Ipv4Address ("11.22.33.44"));
82  msg1.SetVTime (Seconds (9));
83  msg1.SetMessageSequenceNumber (7);
84 
85  // MID message #2
86  {
87  std::vector<Ipv4Address> &addresses = mid2.interfaceAddresses;
88  addresses.clear ();
89  addresses.push_back (Ipv4Address ("2.2.3.4"));
90  addresses.push_back (Ipv4Address ("2.2.3.5"));
91  }
92 
93  msg2.SetTimeToLive (254);
94  msg2.SetOriginatorAddress (Ipv4Address ("12.22.33.44"));
95  msg2.SetVTime (Seconds (10));
96  msg2.SetMessageType (olsr::MessageHeader::MID_MESSAGE);
97  msg2.SetMessageSequenceNumber (7);
98 
99  // Build an OLSR packet header
100  hdr.SetPacketLength (hdr.GetSerializedSize () + msg1.GetSerializedSize () + msg2.GetSerializedSize ());
101  hdr.SetPacketSequenceNumber (123);
102 
103 
104  // Now add all the headers in the correct order
105  packet.AddHeader (msg2);
106  packet.AddHeader (msg1);
107  packet.AddHeader (hdr);
108  }
109 
110  {
111  olsr::PacketHeader hdr;
112  packet.RemoveHeader (hdr);
113  NS_TEST_ASSERT_MSG_EQ (hdr.GetPacketSequenceNumber (), 123, "200");
114  uint32_t sizeLeft = hdr.GetPacketLength () - hdr.GetSerializedSize ();
115  {
116  olsr::MessageHeader msg1;
117 
118  packet.RemoveHeader (msg1);
119 
120  NS_TEST_ASSERT_MSG_EQ (msg1.GetTimeToLive (), 255, "201");
121  NS_TEST_ASSERT_MSG_EQ (msg1.GetOriginatorAddress (), Ipv4Address ("11.22.33.44"), "202");
122  NS_TEST_ASSERT_MSG_EQ (msg1.GetVTime (), Seconds (9), "203");
123  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "204");
124  NS_TEST_ASSERT_MSG_EQ (msg1.GetMessageSequenceNumber (), 7, "205");
125 
126  olsr::MessageHeader::Mid &mid1 = msg1.GetMid ();
127  NS_TEST_ASSERT_MSG_EQ (mid1.interfaceAddresses.size (), 2, "206");
128  NS_TEST_ASSERT_MSG_EQ (*mid1.interfaceAddresses.begin (), Ipv4Address ("1.2.3.4"), "207");
129 
130  sizeLeft -= msg1.GetSerializedSize ();
131  NS_TEST_ASSERT_MSG_EQ ((sizeLeft > 0), true, "208");
132  }
133  {
134  // now read the second message
135  olsr::MessageHeader msg2;
136 
137  packet.RemoveHeader (msg2);
138 
139  NS_TEST_ASSERT_MSG_EQ (msg2.GetTimeToLive (), 254, "209");
140  NS_TEST_ASSERT_MSG_EQ (msg2.GetOriginatorAddress (), Ipv4Address ("12.22.33.44"), "210");
141  NS_TEST_ASSERT_MSG_EQ (msg2.GetVTime (), Seconds (10), "211");
142  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageType (), olsr::MessageHeader::MID_MESSAGE, "212");
143  NS_TEST_ASSERT_MSG_EQ (msg2.GetMessageSequenceNumber (), 7, "213");
144 
145  olsr::MessageHeader::Mid mid2 = msg2.GetMid ();
146  NS_TEST_ASSERT_MSG_EQ (mid2.interfaceAddresses.size (), 2, "214");
147  NS_TEST_ASSERT_MSG_EQ (*mid2.interfaceAddresses.begin (), Ipv4Address ("2.2.3.4"), "215");
148 
149  sizeLeft -= msg2.GetSerializedSize ();
150  NS_TEST_ASSERT_MSG_EQ (sizeLeft, 0, "216");
151  }
152  }
153 }
154 
155 
156 class OlsrHelloTestCase : public TestCase {
157 public:
159  virtual void DoRun (void);
160 };
161 
163  : TestCase ("Check Hello olsr messages")
164 {
165 }
166 void
168 {
169  Packet packet;
170  olsr::MessageHeader msgIn;
171  olsr::MessageHeader::Hello &helloIn = msgIn.GetHello ();
172 
173  helloIn.SetHTime (Seconds (7));
174  helloIn.willingness = 66;
175 
176  {
178  lm1.linkCode = 2;
179  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.4"));
180  lm1.neighborInterfaceAddresses.push_back (Ipv4Address ("1.2.3.5"));
181  helloIn.linkMessages.push_back (lm1);
182 
184  lm2.linkCode = 3;
185  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.4"));
186  lm2.neighborInterfaceAddresses.push_back (Ipv4Address ("2.2.3.5"));
187  helloIn.linkMessages.push_back (lm2);
188  }
189 
190  packet.AddHeader (msgIn);
191 
192  olsr::MessageHeader msgOut;
193  packet.RemoveHeader (msgOut);
194  olsr::MessageHeader::Hello &helloOut = msgOut.GetHello ();
195 
196  NS_TEST_ASSERT_MSG_EQ (helloOut.GetHTime (), Seconds (7), "300");
197  NS_TEST_ASSERT_MSG_EQ (helloOut.willingness, 66, "301");
198  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages.size (), 2, "302");
199 
200  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].linkCode, 2, "303");
201  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[0],
202  Ipv4Address ("1.2.3.4"), "304");
203  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[0].neighborInterfaceAddresses[1],
204  Ipv4Address ("1.2.3.5"), "305");
205 
206  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].linkCode, 3, "306");
207  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[0],
208  Ipv4Address ("2.2.3.4"), "307");
209  NS_TEST_ASSERT_MSG_EQ (helloOut.linkMessages[1].neighborInterfaceAddresses[1],
210  Ipv4Address ("2.2.3.5"), "308");
211 
212  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
213 
214 }
215 
216 class OlsrTcTestCase : public TestCase {
217 public:
218  OlsrTcTestCase ();
219  virtual void DoRun (void);
220 };
221 
223  : TestCase ("Check Tc olsr messages")
224 {
225 }
226 void
228 {
229  Packet packet;
230  olsr::MessageHeader msgIn;
231  olsr::MessageHeader::Tc &tcIn = msgIn.GetTc ();
232 
233  tcIn.ansn = 0x1234;
234  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.4"));
235  tcIn.neighborAddresses.push_back (Ipv4Address ("1.2.3.5"));
236  packet.AddHeader (msgIn);
237 
238  olsr::MessageHeader msgOut;
239  packet.RemoveHeader (msgOut);
240  olsr::MessageHeader::Tc &tcOut = msgOut.GetTc ();
241 
242  NS_TEST_ASSERT_MSG_EQ (tcOut.ansn, 0x1234, "400");
243  NS_TEST_ASSERT_MSG_EQ (tcOut.neighborAddresses.size (), 2, "401");
244 
246  Ipv4Address ("1.2.3.4"), "402");
248  Ipv4Address ("1.2.3.5"), "403");
249 
250  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "404");
251 
252 }
253 
254 class OlsrHnaTestCase : public TestCase {
255 public:
256  OlsrHnaTestCase ();
257  virtual void DoRun (void);
258 };
259 
261  : TestCase ("Check Hna olsr messages")
262 {
263 }
264 
265 void
267 {
268  Packet packet;
269  olsr::MessageHeader msgIn;
270  olsr::MessageHeader::Hna &hnaIn = msgIn.GetHna ();
271 
273  { Ipv4Address ("1.2.3.4"), Ipv4Mask ("255.255.255.0")});
275  { Ipv4Address ("1.2.3.5"), Ipv4Mask ("255.255.0.0")});
276  packet.AddHeader (msgIn);
277 
278  olsr::MessageHeader msgOut;
279  packet.RemoveHeader (msgOut);
280  olsr::MessageHeader::Hna &hnaOut = msgOut.GetHna ();
281 
282  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations.size (), 2, "500");
283 
284  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[0].address,
285  Ipv4Address ("1.2.3.4"), "501");
286  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[0].mask,
287  Ipv4Mask ("255.255.255.0"), "502");
288 
289  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].address,
290  Ipv4Address ("1.2.3.5"), "503");
291  NS_TEST_ASSERT_MSG_EQ (hnaOut.associations[1].mask,
292  Ipv4Mask ("255.255.0.0"), "504");
293 
294  NS_TEST_ASSERT_MSG_EQ (packet.GetSize (), 0, "All bytes in packet were not read");
295 
296 }
297 
298 
299 static class OlsrTestSuite : public TestSuite
300 {
301 public:
302  OlsrTestSuite ();
304 
306  : TestSuite ("routing-olsr-header", UNIT)
307 {
308  AddTestCase (new OlsrHnaTestCase (), TestCase::QUICK);
309  AddTestCase (new OlsrTcTestCase (), TestCase::QUICK);
310  AddTestCase (new OlsrHelloTestCase (), TestCase::QUICK);
311  AddTestCase (new OlsrMidTestCase (), TestCase::QUICK);
312  AddTestCase (new OlsrEmfTestCase (), TestCase::QUICK);
313 }
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.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
A suite of tests to run.
Definition: test.h:1105
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:744
std::vector< LinkMessage > linkMessages
Definition: olsr-header.h:276
encapsulates test code
Definition: test.h:929
network packets
Definition: packet.h:223
virtual void DoRun(void)
Implementation to actually run this TestCase.
std::vector< Ipv4Address > interfaceAddresses
Definition: olsr-header.h:225
std::vector< Ipv4Address > neighborAddresses
Definition: olsr-header.h:302
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:148
std::vector< Association > associations
Definition: olsr-header.h:338
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint8_t SecondsToEmf(double seconds)
Converts a decimal number of seconds to the mantissa/exponent format.
Definition: olsr-header.cc:48
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:87
OlsrTestSuite g_olsrTestSuite
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253