A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
lr-wpan-packet-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 The Boeing Company
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: Tom Henderson <thomas.r.henderson@boeing.com>
18  */
19 #include <ns3/log.h>
20 #include <ns3/lr-wpan-mac-header.h>
21 #include <ns3/lr-wpan-mac-trailer.h>
22 #include <ns3/mac16-address.h>
23 #include <ns3/mac64-address.h>
24 #include <ns3/packet.h>
25 #include <ns3/test.h>
26 
27 #include <vector>
28 
29 using namespace ns3;
30 
31 NS_LOG_COMPONENT_DEFINE("lr-wpan-packet-test");
32 
40 {
41  public:
43  ~LrWpanPacketTestCase() override;
44 
45  private:
46  void DoRun() override;
47 };
48 
50  : TestCase("Test the 802.15.4 MAC header and trailer classes")
51 {
52 }
53 
55 {
56 }
57 
58 void
60 {
61  LrWpanMacHeader macHdr(LrWpanMacHeader::LRWPAN_MAC_BEACON, 0); // sequence number set to 0
62  macHdr.SetSrcAddrMode(LrWpanMacHeader::SHORTADDR); // short addr
63  macHdr.SetDstAddrMode(LrWpanMacHeader::NOADDR);
64  macHdr.SetSecDisable();
65  macHdr.SetNoPanIdComp();
66  // ... other setters
67 
68  uint16_t srcPanId = 100;
69  Mac16Address srcWpanAddr("00:11");
70  macHdr.SetSrcAddrFields(srcPanId, srcWpanAddr);
71 
72  LrWpanMacTrailer macTrailer;
73 
74  Ptr<Packet> p = Create<Packet>(20); // 20 bytes of dummy data
75  NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 20, "Packet created with unexpected size");
76  p->AddHeader(macHdr);
77  std::cout << " <--Mac Header added " << std::endl;
78 
79  NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 27, "Packet wrong size after macHdr addition");
80  p->AddTrailer(macTrailer);
81  NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 29, "Packet wrong size after macTrailer addition");
82 
83  // Test serialization and deserialization
84  uint32_t size = p->GetSerializedSize();
85  std::vector<uint8_t> buffer(size);
86  p->Serialize(buffer.data(), size);
87  Ptr<Packet> p2 = Create<Packet>(buffer.data(), size, true);
88 
89  p2->Print(std::cout);
90  std::cout << " <--Packet P2 " << std::endl;
91 
92  NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 29, "Packet wrong size after deserialization");
93 
94  LrWpanMacHeader receivedMacHdr;
95  p2->RemoveHeader(receivedMacHdr);
96 
97  receivedMacHdr.Print(std::cout);
98  std::cout << " <--P2 Mac Header " << std::endl;
99 
100  NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 22, "Packet wrong size after removing machdr");
101 
102  LrWpanMacTrailer receivedMacTrailer;
103  p2->RemoveTrailer(receivedMacTrailer);
105  20,
106  "Packet wrong size after removing headers and trailers");
107  // Compare macHdr with receivedMacHdr, macTrailer with receivedMacTrailer,...
108 }
109 
117 {
118  public:
120 };
121 
123  : TestSuite("lr-wpan-packet", UNIT)
124 {
125  AddTestCase(new LrWpanPacketTestCase, TestCase::QUICK);
126 }
127 
LrWpan header and trailer Test.
void DoRun() override
Implementation to actually run this TestCase.
LrWpan header and trailer TestSuite.
Represent the Mac Header with the Frame Control and Sequence Number fields.
void SetNoPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to false.
void SetSrcAddrMode(uint8_t addrMode)
Set the Source address mode.
void SetSecDisable()
Set the Frame Control field "Security Enabled" bit to false.
void SetSrcAddrFields(uint16_t panId, Mac16Address addr)
Set Source address fields.
void Print(std::ostream &os) const override
void SetDstAddrMode(uint8_t addrMode)
Set the Destination address mode.
Represent the Mac Trailer with the Frame Check Sequence field.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:336
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 GetSerializedSize() const
Returns number of bytes required for packet serialization.
Definition: packet.cc:610
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:663
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:456
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:324
encapsulates test code
Definition: test.h:1060
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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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
static LrWpanPacketTestSuite g_lrWpanPacketTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.