A Discrete-Event Network Simulator
API
hello-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 "hello-regression-test.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/double.h"
24 #include "ns3/internet-stack-helper.h"
25 #include "ns3/ipv4-address-helper.h"
26 #include "ns3/ipv4-raw-socket-factory.h"
27 #include "ns3/olsr-header.h"
28 #include "ns3/olsr-helper.h"
29 #include "ns3/random-variable-stream.h"
30 #include "ns3/rng-seed-manager.h"
31 #include "ns3/simple-net-device-helper.h"
32 #include "ns3/simulator.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/string.h"
35 #include "ns3/udp-header.h"
36 #include "ns3/udp-l4-protocol.h"
37 #include "ns3/uinteger.h"
38 
39 #include <vector>
40 
41 namespace ns3
42 {
43 namespace olsr
44 {
45 
47  : TestCase("Test OLSR Hello messages generation"),
48  m_time(Seconds(5)),
49  m_countA(0),
50  m_countB(0)
51 {
52 }
53 
55 {
56 }
57 
58 void
60 {
63  CreateNodes();
64 
67 
68  m_rxSocketA = nullptr;
69  m_rxSocketB = nullptr;
71 }
72 
73 void
75 {
76  // create 2 nodes
77  NodeContainer c;
78  c.Create(2);
79  // install TCP/IP & OLSR
81  InternetStackHelper internet;
82  internet.SetRoutingHelper(olsr);
83  internet.Install(c);
84  // Assign OLSR RVs to specific streams
85  int64_t streamsUsed = olsr.AssignStreams(c, 0);
86  NS_TEST_ASSERT_MSG_EQ(streamsUsed, 2, "Should have assigned 2 streams");
87  // create channel & devices
88  SimpleNetDeviceHelper simpleNetHelper;
89  simpleNetHelper.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
90  simpleNetHelper.SetChannelAttribute("Delay", StringValue("2ms"));
91  NetDeviceContainer nd = simpleNetHelper.Install(c);
92  // setup IP addresses
93  Ipv4AddressHelper ipv4;
94  ipv4.SetBase("10.1.1.0", "255.255.255.0");
95  ipv4.Assign(nd);
96 
97  // Create the sockets
98  Ptr<SocketFactory> rxSocketFactoryA = c.Get(0)->GetObject<Ipv4RawSocketFactory>();
99  m_rxSocketA = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryA->CreateSocket());
102 
103  Ptr<SocketFactory> rxSocketFactoryB = c.Get(1)->GetObject<Ipv4RawSocketFactory>();
104  m_rxSocketB = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryB->CreateSocket());
107 }
108 
109 void
111 {
112  uint32_t availableData;
113  availableData = socket->GetRxAvailable();
114  Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
115  NS_ASSERT(availableData == receivedPacketProbe->GetSize());
116 
117  Ipv4Header ipHdr;
118  receivedPacketProbe->RemoveHeader(ipHdr);
119  UdpHeader udpHdr;
120  receivedPacketProbe->RemoveHeader(udpHdr);
121  PacketHeader pktHdr;
122  receivedPacketProbe->RemoveHeader(pktHdr);
123  MessageHeader msgHdr;
124  receivedPacketProbe->RemoveHeader(msgHdr);
125 
126  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
128  Ipv4Address("10.1.1.2"),
129  "Originator address.");
130 
131  if (m_countA == 0)
132  {
133  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "No Link messages on the first Hello.");
134  }
135  else
136  {
137  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
138  1,
139  "One Link message on the second and third Hello.");
140  }
141 
142  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
143  for (iter = hello.linkMessages.begin(); iter != hello.linkMessages.end(); iter++)
144  {
145  if (m_countA == 1)
146  {
147  NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 1, "Asymmetric link on second Hello.");
148  }
149  else
150  {
151  NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 6, "Symmetric link on second Hello.");
152  }
153 
154  NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses.size(), 1, "Only one neighbor.");
155  NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses[0],
156  Ipv4Address("10.1.1.1"),
157  "Only one neighbor.");
158  }
159 
160  m_countA++;
161 }
162 
163 void
165 {
166  uint32_t availableData;
167  availableData = socket->GetRxAvailable();
168  Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
169  NS_ASSERT(availableData == receivedPacketProbe->GetSize());
170 
171  Ipv4Header ipHdr;
172  receivedPacketProbe->RemoveHeader(ipHdr);
173  UdpHeader udpHdr;
174  receivedPacketProbe->RemoveHeader(udpHdr);
175  PacketHeader pktHdr;
176  receivedPacketProbe->RemoveHeader(pktHdr);
177  MessageHeader msgHdr;
178  receivedPacketProbe->RemoveHeader(msgHdr);
179 
180  const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
182  Ipv4Address("10.1.1.1"),
183  "Originator address.");
184 
185  if (m_countA == 0)
186  {
187  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "No Link messages on the first Hello.");
188  }
189  else
190  {
191  NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(),
192  1,
193  "One Link message on the second and third Hello.");
194  }
195 
196  std::vector<olsr::MessageHeader::Hello::LinkMessage>::const_iterator iter;
197  for (iter = hello.linkMessages.begin(); iter != hello.linkMessages.end(); iter++)
198  {
199  if (m_countA == 1)
200  {
201  NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 1, "Asymmetric link on second Hello.");
202  }
203  else
204  {
205  NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 6, "Symmetric link on second Hello.");
206  }
207 
208  NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses.size(), 1, "Only one neighbor.");
209  NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses[0],
210  Ipv4Address("10.1.1.2"),
211  "Only one neighbor.");
212  }
213 
214  m_countB++;
215 }
216 
217 } // namespace olsr
218 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:43
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
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
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:863
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
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:126
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)
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
Ptr< Ipv4RawSocketImpl > m_rxSocketB
Receiving socket on node B.
const Time m_time
Total simulation time.
void DoRun() override
Implementation to actually run this TestCase.
uint8_t m_countA
Packet counter on node A.
uint8_t m_countB
Packet counter on node B.
void CreateNodes()
Create & configure test network.
void ReceivePktProbeB(Ptr< Socket > socket)
Receive raw data on node B.
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:159
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Definition: olsr-header.h:224
Hello & GetHello()
Set the message type to HELLO and return the message content.
Definition: olsr-header.h:599
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition: olsr-header.h:77
#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_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:144
#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:1336
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:707
Definition: olsr.py:1
HELLO Message Format.
Definition: olsr-header.h:384
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:417