A Discrete-Event Network Simulator
API
wimax-fragmentation-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009-2010 TELEMATICS LAB - Poliotecnico di Bari
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Giuseppe Piro <g.piro@poliba.it>
19  * <peppe.piro@gmail.com>
20  */
21 #include "ns3/log.h"
22 #include "ns3/test.h"
23 #include "ns3/ptr.h"
24 #include "ns3/packet.h"
25 #include "ns3/simulator.h"
26 #include "ns3/wimax-mac-header.h"
27 #include "ns3/mac-messages.h"
28 #include "ns3/cid.h"
29 #include "ns3/wimax-connection.h"
30 
31 using namespace ns3;
32 
40 {
41 public:
44 
45 private:
46  virtual void DoRun (void);
47 
48 };
49 
51  : TestCase ("Test the packet fragmentation and defragmentation.")
52 {
53 }
54 
56 {
57 }
58 
59 void
61 {
62  GenericMacHeader gnrcMacHdr;
63  ManagementMessageType msgType;
64  FragmentationSubheader fragSubhdr;
65  GenericMacHeader hdr;
66 
67  Cid cid;
68  WimaxConnection *connectionTx = new WimaxConnection (cid, Cid::TRANSPORT);
69  WimaxConnection *connectionRx = new WimaxConnection (cid, Cid::TRANSPORT);
70 
71  // A Packet of 1000 bytes has been created.
72  // It will be fragmentated into 4 fragments and then defragmentated into fullPacket.
73  Ptr<Packet> packet = Create<Packet> (1000);
74  Ptr<Packet> fragment;
75  Ptr<Packet> fullPacket = Create<Packet> ();
76 
77  // Enqueued packet
78  hdr.SetLen (packet->GetSize () + hdr.GetSerializedSize ());
79  hdr.SetCid (connectionTx->GetCid ());
80  MacHeaderType::HeaderType packetType = MacHeaderType::HEADER_TYPE_GENERIC;
81 
82  connectionTx->Enqueue (packet, packetType, hdr);
83 
84  uint32_t availableByteForFragment = 280;
85  for (int i = 0; i < 4; i++)
86  {
87  // dequeue a fragment
88  if (connectionTx->GetQueue ()->GetFirstPacketRequiredByte (packetType) > availableByteForFragment)
89  {
90  fragment = connectionTx->Dequeue (packetType, availableByteForFragment);
91  }
92  else
93  {
94  fragment = connectionTx->Dequeue (packetType);
95  }
96 // *** send packet -----> receive packet ----**
97 
98  // check if receive packet is a fragment
99  fragment->RemoveHeader (gnrcMacHdr);
100  uint8_t type = gnrcMacHdr.GetType ();
101  if (type)
102  {
103  // Check if there is a fragmentation Subheader
104  NS_TEST_EXPECT_MSG_EQ (((type >> 2) & 1), 1, "The packet is not a fragment");
105  }
106 
107  // remove header from the received fragment
108  fragment->RemoveHeader (fragSubhdr);
109  uint32_t fc = fragSubhdr.GetFc ();
110 
111  NS_TEST_EXPECT_MSG_EQ ((fc == 1 && i != 0), false, "The fragment in not the first one");
112  NS_TEST_EXPECT_MSG_EQ ((fc == 2 && i != 3), false, "The fragment in not the latest one");
113  NS_TEST_EXPECT_MSG_EQ (((fc == 3 && i != 1) && (fc == 3 && i != 2)), false, "The fragment in not the middle one");
114 
115  if (fc != 2)
116  {
117  // This is the first or middle fragment.
118  // Take the fragment queue, store the fragment into the queue
119  connectionRx->FragmentEnqueue (fragment);
120  }
121  else
122  {
123  // This is the latest fragment.
124  // Take the fragment queue, defragment a packet and send it to the upper layer
125  connectionRx->FragmentEnqueue (fragment);
126  WimaxConnection::FragmentsQueue fragmentsQueue = connectionRx->GetFragmentsQueue ();
127 
128  // DEFRAGMENTATION
129  for (std::list<Ptr<const Packet> >::const_iterator iter = fragmentsQueue.begin ();
130  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;
142  Simulator::Destroy ();
143 }
144 
152 {
153 public:
155 };
156 
158  : TestSuite ("wimax-fragmentation", UNIT)
159 {
160  AddTestCase (new Ns3WimaxFragmentationTestCase, TestCase::QUICK);
161 }
162 
static Ns3WimaxFragmentationTestSuite ns3WimaxFragmentationTestSuite
the test suite
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
HeaderType
Header type enumeration.
uint32_t GetSerializedSize(void) const
const FragmentsQueue GetFragmentsQueue(void) const
get a queue of received fragments
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Mac Management messages Section 6.3.2.3 MAC Management messages page 42, Table 14 page 43...
Definition: mac-messages.h:43
A suite of tests to run.
Definition: test.h:1341
void FragmentEnqueue(Ptr< const Packet > fragment)
enqueue a received packet (that is a fragment) into the fragments queue
#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:283
encapsulates test code
Definition: test.h:1153
Class to represent WiMAX connections.
This class implements the Generic mac Header as described by IEEE Standard for Local and metropolitan...
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void SetCid(Cid cid)
Set CID field.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ns3 Wimax Fragmentation Test Suite.
Cid GetCid(void) const
Get CID function.
Ptr< WimaxMacQueue > GetQueue(void) const
#define list
uint8_t GetFc(void) const
Get FC field.
void ClearFragmentsQueue(void)
delete all enqueued fragments
bool Enqueue(Ptr< Packet > packet, const MacHeaderType &hdrType, const GenericMacHeader &hdr)
enqueue a packet in the queue of the connection
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Cid class.
Definition: cid.h:37
std::list< Ptr< const Packet > > FragmentsQueue
Definition of Fragments Queue data type.
Ptr< Packet > Dequeue(MacHeaderType::HeaderType packetType=MacHeaderType::HEADER_TYPE_GENERIC)
dequeue a packet from the queue of the connection
This class implements the fragmentation sub-header as described by IEEE Standard for Local and metrop...
Test the wimax packet fragmentation.
uint8_t GetType(void) const
Get type field.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SetLen(uint16_t len)
Set length field.