A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("lr-wpan-packet-test");
30 
31 using namespace ns3;
32 
33 // This is an example TestCase.
35 {
36 public:
38  virtual ~LrWpanPacketTestCase ();
39 
40 private:
41  virtual void DoRun (void);
42 };
43 
45  : TestCase ("Test the 802.15.4 MAC header and trailer classes")
46 {
47 }
48 
50 {
51 }
52 
53 void
55 {
56 
57  LrWpanMacHeader macHdr (LrWpanMacHeader::LRWPAN_MAC_BEACON, 0); //sequence number set to 0
58  macHdr.SetSrcAddrMode (LrWpanMacHeader::SHORTADDR); // short addr
59  macHdr.SetDstAddrMode (LrWpanMacHeader::NOADDR);
60  macHdr.SetSecDisable ();
61  macHdr.SetNoPanIdComp ();
62  // ... other setters
63 
64  uint16_t srcPanId = 100;
65  Mac16Address srcWpanAddr ("00:11");
66  macHdr.SetSrcAddrFields (srcPanId, srcWpanAddr);
67 
68  LrWpanMacTrailer macTrailer;
69 
70 
71  Ptr<Packet> p = Create<Packet> (20); // 20 bytes of dummy data
72  NS_TEST_ASSERT_MSG_EQ (p->GetSize (), 20, "Packet created with unexpected size");
73  p->AddHeader (macHdr);
74  std::cout << " <--Mac Header added " << std::endl;
75 
76  NS_TEST_ASSERT_MSG_EQ (p->GetSize (), 27, "Packet wrong size after macHdr addition");
77  p->AddTrailer (macTrailer);
78  NS_TEST_ASSERT_MSG_EQ (p->GetSize (), 29, "Packet wrong size after macTrailer addition");
79 
80  // Test serialization and deserialization
81  uint32_t size = p->GetSerializedSize ();
82  uint8_t buffer[size];
83  p->Serialize (buffer, size);
84  Ptr<Packet> p2 = Create<Packet> (buffer, size, true);
85 
86 
87  p2->Print (std::cout);
88  std::cout << " <--Packet P2 " << std::endl;
89 
90  NS_TEST_ASSERT_MSG_EQ (p2->GetSize (), 29, "Packet wrong size after deserialization");
91 
92  LrWpanMacHeader receivedMacHdr;
93  p2->RemoveHeader (receivedMacHdr);
94 
95  receivedMacHdr.Print (std::cout);
96  std::cout << " <--P2 Mac Header " << std::endl;
97 
98  NS_TEST_ASSERT_MSG_EQ (p2->GetSize (), 22, "Packet wrong size after removing machdr");
99 
100  LrWpanMacTrailer receivedMacTrailer;
101  p2->RemoveTrailer (receivedMacTrailer);
102  NS_TEST_ASSERT_MSG_EQ (p2->GetSize (), 20, "Packet wrong size after removing headers and trailers");
103  // Compare macHdr with receivedMacHdr, macTrailer with receivedMacTrailer,...
104 
105 }
106 
107 // ==============================================================================
109 {
110 public:
112 };
113 
115  : TestSuite ("lr-wpan-packet", UNIT)
116 {
117  AddTestCase (new LrWpanPacketTestCase, TestCase::QUICK);
118 }
119 
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:268
A suite of tests to run.
Definition: test.h:1105
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
Definition: packet.cc:608
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
Doxygen introspection did not find any typical Config paths.
encapsulates test code
Definition: test.h:929
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:429
void SetSrcAddrFields(uint16_t panId, Mac16Address addr)
static LrWpanPacketTestSuite lrWpanPacketTestSuite
#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:148
void SetDstAddrMode(uint8_t addrMode)
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Definition: packet.cc:284
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
Definition: packet.cc:300
This class can contain 16 bit addresses.
Definition: mac16-address.h:39
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
Represent the Mac Trailer with the Frame Check Sequence field.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:565
void SetSrcAddrMode(uint8_t addrMode)
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253