A Discrete-Event Network Simulator
API
tc-regression-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
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  * Authors: Pavel Boyko <boyko@iitp.ru>
19  */
20 
21 #include "tc-regression-test.h"
22 #include "ns3/simulator.h"
23 #include "ns3/random-variable-stream.h"
24 #include "ns3/rng-seed-manager.h"
25 #include "ns3/boolean.h"
26 #include "ns3/double.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/string.h"
29 #include "ns3/olsr-helper.h"
30 #include "ns3/internet-stack-helper.h"
31 #include "ns3/ipv4-address-helper.h"
32 #include "ns3/abort.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/ipv4-raw-socket-factory.h"
35 #include "ns3/udp-l4-protocol.h"
36 #include "ns3/udp-header.h"
37 #include "ns3/olsr-header.h"
38 #include "ns3/simple-net-device-helper.h"
39 #include "ns3/simple-net-device.h"
40 
41 namespace ns3 {
42 namespace olsr {
43 
45  TestCase ("Test OLSR Topology Control message generation"),
46  m_time (Seconds (20)),
47  m_countA (0), m_countB (0), m_countC (0)
48 {
49 }
50 
52 {
53 }
54 
55 void
57 {
60  CreateNodes ();
61 
63  Simulator::Run ();
64 
65  m_rxSocketA = 0;
66  m_rxSocketB = 0;
67  m_rxSocketC = 0;
69 }
70 
71 void
73 {
74  // create 3 nodes
75  NodeContainer c;
76  c.Create (3);
77 
78  // install TCP/IP & OLSR
80  InternetStackHelper internet;
81  internet.SetRoutingHelper (olsr);
82  internet.Install (c);
83  int64_t streamsUsed = olsr.AssignStreams (c, 0);
84  NS_TEST_EXPECT_MSG_EQ (streamsUsed, 3, "Should have assigned 3 streams");
85 
86  // create channel & devices
87  SimpleNetDeviceHelper simpleNetHelper;
88  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
89  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
90  NetDeviceContainer nd = simpleNetHelper.Install (c);
91 
92  // Blacklist some devices (equivalent to Wireless out of range)
93  Ptr<SimpleNetDevice> nd0 = DynamicCast<SimpleNetDevice> (nd.Get (0));
94  Ptr<SimpleNetDevice> nd2 = DynamicCast<SimpleNetDevice> (nd.Get (2));
95  Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel> (nd.Get (0)->GetChannel ());
96  ch->BlackList (nd0, nd2);
97  ch->BlackList (nd2, nd0);
98 
99  // setup IP addresses
100  Ipv4AddressHelper ipv4;
101  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
102  ipv4.Assign (nd);
103 
104  // Create the sockets
105  Ptr<SocketFactory> rxSocketFactoryA = c.Get (0)->GetObject<Ipv4RawSocketFactory> ();
106  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryA->CreateSocket ());
109 
110  Ptr<SocketFactory> rxSocketFactoryB = c.Get (1)->GetObject<Ipv4RawSocketFactory> ();
111  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryB->CreateSocket ());
114 
115  Ptr<SocketFactory> rxSocketFactoryC = c.Get (2)->GetObject<Ipv4RawSocketFactory> ();
116  m_rxSocketC = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryC->CreateSocket ());
119 }
120 
121 // Note: this is identical to ReceivePktProbeC, but the packet counter needs to be different.
122 void
124 {
125  uint32_t availableData;
126  availableData = socket->GetRxAvailable ();
127  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
128  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
129 
130  Ipv4Header ipHdr;
131  receivedPacketProbe->RemoveHeader (ipHdr);
132  UdpHeader udpHdr;
133  receivedPacketProbe->RemoveHeader (udpHdr);
134  PacketHeader pktHdr;
135  receivedPacketProbe->RemoveHeader (pktHdr);
136 
137  if (m_countA == 0)
138  {
139  MessageHeader msgHdr;
140  receivedPacketProbe->RemoveHeader (msgHdr);
141  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
142  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
143  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "0 - Hello, No messages.");
144  }
145  else if (m_countA == 1)
146  {
147  MessageHeader msgHdr;
148  receivedPacketProbe->RemoveHeader (msgHdr);
149  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
150  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
151  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, "1 - Hello, one message.");
152  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
153  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
154  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), "1 - Neighbor.");
155  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
156  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
157  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "1 - Neighbor.");
158  }
159  else
160  {
161  if (m_countA == 5 || m_countA == 8)
162  {
163  MessageHeader msgHdr;
164  receivedPacketProbe->RemoveHeader (msgHdr);
165  const olsr::MessageHeader::Tc &tc = msgHdr.GetTc ();
166  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
167  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses.size (), 2, int(m_countA) << " - TC, one message.");
168  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countA) << " - Neighbor.");
169  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[1], Ipv4Address ("10.1.1.1"), int(m_countA) << " - Neighbor.");
170  }
171  if (m_countA != 8)
172  {
173  MessageHeader msgHdr;
174  receivedPacketProbe->RemoveHeader (msgHdr);
175  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
176  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
177  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, int(m_countA) << " - Hello, one message.");
178  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countA) << " - Symmetric Link.");
179  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, int(m_countA) << " - Neighbor.");
180  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countA) << " - Neighbor.");
181  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 6, int(m_countA) << " - Symmetric Link.");
182  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, int(m_countA) << " - Neighbor.");
183  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), int(m_countA) << " - Neighbor.");
184  }
185  }
186  m_countA++;
187 }
188 
189 void
191 {
192  uint32_t availableData;
193  availableData = socket->GetRxAvailable ();
194  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
195  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
196 
197  Ipv4Header ipHdr;
198  receivedPacketProbe->RemoveHeader (ipHdr);
199  UdpHeader udpHdr;
200  receivedPacketProbe->RemoveHeader (udpHdr);
201  PacketHeader pktHdr;
202  receivedPacketProbe->RemoveHeader (pktHdr);
203 
204  MessageHeader msgHdr;
205  receivedPacketProbe->RemoveHeader (msgHdr);
206  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
207 
208  if (m_countB == 0 || m_countB == 2 || m_countB == 5 || m_countB == 6 || m_countB == 8
209  || m_countB == 10 || m_countB == 13 || m_countB == 15 || m_countB == 17 || m_countB == 19)
210  {
211  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.3"), "Originator address.");
212  }
213  else
214  {
215  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.1"), "Originator address.");
216  }
217 
218  if (m_countB == 0 || m_countB == 1)
219  {
220  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, int(m_countC) << " - Hello, links announced.");
221  }
222  else
223  {
224  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, int(m_countC) << " - Hello, links announced.");
225  if (m_countB == 2 || m_countB == 3)
226  {
227  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, int(m_countC) << " - Asymmetric Link.");
228  }
229  else if (m_countB == 4 || m_countB == 5)
230  {
231  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countC) << " - Symmetric Link.");
232  }
233  else
234  {
235  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 10, int(m_countC) << " - MPR Link.");
236  }
237 
238  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.2"), int(m_countC) << " - Neighbor.");
239  }
240 
241  m_countB++;
242 }
243 
244 // Note: this is identical to ReceivePktProbeA, but the packet counter needs to be different.
245 void
247 {
248  uint32_t availableData;
249  availableData = socket->GetRxAvailable ();
250  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
251  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
252 
253  Ipv4Header ipHdr;
254  receivedPacketProbe->RemoveHeader (ipHdr);
255  UdpHeader udpHdr;
256  receivedPacketProbe->RemoveHeader (udpHdr);
257  PacketHeader pktHdr;
258  receivedPacketProbe->RemoveHeader (pktHdr);
259 
260  if (m_countC == 0)
261  {
262  MessageHeader msgHdr;
263  receivedPacketProbe->RemoveHeader (msgHdr);
264  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
265  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
266  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "0 - Hello, No messages.");
267  }
268  else if (m_countC == 1)
269  {
270  MessageHeader msgHdr;
271  receivedPacketProbe->RemoveHeader (msgHdr);
272  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
273  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
274  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, "1 - Hello, one message.");
275  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
276  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
277  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), "1 - Neighbor.");
278  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
279  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
280  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "1 - Neighbor.");
281  }
282  else
283  {
284  if (m_countC == 5 || m_countC == 8)
285  {
286  MessageHeader msgHdr;
287  receivedPacketProbe->RemoveHeader (msgHdr);
288  const olsr::MessageHeader::Tc &tc = msgHdr.GetTc ();
289  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
290  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses.size (), 2, int(m_countC) << " - TC, one message.");
291  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countC) << " - Neighbor.");
292  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[1], Ipv4Address ("10.1.1.1"), int(m_countC) << " - Neighbor.");
293  }
294  if (m_countC != 8)
295  {
296  MessageHeader msgHdr;
297  receivedPacketProbe->RemoveHeader (msgHdr);
298  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
299  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
300  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, int(m_countC) << " - Hello, one message.");
301  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countC) << " - Symmetric Link.");
302  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, int(m_countC) << " - Neighbor.");
303  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countC) << " - Neighbor.");
304  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 6, int(m_countC) << " - Symmetric Link.");
305  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, int(m_countC) << " - Neighbor.");
306  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), int(m_countC) << " - Neighbor.");
307  }
308  }
309  m_countC++;
310 }
311 
312 }
313 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
void CreateNodes()
Create & configure test network.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:157
Hold variables of type string.
Definition: string.h:41
TC Message Format
Definition: olsr-header.h:458
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
aggregate IP/TCP/UDP functionality to existing Nodes.
#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:285
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:40
Tc & GetTc()
Set the message type to TC and return the message content.
Definition: olsr-header.h:608
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:408
encapsulates test code
Definition: test.h:1155
API to create RAW socket instances.
void ReceivePktProbeC(Ptr< Socket > socket)
Receive raw data on node C.
Ptr< Ipv4RawSocketImpl > m_rxSocketB
Receiving socket on node B.
uint8_t m_countA
Packet counter on node A.
static void SetRun(uint64_t run)
Set the run number of simulation.
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.
Definition: olsr-header.h:460
Packet header for IPv4.
Definition: ipv4-header.h:33
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
void DoRun()
Implementation to actually run this TestCase.
#define max(a, b)
Definition: 80211b.c:43
holds a vector of ns3::NetDevice pointers
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Ptr< Ipv4RawSocketImpl > m_rxSocketC
Receiving socket on node C.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
void ReceivePktProbeB(Ptr< Socket > socket)
Receive raw data on node B.
uint8_t m_countC
Packet counter on node C.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Definition: olsr-header.h:220
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Packet header for UDP packets.
Definition: udp-header.h:39
Definition: olsr.py:1
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers): ...
Definition: olsr-header.h:75
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
static void SetSeed(uint32_t seed)
Set the seed.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Hello & GetHello()
Set the message type to HELLO and return the message content.
Definition: olsr-header.h:591
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
HELLO Message Format
Definition: olsr-header.h:376
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
const Time m_time
Total simulation time.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
build a set of SimpleNetDevice objects
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
static const uint8_t PROT_NUMBER
protocol number (0x11)
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void SetProtocol(uint16_t protocol)
Set protocol field.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
virtual uint32_t GetRxAvailable(void) const =0
Return number of bytes which can be returned from one or multiple calls to Recv.
uint8_t m_countB
Packet counter on node B.