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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Pavel Boyko <boyko@iitp.ru>
7 */
8
10
11#include "ns3/abort.h"
12#include "ns3/double.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-address-helper.h"
15#include "ns3/ipv4-raw-socket-factory.h"
16#include "ns3/olsr-header.h"
17#include "ns3/olsr-helper.h"
18#include "ns3/random-variable-stream.h"
19#include "ns3/rng-seed-manager.h"
20#include "ns3/simple-net-device-helper.h"
21#include "ns3/simulator.h"
22#include "ns3/socket-factory.h"
23#include "ns3/string.h"
24#include "ns3/udp-header.h"
25#include "ns3/udp-l4-protocol.h"
26#include "ns3/uinteger.h"
27
28#include <vector>
29
30namespace ns3
31{
32namespace olsr
33{
34
36 : TestCase("Test OLSR Hello messages generation"),
37 m_time(Seconds(5)),
38 m_countA(0),
39 m_countB(0)
40{
41}
42
46
47void
61
62void
64{
65 // create 2 nodes
67 c.Create(2);
68 // install TCP/IP & OLSR
70 InternetStackHelper internet;
71 internet.SetRoutingHelper(olsr);
72 internet.Install(c);
73 // Assign OLSR RVs to specific streams
74 int64_t streamsUsed = olsr.AssignStreams(c, 0);
75 NS_TEST_ASSERT_MSG_EQ(streamsUsed, 2, "Should have assigned 2 streams");
76 // create channel & devices
77 SimpleNetDeviceHelper simpleNetHelper;
78 simpleNetHelper.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
79 simpleNetHelper.SetChannelAttribute("Delay", StringValue("2ms"));
80 NetDeviceContainer nd = simpleNetHelper.Install(c);
81 // setup IP addresses
83 ipv4.SetBase("10.1.1.0", "255.255.255.0");
84 ipv4.Assign(nd);
85
86 // Create the sockets
87 Ptr<SocketFactory> rxSocketFactoryA = c.Get(0)->GetObject<Ipv4RawSocketFactory>();
88 m_rxSocketA = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryA->CreateSocket());
91
92 Ptr<SocketFactory> rxSocketFactoryB = c.Get(1)->GetObject<Ipv4RawSocketFactory>();
93 m_rxSocketB = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryB->CreateSocket());
96}
97
98void
100{
101 uint32_t availableData;
102 availableData = socket->GetRxAvailable();
103 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
104 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
105
106 Ipv4Header ipHdr;
107 receivedPacketProbe->RemoveHeader(ipHdr);
108 UdpHeader udpHdr;
109 receivedPacketProbe->RemoveHeader(udpHdr);
110 PacketHeader pktHdr;
111 receivedPacketProbe->RemoveHeader(pktHdr);
112 MessageHeader msgHdr;
113 receivedPacketProbe->RemoveHeader(msgHdr);
114
115 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
117 Ipv4Address("10.1.1.2"),
118 "Originator address.");
119
120 if (m_countA == 0)
121 {
122 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "No Link messages on the first Hello.");
123 }
124 else
125 {
127 1,
128 "One Link message on the second and third Hello.");
129 }
130
131 for (auto iter = hello.linkMessages.begin(); iter != hello.linkMessages.end(); iter++)
132 {
133 if (m_countA == 1)
134 {
135 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 1, "Asymmetric link on second Hello.");
136 }
137 else
138 {
139 NS_TEST_EXPECT_MSG_EQ(iter->linkCode, 6, "Symmetric link on second Hello.");
140 }
141
142 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses.size(), 1, "Only one neighbor.");
143 NS_TEST_EXPECT_MSG_EQ(iter->neighborInterfaceAddresses[0],
144 Ipv4Address("10.1.1.1"),
145 "Only one neighbor.");
146 }
147
148 m_countA++;
149}
150
151void
153{
154 uint32_t availableData;
155 availableData = socket->GetRxAvailable();
156 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
157 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
158
159 Ipv4Header ipHdr;
160 receivedPacketProbe->RemoveHeader(ipHdr);
161 UdpHeader udpHdr;
162 receivedPacketProbe->RemoveHeader(udpHdr);
163 PacketHeader pktHdr;
164 receivedPacketProbe->RemoveHeader(pktHdr);
165 MessageHeader msgHdr;
166 receivedPacketProbe->RemoveHeader(msgHdr);
167
168 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
170 Ipv4Address("10.1.1.1"),
171 "Originator address.");
172
173 if (m_countA == 0)
174 {
175 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "No Link messages on the first Hello.");
176 }
177 else
178 {
180 1,
181 "One Link message on the second and third Hello.");
182 }
183
184 for (auto 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],
197 Ipv4Address("10.1.1.2"),
198 "Only one neighbor.");
199 }
200
201 m_countB++;
202}
203
204} // namespace olsr
205} // 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.
Packet header for IPv4.
Definition ipv4-header.h:23
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:511
Helper class that adds OLSR routing to nodes.
Definition olsr-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
Hold variables of type string.
Definition string.h:45
encapsulates test code
Definition test.h:1050
Packet header for UDP packets.
Definition udp-header.h:30
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.
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Hello & GetHello()
Set the message type to HELLO and return the message content.
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers):
Definition olsr-header.h:68
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:134
#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:241
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
Definition olsr.py:1
HELLO Message Format.
std::vector< LinkMessage > linkMessages
Link messages container.