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 * 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 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20/*
21 * This file includes testing for the following components:
22 * - NetworkServer
23 */
24
25// Include headers of classes to test
26#include "utilities.h"
27
28#include "ns3/callback.h"
29#include "ns3/core-module.h"
30#include "ns3/log.h"
31#include "ns3/network-server-helper.h"
32#include "ns3/network-server.h"
33
34// An essential include is test.h
35#include "ns3/test.h"
36
37using namespace ns3;
38using namespace lorawan;
39
40NS_LOG_COMPONENT_DEFINE("NetworkServerTestSuite");
41
42/**
43 * \ingroup lorawan
44 *
45 * It verifies that the NetworkServer application can receive packets sent in uplink by devices
46 */
48{
49 public:
50 UplinkPacketTest(); //!< Default constructor
51 ~UplinkPacketTest() override; //!< Destructor
52
53 /**
54 * Callback for tracing ReceivedPacket.
55 *
56 * \param packet The packet received.
57 */
59
60 /**
61 * Send a packet from the input end device.
62 *
63 * \param endDevice A pointer to the end device Node.
64 */
65 void SendPacket(Ptr<Node> endDevice);
66
67 private:
68 void DoRun() override;
69
70 bool m_receivedPacket = false; //!< Set to true if a packet is received by the server
71};
72
73// Add some help text to this case to describe what it is intended to test
75 : TestCase("Verify that the NetworkServer application can receive"
76 " packets sent in the uplink by devices")
77{
78}
79
80// Reminder that the test case should clean up after itself
82{
83}
84
85void
87{
88 NS_LOG_DEBUG("Received a packet at the network server");
89 m_receivedPacket = true;
90}
91
92void
94{
95 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
96}
97
98// This method is the pure virtual method from class TestCase that every
99// TestCase must implement
100void
102{
103 NS_LOG_DEBUG("UplinkPacketTest");
104
105 // Create a bunch of actual devices
106 NetworkComponents components = InitializeNetwork(1, 1);
107
108 Ptr<LoraChannel> channel = components.channel;
109 NodeContainer endDevices = components.endDevices;
110 NodeContainer gateways = components.gateways;
111 Ptr<Node> nsNode = components.nsNode;
112
113 // Connect the trace source for received packets
114 nsNode->GetApplication(0)->TraceConnectWithoutContext(
115 "ReceivedPacket",
117
118 // Send a packet
120
124
125 // Check that we received the packet
127}
128
129/**
130 * \ingroup lorawan
131 *
132 * It verifies that devices requesting an acknowledgment receive a reply from the network server
133 */
135{
136 public:
137 DownlinkPacketTest(); //!< Default constructor
138 ~DownlinkPacketTest() override; //!< Destructor
139
140 /**
141 * Record the exit status of a MAC layer packet retransmission process of an end device.
142 *
143 * This trace sink is only used here to determine whether an ack was received by the end device
144 * after sending a package requiring an acknowledgement.
145 *
146 * \param requiredTransmissions Number of transmissions attempted during the process.
147 * \param success Whether the retransmission procedure was successful.
148 * \param time Timestamp of the initial transmission attempt.
149 * \param packet The packet being retransmitted.
150 */
151 void ReceivedPacketAtEndDevice(uint8_t requiredTransmissions,
152 bool success,
153 Time time,
154 Ptr<Packet> packet);
155
156 /**
157 * Send a packet from the input end device.
158 *
159 * \param endDevice A pointer to the end device Node.
160 * \param requestAck Whether to require an acknowledgement from the server.
161 */
162 void SendPacket(Ptr<Node> endDevice, bool requestAck);
163
164 private:
165 void DoRun() override;
166
167 bool m_receivedPacketAtEd = false; //!< Set to true if a packet is received by the end device
168};
169
170// Add some help text to this case to describe what it is intended to test
172 : TestCase("Verify that devices requesting an acknowledgment receive"
173 " a reply from the network server.")
174{
175}
176
177// Reminder that the test case should clean up after itself
179{
180}
181
182void
184 bool success,
185 Time time,
186 Ptr<Packet> packet)
187{
188 NS_LOG_DEBUG("Received a packet at the end device");
189 m_receivedPacketAtEd = success;
190}
191
192void
193DownlinkPacketTest::SendPacket(Ptr<Node> endDevice, bool requestAck)
194{
195 if (requestAck)
196 {
197 endDevice->GetDevice(0)
198 ->GetObject<LoraNetDevice>()
199 ->GetMac()
202 }
203 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
204}
205
206// This method is the pure virtual method from class TestCase that every
207// TestCase must implement
208void
210{
211 NS_LOG_DEBUG("DownlinkPacketTest");
212
213 // Create a bunch of actual devices
214 NetworkComponents components = InitializeNetwork(1, 1);
215
216 Ptr<LoraChannel> channel = components.channel;
217 NodeContainer endDevices = components.endDevices;
218 NodeContainer gateways = components.gateways;
219 Ptr<Node> nsNode = components.nsNode;
220
221 // Connect the end device's trace source for received packets
222 endDevices.Get(0)
223 ->GetDevice(0)
224 ->GetObject<LoraNetDevice>()
225 ->GetMac()
227 ->TraceConnectWithoutContext(
228 "RequiredTransmissions",
230
231 // Send a packet in uplink
232 Simulator::Schedule(Seconds(1), &DownlinkPacketTest::SendPacket, this, endDevices.Get(0), true);
233
234 Simulator::Stop(Seconds(10)); // Allow for time to receive a downlink packet
237
239}
240
241/**
242 * \ingroup lorawan
243 *
244 * It verifies that the NetworkServer application correctly responds to LinkCheck requests
245 */
247{
248 public:
249 LinkCheckTest(); //!< Default constructor
250 ~LinkCheckTest() override; //!< Destructor
251
252 /**
253 * Trace changes in the last known gateway count variable (updated on reception of
254 * LinkCheckAns MAC commands) of an end device.
255 *
256 * \param newValue The updated value.
257 * \param oldValue The previous value.
258 */
259 void LastKnownGatewayCount(int newValue, int oldValue);
260
261 /**
262 * Send a packet containing a LinkCheckReq MAC command from the input end device.
263 *
264 * \param endDevice A pointer to the end device Node.
265 * \param requestAck Whether to require an acknowledgement from the server.
266 */
267 void SendPacket(Ptr<Node> endDevice, bool requestAck);
268
269 private:
270 void DoRun() override;
271 bool m_receivedPacketAtEd = false; //!< Set to true if a packet containing a LinkCheckAns MAC
272 //!< command is received by the end device
274 0; //!< Stores the number of gateways that received the last packet carrying a
275 //!< LinkCheckReq MAC command
276};
277
278// Add some help text to this case to describe what it is intended to test
280 : TestCase("Verify that the NetworkServer application correctly responds to "
281 "LinkCheck requests")
282{
283}
284
285// Reminder that the test case should clean up after itself
287{
288}
289
290void
291LinkCheckTest::LastKnownGatewayCount(int newValue, int oldValue)
292{
293 NS_LOG_DEBUG("Updated gateway count");
295
297}
298
299void
300LinkCheckTest::SendPacket(Ptr<Node> endDevice, bool requestAck)
301{
302 Ptr<EndDeviceLorawanMac> macLayer = endDevice->GetDevice(0)
303 ->GetObject<LoraNetDevice>()
304 ->GetMac()
306
307 if (requestAck)
308 {
309 macLayer->SetMType(LorawanMacHeader::CONFIRMED_DATA_UP);
310 }
311
312 macLayer->AddMacCommand(Create<LinkCheckReq>());
313
314 endDevice->GetDevice(0)->Send(Create<Packet>(20), Address(), 0);
315}
316
317// This method is the pure virtual method from class TestCase that every
318// TestCase must implement
319void
321{
322 NS_LOG_DEBUG("LinkCheckTest");
323
324 // Create a bunch of actual devices
325 NetworkComponents components = InitializeNetwork(1, 1);
326
327 Ptr<LoraChannel> channel = components.channel;
328 NodeContainer endDevices = components.endDevices;
329 NodeContainer gateways = components.gateways;
330 Ptr<Node> nsNode = components.nsNode;
331
332 // Connect the end device's trace source for last known gateway count
333 endDevices.Get(0)
334 ->GetDevice(0)
335 ->GetObject<LoraNetDevice>()
336 ->GetMac()
338 ->TraceConnectWithoutContext("LastKnownGatewayCount",
340
341 // Send a packet in uplink
342 Simulator::Schedule(Seconds(1), &LinkCheckTest::SendPacket, this, endDevices.Get(0), true);
343
344 Simulator::Stop(Seconds(10)); // Allow for time to receive a downlink packet
347
349}
350
351/**
352 * \ingroup lorawan
353 *
354 * The TestSuite class names the TestSuite, identifies what type of TestSuite, and enables the
355 * TestCases to be run. Typically, only the constructor for this class must be defined
356 */
358{
359 public:
360 NetworkServerTestSuite(); //!< Default constructor
361};
362
364 : TestSuite("network-server", Type::UNIT)
365{
366 LogComponentEnable("NetworkServerTestSuite", LOG_LEVEL_DEBUG);
367
368 LogComponentEnable("NetworkServer", LOG_LEVEL_ALL);
369 LogComponentEnable("NetworkStatus", LOG_LEVEL_ALL);
370 LogComponentEnable("NetworkScheduler", LOG_LEVEL_ALL);
371 LogComponentEnable("NetworkController", LOG_LEVEL_ALL);
372 LogComponentEnable("NetworkControllerComponent", LOG_LEVEL_ALL);
373 LogComponentEnable("LoraNetDevice", LOG_LEVEL_ALL);
374 LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
375 LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
376 LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
377 LogComponentEnable("EndDeviceStatus", LOG_LEVEL_ALL);
378
382
383 // TestDuration for TestCase can be QUICK, EXTENSIVE or TAKES_FOREVER
387}
388
389// Do not forget to allocate an instance of this TestSuite
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:101
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:149
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
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
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:302
A suite of tests to run.
Definition: test.h:1273
Type
Type of test.
Definition: test.h:1280
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Class representing the MAC layer of a LoRaWAN device.
Hold together all LoRa related objects.
#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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
NetworkComponents InitializeNetwork(int nDevices, int nGateways)
Definition: utilities.cc:128
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
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:700
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition: log.h:113
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
static NetworkServerTestSuite lorawanTestSuite
Stores the main elements of a simulated LoRaWAN network.
Definition: utilities.h:40
Ptr< Node > nsNode
A pointer to the network server Node.
Definition: utilities.h:44
Ptr< LoraChannel > channel
A pointer to the LoraChannel object.
Definition: utilities.h:41
NodeContainer endDevices
Container of the end device nodes.
Definition: utilities.h:42
NodeContainer gateways
Container of the gateway nodes.
Definition: utilities.h:43