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