A Discrete-Event Network Simulator
API
test-lte-rlc-header.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011, 2012, 2013 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
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  * Author: Lluis Parcerisa <lparcerisa@cttc.cat> (TestUtils from test-asn1-encoding.cc)
19  * Nicola Baldo <nbaldo@cttc.es> (actual test)
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/test.h"
24 #include "ns3/ptr.h"
25 #include "ns3/packet.h"
26 
27 #include "ns3/lte-rlc-am-header.h"
28 
29 #include <list>
30 #include <bitset>
31 #include <iomanip>
32 
33 
34 NS_LOG_COMPONENT_DEFINE ("TestLteRlcHeader");
35 
36 namespace ns3 {
37 
38 class TestUtils
39 {
40 public:
41  // Function to convert packet contents in hex format
42  static std::string sprintPacketContentsHex (Ptr<Packet> pkt)
43  {
44  uint32_t psize = pkt->GetSize ();
45  uint8_t buffer[psize];
46  std::ostringstream oss (std::ostringstream::out);
47  pkt->CopyData (buffer, psize);
48  for (uint32_t i = 0; i < psize; i++)
49  {
50  oss << std::setfill ('0') << std::setw (2) << std::hex << (uint32_t) buffer[i];
51  }
52  return oss.str ();
53  }
54 
55  // Function to convert packet contents in binary format
56  static std::string sprintPacketContentsBin (Ptr<Packet> pkt)
57  {
58  uint32_t psize = pkt->GetSize ();
59  uint8_t buffer[psize];
60  std::ostringstream oss (std::ostringstream::out);
61  pkt->CopyData (buffer, psize);
62  for (uint32_t i = 0; i < psize; i++)
63  {
64  oss << (std::bitset<8> (buffer[i]));
65  }
66  return std::string (oss.str () + "\n");
67  }
68 
69  // Function to log packet contents
70  static void LogPacketContents (Ptr<Packet> pkt)
71  {
72  NS_LOG_DEBUG ("---- SERIALIZED PACKET CONTENTS (HEX): -------");
75  }
76 
77  template <class T>
78  static void LogPacketInfo (T source,std::string s)
79  {
80  NS_LOG_DEBUG ("--------- " << s.data () << " INFO: -------");
81  std::ostringstream oss (std::ostringstream::out);
82  source.Print (oss);
83  NS_LOG_DEBUG (oss.str ());
84  }
85 };
86 
87 
89 {
90 public:
92  std::list<SequenceNumber10> nackSnList,
93  std::string hex);
94 
95 protected:
96  virtual void DoRun (void);
97 
99  std::list<SequenceNumber10> m_nackSnList;
100  std::string m_hex;
101 
102 };
103 
104 
106  std::list<SequenceNumber10> nackSnList ,
107  std::string hex)
108  : TestCase (hex),
109  m_ackSn (ackSn),
110  m_nackSnList (nackSnList),
111  m_hex (hex)
112 {
113  NS_LOG_FUNCTION (this << hex);
114 }
115 
116 void
117 
119 {
120  NS_LOG_FUNCTION (this);
121 
122  Ptr<Packet> p = Create<Packet> ();
123  LteRlcAmHeader h;
125  h.SetAckSn (m_ackSn);
126  for (std::list<SequenceNumber10>::iterator it = m_nackSnList.begin ();
127  it != m_nackSnList.end ();
128  ++it)
129  {
130  h.PushNack (it->GetValue ());
131  }
132  p->AddHeader (h);
133 
135  std::string hex = TestUtils::sprintPacketContentsHex (p);
136  NS_TEST_ASSERT_MSG_EQ (m_hex, hex, "serialized packet content " << hex << " differs from test vector " << m_hex);
137 
138  LteRlcAmHeader h2;
139  p->RemoveHeader (h2);
140  SequenceNumber10 ackSn = h2.GetAckSn ();
141  NS_TEST_ASSERT_MSG_EQ (ackSn, m_ackSn, "deserialized ACK SN differs from test vector");
142 
143  for (std::list<SequenceNumber10>::iterator it = m_nackSnList.begin ();
144  it != m_nackSnList.end ();
145  ++it)
146  {
147  int nackSn = h2.PopNack ();
148  NS_TEST_ASSERT_MSG_GT (nackSn, -1, "not enough elements in deserialized NACK list");
149  NS_TEST_ASSERT_MSG_EQ (nackSn, it->GetValue (), "deserialized NACK SN differs from test vector");
150  }
151  int retVal = h2.PopNack ();
152  NS_TEST_ASSERT_MSG_LT (retVal, 0, "too many elements in deserialized NACK list");
153 }
154 
155 
157 {
158 public:
161 
163  : TestSuite ("lte-rlc-header", UNIT)
164 {
165  NS_LOG_FUNCTION (this);
166 
167  {
168  SequenceNumber10 ackSn (8);
169  std::list<SequenceNumber10> nackSnList;
170  std::string hex ("0020");
171  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
172  }
173 
174  {
175  SequenceNumber10 ackSn (873);
176  std::list<SequenceNumber10> nackSnList;
177  std::string hex ("0da4");
178  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
179  }
180 
181  {
182  SequenceNumber10 ackSn (2);
183  std::list<SequenceNumber10> nackSnList;
184  nackSnList.push_back (SequenceNumber10 (873));
185  std::string hex ("000bb480");
186  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
187  }
188 
189 
190  {
191  SequenceNumber10 ackSn (2);
192  std::list<SequenceNumber10> nackSnList;
193  nackSnList.push_back (SequenceNumber10 (1021));
194  nackSnList.push_back (SequenceNumber10 (754));
195  std::string hex ("000bfed790");
196  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
197  }
198 
199 
200  {
201  SequenceNumber10 ackSn (2);
202  std::list<SequenceNumber10> nackSnList;
203  nackSnList.push_back (SequenceNumber10 (1021));
204  nackSnList.push_back (SequenceNumber10 (754));
205  nackSnList.push_back (SequenceNumber10 (947));
206  std::string hex ("000bfed795d980");
207  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
208  }
209 
210 
211  {
212  SequenceNumber10 ackSn (2);
213  std::list<SequenceNumber10> nackSnList;
214  nackSnList.push_back (SequenceNumber10 (1021));
215  nackSnList.push_back (SequenceNumber10 (754));
216  nackSnList.push_back (SequenceNumber10 (947));
217  nackSnList.push_back (SequenceNumber10 (347));
218  std::string hex ("000bfed795d9cad8");
219  AddTestCase (new RlcAmStatusPduTestCase (ackSn, nackSnList, hex), TestCase::QUICK);
220  }
221 
222 }
223 
224 
225 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
static void LogPacketContents(Ptr< Packet > pkt)
static void LogPacketInfo(T source, std::string s)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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
static std::string sprintPacketContentsHex(Ptr< Packet > pkt)
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:161
void SetControlPdu(uint8_t controlPduType)
The packet header for the AM Radio Link Control (RLC) protocol packets.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
RlcAmStatusPduTestCase(SequenceNumber10 ackSn, std::list< SequenceNumber10 > nackSnList, std::string hex)
ns3::LteRlcHeaderTestSuite staticLteRlcHeaderTestSuiteInstance
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:356
int PopNack(void)
Retrieve one NACK from the CONTROL PDU.
virtual void DoRun(void)
Implementation to actually run this TestCase.
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition: test.h:990
static std::string sprintPacketContentsBin(Ptr< Packet > pkt)
std::list< SequenceNumber10 > m_nackSnList
#define NS_TEST_ASSERT_MSG_LT(actual, limit, msg)
Test that an actual value is less than a limit and report and abort if not.
Definition: test.h:804
SequenceNumber10 GetAckSn() const
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257