A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wimax-fragmentation-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2010 TELEMATICS LAB - Poliotecnico di Bari
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Giuseppe Piro <g.piro@poliba.it>
7 * <peppe.piro@gmail.com>
8 */
9#include "ns3/cid.h"
10#include "ns3/log.h"
11#include "ns3/mac-messages.h"
12#include "ns3/packet.h"
13#include "ns3/ptr.h"
14#include "ns3/simulator.h"
15#include "ns3/test.h"
16#include "ns3/wimax-connection.h"
17#include "ns3/wimax-mac-header.h"
18
19using namespace ns3;
20
21/**
22 * @ingroup wimax-test
23 * @ingroup tests
24 *
25 * @brief Test the wimax packet fragmentation.
26 */
28{
29 public:
32
33 private:
34 void DoRun() override;
35};
36
38 : TestCase("Test the packet fragmentation and defragmentation.")
39{
40}
41
45
46void
48{
49 GenericMacHeader gnrcMacHdr;
51 FragmentationSubheader fragSubhdr;
53
54 Cid cid;
55 auto connectionTx = new WimaxConnection(cid, Cid::TRANSPORT);
56 auto connectionRx = new WimaxConnection(cid, Cid::TRANSPORT);
57
58 // A Packet of 1000 bytes has been created.
59 // It will be fragmentated into 4 fragments and then defragmentated into fullPacket.
60 Ptr<Packet> packet = Create<Packet>(1000);
61 Ptr<Packet> fragment;
62 Ptr<Packet> fullPacket = Create<Packet>();
63
64 // Enqueued packet
65 hdr.SetLen(packet->GetSize() + hdr.GetSerializedSize());
66 hdr.SetCid(connectionTx->GetCid());
68
69 connectionTx->Enqueue(packet, packetType, hdr);
70
71 uint32_t availableByteForFragment = 280;
72 for (int i = 0; i < 4; i++)
73 {
74 // dequeue a fragment
75 if (connectionTx->GetQueue()->GetFirstPacketRequiredByte(packetType) >
76 availableByteForFragment)
77 {
78 fragment = connectionTx->Dequeue(packetType, availableByteForFragment);
79 }
80 else
81 {
82 fragment = connectionTx->Dequeue(packetType);
83 }
84 // *** send packet -----> receive packet ----**
85
86 // check if receive packet is a fragment
87 fragment->RemoveHeader(gnrcMacHdr);
88 uint8_t type = gnrcMacHdr.GetType();
89 if (type)
90 {
91 // Check if there is a fragmentation Subheader
92 NS_TEST_EXPECT_MSG_EQ(((type >> 2) & 1), 1, "The packet is not a fragment");
93 }
94
95 // remove header from the received fragment
96 fragment->RemoveHeader(fragSubhdr);
97 uint32_t fc = fragSubhdr.GetFc();
98
99 NS_TEST_EXPECT_MSG_EQ((fc == 1 && i != 0), false, "The fragment in not the first one");
100 NS_TEST_EXPECT_MSG_EQ((fc == 2 && i != 3), false, "The fragment in not the latest one");
101 NS_TEST_EXPECT_MSG_EQ(((fc == 3 && i != 1) && (fc == 3 && i != 2)),
102 false,
103 "The fragment in not the middle one");
104
105 if (fc != 2)
106 {
107 // This is the first or middle fragment.
108 // Take the fragment queue, store the fragment into the queue
109 connectionRx->FragmentEnqueue(fragment);
110 }
111 else
112 {
113 // This is the latest fragment.
114 // Take the fragment queue, defragment a packet and send it to the upper layer
115 connectionRx->FragmentEnqueue(fragment);
116 WimaxConnection::FragmentsQueue fragmentsQueue = connectionRx->GetFragmentsQueue();
117
118 // DEFRAGMENTATION
119 for (auto iter = fragmentsQueue.begin(); iter != fragmentsQueue.end(); ++iter)
120 {
121 // Create the whole Packet
122 fullPacket->AddAtEnd(*iter);
123 }
124 connectionRx->ClearFragmentsQueue();
125
126 NS_TEST_EXPECT_MSG_EQ(fullPacket->GetSize(), 1000, "The defragmentation is incorrect");
127 }
128 }
129 delete connectionTx;
130 delete connectionRx;
132}
133
134/**
135 * @ingroup wimax-test
136 * @ingroup tests
137 *
138 * @brief Ns3 Wimax Fragmentation Test Suite
139 */
145
147 : TestSuite("wimax-fragmentation", Type::UNIT)
148{
149 AddTestCase(new Ns3WimaxFragmentationTestCase, TestCase::Duration::QUICK);
150}
151
Test the wimax packet fragmentation.
void DoRun() override
Implementation to actually run this TestCase.
Ns3 Wimax Fragmentation Test Suite.
Cid class.
Definition cid.h:26
@ TRANSPORT
Definition cid.h:35
This class implements the fragmentation sub-header as described by IEEE Standard for Local and metrop...
uint8_t GetFc() const
Get FC field.
This class implements the Generic mac Header as described by IEEE Standard for Local and metropolitan...
uint8_t GetType() const
Get type field.
uint32_t GetSerializedSize() const override
void SetLen(uint16_t len)
Set length field.
void SetCid(Cid cid)
Set CID field.
HeaderType
Header type enumeration.
Mac Management messages Section 6.3.2.3 MAC Management messages page 42, Table 14 page 43.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Class to represent WiMAX connections.
std::list< Ptr< const Packet > > FragmentsQueue
Definition of Fragments Queue data type.
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_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Ns3WimaxFragmentationTestSuite ns3WimaxFragmentationTestSuite
the test suite