A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tc-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/boolean.h"
13#include "ns3/double.h"
14#include "ns3/internet-stack-helper.h"
15#include "ns3/ipv4-address-helper.h"
16#include "ns3/ipv4-raw-socket-factory.h"
17#include "ns3/olsr-header.h"
18#include "ns3/olsr-helper.h"
19#include "ns3/random-variable-stream.h"
20#include "ns3/rng-seed-manager.h"
21#include "ns3/simple-net-device-helper.h"
22#include "ns3/simple-net-device.h"
23#include "ns3/simulator.h"
24#include "ns3/socket-factory.h"
25#include "ns3/string.h"
26#include "ns3/udp-header.h"
27#include "ns3/udp-l4-protocol.h"
28#include "ns3/uinteger.h"
29
30namespace ns3
31{
32namespace olsr
33{
34
36 : TestCase("Test OLSR Topology Control message generation"),
37 m_time(Seconds(20)),
38 m_countA(0),
39 m_countB(0),
40 m_countC(0)
41{
42}
43
47
48void
63
64void
66{
67 // create 3 nodes
69 c.Create(3);
70
71 // install TCP/IP & OLSR
73 InternetStackHelper internet;
74 internet.SetRoutingHelper(olsr);
75 internet.Install(c);
76 int64_t streamsUsed = olsr.AssignStreams(c, 0);
77 NS_TEST_EXPECT_MSG_EQ(streamsUsed, 3, "Should have assigned 3 streams");
78
79 // create channel & devices
80 SimpleNetDeviceHelper simpleNetHelper;
81 simpleNetHelper.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
82 simpleNetHelper.SetChannelAttribute("Delay", StringValue("2ms"));
83 NetDeviceContainer nd = simpleNetHelper.Install(c);
84
85 // Blacklist some devices (equivalent to Wireless out of range)
88 Ptr<SimpleChannel> ch = DynamicCast<SimpleChannel>(nd.Get(0)->GetChannel());
89 ch->BlackList(nd0, nd2);
90 ch->BlackList(nd2, nd0);
91
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 Ptr<SocketFactory> rxSocketFactoryC = c.Get(2)->GetObject<Ipv4RawSocketFactory>();
109 m_rxSocketC = DynamicCast<Ipv4RawSocketImpl>(rxSocketFactoryC->CreateSocket());
112}
113
114// Note: this is identical to ReceivePktProbeC, but the packet counter needs to be different.
115void
117{
118 uint32_t availableData;
119 availableData = socket->GetRxAvailable();
120 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
121 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
122
123 Ipv4Header ipHdr;
124 receivedPacketProbe->RemoveHeader(ipHdr);
125 UdpHeader udpHdr;
126 receivedPacketProbe->RemoveHeader(udpHdr);
127 PacketHeader pktHdr;
128 receivedPacketProbe->RemoveHeader(pktHdr);
129
130 if (m_countA == 0)
131 {
132 MessageHeader msgHdr;
133 receivedPacketProbe->RemoveHeader(msgHdr);
134 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
136 Ipv4Address("10.1.1.2"),
137 "Originator address.");
138 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "0 - Hello, No messages.");
139 }
140 else if (m_countA == 1)
141 {
142 MessageHeader msgHdr;
143 receivedPacketProbe->RemoveHeader(msgHdr);
145 Ipv4Address("10.1.1.2"),
146 "Originator address.");
147 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
148 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 2, "1 - Hello, one message.");
149 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
150 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
151 1,
152 "1 - Neighbor.");
153 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
154 Ipv4Address("10.1.1.3"),
155 "1 - Neighbor.");
156 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
157 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
158 1,
159 "1 - Neighbor.");
160 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
161 Ipv4Address("10.1.1.1"),
162 "1 - Neighbor.");
163 }
164 else
165 {
166 if (m_countA == 5 || m_countA == 8)
167 {
168 MessageHeader msgHdr;
169 receivedPacketProbe->RemoveHeader(msgHdr);
170 const olsr::MessageHeader::Tc& tc = msgHdr.GetTc();
172 Ipv4Address("10.1.1.2"),
173 "Originator address.");
175 2,
176 int(m_countA) << " - TC, one message.");
178 Ipv4Address("10.1.1.3"),
179 int(m_countA) << " - Neighbor.");
181 Ipv4Address("10.1.1.1"),
182 int(m_countA) << " - Neighbor.");
183 }
184 if (m_countA != 8)
185 {
186 MessageHeader msgHdr;
187 receivedPacketProbe->RemoveHeader(msgHdr);
189 Ipv4Address("10.1.1.2"),
190 "Originator address.");
191 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
193 2,
194 int(m_countA) << " - Hello, one message.");
195 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
196 6,
197 int(m_countA) << " - Symmetric Link.");
198 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
199 1,
200 int(m_countA) << " - Neighbor.");
201 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
202 Ipv4Address("10.1.1.3"),
203 int(m_countA) << " - Neighbor.");
204 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode,
205 6,
206 int(m_countA) << " - Symmetric Link.");
207 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
208 1,
209 int(m_countA) << " - Neighbor.");
210 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
211 Ipv4Address("10.1.1.1"),
212 int(m_countA) << " - Neighbor.");
213 }
214 }
215 m_countA++;
216}
217
218void
220{
221 uint32_t availableData;
222 availableData = socket->GetRxAvailable();
223 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
224 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
225
226 Ipv4Header ipHdr;
227 receivedPacketProbe->RemoveHeader(ipHdr);
228 UdpHeader udpHdr;
229 receivedPacketProbe->RemoveHeader(udpHdr);
230 PacketHeader pktHdr;
231 receivedPacketProbe->RemoveHeader(pktHdr);
232
233 MessageHeader msgHdr;
234 receivedPacketProbe->RemoveHeader(msgHdr);
235 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
236
237 if (m_countB == 0 || m_countB == 2 || m_countB == 5 || m_countB == 6 || m_countB == 8 ||
238 m_countB == 10 || m_countB == 13 || m_countB == 15 || m_countB == 17 || m_countB == 19)
239 {
241 Ipv4Address("10.1.1.3"),
242 "Originator address.");
243 }
244 else
245 {
247 Ipv4Address("10.1.1.1"),
248 "Originator address.");
249 }
250
251 if (m_countB == 0 || m_countB == 1)
252 {
254 0,
255 int(m_countC) << " - Hello, links announced.");
256 }
257 else
258 {
260 1,
261 int(m_countC) << " - Hello, links announced.");
262 if (m_countB == 2 || m_countB == 3)
263 {
264 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
265 1,
266 int(m_countC) << " - Asymmetric Link.");
267 }
268 else if (m_countB == 4 || m_countB == 5)
269 {
270 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
271 6,
272 int(m_countC) << " - Symmetric Link.");
273 }
274 else
275 {
276 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
277 10,
278 int(m_countC) << " - MPR Link.");
279 }
280
281 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
282 Ipv4Address("10.1.1.2"),
283 int(m_countC) << " - Neighbor.");
284 }
285
286 m_countB++;
287}
288
289// Note: this is identical to ReceivePktProbeA, but the packet counter needs to be different.
290void
292{
293 uint32_t availableData;
294 availableData = socket->GetRxAvailable();
295 Ptr<Packet> receivedPacketProbe = socket->Recv(std::numeric_limits<uint32_t>::max(), 0);
296 NS_ASSERT(availableData == receivedPacketProbe->GetSize());
297
298 Ipv4Header ipHdr;
299 receivedPacketProbe->RemoveHeader(ipHdr);
300 UdpHeader udpHdr;
301 receivedPacketProbe->RemoveHeader(udpHdr);
302 PacketHeader pktHdr;
303 receivedPacketProbe->RemoveHeader(pktHdr);
304
305 if (m_countC == 0)
306 {
307 MessageHeader msgHdr;
308 receivedPacketProbe->RemoveHeader(msgHdr);
309 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
311 Ipv4Address("10.1.1.2"),
312 "Originator address.");
313 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 0, "0 - Hello, No messages.");
314 }
315 else if (m_countC == 1)
316 {
317 MessageHeader msgHdr;
318 receivedPacketProbe->RemoveHeader(msgHdr);
320 Ipv4Address("10.1.1.2"),
321 "Originator address.");
322 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
323 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages.size(), 2, "1 - Hello, one message.");
324 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode, 1, "1 - Asymmetric Link.");
325 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
326 1,
327 "1 - Neighbor.");
328 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
329 Ipv4Address("10.1.1.3"),
330 "1 - Neighbor.");
331 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode, 1, "1 - Asymmetric Link.");
332 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
333 1,
334 "1 - Neighbor.");
335 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
336 Ipv4Address("10.1.1.1"),
337 "1 - Neighbor.");
338 }
339 else
340 {
341 if (m_countC == 5 || m_countC == 8)
342 {
343 MessageHeader msgHdr;
344 receivedPacketProbe->RemoveHeader(msgHdr);
345 const olsr::MessageHeader::Tc& tc = msgHdr.GetTc();
347 Ipv4Address("10.1.1.2"),
348 "Originator address.");
350 2,
351 int(m_countC) << " - TC, one message.");
353 Ipv4Address("10.1.1.3"),
354 int(m_countC) << " - Neighbor.");
356 Ipv4Address("10.1.1.1"),
357 int(m_countC) << " - Neighbor.");
358 }
359 if (m_countC != 8)
360 {
361 MessageHeader msgHdr;
362 receivedPacketProbe->RemoveHeader(msgHdr);
364 Ipv4Address("10.1.1.2"),
365 "Originator address.");
366 const olsr::MessageHeader::Hello& hello = msgHdr.GetHello();
368 2,
369 int(m_countC) << " - Hello, one message.");
370 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].linkCode,
371 6,
372 int(m_countC) << " - Symmetric Link.");
373 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses.size(),
374 1,
375 int(m_countC) << " - Neighbor.");
376 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[0].neighborInterfaceAddresses[0],
377 Ipv4Address("10.1.1.3"),
378 int(m_countC) << " - Neighbor.");
379 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].linkCode,
380 6,
381 int(m_countC) << " - Symmetric Link.");
382 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses.size(),
383 1,
384 int(m_countC) << " - Neighbor.");
385 NS_TEST_EXPECT_MSG_EQ(hello.linkMessages[1].neighborInterfaceAddresses[0],
386 Ipv4Address("10.1.1.1"),
387 int(m_countC) << " - Neighbor.");
388 }
389 }
390 m_countC++;
391}
392
393} // namespace olsr
394} // 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
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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)
This header can store HELP, TC, MID and HNA messages.
Ipv4Address GetOriginatorAddress() const
Get the originator address.
Tc & GetTc()
Set the message type to TC and return the message content.
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
void CreateNodes()
Create & configure test network.
void ReceivePktProbeB(Ptr< Socket > socket)
Receive raw data on node B.
uint8_t m_countB
Packet counter on node B.
Ptr< Ipv4RawSocketImpl > m_rxSocketB
Receiving socket on node B.
void ReceivePktProbeC(Ptr< Socket > socket)
Receive raw data on node C.
uint8_t m_countC
Packet counter on node C.
void DoRun() override
Implementation to actually run this TestCase.
Ptr< Ipv4RawSocketImpl > m_rxSocketC
Receiving socket on node C.
void ReceivePktProbeA(Ptr< Socket > socket)
Receive raw data on node A.
const Time m_time
Total simulation time.
Ptr< Ipv4RawSocketImpl > m_rxSocketA
Receiving socket on node A.
uint8_t m_countA
Packet counter on node A.
#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_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:1344
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.
std::vector< Ipv4Address > neighborAddresses
Neighbor address container.