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 * 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 * Giuseppe Piro <g.piro@poliba.it>
18 * <peppe.piro@gmail.com>
19 */
20#include "ns3/cid.h"
21#include "ns3/log.h"
22#include "ns3/mac-messages.h"
23#include "ns3/packet.h"
24#include "ns3/ptr.h"
25#include "ns3/simulator.h"
26#include "ns3/test.h"
27#include "ns3/wimax-connection.h"
28#include "ns3/wimax-mac-header.h"
29
30using namespace ns3;
31
32/**
33 * \ingroup wimax-test
34 * \ingroup tests
35 *
36 * \brief Test the wimax packet fragmentation.
37 */
39{
40 public:
43
44 private:
45 void DoRun() override;
46};
47
49 : TestCase("Test the packet fragmentation and defragmentation.")
50{
51}
52
54{
55}
56
57void
59{
60 GenericMacHeader gnrcMacHdr;
62 FragmentationSubheader fragSubhdr;
64
65 Cid cid;
66 auto connectionTx = new WimaxConnection(cid, Cid::TRANSPORT);
67 auto connectionRx = new WimaxConnection(cid, Cid::TRANSPORT);
68
69 // A Packet of 1000 bytes has been created.
70 // It will be fragmentated into 4 fragments and then defragmentated into fullPacket.
71 Ptr<Packet> packet = Create<Packet>(1000);
72 Ptr<Packet> fragment;
73 Ptr<Packet> fullPacket = Create<Packet>();
74
75 // Enqueued packet
76 hdr.SetLen(packet->GetSize() + hdr.GetSerializedSize());
77 hdr.SetCid(connectionTx->GetCid());
79
80 connectionTx->Enqueue(packet, packetType, hdr);
81
82 uint32_t availableByteForFragment = 280;
83 for (int i = 0; i < 4; i++)
84 {
85 // dequeue a fragment
86 if (connectionTx->GetQueue()->GetFirstPacketRequiredByte(packetType) >
87 availableByteForFragment)
88 {
89 fragment = connectionTx->Dequeue(packetType, availableByteForFragment);
90 }
91 else
92 {
93 fragment = connectionTx->Dequeue(packetType);
94 }
95 // *** send packet -----> receive packet ----**
96
97 // check if receive packet is a fragment
98 fragment->RemoveHeader(gnrcMacHdr);
99 uint8_t type = gnrcMacHdr.GetType();
100 if (type)
101 {
102 // Check if there is a fragmentation Subheader
103 NS_TEST_EXPECT_MSG_EQ(((type >> 2) & 1), 1, "The packet is not a fragment");
104 }
105
106 // remove header from the received fragment
107 fragment->RemoveHeader(fragSubhdr);
108 uint32_t fc = fragSubhdr.GetFc();
109
110 NS_TEST_EXPECT_MSG_EQ((fc == 1 && i != 0), false, "The fragment in not the first one");
111 NS_TEST_EXPECT_MSG_EQ((fc == 2 && i != 3), false, "The fragment in not the latest one");
112 NS_TEST_EXPECT_MSG_EQ(((fc == 3 && i != 1) && (fc == 3 && i != 2)),
113 false,
114 "The fragment in not the middle one");
115
116 if (fc != 2)
117 {
118 // This is the first or middle fragment.
119 // Take the fragment queue, store the fragment into the queue
120 connectionRx->FragmentEnqueue(fragment);
121 }
122 else
123 {
124 // This is the latest fragment.
125 // Take the fragment queue, defragment a packet and send it to the upper layer
126 connectionRx->FragmentEnqueue(fragment);
127 WimaxConnection::FragmentsQueue fragmentsQueue = connectionRx->GetFragmentsQueue();
128
129 // DEFRAGMENTATION
130 for (auto iter = fragmentsQueue.begin(); iter != fragmentsQueue.end(); ++iter)
131 {
132 // Create the whole Packet
133 fullPacket->AddAtEnd(*iter);
134 }
135 connectionRx->ClearFragmentsQueue();
136
137 NS_TEST_EXPECT_MSG_EQ(fullPacket->GetSize(), 1000, "The defragmentation is incorrect");
138 }
139 }
140 delete connectionTx;
141 delete connectionRx;
143}
144
145/**
146 * \ingroup wimax-test
147 * \ingroup tests
148 *
149 * \brief Ns3 Wimax Fragmentation Test Suite
150 */
152{
153 public:
155};
156
158 : TestSuite("wimax-fragmentation", Type::UNIT)
159{
160 AddTestCase(new Ns3WimaxFragmentationTestCase, TestCase::Duration::QUICK);
161}
162
Test the wimax packet fragmentation.
void DoRun() override
Implementation to actually run this TestCase.
Ns3 Wimax Fragmentation Test Suite.
Cid class.
Definition: cid.h:37
@ TRANSPORT
Definition: cid.h:46
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.
Definition: mac-messages.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
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:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Class to represent WiMAX connections.
std::list< Ptr< const Packet > > FragmentsQueue
Definition of Fragments Queue data type.
#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:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static Ns3WimaxFragmentationTestSuite ns3WimaxFragmentationTestSuite
the test suite