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/packet.h"
22 #include "ns3/test.h"
23 
24 using namespace ns3;
25 
32 class OlsrEmfTestCase : public TestCase
33 {
34  public:
36  void DoRun() override;
37 };
38 
40  : TestCase("Check Emf olsr time conversion")
41 {
42 }
43 
44 void
46 {
47  for (int time = 1; time <= 30; time++)
48  {
49  uint8_t emf = olsr::SecondsToEmf(time);
50  double seconds = olsr::EmfToSeconds(emf);
51  NS_TEST_ASSERT_MSG_EQ((seconds < 0 || std::fabs(seconds - time) > 0.1), false, "100");
52  }
53 }
54 
61 class OlsrMidTestCase : public TestCase
62 {
63  public:
65  void DoRun() override;
66 };
67 
69  : TestCase("Check Mid olsr messages")
70 {
71 }
72 
73 void
75 {
76  Packet packet;
77 
78  {
79  olsr::PacketHeader hdr;
80  olsr::MessageHeader msg1;
81  olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
82  olsr::MessageHeader msg2;
83  olsr::MessageHeader::Mid& mid2 = msg2.GetMid();
84 
85  // MID message #1
86  {
87  std::vector<Ipv4Address>& addresses = mid1.interfaceAddresses;
88  addresses.clear();
89  addresses.emplace_back("1.2.3.4");
90  addresses.emplace_back("1.2.3.5");
91  }
92 
93  msg1.SetTimeToLive(255);
94  msg1.SetOriginatorAddress(Ipv4Address("11.22.33.44"));
95  msg1.SetVTime(Seconds(9));
96  msg1.SetMessageSequenceNumber(7);
97 
98  // MID message #2
99  {
100  std::vector<Ipv4Address>& addresses = mid2.interfaceAddresses;
101  addresses.clear();
102  addresses.emplace_back("2.2.3.4");
103  addresses.emplace_back("2.2.3.5");
104  }
105 
106  msg2.SetTimeToLive(254);
107  msg2.SetOriginatorAddress(Ipv4Address("12.22.33.44"));
108  msg2.SetVTime(Seconds(10));
109  msg2.SetMessageType(olsr::MessageHeader::MID_MESSAGE);
110  msg2.SetMessageSequenceNumber(7);
111 
112  // Build an OLSR packet header
113  hdr.SetPacketLength(hdr.GetSerializedSize() + msg1.GetSerializedSize() +
114  msg2.GetSerializedSize());
115  hdr.SetPacketSequenceNumber(123);
116 
117  // Now add all the headers in the correct order
118  packet.AddHeader(msg2);
119  packet.AddHeader(msg1);
120  packet.AddHeader(hdr);
121  }
122 
123  {
124  olsr::PacketHeader hdr;
125  packet.RemoveHeader(hdr);
126  NS_TEST_ASSERT_MSG_EQ(hdr.GetPacketSequenceNumber(), 123, "200");
127  uint32_t sizeLeft = hdr.GetPacketLength() - hdr.GetSerializedSize();
128  {
129  olsr::MessageHeader msg1;
130 
131  packet.RemoveHeader(msg1);
132 
133  NS_TEST_ASSERT_MSG_EQ(msg1.GetTimeToLive(), 255, "201");
134  NS_TEST_ASSERT_MSG_EQ(msg1.GetOriginatorAddress(), Ipv4Address("11.22.33.44"), "202");
135  NS_TEST_ASSERT_MSG_EQ(msg1.GetVTime(), Seconds(9), "203");
136  NS_TEST_ASSERT_MSG_EQ(msg1.GetMessageType(), olsr::MessageHeader::MID_MESSAGE, "204");
137  NS_TEST_ASSERT_MSG_EQ(msg1.GetMessageSequenceNumber(), 7, "205");
138 
139  olsr::MessageHeader::Mid& mid1 = msg1.GetMid();
140  NS_TEST_ASSERT_MSG_EQ(mid1.interfaceAddresses.size(), 2, "206");
141  NS_TEST_ASSERT_MSG_EQ(*mid1.interfaceAddresses.begin(), Ipv4Address("1.2.3.4"), "207");
142 
143  sizeLeft -= msg1.GetSerializedSize();
144  NS_TEST_ASSERT_MSG_EQ((sizeLeft > 0), true, "208");
145  }
146  {
147  // now read the second message
148  olsr::MessageHeader msg2;
149 
150  packet.RemoveHeader(msg2);
151 
152  NS_TEST_ASSERT_MSG_EQ(msg2.GetTimeToLive(), 254, "209");
153  NS_TEST_ASSERT_MSG_EQ(msg2.GetOriginatorAddress(), Ipv4Address("12.22.33.44"), "210");
154  NS_TEST_ASSERT_MSG_EQ(msg2.GetVTime(), Seconds(10), "211");
155  NS_TEST_ASSERT_MSG_EQ(msg2.GetMessageType(), olsr::MessageHeader::MID_MESSAGE, "212");
156  NS_TEST_ASSERT_MSG_EQ(msg2.GetMessageSequenceNumber(), 7, "213");
157 
158  olsr::MessageHeader::Mid mid2 = msg2.GetMid();
159  NS_TEST_ASSERT_MSG_EQ(mid2.interfaceAddresses.size(), 2, "214");
160  NS_TEST_ASSERT_MSG_EQ(*mid2.interfaceAddresses.begin(), Ipv4Address("2.2.3.4"), "215");
161 
162  sizeLeft -= msg2.GetSerializedSize();
163  NS_TEST_ASSERT_MSG_EQ(sizeLeft, 0, "216");
164  }
165  }
166 }
167 
175 {
176  public:
178  void DoRun() override;
179 };
180 
182  : TestCase("Check Hello olsr messages")
183 {
184 }
185 
186 void
188 {
189  Packet packet;
190  olsr::MessageHeader msgIn;
191  olsr::MessageHeader::Hello& helloIn = msgIn.GetHello();
192 
193  helloIn.SetHTime(Seconds(7));
194  helloIn.willingness = 66;
195 
196  {
198  lm1.linkCode = 2;
199  lm1.neighborInterfaceAddresses.emplace_back("1.2.3.4");
200  lm1.neighborInterfaceAddresses.emplace_back("1.2.3.5");
201  helloIn.linkMessages.push_back(lm1);
202 
204  lm2.linkCode = 3;
205  lm2.neighborInterfaceAddresses.emplace_back("2.2.3.4");
206  lm2.neighborInterfaceAddresses.emplace_back("2.2.3.5");
207  helloIn.linkMessages.push_back(lm2);
208  }
209 
210  packet.AddHeader(msgIn);
211 
212  olsr::MessageHeader msgOut;
213  packet.RemoveHeader(msgOut);
214  olsr::MessageHeader::Hello& helloOut = msgOut.GetHello();
215 
216  NS_TEST_ASSERT_MSG_EQ(helloOut.GetHTime(), Seconds(7), "300");
217  NS_TEST_ASSERT_MSG_EQ(helloOut.willingness, 66, "301");
218  NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages.size(), 2, "302");
219 
220  NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].linkCode, 2, "303");
221  NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].neighborInterfaceAddresses[0],
222  Ipv4Address("1.2.3.4"),
223  "304");
224  NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[0].neighborInterfaceAddresses[1],
225  Ipv4Address("1.2.3.5"),
226  "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"),
231  "307");
232  NS_TEST_ASSERT_MSG_EQ(helloOut.linkMessages[1].neighborInterfaceAddresses[1],
233  Ipv4Address("2.2.3.5"),
234  "308");
235 
236  NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "All bytes in packet were not read");
237 }
238 
245 class OlsrTcTestCase : public TestCase
246 {
247  public:
248  OlsrTcTestCase();
249  void DoRun() override;
250 };
251 
253  : TestCase("Check Tc olsr messages")
254 {
255 }
256 
257 void
259 {
260  Packet packet;
261  olsr::MessageHeader msgIn;
262  olsr::MessageHeader::Tc& tcIn = msgIn.GetTc();
263 
264  tcIn.ansn = 0x1234;
265  tcIn.neighborAddresses.emplace_back("1.2.3.4");
266  tcIn.neighborAddresses.emplace_back("1.2.3.5");
267  packet.AddHeader(msgIn);
268 
269  olsr::MessageHeader msgOut;
270  packet.RemoveHeader(msgOut);
271  olsr::MessageHeader::Tc& tcOut = msgOut.GetTc();
272 
273  NS_TEST_ASSERT_MSG_EQ(tcOut.ansn, 0x1234, "400");
274  NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses.size(), 2, "401");
275 
276  NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses[0], Ipv4Address("1.2.3.4"), "402");
277  NS_TEST_ASSERT_MSG_EQ(tcOut.neighborAddresses[1], Ipv4Address("1.2.3.5"), "403");
278 
279  NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "404");
280 }
281 
288 class OlsrHnaTestCase : public TestCase
289 {
290  public:
291  OlsrHnaTestCase();
292  void DoRun() override;
293 };
294 
296  : TestCase("Check Hna olsr messages")
297 {
298 }
299 
300 void
302 {
303  Packet packet;
304  olsr::MessageHeader msgIn;
305  olsr::MessageHeader::Hna& hnaIn = msgIn.GetHna();
306 
307  hnaIn.associations.push_back(
308  (olsr::MessageHeader::Hna::Association){Ipv4Address("1.2.3.4"), Ipv4Mask("255.255.255.0")});
309  hnaIn.associations.push_back(
310  (olsr::MessageHeader::Hna::Association){Ipv4Address("1.2.3.5"), Ipv4Mask("255.255.0.0")});
311  packet.AddHeader(msgIn);
312 
313  olsr::MessageHeader msgOut;
314  packet.RemoveHeader(msgOut);
315  olsr::MessageHeader::Hna& hnaOut = msgOut.GetHna();
316 
317  NS_TEST_ASSERT_MSG_EQ(hnaOut.associations.size(), 2, "500");
318 
319  NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[0].address, Ipv4Address("1.2.3.4"), "501");
320  NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[0].mask, Ipv4Mask("255.255.255.0"), "502");
321 
322  NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[1].address, Ipv4Address("1.2.3.5"), "503");
323  NS_TEST_ASSERT_MSG_EQ(hnaOut.associations[1].mask, Ipv4Mask("255.255.0.0"), "504");
324 
325  NS_TEST_ASSERT_MSG_EQ(packet.GetSize(), 0, "All bytes in packet were not read");
326 }
327 
334 class OlsrTestSuite : public TestSuite
335 {
336  public:
337  OlsrTestSuite();
338 };
339 
341  : TestSuite("routing-olsr-header", UNIT)
342 {
343  AddTestCase(new OlsrHnaTestCase(), TestCase::QUICK);
344  AddTestCase(new OlsrTcTestCase(), TestCase::QUICK);
345  AddTestCase(new OlsrHelloTestCase(), TestCase::QUICK);
346  AddTestCase(new OlsrMidTestCase(), TestCase::QUICK);
347  AddTestCase(new OlsrEmfTestCase(), TestCase::QUICK);
348 }
349 
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:43
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
network packets
Definition: packet.h:241
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:863
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:305
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:1336
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:384
void SetHTime(Time time)
Set the HELLO emission interval.
Definition: olsr-header.h:401
uint8_t willingness
The willingness of a node to carry and forward traffic for other nodes.
Definition: olsr-header.h:415
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:417
Time GetHTime() const
Get the HELLO emission interval.
Definition: olsr-header.h:410
HNA (Host Network Association) Message Format.
Definition: olsr-header.h:523
std::vector< Association > associations
Association container.
Definition: olsr-header.h:533
MID Message Format.
Definition: olsr-header.h:321
std::vector< Ipv4Address > interfaceAddresses
Interface Address container.
Definition: olsr-header.h:322
TC Message Format.
Definition: olsr-header.h:468
uint16_t ansn
Advertised Neighbor Sequence Number.
Definition: olsr-header.h:470
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:469