A Discrete-Event Network Simulator
API
tc-regression-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
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  * Authors: Pavel Boyko <boyko@iitp.ru>
18  */
19 
20 #include "tc-regression-test.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/boolean.h"
24 #include "ns3/double.h"
25 #include "ns3/internet-stack-helper.h"
26 #include "ns3/ipv4-address-helper.h"
27 #include "ns3/ipv4-raw-socket-factory.h"
28 #include "ns3/olsr-header.h"
29 #include "ns3/olsr-helper.h"
30 #include "ns3/random-variable-stream.h"
31 #include "ns3/rng-seed-manager.h"
32 #include "ns3/simple-net-device-helper.h"
33 #include "ns3/simple-net-device.h"
34 #include "ns3/simulator.h"
35 #include "ns3/socket-factory.h"
36 #include "ns3/string.h"
37 #include "ns3/udp-header.h"
38 #include "ns3/udp-l4-protocol.h"
39 #include "ns3/uinteger.h"
40 
41 namespace ns3
42 {
43 namespace olsr
44 {
45 
47  : TestCase("Test OLSR Topology Control message generation"),
48  m_time(Seconds(20)),
49  m_countA(0),
50  m_countB(0),
51  m_countC(0)
52 {
53 }
54 
56 {
57 }
58 
59 void
61 {
64  CreateNodes();
65 
68 
69  m_rxSocketA = nullptr;
70  m_rxSocketB = nullptr;
71  m_rxSocketC = nullptr;
73 }
74 
75 void
77 {
78  // create 3 nodes
79  NodeContainer c;
80  c.Create(3);
81 
82  // install TCP/IP & OLSR
85  internet.SetRoutingHelper(olsr);
86  internet.Install(c);
87  int64_t streamsUsed = olsr.AssignStreams(c, 0);
88  NS_TEST_EXPECT_MSG_EQ(streamsUsed, 3, "Should have assigned 3 streams");
89 
90  // create channel & devices
91  SimpleNetDeviceHelper simpleNetHelper;
92  simpleNetHelper.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
93  simpleNetHelper.SetChannelAttribute("Delay", StringValue("2ms"));
94  NetDeviceContainer nd = simpleNetHelper.Install(c);
95 
96  // Blacklist some devices (equivalent to Wireless out of range)
97  Ptr<SimpleNetDevice> nd0 = DynamicCast<SimpleNetDevice>(nd.Get(0));
98  Ptr<SimpleNetDevice> nd2 = DynamicCast<SimpleNetDevice>(nd.Get(2));
99  Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel>(nd.Get(0)->GetChannel());
100  ch->BlackList(nd0, nd2);
101  ch->BlackList(nd2, nd0);
102 
103  // setup IP addresses
105  ipv4.SetBase("10.1.1.0", "255.255.255.0");
106  ipv4.Assign(nd);
107 
108  // Create the sockets
109  Ptr<SocketFactory> rxSocketFactoryA = c.Get(0)->GetObject<Ipv4RawSocketFactory>();
110  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryA->CreateSocket());
113 
114  Ptr<SocketFactory> rxSocketFactoryB = c.Get(1)->GetObject<Ipv4RawSocketFactory>();
115  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryB->CreateSocket());
118 
119  Ptr<SocketFactory> rxSocketFactoryC = c.Get(2)->GetObject<Ipv4RawSocketFactory>();
120  m_rxSocketC = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryC->CreateSocket());
123 }
124 
125 // Note: this is identical to ReceivePktProbeC, but the packet counter needs to be different.
126 void
128 {
129  uint32_t availableData;
130  availableData = socket->GetRxAvailable();
131  Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
132  NS_ASSERT(availableData == receivedPacketProbe->GetSize());
133 
134  Ipv4Header ipHdr;
135  receivedPacketProbe->RemoveHeader(ipHdr);
136  UdpHeader udpHdr;
137  receivedPacketProbe->RemoveHeader(udpHdr);
138  PacketHeader pktHdr;
139  receivedPacketProbe->RemoveHeader(pktHdr);
140 
141  if (m_countA == 0)
142  {
143  MessageHeader msgHdr;
144  receivedPacketProbe->RemoveHeader(msgHdr);
145  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
147  Ipv4Address("10.1.1.2"),
148  "Originator address.");
149  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "0 - Hello, No messages.");
150  }
151  else if (m_countA == 1)
152  {
153  MessageHeader msgHdr;
154  receivedPacketProbe->RemoveHeader(msgHdr);
156  Ipv4Address("10.1.1.2"),
157  "Originator address.");
158  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
159  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 2, "1 - Hello, one message.");
160  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
161  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
162  1,
163  "1 - Neighbor.");
164  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
165  Ipv4Address("10.1.1.3"),
166  "1 - Neighbor.");
167  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
168  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
169  1,
170  "1 - Neighbor.");
171  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
172  Ipv4Address("10.1.1.1"),
173  "1 - Neighbor.");
174  }
175  else
176  {
177  if (m_countA == 5 || m_countA == 8)
178  {
179  MessageHeader msgHdr;
180  receivedPacketProbe->RemoveHeader(msgHdr);
181  const olsr::MessageHeader::Tc& tc = msgHdr.GetTc();
183  Ipv4Address("10.1.1.2"),
184  "Originator address.");
186  2,
187  int(m_countA) << " - TC, one message.");
189  Ipv4Address("10.1.1.3"),
190  int(m_countA) << " - Neighbor.");
192  Ipv4Address("10.1.1.1"),
193  int(m_countA) << " - Neighbor.");
194  }
195  if (m_countA != 8)
196  {
197  MessageHeader msgHdr;
198  receivedPacketProbe->RemoveHeader(msgHdr);
200  Ipv4Address("10.1.1.2"),
201  "Originator address.");
202  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
203  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
204  2,
205  int(m_countA) << " - Hello, one message.");
206  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
207  6,
208  int(m_countA) << " - Symmetric Link.");
209  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
210  1,
211  int(m_countA) << " - Neighbor.");
212  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
213  Ipv4Address("10.1.1.3"),
214  int(m_countA) << " - Neighbor.");
215  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode,
216  6,
217  int(m_countA) << " - Symmetric Link.");
218  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
219  1,
220  int(m_countA) << " - Neighbor.");
221  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
222  Ipv4Address("10.1.1.1"),
223  int(m_countA) << " - Neighbor.");
224  }
225  }
226  m_countA++;
227 }
228 
229 void
231 {
232  uint32_t availableData;
233  availableData = socket->GetRxAvailable();
234  Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
235  NS_ASSERT(availableData == receivedPacketProbe->GetSize());
236 
237  Ipv4Header ipHdr;
238  receivedPacketProbe->RemoveHeader(ipHdr);
239  UdpHeader udpHdr;
240  receivedPacketProbe->RemoveHeader(udpHdr);
241  PacketHeader pktHdr;
242  receivedPacketProbe->RemoveHeader(pktHdr);
243 
244  MessageHeader msgHdr;
245  receivedPacketProbe->RemoveHeader(msgHdr);
246  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
247 
248  if (m_countB == 0 || m_countB == 2 || m_countB == 5 || m_countB == 6 || m_countB == 8 ||
249  m_countB == 10 || m_countB == 13 || m_countB == 15 || m_countB == 17 || m_countB == 19)
250  {
252  Ipv4Address("10.1.1.3"),
253  "Originator address.");
254  }
255  else
256  {
258  Ipv4Address("10.1.1.1"),
259  "Originator address.");
260  }
261 
262  if (m_countB == 0 || m_countB == 1)
263  {
264  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
265  0,
266  int(m_countC) << " - Hello, links announced.");
267  }
268  else
269  {
270  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
271  1,
272  int(m_countC) << " - Hello, links announced.");
273  if (m_countB == 2 || m_countB == 3)
274  {
275  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
276  1,
277  int(m_countC) << " - Asymmetric Link.");
278  }
279  else if (m_countB == 4 || m_countB == 5)
280  {
281  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
282  6,
283  int(m_countC) << " - Symmetric Link.");
284  }
285  else
286  {
287  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
288  10,
289  int(m_countC) << " - MPR Link.");
290  }
291 
292  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
293  Ipv4Address("10.1.1.2"),
294  int(m_countC) << " - Neighbor.");
295  }
296 
297  m_countB++;
298 }
299 
300 // Note: this is identical to ReceivePktProbeA, but the packet counter needs to be different.
301 void
303 {
304  uint32_t availableData;
305  availableData = socket->GetRxAvailable();
306  Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
307  NS_ASSERT(availableData == receivedPacketProbe->GetSize());
308 
309  Ipv4Header ipHdr;
310  receivedPacketProbe->RemoveHeader(ipHdr);
311  UdpHeader udpHdr;
312  receivedPacketProbe->RemoveHeader(udpHdr);
313  PacketHeader pktHdr;
314  receivedPacketProbe->RemoveHeader(pktHdr);
315 
316  if (m_countC == 0)
317  {
318  MessageHeader msgHdr;
319  receivedPacketProbe->RemoveHeader(msgHdr);
320  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
322  Ipv4Address("10.1.1.2"),
323  "Originator address.");
324  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "0 - Hello, No messages.");
325  }
326  else if (m_countC == 1)
327  {
328  MessageHeader msgHdr;
329  receivedPacketProbe->RemoveHeader(msgHdr);
331  Ipv4Address("10.1.1.2"),
332  "Originator address.");
333  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
334  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 2, "1 - Hello, one message.");
335  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
336  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
337  1,
338  "1 - Neighbor.");
339  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
340  Ipv4Address("10.1.1.3"),
341  "1 - Neighbor.");
342  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
343  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
344  1,
345  "1 - Neighbor.");
346  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
347  Ipv4Address("10.1.1.1"),
348  "1 - Neighbor.");
349  }
350  else
351  {
352  if (m_countC == 5 || m_countC == 8)
353  {
354  MessageHeader msgHdr;
355  receivedPacketProbe->RemoveHeader(msgHdr);
356  const olsr::MessageHeader::Tc& tc = msgHdr.GetTc();
358  Ipv4Address("10.1.1.2"),
359  "Originator address.");
361  2,
362  int(m_countC) << " - TC, one message.");
364  Ipv4Address("10.1.1.3"),
365  int(m_countC) << " - Neighbor.");
367  Ipv4Address("10.1.1.1"),
368  int(m_countC) << " - Neighbor.");
369  }
370  if (m_countC != 8)
371  {
372  MessageHeader msgHdr;
373  receivedPacketProbe->RemoveHeader(msgHdr);
375  Ipv4Address("10.1.1.2"),
376  "Originator address.");
377  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
378  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
379  2,
380  int(m_countC) << " - Hello, one message.");
381  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
382  6,
383  int(m_countC) << " - Symmetric Link.");
384  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
385  1,
386  int(m_countC) << " - Neighbor.");
387  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
388  Ipv4Address("10.1.1.3"),
389  int(m_countC) << " - Neighbor.");
390  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode,
391  6,
392  int(m_countC) << " - Symmetric Link.");
393  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
394  1,
395  int(m_countC) << " - Neighbor.");
396  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
397  Ipv4Address("10.1.1.1"),
398  int(m_countC) << " - Neighbor.");
399  }
400  }
401  m_countC++;
402 }
403 
404 } // namespace olsr
405 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
API to create RAW socket instances.
void SetProtocol(uint16_t protocol)
Set protocol field.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:42
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
build a set of SimpleNetDevice objects
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
virtual uint32_t GetRxAvailable() const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1060
Packet header for UDP packets.
Definition: udp-header.h:41
static const uint8_t PROT_NUMBER
protocol number (0x11)
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:161
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Definition: olsr-header.h:226
Hello & GetHello()
Set the message type to HELLO and return the message content.
Definition: olsr-header.h:601
Tc & GetTc()
Set the message type to TC and return the message content.
Definition: olsr-header.h:618
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition: olsr-header.h:79
void CreateNodes()
Create & configure test network.
void ReceivePktProbeB(Ptr< Socket > socket)
Receive raw data on node B.
uint8_t m_countB
Packet counter on node B.
Ptr< Ipv4RawSocketImpl > m_rxSocketB
Receiving socket on node B.
void ReceivePktProbeC(Ptr< Socket > socket)
Receive raw data on node C.
uint8_t m_countC
Packet counter on node C.
void DoRun() override
Implementation to actually run this TestCase.
Ptr< Ipv4RawSocketImpl > m_rxSocketC
Receiving socket on node C.
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
const Time m_time
Total simulation time.
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
uint8_t m_countA
Packet counter on node A.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:251
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Definition: olsr.py:1
double m_time
Definition: qkd_etsi_014.cc:62
HELLO Message Format.
Definition: olsr-header.h:386
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:419
TC Message Format.
Definition: olsr-header.h:470
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:471