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 {
43 namespace olsr
44 {
45 
47  TestCase ("Test OLSR Topology Control message generation"),
48  m_time (Seconds (20)),
49  m_countA (0), m_countB (0), m_countC (0)
50 {
51 }
52 
54 {
55 }
56 
57 void
59 {
62  CreateNodes ();
63 
65  Simulator::Run ();
66 
67  m_rxSocketA = 0;
68  m_rxSocketB = 0;
69  m_rxSocketC = 0;
71 }
72 
73 void
75 {
76  // create 3 nodes
77  NodeContainer c;
78  c.Create (3);
79 
80  // install TCP/IP & OLSR
82  InternetStackHelper internet;
83  internet.SetRoutingHelper (olsr);
84  internet.Install (c);
85  int64_t streamsUsed = olsr.AssignStreams (c, 0);
86  NS_TEST_EXPECT_MSG_EQ (streamsUsed, 3, "Should have assigned 3 streams");
87 
88  // create channel & devices
89  SimpleNetDeviceHelper simpleNetHelper;
90  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
91  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
92  NetDeviceContainer nd = simpleNetHelper.Install (c);
93 
94  // Blacklist some devices (equivalent to Wireless out of range)
95  Ptr<SimpleNetDevice> nd0 = DynamicCast<SimpleNetDevice> (nd.Get (0));
96  Ptr<SimpleNetDevice> nd2 = DynamicCast<SimpleNetDevice> (nd.Get (2));
97  Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel> (nd.Get (0)->GetChannel ());
98  ch->BlackList (nd0, nd2);
99  ch->BlackList (nd2, nd0);
100 
101  // setup IP addresses
102  Ipv4AddressHelper ipv4;
103  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
104  ipv4.Assign (nd);
105 
106  // Create the sockets
107  Ptr<SocketFactory> rxSocketFactoryA = c.Get (0)->GetObject<Ipv4RawSocketFactory> ();
108  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryA->CreateSocket ());
109  m_rxSocketA->SetProtocol (UdpL4Protocol::PROT_NUMBER);
110  m_rxSocketA->SetRecvCallback (MakeCallback (&TcRegressionTest::ReceivePktProbeA, this));
111 
112  Ptr<SocketFactory> rxSocketFactoryB = c.Get (1)->GetObject<Ipv4RawSocketFactory> ();
113  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryB->CreateSocket ());
114  m_rxSocketB->SetProtocol (UdpL4Protocol::PROT_NUMBER);
115  m_rxSocketB->SetRecvCallback (MakeCallback (&TcRegressionTest::ReceivePktProbeB, this));
116 
117  Ptr<SocketFactory> rxSocketFactoryC = c.Get (2)->GetObject<Ipv4RawSocketFactory> ();
118  m_rxSocketC = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryC->CreateSocket ());
119  m_rxSocketC->SetProtocol (UdpL4Protocol::PROT_NUMBER);
120  m_rxSocketC->SetRecvCallback (MakeCallback (&TcRegressionTest::ReceivePktProbeC, this));
121 }
122 
123 // Note: this is identical to ReceivePktProbeC, but the packet counter needs to be different.
124 void
126 {
127  uint32_t availableData;
128  availableData = socket->GetRxAvailable ();
129  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
130  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
131 
132  Ipv4Header ipHdr;
133  receivedPacketProbe->RemoveHeader (ipHdr);
134  UdpHeader udpHdr;
135  receivedPacketProbe->RemoveHeader (udpHdr);
136  PacketHeader pktHdr;
137  receivedPacketProbe->RemoveHeader (pktHdr);
138 
139  if (m_countA == 0)
140  {
141  MessageHeader msgHdr;
142  receivedPacketProbe->RemoveHeader (msgHdr);
143  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
144  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
145  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "0 - Hello, No messages.");
146  }
147  else if (m_countA == 1)
148  {
149  MessageHeader msgHdr;
150  receivedPacketProbe->RemoveHeader (msgHdr);
151  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
152  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
153  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, "1 - Hello, one message.");
154  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
155  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
156  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), "1 - Neighbor.");
157  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
158  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
159  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "1 - Neighbor.");
160  }
161  else
162  {
163  if (m_countA == 5 || m_countA == 8)
164  {
165  MessageHeader msgHdr;
166  receivedPacketProbe->RemoveHeader (msgHdr);
167  const olsr::MessageHeader::Tc &tc = msgHdr.GetTc ();
168  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
169  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses.size (), 2, int(m_countA) << " - TC, one message.");
170  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countA) << " - Neighbor.");
171  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[1], Ipv4Address ("10.1.1.1"), int(m_countA) << " - Neighbor.");
172  }
173  if (m_countA != 8)
174  {
175  MessageHeader msgHdr;
176  receivedPacketProbe->RemoveHeader (msgHdr);
177  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
178  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
179  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, int(m_countA) << " - Hello, one message.");
180  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countA) << " - Symmetric Link.");
181  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, int(m_countA) << " - Neighbor.");
182  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countA) << " - Neighbor.");
183  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 6, int(m_countA) << " - Symmetric Link.");
184  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, int(m_countA) << " - Neighbor.");
185  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), int(m_countA) << " - Neighbor.");
186  }
187  }
188  m_countA ++;
189 }
190 
191 void
193 {
194  uint32_t availableData;
195  availableData = socket->GetRxAvailable ();
196  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
197  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
198 
199  Ipv4Header ipHdr;
200  receivedPacketProbe->RemoveHeader (ipHdr);
201  UdpHeader udpHdr;
202  receivedPacketProbe->RemoveHeader (udpHdr);
203  PacketHeader pktHdr;
204  receivedPacketProbe->RemoveHeader (pktHdr);
205 
206  MessageHeader msgHdr;
207  receivedPacketProbe->RemoveHeader (msgHdr);
208  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
209 
210  if (m_countB == 0 || m_countB == 2 || m_countB == 5 || m_countB == 6 || m_countB == 8 ||
211  m_countB == 10 || m_countB == 13 || m_countB == 15 || m_countB == 17 || m_countB == 19)
212  {
213  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.3"), "Originator address.");
214  }
215  else
216  {
217  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.1"), "Originator address.");
218  }
219 
220  if (m_countB == 0 || m_countB == 1)
221  {
222  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, int(m_countC) << " - Hello, links announced.");
223  }
224  else
225  {
226  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, int(m_countC) << " - Hello, links announced.");
227  if (m_countB == 2 || m_countB == 3)
228  {
229  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, int(m_countC) << " - Asymmetric Link.");
230  }
231  else if (m_countB == 4 || m_countB == 5)
232  {
233  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countC) << " - Symmetric Link.");
234  }
235  else
236  {
237  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 10, int(m_countC) << " - MPR Link.");
238  }
239 
240  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.2"), int(m_countC) << " - Neighbor.");
241  }
242 
243  m_countB ++;
244 }
245 
246 // Note: this is identical to ReceivePktProbeA, but the packet counter needs to be different.
247 void
249 {
250  uint32_t availableData;
251  availableData = socket->GetRxAvailable ();
252  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
253  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
254 
255  Ipv4Header ipHdr;
256  receivedPacketProbe->RemoveHeader (ipHdr);
257  UdpHeader udpHdr;
258  receivedPacketProbe->RemoveHeader (udpHdr);
259  PacketHeader pktHdr;
260  receivedPacketProbe->RemoveHeader (pktHdr);
261 
262  if (m_countC == 0)
263  {
264  MessageHeader msgHdr;
265  receivedPacketProbe->RemoveHeader (msgHdr);
266  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
267  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
268  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "0 - Hello, No messages.");
269  }
270  else if (m_countC == 1)
271  {
272  MessageHeader msgHdr;
273  receivedPacketProbe->RemoveHeader (msgHdr);
274  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
275  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
276  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, "1 - Hello, one message.");
277  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
278  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
279  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), "1 - Neighbor.");
280  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
281  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, "1 - Neighbor.");
282  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "1 - Neighbor.");
283  }
284  else
285  {
286  if (m_countC == 5 || m_countC == 8)
287  {
288  MessageHeader msgHdr;
289  receivedPacketProbe->RemoveHeader (msgHdr);
290  const olsr::MessageHeader::Tc &tc = msgHdr.GetTc ();
291  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
292  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses.size (), 2, int(m_countC) << " - TC, one message.");
293  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countC) << " - Neighbor.");
294  NS_TEST_EXPECT_MSG_EQ (tc.neighborAddresses[1], Ipv4Address ("10.1.1.1"), int(m_countC) << " - Neighbor.");
295  }
296  if (m_countC != 8)
297  {
298  MessageHeader msgHdr;
299  receivedPacketProbe->RemoveHeader (msgHdr);
300  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
301  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
302  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 2, int(m_countC) << " - Hello, one message.");
303  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].linkCode, 6, int(m_countC) << " - Symmetric Link.");
304  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses.size (), 1, int(m_countC) << " - Neighbor.");
305  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[0].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.3"), int(m_countC) << " - Neighbor.");
306  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].linkCode, 6, int(m_countC) << " - Symmetric Link.");
307  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses.size (), 1, int(m_countC) << " - Neighbor.");
308  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages[1].neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), int(m_countC) << " - Neighbor.");
309  }
310  }
311  m_countC ++;
312 }
313 
314 }
315 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:266
void CreateNodes()
Create & configure test network.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Ipv4Address GetOriginatorAddress() const
Definition: olsr-header.h:152
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Introspection did not find any typical Config paths.
Definition: olsr-header.h:116
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#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:200
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: olsr-helper.cc:87
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:278
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:38
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
std::vector< LinkMessage > linkMessages
Definition: olsr-header.h:276
encapsulates test code
Definition: test.h:1147
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
Definition: olsr-header.h:302
Packet header for IPv4.
Definition: ipv4-header.h:31
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
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:1480
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:164
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
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Definition: olsr.py:1
Introspection did not find any typical Config paths.
Definition: olsr-header.h:69
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
static void SetSeed(uint32_t seed)
Set the seed.
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...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
const Time m_time
Total simulation time.
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
static const uint8_t PROT_NUMBER
protocol number (0x11)
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
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.