A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
29using namespace ns3;
30using namespace ns3::lrwpan;
31
32NS_LOG_COMPONENT_DEFINE("lr-wpan-packet-test");
33
34/**
35 * \ingroup lr-wpan-test
36 * \ingroup tests
37 *
38 * \brief LrWpan header and trailer Test
39 */
41{
42 public:
44 ~LrWpanPacketTestCase() override;
45
46 private:
47 void DoRun() override;
48};
49
51 : TestCase("Test the 802.15.4 MAC header and trailer classes")
52{
53}
54
56{
57}
58
59void
61{
62 LrWpanMacHeader macHdr(LrWpanMacHeader::LRWPAN_MAC_BEACON, 0); // sequence number set to 0
63 macHdr.SetSrcAddrMode(LrWpanMacHeader::SHORTADDR); // short addr
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 Ptr<Packet> p = Create<Packet>(20); // 20 bytes of dummy data
76 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 20, "Packet created with unexpected size");
77 p->AddHeader(macHdr);
78 std::cout << " <--Mac Header added " << std::endl;
79
80 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 27, "Packet wrong size after macHdr addition");
81 p->AddTrailer(macTrailer);
82 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 29, "Packet wrong size after macTrailer addition");
83
84 // Test serialization and deserialization
85 uint32_t size = p->GetSerializedSize();
86 std::vector<uint8_t> buffer(size);
87 p->Serialize(buffer.data(), size);
88 Ptr<Packet> p2 = Create<Packet>(buffer.data(), size, true);
89
90 p2->Print(std::cout);
91 std::cout << " <--Packet P2 " << std::endl;
92
93 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 29, "Packet wrong size after deserialization");
94
95 LrWpanMacHeader receivedMacHdr;
96 p2->RemoveHeader(receivedMacHdr);
97
98 receivedMacHdr.Print(std::cout);
99 std::cout << " <--P2 Mac Header " << std::endl;
100
101 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 22, "Packet wrong size after removing machdr");
102
103 LrWpanMacTrailer receivedMacTrailer;
104 p2->RemoveTrailer(receivedMacTrailer);
105 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(),
106 20,
107 "Packet wrong size after removing headers and trailers");
108 // Compare macHdr with receivedMacHdr, macTrailer with receivedMacTrailer,...
109}
110
111/**
112 * \ingroup lr-wpan-test
113 * \ingroup tests
114 *
115 * \brief LrWpan header and trailer TestSuite
116 */
118{
119 public:
121};
122
124 : TestSuite("lr-wpan-packet", Type::UNIT)
125{
126 AddTestCase(new LrWpanPacketTestCase, TestCase::Duration::QUICK);
127}
128
129static LrWpanPacketTestSuite g_lrWpanPacketTestSuite; //!< Static variable for test initialization
LrWpan header and trailer Test.
void DoRun() override
Implementation to actually run this TestCase.
LrWpan header and trailer TestSuite.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
Represent the Mac Header with the Frame Control and Sequence Number fields.
void SetDstAddrMode(uint8_t addrMode)
Set the Destination address mode.
void Print(std::ostream &os) const override
@ LRWPAN_MAC_BEACON
LRWPAN_MAC_BEACON.
void SetSrcAddrMode(uint8_t addrMode)
Set the Source address mode.
void SetSrcAddrFields(uint16_t panId, Mac16Address addr)
Set Source address fields.
void SetNoPanIdComp()
Set the Frame Control field "PAN ID Compression" bit to false.
void SetSecDisable()
Set the Frame Control field "Security Enabled" bit to false.
Represent the Mac Trailer with the Frame Check Sequence field.
#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:145
static LrWpanPacketTestSuite g_lrWpanPacketTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.