A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/abort.h"
23 #include "ns3/test.h"
24 #include "ns3/uinteger.h"
25 #include "ns3/inet-socket-address.h"
26 #include "ns3/point-to-point-helper.h"
27 #include "ns3/internet-stack-helper.h"
28 #include "ns3/ipv4-address-helper.h"
29 #include "ns3/ipv4-header.h"
30 #include "ns3/packet-sink-helper.h"
31 #include "ns3/udp-client-server-helper.h"
32 #include "ns3/udp-header.h"
33 #include "ns3/simulator.h"
34 #include "ns3/wimax-helper.h"
35 #include "ns3/mobility-helper.h"
36 #include "ns3/global-route-manager.h"
37 #include "ns3/wimax-tlv.h"
38 #include "ns3/ipcs-classifier-record.h"
39 #include "ns3/service-flow.h"
40 #include "ns3/wimax-connection.h"
41 #include <iostream>
42 
43 using namespace ns3;
44 
45 /*
46  * Test the wimax packet fragmentation.
47  */
49 {
50 public:
53 
54 private:
55  virtual void DoRun (void);
56 
57 };
58 
60  : TestCase ("Test the packet fragmentation and defragmentation.")
61 {
62 }
63 
65 {
66 }
67 
68 void
70 {
71  GenericMacHeader gnrcMacHdr;
72  ManagementMessageType msgType;
73  FragmentationSubheader fragSubhdr;
74  GenericMacHeader hdr;
75 
76  Cid cid;
77  WimaxConnection *connectionTx = new WimaxConnection (cid, Cid::TRANSPORT);
78  WimaxConnection *connectionRx = new WimaxConnection (cid, Cid::TRANSPORT);
79 
80  // A Packet of 1000 bytes has been created.
81  // It will be fragmentated into 4 fragments and then defragmentated into fullPacket.
82  Ptr<Packet> packet = Create<Packet> (1000);
83  Ptr<Packet> fragment;
84  Ptr<Packet> fullPacket = Create<Packet> ();
85 
86  // Enqueued packet
87  hdr.SetLen (packet->GetSize () + hdr.GetSerializedSize ());
88  hdr.SetCid (connectionTx->GetCid ());
89  MacHeaderType::HeaderType packetType = MacHeaderType::HEADER_TYPE_GENERIC;
90 
91  connectionTx->Enqueue (packet, packetType, hdr);
92 
93  uint32_t availableByteForFragment = 280;
94  for (int i = 0; i < 4; i++)
95  {
96  // dequeue a fragment
97  if (connectionTx->GetQueue ()->GetFirstPacketRequiredByte (packetType) > availableByteForFragment)
98  {
99  fragment = connectionTx->Dequeue (packetType, availableByteForFragment);
100  }
101  else
102  {
103  fragment = connectionTx->Dequeue (packetType);
104  }
105 // *** send packet -----> receive packet ----**
106 
107  // check if receive packet is a fragment
108  fragment->RemoveHeader (gnrcMacHdr);
109  uint8_t type = gnrcMacHdr.GetType ();
110  if (type)
111  {
112  // Check if there is a fragmentation Subheader
113  NS_TEST_EXPECT_MSG_EQ (((type >> 2) & 1), 1, "The packet is not a fragment");
114  }
115 
116  // remove header from the received fragment
117  fragment->RemoveHeader (fragSubhdr);
118  uint32_t fc = fragSubhdr.GetFc ();
119 
120  NS_TEST_EXPECT_MSG_EQ ((fc == 1 && i != 0), false, "The fragment in not the first one");
121  NS_TEST_EXPECT_MSG_EQ ((fc == 2 && i != 3), false, "The fragment in not the latest one");
122  NS_TEST_EXPECT_MSG_EQ (((fc == 3 && i != 1) && (fc == 3 && i != 2)), false, "The fragment in not the middle one");
123 
124  if (fc != 2)
125  {
126  // This is the first or middle fragment.
127  // Take the fragment queue, store the fragment into the queue
128  connectionRx->FragmentEnqueue (fragment);
129  }
130  else
131  {
132  // This is the latest fragment.
133  // Take the fragment queue, defragment a packet and send it to the upper layer
134  connectionRx->FragmentEnqueue (fragment);
135  WimaxConnection::FragmentsQueue fragmentsQueue = connectionRx->GetFragmentsQueue ();
136 
137  // DEFRAGMENTATION
138  for (std::list<Ptr<const Packet> >::const_iterator iter = fragmentsQueue.begin ();
139  iter != fragmentsQueue.end (); ++iter)
140  {
141  // Create the whole Packet
142  fullPacket->AddAtEnd (*iter);
143  }
144  connectionRx->ClearFragmentsQueue ();
145 
146  NS_TEST_EXPECT_MSG_EQ (fullPacket->GetSize (), 1000, "The defragmentation is incorrect");
147  }
148  }
149  delete connectionTx;
150  delete connectionRx;
151  Simulator::Destroy ();
152 }
153 // ==============================================================================
154 
156 {
157 public:
159 };
160 
162  : TestSuite ("wimax-fragmentation", UNIT)
163 {
164  AddTestCase (new Ns3WimaxFragmentationTestCase, TestCase::QUICK);
165 }
166 
static Ns3WimaxFragmentationTestSuite ns3WimaxFragmentationTestSuite
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
HeaderType
this class implements the mac header type field.
Doxygen introspection did not find any typical Config paths.
Definition: mac-messages.h:41
A suite of tests to run.
Definition: test.h:1105
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:265
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
encapsulates test code
Definition: test.h:929
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:317
uint32_t GetSerializedSize(void) const
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
Definition: cid.h:35
Cid GetCid(void) const
std::list< Ptr< const Packet > > FragmentsQueue
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
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)