A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
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
41namespace ns3
42{
43namespace 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
58void
60{
64
67
68 m_rxSocketA = nullptr;
69 m_rxSocketB = nullptr;
71}
72
73void
75{
76 // create 2 nodes
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
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
109void
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 {
138 1,
139 "One Link message on the second and third Hello.");
140 }
141
142 for (auto iter = hello.linkMessages.begin(); iter != hello.linkMessages.end(); iter++)
143 {
144 if (m_countA == 1)
145 {
146 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 1, "Asymmetric link on second Hello.");
147 }
148 else
149 {
150 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 6, "Symmetric link on second Hello.");
151 }
152
153 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses.size(), 1, "Only one neighbor.");
154 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses[0],
155 Ipv4Address("10.1.1.1"),
156 "Only one neighbor.");
157 }
158
159 m_countA++;
160}
161
162void
164{
165 uint32_t availableData;
166 availableData = socket->GetRxAvailable();
167 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
168 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
169
170 Ipv4Header ipHdr;
171 receivedPacketProbe->RemoveHeader(ipHdr);
172 UdpHeader udpHdr;
173 receivedPacketProbe->RemoveHeader(udpHdr);
174 PacketHeader pktHdr;
175 receivedPacketProbe->RemoveHeader(pktHdr);
176 MessageHeader msgHdr;
177 receivedPacketProbe->RemoveHeader(msgHdr);
178
179 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
181 Ipv4Address("10.1.1.1"),
182 "Originator address.");
183
184 if (m_countA == 0)
185 {
186 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "No Link messages on the first Hello.");
187 }
188 else
189 {
191 1,
192 "One Link message on the second and third Hello.");
193 }
194
195 for (auto iter = hello.linkMessages.begin(); iter != hello.linkMessages.end(); iter++)
196 {
197 if (m_countA == 1)
198 {
199 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 1, "Asymmetric link on second Hello.");
200 }
201 else
202 {
203 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 6, "Symmetric link on second Hello.");
204 }
205
206 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses.size(), 1, "Only one neighbor.");
207 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses[0],
208 Ipv4Address("10.1.1.2"),
209 "Only one neighbor.");
210 }
211
212 m_countB++;
213}
214
215} // namespace olsr
216} // namespace ns3
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
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:522
Helper class that adds OLSR routing to nodes.
Definition: olsr-helper.h:42
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:128
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1061
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:161
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Definition: olsr-header.h:226
Hello & GetHello()
Set the message type to HELLO and return the message content.
Definition: olsr-header.h:601
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition: olsr-header.h:79
#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:145
#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:252
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:704
Definition: olsr.py:1
HELLO Message Format.
Definition: olsr-header.h:386
std::vector< LinkMessage > linkMessages
Link messages container.
Definition: olsr-header.h:419