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