A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
error-model-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 University of Washington
3 * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9/* BurstErrorModel additions
10 *
11 * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
12 * ResiliNets Research Group https://resilinets.org/
13 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
14 */
15
16#include "ns3/address.h"
17#include "ns3/callback.h"
18#include "ns3/double.h"
19#include "ns3/error-model.h"
20#include "ns3/mac48-address.h"
21#include "ns3/node.h"
22#include "ns3/packet.h"
23#include "ns3/pointer.h"
24#include "ns3/queue.h"
25#include "ns3/rng-seed-manager.h"
26#include "ns3/simple-channel.h"
27#include "ns3/simple-net-device.h"
28#include "ns3/simulator.h"
29#include "ns3/string.h"
30#include "ns3/test.h"
31
32using namespace ns3;
33
34static void
35SendPacket(int num, Ptr<NetDevice> device, Address& addr)
36{
37 for (int i = 0; i < num; i++)
38 {
39 Ptr<Packet> pkt = Create<Packet>(1000); // 1000 dummy bytes of data
40 device->Send(pkt, addr, 0);
41 }
42}
43
44// Two nodes, two devices, one channel
45static void
47 Ptr<Node> b,
50 Ptr<SimpleChannel> channel)
51{
52 ObjectFactory queueFactory;
53 queueFactory.SetTypeId("ns3::DropTailQueue<Packet>");
54 queueFactory.Set("MaxSize", StringValue("100000p")); // Much larger than we need
55 Ptr<Queue<Packet>> queueA = queueFactory.Create<Queue<Packet>>();
56 Ptr<Queue<Packet>> queueB = queueFactory.Create<Queue<Packet>>();
57
58 input->SetQueue(queueA);
59 output->SetQueue(queueB);
60 a->AddDevice(input);
61 b->AddDevice(output);
62 input->SetAddress(Mac48Address::Allocate());
63 input->SetChannel(channel);
64 input->SetNode(a);
65 output->SetChannel(channel);
66 output->SetNode(b);
67 output->SetAddress(Mac48Address::Allocate());
68}
69
70/**
71 * @ingroup network-test
72 * @ingroup tests
73 *
74 * ErrorModel unit tests.
75 */
77{
78 public:
80 ~ErrorModelSimple() override;
81
82 private:
83 void DoRun() override;
84 /**
85 * Receive form a NetDevice
86 * @param nd The NetDevice.
87 * @param p The received packet.
88 * @param protocol The protocol received.
89 * @param addr The sender address.
90 * @return True on success.
91 */
92 bool Receive(Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
93 /**
94 * Register a Drop
95 * @param p The dropped packet
96 */
98
99 uint32_t m_count; //!< The received packets counter.
100 uint32_t m_drops; //!< The dropped packets counter.
101};
102
103// Add some help text to this case to describe what it is intended to test
105 : TestCase("ErrorModel and PhyRxDrop trace for SimpleNetDevice"),
106 m_count(0),
107 m_drops(0)
108{
109}
110
114
115bool
118 uint16_t protocol,
119 const Address& addr)
120{
121 m_count++;
122 return true;
123}
124
125void
130
131void
133{
134 // Set some arbitrary deterministic values
137
140
144 BuildSimpleTopology(a, b, input, output, channel);
145
146 output->SetReceiveCallback(MakeCallback(&ErrorModelSimple::Receive, this));
148 // Set this variable to a specific stream
149 uv->SetStream(50);
150
152 em->SetRandomVariable(uv);
153 em->SetAttribute("ErrorRate", DoubleValue(0.001));
154 em->SetAttribute("ErrorUnit", StringValue("ERROR_UNIT_PACKET"));
155
156 // The below hooks will cause drops and receptions to be counted
157 output->SetAttribute("ReceiveErrorModel", PointerValue(em));
158 output->TraceConnectWithoutContext("PhyRxDrop",
160
161 // Send 10000 packets
162 Simulator::Schedule(Seconds(0), &SendPacket, 10000, input, output->GetAddress());
163
166
167 // For this combination of values, we expect about 1 packet in 1000 to be
168 // dropped. For this specific RNG stream, we see 9991 receptions and 9 drops
169 NS_TEST_ASSERT_MSG_EQ(m_count, 9991, "Wrong number of receptions.");
170 NS_TEST_ASSERT_MSG_EQ(m_drops, 9, "Wrong number of drops.");
171}
172
173/**
174 * @ingroup network-test
175 * @ingroup tests
176 *
177 * BurstErrorModel unit tests.
178 */
180{
181 public:
183 ~BurstErrorModelSimple() override;
184
185 private:
186 void DoRun() override;
187 /**
188 * Receive form a NetDevice
189 * @param nd The NetDevice.
190 * @param p The received packet.
191 * @param protocol The protocol received.
192 * @param addr The sender address.
193 * @return True on success.
194 */
195 bool Receive(Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
196 /**
197 * Register a Drop
198 * @param p The dropped packet
199 */
201
202 uint32_t m_count; //!< The received packets counter.
203 uint32_t m_drops; //!< The dropped packets counter.
204};
205
206// Add some help text to this case to describe what it is intended to test
208 : TestCase("ErrorModel and PhyRxDrop trace for SimpleNetDevice"),
209 m_count(0),
210 m_drops(0)
211{
212}
213
217
218bool
221 uint16_t protocol,
222 const Address& addr)
223{
224 m_count++;
225 return true;
226}
227
228void
233
234void
236{
237 // Set some arbitrary deterministic values
240
243
247 BuildSimpleTopology(a, b, input, output, channel);
248
249 output->SetReceiveCallback(MakeCallback(&BurstErrorModelSimple::Receive, this));
251 // Set this variable to a specific stream
252 uv->SetStream(50);
253
255 em->SetRandomVariable(uv);
256 em->SetAttribute("ErrorRate", DoubleValue(0.01));
257
258 // Assign the underlying error model random variables to specific streams
259 em->AssignStreams(51);
260
261 // The below hooks will cause drops and receptions to be counted
262 output->SetAttribute("ReceiveErrorModel", PointerValue(em));
263 output->TraceConnectWithoutContext("PhyRxDrop",
265
266 // Send 10000 packets
267 Simulator::Schedule(Seconds(0), &SendPacket, 10000, input, output->GetAddress());
268
271
272 // With the burst error rate to be 0.01 and the burst size to be from 1 to 4,
273 // we expect about 2.5 packets being dropped every 1000 packets.
274 // That means for 10000 packets, we expect a total of about 250 packet drops.
275 // For this specific RNG seed, we see 9740 receptions and 260 drops.
276 NS_TEST_ASSERT_MSG_EQ(m_count, 9740, "Wrong number of receptions.");
277 NS_TEST_ASSERT_MSG_EQ(m_drops, 260, "Wrong number of drops.");
278}
279
280/**
281 * @ingroup network-test
282 * @ingroup tests
283 *
284 * @brief ErrorModel TestSuite
285 *
286 * This is the start of an error model test suite. For starters, this is
287 * just testing that the SimpleNetDevice is working but this can be
288 * extended to many more test cases in the future
289 */
291{
292 public:
294};
295
302
303// Do not forget to allocate an instance of this TestSuite
304static ErrorModelTestSuite errorModelTestSuite; //!< Static variable for test initialization
BurstErrorModel unit tests.
bool Receive(Ptr< NetDevice > nd, Ptr< const Packet > p, uint16_t protocol, const Address &addr)
Receive form a NetDevice.
void DoRun() override
Implementation to actually run this TestCase.
uint32_t m_count
The received packets counter.
uint32_t m_drops
The dropped packets counter.
void DropEvent(Ptr< const Packet > p)
Register a Drop.
ErrorModel unit tests.
uint32_t m_drops
The dropped packets counter.
bool Receive(Ptr< NetDevice > nd, Ptr< const Packet > p, uint16_t protocol, const Address &addr)
Receive form a NetDevice.
uint32_t m_count
The received packets counter.
void DropEvent(Ptr< const Packet > p)
Register a Drop.
void DoRun() override
Implementation to actually run this TestCase.
ErrorModel TestSuite.
a polymophic address class
Definition address.h:90
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
static Mac48Address Allocate()
Allocate a new Mac48Address.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
Template class for packet Queues.
Definition queue.h:257
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:561
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
@ QUICK
Fast test.
Definition test.h:1055
TestCase(const TestCase &)=delete
Type
Type of test.
Definition test.h:1274
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:490
static constexpr auto UNIT
Definition test.h:1291
static ErrorModelTestSuite errorModelTestSuite
Static variable for test initialization.
static void SendPacket(int num, Ptr< NetDevice > device, Address &addr)
static void BuildSimpleTopology(Ptr< Node > a, Ptr< Node > b, Ptr< SimpleNetDevice > input, Ptr< SimpleNetDevice > output, Ptr< SimpleChannel > channel)
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1345
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