A Discrete-Event Network Simulator
API
hello-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 <vector>
22 #include "hello-regression-test.h"
23 #include "ns3/simulator.h"
24 #include "ns3/random-variable-stream.h"
25 #include "ns3/rng-seed-manager.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/simple-net-device-helper.h"
32 #include "ns3/ipv4-address-helper.h"
33 #include "ns3/abort.h"
34 #include "ns3/socket-factory.h"
35 #include "ns3/ipv4-raw-socket-factory.h"
36 #include "ns3/udp-l4-protocol.h"
37 #include "ns3/udp-header.h"
38 #include "ns3/olsr-header.h"
39 
40 namespace ns3 {
41 namespace olsr {
42 
44  TestCase ("Test OLSR Hello messages generation"),
45  m_time (Seconds (5)), m_countA (0), m_countB (0)
46 {
47 }
48 
50 {
51 }
52 
53 void
55 {
58  CreateNodes ();
59 
61  Simulator::Run ();
62 
63  m_rxSocketA = 0;
64  m_rxSocketB = 0;
66 }
67 
68 void
70 {
71  // create 2 nodes
72  NodeContainer c;
73  c.Create (2);
74  // install TCP/IP & OLSR
76  InternetStackHelper internet;
77  internet.SetRoutingHelper (olsr);
78  internet.Install (c);
79  // Assign OLSR RVs to specific streams
80  int64_t streamsUsed = olsr.AssignStreams (c, 0);
81  NS_TEST_ASSERT_MSG_EQ (streamsUsed, 2, "Should have assigned 2 streams");
82  // create channel & devices
83  SimpleNetDeviceHelper simpleNetHelper;
84  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
85  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
86  NetDeviceContainer nd = simpleNetHelper.Install (c);
87  // setup IP addresses
88  Ipv4AddressHelper ipv4;
89  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
90  ipv4.Assign (nd);
91 
92  // Create the sockets
93  Ptr<SocketFactory> rxSocketFactoryA = c.Get (0)->GetObject<Ipv4RawSocketFactory> ();
94  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryA->CreateSocket ());
97 
98  Ptr<SocketFactory> rxSocketFactoryB = c.Get (1)->GetObject<Ipv4RawSocketFactory> ();
99  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryB->CreateSocket ());
102 }
103 
104 void
106 {
107  uint32_t availableData;
108  availableData = socket->GetRxAvailable ();
109  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
110  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
111 
112  Ipv4Header ipHdr;
113  receivedPacketProbe->RemoveHeader (ipHdr);
114  UdpHeader udpHdr;
115  receivedPacketProbe->RemoveHeader (udpHdr);
116  PacketHeader pktHdr;
117  receivedPacketProbe->RemoveHeader (pktHdr);
118  MessageHeader msgHdr;
119  receivedPacketProbe->RemoveHeader (msgHdr);
120 
121  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
122  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
123 
124  if (m_countA == 0)
125  {
126  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "No Link messages on the first Hello.");
127  }
128  else
129  {
130  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, "One Link message on the second and third Hello.");
131  }
132 
133  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
134  for (iter = hello.linkMessages.begin (); iter != hello.linkMessages.end (); iter++)
135  {
136  if (m_countA == 1)
137  {
138  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 1, "Asymmetric link on second Hello.");
139  }
140  else
141  {
142  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 6, "Symmetric link on second Hello.");
143  }
144 
145  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses.size (), 1, "Only one neighbor.");
146  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "Only one neighbor.");
147  }
148 
149  m_countA++;
150 }
151 
152 void
154 {
155  uint32_t availableData;
156  availableData = socket->GetRxAvailable ();
157  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
158  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
159 
160  Ipv4Header ipHdr;
161  receivedPacketProbe->RemoveHeader (ipHdr);
162  UdpHeader udpHdr;
163  receivedPacketProbe->RemoveHeader (udpHdr);
164  PacketHeader pktHdr;
165  receivedPacketProbe->RemoveHeader (pktHdr);
166  MessageHeader msgHdr;
167  receivedPacketProbe->RemoveHeader (msgHdr);
168 
169  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
170  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.1"), "Originator address.");
171 
172  if (m_countA == 0)
173  {
174  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "No Link messages on the first Hello.");
175  }
176  else
177  {
178  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, "One Link message on the second and third Hello.");
179  }
180 
181  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
182  for (iter = hello.linkMessages.begin (); iter != hello.linkMessages.end (); iter++)
183  {
184  if (m_countA == 1)
185  {
186  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 1, "Asymmetric link on second Hello.");
187  }
188  else
189  {
190  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 6, "Symmetric link on second Hello.");
191  }
192 
193  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses.size (), 1, "Only one neighbor.");
194  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.2"), "Only one neighbor.");
195  }
196 
197  m_countB++;
198 }
199 
200 }
201 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
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:852
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:157
Hold variables of type string.
Definition: string.h:41
#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:170
const Time m_time
Total simulation time.
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
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.
static void SetRun(uint64_t run)
Set the run number of simulation.
Packet header for IPv4.
Definition: ipv4-header.h:33
#define max(a, b)
Definition: 80211b.c:43
#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:168
holds a vector of ns3::NetDevice pointers
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
void DoRun()
Implementation to actually run this TestCase.
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
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
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
uint8_t m_countB
Packet counter on node B.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
uint8_t m_countA
Packet counter on node A.
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
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:178
void CreateNodes()
Create & configure test network.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
void ReceivePktProbeB(Ptr< Socket > socket)
Receive raw data on node B.
Ptr< Ipv4RawSocketImpl > m_rxSocketB
Receiving socket on node B.
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
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)
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
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.