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 {
42 namespace olsr
43 {
44 
46  TestCase ("Test OLSR Hello messages generation"),
47  m_time (Seconds (5)), m_countA (0), m_countB (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;
68 }
69 
70 void
72 {
73  // create 2 nodes
74  NodeContainer c;
75  c.Create (2);
76  // install TCP/IP & OLSR
78  InternetStackHelper internet;
79  internet.SetRoutingHelper (olsr);
80  internet.Install (c);
81  // Assign OLSR RVs to specific streams
82  int64_t streamsUsed = olsr.AssignStreams (c, 0);
83  NS_TEST_ASSERT_MSG_EQ (streamsUsed, 2, "Should have assigned 2 streams");
84  // create channel & devices
85  SimpleNetDeviceHelper simpleNetHelper;
86  simpleNetHelper.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
87  simpleNetHelper.SetChannelAttribute ("Delay", StringValue ("2ms"));
88  NetDeviceContainer nd = simpleNetHelper.Install (c);
89  // setup IP addresses
90  Ipv4AddressHelper ipv4;
91  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
92  ipv4.Assign (nd);
93 
94  // Create the sockets
95  Ptr<SocketFactory> rxSocketFactoryA = c.Get (0)->GetObject<Ipv4RawSocketFactory> ();
96  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryA->CreateSocket ());
97  m_rxSocketA->SetProtocol (UdpL4Protocol::PROT_NUMBER);
98  m_rxSocketA->SetRecvCallback (MakeCallback (&HelloRegressionTest::ReceivePktProbeA, this));
99 
100  Ptr<SocketFactory> rxSocketFactoryB = c.Get (1)->GetObject<Ipv4RawSocketFactory> ();
101  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl> (rxSocketFactoryB->CreateSocket ());
102  m_rxSocketB->SetProtocol (UdpL4Protocol::PROT_NUMBER);
103  m_rxSocketB->SetRecvCallback (MakeCallback (&HelloRegressionTest::ReceivePktProbeB, this));
104 }
105 
106 void
108 {
109  uint32_t availableData;
110  availableData = socket->GetRxAvailable ();
111  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
112  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
113 
114  Ipv4Header ipHdr;
115  receivedPacketProbe->RemoveHeader (ipHdr);
116  UdpHeader udpHdr;
117  receivedPacketProbe->RemoveHeader (udpHdr);
118  PacketHeader pktHdr;
119  receivedPacketProbe->RemoveHeader (pktHdr);
120  MessageHeader msgHdr;
121  receivedPacketProbe->RemoveHeader (msgHdr);
122 
123  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
124  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.2"), "Originator address.");
125 
126  if (m_countA == 0)
127  {
128  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "No Link messages on the first Hello.");
129  }
130  else
131  {
132  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, "One Link message on the second and third Hello.");
133  }
134 
135  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
136  for (iter = hello.linkMessages.begin (); iter != hello.linkMessages.end (); iter ++)
137  {
138  if (m_countA == 1)
139  {
140  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 1, "Asymmetric link on second Hello.");
141  }
142  else
143  {
144  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 6, "Symmetric link on second Hello.");
145  }
146 
147  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses.size (), 1, "Only one neighbor.");
148  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.1"), "Only one neighbor.");
149  }
150 
151  m_countA ++;
152 }
153 
154 void
156 {
157  uint32_t availableData;
158  availableData = socket->GetRxAvailable ();
159  Ptr<Packet> receivedPacketProbe = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);
160  NS_ASSERT (availableData == receivedPacketProbe->GetSize ());
161 
162  Ipv4Header ipHdr;
163  receivedPacketProbe->RemoveHeader (ipHdr);
164  UdpHeader udpHdr;
165  receivedPacketProbe->RemoveHeader (udpHdr);
166  PacketHeader pktHdr;
167  receivedPacketProbe->RemoveHeader (pktHdr);
168  MessageHeader msgHdr;
169  receivedPacketProbe->RemoveHeader (msgHdr);
170 
171  const olsr::MessageHeader::Hello &hello = msgHdr.GetHello ();
172  NS_TEST_EXPECT_MSG_EQ (msgHdr.GetOriginatorAddress (), Ipv4Address ("10.1.1.1"), "Originator address.");
173 
174  if (m_countA == 0)
175  {
176  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 0, "No Link messages on the first Hello.");
177  }
178  else
179  {
180  NS_TEST_EXPECT_MSG_EQ (hello.linkMessages.size (), 1, "One Link message on the second and third Hello.");
181  }
182 
183  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
184  for (iter = hello.linkMessages.begin (); iter != hello.linkMessages.end (); iter ++)
185  {
186  if (m_countA == 1)
187  {
188  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 1, "Asymmetric link on second Hello.");
189  }
190  else
191  {
192  NS_TEST_EXPECT_MSG_EQ (iter->linkCode, 6, "Symmetric link on second Hello.");
193  }
194 
195  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses.size (), 1, "Only one neighbor.");
196  NS_TEST_EXPECT_MSG_EQ (iter->neighborInterfaceAddresses[0], Ipv4Address ("10.1.1.2"), "Only one neighbor.");
197  }
198 
199  m_countB ++;
200 }
201 
202 }
203 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:266
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
#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
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: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.
static void SetRun(uint64_t run)
Set the run number of simulation.
Packet header for IPv4.
Definition: ipv4-header.h:31
#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:161
holds a vector of ns3::NetDevice pointers
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
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
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
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.
void CreateNodes()
Create & configure test network.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
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.
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)
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
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.