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 
33 /*
34  * Test the wimax packet fragmentation.
35  */
37 {
38 public:
41 
42 private:
43  virtual void DoRun (void);
44 
45 };
46 
48  : TestCase ("Test the packet fragmentation and defragmentation.")
49 {
50 }
51 
53 {
54 }
55 
56 void
58 {
59  GenericMacHeader gnrcMacHdr;
60  ManagementMessageType msgType;
61  FragmentationSubheader fragSubhdr;
62  GenericMacHeader hdr;
63 
64  Cid cid;
65  WimaxConnection *connectionTx = new WimaxConnection (cid, Cid::TRANSPORT);
66  WimaxConnection *connectionRx = new WimaxConnection (cid, Cid::TRANSPORT);
67 
68  // A Packet of 1000 bytes has been created.
69  // It will be fragmentated into 4 fragments and then defragmentated into fullPacket.
70  Ptr<Packet> packet = Create<Packet> (1000);
71  Ptr<Packet> fragment;
72  Ptr<Packet> fullPacket = Create<Packet> ();
73 
74  // Enqueued packet
75  hdr.SetLen (packet->GetSize () + hdr.GetSerializedSize ());
76  hdr.SetCid (connectionTx->GetCid ());
77  MacHeaderType::HeaderType packetType = MacHeaderType::HEADER_TYPE_GENERIC;
78 
79  connectionTx->Enqueue (packet, packetType, hdr);
80 
81  uint32_t availableByteForFragment = 280;
82  for (int i = 0; i < 4; i++)
83  {
84  // dequeue a fragment
85  if (connectionTx->GetQueue ()->GetFirstPacketRequiredByte (packetType) > availableByteForFragment)
86  {
87  fragment = connectionTx->Dequeue (packetType, availableByteForFragment);
88  }
89  else
90  {
91  fragment = connectionTx->Dequeue (packetType);
92  }
93 // *** send packet -----> receive packet ----**
94 
95  // check if receive packet is a fragment
96  fragment->RemoveHeader (gnrcMacHdr);
97  uint8_t type = gnrcMacHdr.GetType ();
98  if (type)
99  {
100  // Check if there is a fragmentation Subheader
101  NS_TEST_EXPECT_MSG_EQ (((type >> 2) & 1), 1, "The packet is not a fragment");
102  }
103 
104  // remove header from the received fragment
105  fragment->RemoveHeader (fragSubhdr);
106  uint32_t fc = fragSubhdr.GetFc ();
107 
108  NS_TEST_EXPECT_MSG_EQ ((fc == 1 && i != 0), false, "The fragment in not the first one");
109  NS_TEST_EXPECT_MSG_EQ ((fc == 2 && i != 3), false, "The fragment in not the latest one");
110  NS_TEST_EXPECT_MSG_EQ (((fc == 3 && i != 1) && (fc == 3 && i != 2)), false, "The fragment in not the middle one");
111 
112  if (fc != 2)
113  {
114  // This is the first or middle fragment.
115  // Take the fragment queue, store the fragment into the queue
116  connectionRx->FragmentEnqueue (fragment);
117  }
118  else
119  {
120  // This is the latest fragment.
121  // Take the fragment queue, defragment a packet and send it to the upper layer
122  connectionRx->FragmentEnqueue (fragment);
123  WimaxConnection::FragmentsQueue fragmentsQueue = connectionRx->GetFragmentsQueue ();
124 
125  // DEFRAGMENTATION
126  for (std::list<Ptr<const Packet> >::const_iterator iter = fragmentsQueue.begin ();
127  iter != fragmentsQueue.end (); ++iter)
128  {
129  // Create the whole Packet
130  fullPacket->AddAtEnd (*iter);
131  }
132  connectionRx->ClearFragmentsQueue ();
133 
134  NS_TEST_EXPECT_MSG_EQ (fullPacket->GetSize (), 1000, "The defragmentation is incorrect");
135  }
136  }
137  delete connectionTx;
138  delete connectionRx;
139  Simulator::Destroy ();
140 }
141 // ==============================================================================
142 
144 {
145 public:
147 };
148 
150  : TestSuite ("wimax-fragmentation", UNIT)
151 {
152  AddTestCase (new Ns3WimaxFragmentationTestCase, TestCase::QUICK);
153 }
154 
static Ns3WimaxFragmentationTestSuite ns3WimaxFragmentationTestSuite
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Introspection did not find any typical Config paths.
Definition: mac-messages.h:41
A suite of tests to run.
Definition: test.h:1333
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:278
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
encapsulates test code
Definition: test.h:1147
this class implements the Generic mac Header as described by IEEE Standard for Local and metropolitan...
uint8_t GetType(void) const
uint8_t GetFc(void) const
Ptr< WimaxMacQueue > GetQueue(void) const
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:313
uint32_t GetSerializedSize(void) const
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
const FragmentsQueue GetFragmentsQueue(void) const
get a queue of received fragments
#define list
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.
Definition: cid.h:35
Cid GetCid(void) const
std::list< Ptr< const Packet > > FragmentsQueue
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...
virtual void DoRun(void)
Implementation to actually run this TestCase.
void SetLen(uint16_t len)
HeaderType
this class implements the mac header type field.