A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-server-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9/*
10 * This file includes testing for the following components:
11 * - NetworkServer
12 */
13
14#include "utilities.h"
15
16// An essential include is test.h
17#include "ns3/application.h"
18#include "ns3/log.h"
19#include "ns3/simulator.h"
20#include "ns3/test.h"
21
22// Include headers of classes to test
23#include "ns3/class-a-end-device-lorawan-mac.h"
24
25using namespace ns3;
26using namespace lorawan;
27
28NS_LOG_COMPONENT_DEFINE("NetworkServerTestSuite");
29
30/**
31 * @ingroup lorawan
32 *
33 * It verifies that the NetworkServer application can receive packets sent in uplink by devices
34 */
36{
37 public:
38 UplinkPacketTest(); //!< Default constructor
39 ~UplinkPacketTest() override; //!< Destructor
40
41 /**
42 * Callback for tracing ReceivedPacket.
43 *
44 * @param packet The packet received.
45 */
47
48 /**
49 * Send a packet from the input end device.
50 *
51 * @param endDevice A pointer to the end device Node.
52 */
53 void SendPacket(Ptr<Node> endDevice);
54
55 private:
56 void DoRun() override;
57
58 bool m_receivedPacket = false; //!< Set to true if a packet is received by the server
59};
60
61// Add some help text to this case to describe what it is intended to test
63 : TestCase("Verify that the NetworkServer application can receive"
64 " packets sent in the uplink by devices")
65{
66}
67
68// Reminder that the test case should clean up after itself
72
73void
75{
76 NS_LOG_DEBUG("Received a packet at the network server");
77 m_receivedPacket = true;
78}
79
80void
82{
83 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
84}
85
86// This method is the pure virtual method from class TestCase that every
87// TestCase must implement
88void
90{
91 NS_LOG_DEBUG("UplinkPacketTest");
92
93 // Create a bunch of actual devices
94 NetworkComponents components = InitializeNetwork(1, 1);
95
96 Ptr<LoraChannel> channel = components.channel;
97 NodeContainer endDevices = components.endDevices;
98 NodeContainer gateways = components.gateways;
99 Ptr<Node> nsNode = components.nsNode;
100
101 // Connect the trace source for received packets
102 nsNode->GetApplication(0)->TraceConnectWithoutContext(
103 "ReceivedPacket",
105
106 // Send a packet
108
112
113 // Check that we received the packet
115}
116
117/**
118 * @ingroup lorawan
119 *
120 * It verifies that devices requesting an acknowledgment receive a reply from the network server
121 */
123{
124 public:
125 DownlinkPacketTest(); //!< Default constructor
126 ~DownlinkPacketTest() override; //!< Destructor
127
128 /**
129 * Trace the outcome of MAC layer's confirmed packet retransmission and acknowledgement.
130 *
131 * This trace sink is only used here to determine whether an ack was received by the end device
132 * after sending a package requiring an acknowledgement.
133 *
134 * @param txCount Number of transmissions attempted during the process.
135 * @param ack Whether the retransmission process led to acknowledgement.
136 * @param firstAttempt Timestamp of the initial transmission attempt.
137 * @param packet The packet being retransmitted.
138 */
139 void ReceivedPacketAtEndDevice(uint8_t txCount,
140 bool ack,
141 Time firstAttempt,
142 Ptr<Packet> packet);
143
144 /**
145 * Send a packet from the input end device.
146 *
147 * @param endDevice A pointer to the end device Node.
148 * @param requestAck Whether to require an acknowledgement from the server.
149 */
150 void SendPacket(Ptr<Node> endDevice, bool requestAck);
151
152 private:
153 void DoRun() override;
154
155 bool m_receivedPacketAtEd = false; //!< Set to true if a packet is received by the end device
156};
157
158// Add some help text to this case to describe what it is intended to test
160 : TestCase("Verify that devices requesting an acknowledgment receive"
161 " a reply from the network server.")
162{
163}
164
165// Reminder that the test case should clean up after itself
169
170void
172 bool ack,
173 Time firstAttempt,
174 Ptr<Packet> packet)
175{
176 NS_LOG_DEBUG("Received a packet at the end device");
178}
179
180void
181DownlinkPacketTest::SendPacket(Ptr<Node> endDevice, bool requestAck)
182{
183 if (requestAck)
184 {
186 DynamicCast<LoraNetDevice>(endDevice->GetDevice(0))->GetMac())
188 }
189 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
190}
191
192// This method is the pure virtual method from class TestCase that every
193// TestCase must implement
194void
196{
197 NS_LOG_DEBUG("DownlinkPacketTest");
198
199 // Create a bunch of actual devices
200 NetworkComponents components = InitializeNetwork(1, 1);
201
202 Ptr<LoraChannel> channel = components.channel;
203 NodeContainer endDevices = components.endDevices;
204 NodeContainer gateways = components.gateways;
205 Ptr<Node> nsNode = components.nsNode;
206
207 // Connect the end device's trace source for received packets
209 DynamicCast<LoraNetDevice>(endDevices.Get(0)->GetDevice(0))->GetMac())
211 "ConfirmedTransmissionOutcome",
213
214 // Send a packet in uplink
215 Simulator::Schedule(Seconds(1), &DownlinkPacketTest::SendPacket, this, endDevices.Get(0), true);
216
217 Simulator::Stop(Seconds(10)); // Allow for time to receive a downlink packet
220
222}
223
224/**
225 * @ingroup lorawan
226 *
227 * It verifies that the NetworkServer application correctly responds to LinkCheck requests
228 */
230{
231 public:
232 LinkCheckTest(); //!< Default constructor
233 ~LinkCheckTest() override; //!< Destructor
234
235 /**
236 * Trace changes in the last known gateway count variable (updated on reception of
237 * LinkCheckAns MAC commands) of an end device.
238 *
239 * @param newValue The updated value.
240 * @param oldValue The previous value.
241 */
242 void LastKnownGatewayCount(uint8_t newValue, uint8_t oldValue);
243
244 /**
245 * Send a packet containing a LinkCheckReq MAC command from the input end device.
246 *
247 * @param endDevice A pointer to the end device Node.
248 * @param requestAck Whether to require an acknowledgement from the server.
249 */
250 void SendPacket(Ptr<Node> endDevice, bool requestAck);
251
252 private:
253 void DoRun() override;
254 bool m_receivedPacketAtEd = false; //!< Set to true if a packet containing a LinkCheckAns MAC
255 //!< command is received by the end device
257 0; //!< Stores the number of gateways that received the last packet carrying a
258 //!< LinkCheckReq MAC command
259};
260
261// Add some help text to this case to describe what it is intended to test
263 : TestCase("Verify that the NetworkServer application correctly responds to "
264 "LinkCheck requests")
265{
266}
267
268// Reminder that the test case should clean up after itself
272
273void
274LinkCheckTest::LastKnownGatewayCount(uint8_t newValue, uint8_t oldValue)
275{
276 NS_LOG_DEBUG("Updated gateway count");
278
280}
281
282void
283LinkCheckTest::SendPacket(Ptr<Node> endDevice, bool requestAck)
284{
286 DynamicCast<LoraNetDevice>(endDevice->GetDevice(0))->GetMac());
287
288 if (requestAck)
289 {
290 macLayer->SetFType(LorawanMacHeader::CONFIRMED_DATA_UP);
291 }
292
293 macLayer->AddMacCommand(Create<LinkCheckReq>());
294
295 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
296}
297
298// This method is the pure virtual method from class TestCase that every
299// TestCase must implement
300void
302{
303 NS_LOG_DEBUG("LinkCheckTest");
304
305 // Create a bunch of actual devices
306 NetworkComponents components = InitializeNetwork(1, 1);
307
308 Ptr<LoraChannel> channel = components.channel;
309 NodeContainer endDevices = components.endDevices;
310 NodeContainer gateways = components.gateways;
311 Ptr<Node> nsNode = components.nsNode;
312
313 // Connect the end device's trace source for last known gateway count
315 DynamicCast<LoraNetDevice>(endDevices.Get(0)->GetDevice(0))->GetMac())
316 ->TraceConnectWithoutContext("LastKnownGatewayCount",
318
319 // Send a packet in uplink
320 Simulator::Schedule(Seconds(1), &LinkCheckTest::SendPacket, this, endDevices.Get(0), true);
321
322 Simulator::Stop(Seconds(10)); // Allow for time to receive a downlink packet
325
327}
328
329/**
330 * @ingroup lorawan
331 *
332 * It verifies that the NetworkServer application responds to uplinks with the ADRACKReq bit set
333 */
335{
336 public:
337 AdrAckReqTest(); //!< Default constructor
338 ~AdrAckReqTest() override; //!< Destructor
339
340 private:
341 void DoRun() override;
342
343 /**
344 * Callback for packet reception by the end device MAC layer
345 *
346 * @param packet The received packet
347 */
348 void OnReception(Ptr<const Packet> packet);
349
350 bool m_adrAckReceived = false; //!< Set to true if a downlink packet is received by the end
351 //!< device after setting the uplink ADRACKReq bit
352};
353
355 : TestCase("Verify that the NetworkServer responds to uplinks with the ADRACKReq bit set")
356{
357}
358
362
363void
368
369void
371{
372 NS_LOG_DEBUG("AdrAckReqTest");
373 auto components = InitializeNetwork(1, 1);
374 auto ed = components.endDevices.Get(0);
375 auto netdev = DynamicCast<LoraNetDevice>(ed->GetDevice(0));
376 auto mac = DynamicCast<ClassAEndDeviceLorawanMac>(netdev->GetMac());
377 // Turn-off ADR downlinks from the server, ADRACKReq mechanism still works
378 mac->SetUplinkAdrBit(false);
380 mac->TraceConnectWithoutContext("ReceivedPacket", cb);
381 // Trigger ADRACKReq bit set
382 for (uint16_t fCnt = 0; fCnt <= EndDeviceLorawanMac::ADR_ACK_LIMIT; ++fCnt)
383 {
386 ed->GetDevice(0),
387 Create<Packet>(20),
388 Address(),
389 0);
391 }
392 NS_TEST_EXPECT_MSG_EQ(m_adrAckReceived, true, "No downlink received by the end device");
393}
394
395/**
396 * @ingroup lorawan
397 *
398 * The TestSuite class names the TestSuite, identifies what type of TestSuite, and enables the
399 * TestCases to be run. Typically, only the constructor for this class must be defined
400 */
402{
403 public:
404 NetworkServerTestSuite(); //!< Default constructor
405};
406
408 : TestSuite("network-server", Type::UNIT)
409{
410 // Activate only at need, as these can create problems among test suites when running ./test.py
411 // LogComponentEnable("NetworkServerTestSuite", LOG_LEVEL_DEBUG);
412 // LogComponentEnable("NetworkServer", LOG_LEVEL_ALL);
413 // LogComponentEnable("NetworkStatus", LOG_LEVEL_ALL);
414 // LogComponentEnable("NetworkScheduler", LOG_LEVEL_ALL);
415 // LogComponentEnable("NetworkController", LOG_LEVEL_ALL);
416 // LogComponentEnable("NetworkControllerComponent", LOG_LEVEL_ALL);
417 // LogComponentEnable("LoraNetDevice", LOG_LEVEL_ALL);
418 // LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
419 // LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
420 // LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
421 // LogComponentEnable("EndDeviceStatus", LOG_LEVEL_ALL);
422 // LogComponentEnableAll(LOG_PREFIX_FUNC);
423 // LogComponentEnableAll(LOG_PREFIX_NODE);
424 // LogComponentEnableAll(LOG_PREFIX_TIME);
425
430}
431
432// Do not forget to allocate an instance of this TestSuite
It verifies that the NetworkServer application responds to uplinks with the ADRACKReq bit set.
bool m_adrAckReceived
Set to true if a downlink packet is received by the end device after setting the uplink ADRACKReq bit...
void OnReception(Ptr< const Packet > packet)
Callback for packet reception by the end device MAC layer.
void DoRun() override
Implementation to actually run this TestCase.
AdrAckReqTest()
Default constructor.
~AdrAckReqTest() override
Destructor.
The TestSuite class names the TestSuite, identifies what type of TestSuite, and enables the TestCases...
NetworkServerTestSuite()
Default constructor.
a polymophic address class
Definition address.h:114
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)=0
keep track of a set of node pointers.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition node.cc:138
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
static constexpr uint16_t ADR_ACK_LIMIT
ADRACKCnt threshold for setting ADRACKReq.
#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
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:690
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#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:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
Time Minutes(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1296
static LorawanTestSuite lorawanTestSuite
NetworkComponents InitializeNetwork(int nDevices, int nGateways)
Definition utilities.cc:124
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643
Stores the main elements of a simulated LoRaWAN network.
Definition utilities.h:28
Ptr< Node > nsNode
A pointer to the network server Node.
Definition utilities.h:32
Ptr< LoraChannel > channel
A pointer to the LoraChannel object.
Definition utilities.h:29
NodeContainer endDevices
Container of the end device nodes.
Definition utilities.h:30
NodeContainer gateways
Container of the gateway nodes.
Definition utilities.h:31