A Discrete-Event Network Simulator
API
test-lte-rlc-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2012, 2013 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
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  * Author: Lluis Parcerisa <lparcerisa@cttc.cat> (TestUtils from test-asn1-encoding.cc)
18  * Nicola Baldo <nbaldo@cttc.es> (actual test)
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/lte-rlc-am-header.h"
23 #include "ns3/packet.h"
24 #include "ns3/ptr.h"
25 #include "ns3/test.h"
26 
27 #include <bitset>
28 #include <iomanip>
29 #include <list>
30 
31 NS_LOG_COMPONENT_DEFINE("TestLteRlcHeader");
32 
33 namespace ns3
34 {
35 
41 class TestUtils
42 {
43  public:
49  static std::string sprintPacketContentsHex(Ptr<Packet> pkt)
50  {
51  uint32_t psize = pkt->GetSize();
52  uint8_t buffer[psize];
53  std::ostringstream oss(std::ostringstream::out);
54  pkt->CopyData(buffer, psize);
55  for (uint32_t i = 0; i < psize; i++)
56  {
57  oss << std::setfill('0') << std::setw(2) << std::hex << (uint32_t)buffer[i];
58  }
59  return oss.str();
60  }
61 
67  static std::string sprintPacketContentsBin(Ptr<Packet> pkt)
68  {
69  uint32_t psize = pkt->GetSize();
70  uint8_t buffer[psize];
71  std::ostringstream oss(std::ostringstream::out);
72  pkt->CopyData(buffer, psize);
73  for (uint32_t i = 0; i < psize; i++)
74  {
75  oss << (std::bitset<8>(buffer[i]));
76  }
77  return std::string(oss.str() + "\n");
78  }
79 
84  static void LogPacketContents(Ptr<Packet> pkt)
85  {
86  NS_LOG_DEBUG("---- SERIALIZED PACKET CONTENTS (HEX): -------");
89  }
90 
96  template <class T>
97  static void LogPacketInfo(T source, std::string s)
98  {
99  NS_LOG_DEBUG("--------- " << s.data() << " INFO: -------");
100  std::ostringstream oss(std::ostringstream::out);
101  source.Print(oss);
102  NS_LOG_DEBUG(oss.str());
103  }
104 };
105 
112 {
113  public:
122  std::list<SequenceNumber10> nackSnList,
123  std::string hex);
124 
125  protected:
126  void DoRun() override;
127 
129  std::list<SequenceNumber10> m_nackSnList;
130  std::string m_hex;
131 };
132 
134  std::list<SequenceNumber10> nackSnList,
135  std::string hex)
136  : TestCase(hex),
137  m_ackSn(ackSn),
138  m_nackSnList(nackSnList),
139  m_hex(hex)
140 {
141  NS_LOG_FUNCTION(this << hex);
142 }
143 
144 void
145 
147 {
148  NS_LOG_FUNCTION(this);
149 
150  Ptr<Packet> p = Create<Packet>();
151  LteRlcAmHeader h;
153  h.SetAckSn(m_ackSn);
154  for (auto it = m_nackSnList.begin(); it != m_nackSnList.end(); ++it)
155  {
156  h.PushNack(it->GetValue());
157  }
158  p->AddHeader(h);
159 
161  std::string hex = TestUtils::sprintPacketContentsHex(p);
163  hex,
164  "serialized packet content " << hex << " differs from test vector "
165  << m_hex);
166 
167  LteRlcAmHeader h2;
168  p->RemoveHeader(h2);
169  SequenceNumber10 ackSn = h2.GetAckSn();
170  NS_TEST_ASSERT_MSG_EQ(ackSn, m_ackSn, "deserialized ACK SN differs from test vector");
171 
172  for (auto it = m_nackSnList.begin(); it != m_nackSnList.end(); ++it)
173  {
174  int nackSn = h2.PopNack();
175  NS_TEST_ASSERT_MSG_GT(nackSn, -1, "not enough elements in deserialized NACK list");
176  NS_TEST_ASSERT_MSG_EQ(nackSn,
177  it->GetValue(),
178  "deserialized NACK SN differs from test vector");
179  }
180  int retVal = h2.PopNack();
181  NS_TEST_ASSERT_MSG_LT(retVal, 0, "too many elements in deserialized NACK list");
182 }
183 
190 {
191  public:
194 
196  : TestSuite("lte-rlc-header", UNIT)
197 {
198  NS_LOG_FUNCTION(this);
199 
200  {
201  SequenceNumber10 ackSn(8);
202  std::list<SequenceNumber10> nackSnList;
203  std::string hex("0020");
204  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
205  }
206 
207  {
208  SequenceNumber10 ackSn(873);
209  std::list<SequenceNumber10> nackSnList;
210  std::string hex("0da4");
211  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
212  }
213 
214  {
215  SequenceNumber10 ackSn(2);
216  const std::list<SequenceNumber10> nackSnList{
217  SequenceNumber10(873),
218  };
219  std::string hex("000bb480");
220  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
221  }
222 
223  {
224  SequenceNumber10 ackSn(2);
225  const std::list<SequenceNumber10> nackSnList{
226  SequenceNumber10(1021),
227  SequenceNumber10(754),
228  };
229  std::string hex("000bfed790");
230  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
231  }
232 
233  {
234  SequenceNumber10 ackSn(2);
235  const std::list<SequenceNumber10> nackSnList{
236  SequenceNumber10(1021),
237  SequenceNumber10(754),
238  SequenceNumber10(947),
239  };
240  std::string hex("000bfed795d980");
241  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
242  }
243 
244  {
245  SequenceNumber10 ackSn(2);
246  const std::list<SequenceNumber10> nackSnList{
247  SequenceNumber10(1021),
248  SequenceNumber10(754),
249  SequenceNumber10(947),
250  SequenceNumber10(347),
251  };
252  std::string hex("000bfed795d9cad8");
253  AddTestCase(new RlcAmStatusPduTestCase(ackSn, nackSnList, hex), TestCase::QUICK);
254  }
255 }
256 
257 } // namespace ns3
The packet header for the AM Radio Link Control (RLC) protocol packets.
SequenceNumber10 GetAckSn() const
Get ack sn function.
void PushNack(int nack)
Add one more NACK to the CONTROL PDU.
static constexpr uint8_t STATUS_PDU
Control PDU type status.
void SetAckSn(SequenceNumber10 ackSn)
Set ack sn function.
int PopNack()
Retrieve one NACK from the CONTROL PDU.
void SetControlPdu(uint8_t controlPduType)
Set control PDU function.
Lte Rlc Header Test Suite.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:400
Rlc Am Status Pdu Test Case.
std::list< SequenceNumber10 > m_nackSnList
list of nack sequence numbers
void DoRun() override
Implementation to actually run this TestCase.
RlcAmStatusPduTestCase(SequenceNumber10 ackSn, std::list< SequenceNumber10 > nackSnList, std::string hex)
Constructor.
SequenceNumber10 m_ackSn
ack sequence number
SequenceNumber10 class.
encapsulates test code
Definition: test.h:1060
@ QUICK
Fast test.
Definition: test.h:1065
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
static std::string sprintPacketContentsBin(Ptr< Packet > pkt)
Function to convert packet contents in binary format.
static std::string sprintPacketContentsHex(Ptr< Packet > pkt)
Function to convert packet contents in hex format.
static void LogPacketInfo(T source, std::string s)
Log packet info function.
static void LogPacketContents(Ptr< Packet > pkt)
Function to log packet contents.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
ns3::LteRlcHeaderTestSuite staticLteRlcHeaderTestSuiteInstance
the test suite
#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:709
#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:144
#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:874
Every class exported by the ns3 library is enclosed in the ns3 namespace.