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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tom Henderson <thomas.r.henderson@boeing.com>
7 */
8#include <ns3/log.h>
9#include <ns3/lr-wpan-mac-header.h>
10#include <ns3/lr-wpan-mac-trailer.h>
11#include <ns3/mac16-address.h>
12#include <ns3/mac64-address.h>
13#include <ns3/packet.h>
14#include <ns3/test.h>
15
16#include <vector>
17
18using namespace ns3;
19using namespace ns3::lrwpan;
20
21NS_LOG_COMPONENT_DEFINE("lr-wpan-packet-test");
22
23/**
24 * @ingroup lr-wpan-test
25 * @ingroup tests
26 *
27 * @brief LrWpan header and trailer Test
28 */
30{
31 public:
33 ~LrWpanPacketTestCase() override;
34
35 private:
36 void DoRun() override;
37};
38
40 : TestCase("Test the 802.15.4 MAC header and trailer classes")
41{
42}
43
47
48void
50{
51 LrWpanMacHeader macHdr(LrWpanMacHeader::LRWPAN_MAC_BEACON, 0); // sequence number set to 0
52 macHdr.SetSrcAddrMode(LrWpanMacHeader::SHORTADDR); // short addr
54 macHdr.SetSecDisable();
55 macHdr.SetNoPanIdComp();
56 // ... other setters
57
58 uint16_t srcPanId = 100;
59 Mac16Address srcWpanAddr("00:11");
60 macHdr.SetSrcAddrFields(srcPanId, srcWpanAddr);
61
62 LrWpanMacTrailer macTrailer;
63
64 Ptr<Packet> p = Create<Packet>(20); // 20 bytes of dummy data
65 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 20, "Packet created with unexpected size");
66 p->AddHeader(macHdr);
67 std::cout << " <--Mac Header added " << std::endl;
68
69 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 27, "Packet wrong size after macHdr addition");
70 p->AddTrailer(macTrailer);
71 NS_TEST_ASSERT_MSG_EQ(p->GetSize(), 29, "Packet wrong size after macTrailer addition");
72
73 // Test serialization and deserialization
74 uint32_t size = p->GetSerializedSize();
75 std::vector<uint8_t> buffer(size);
76 p->Serialize(buffer.data(), size);
77 Ptr<Packet> p2 = Create<Packet>(buffer.data(), size, true);
78
79 p2->Print(std::cout);
80 std::cout << " <--Packet P2 " << std::endl;
81
82 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 29, "Packet wrong size after deserialization");
83
84 LrWpanMacHeader receivedMacHdr;
85 p2->RemoveHeader(receivedMacHdr);
86
87 receivedMacHdr.Print(std::cout);
88 std::cout << " <--P2 Mac Header " << std::endl;
89
90 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(), 22, "Packet wrong size after removing machdr");
91
92 LrWpanMacTrailer receivedMacTrailer;
93 p2->RemoveTrailer(receivedMacTrailer);
94 NS_TEST_ASSERT_MSG_EQ(p2->GetSize(),
95 20,
96 "Packet wrong size after removing headers and trailers");
97 // Compare macHdr with receivedMacHdr, macTrailer with receivedMacTrailer,...
98}
99
100/**
101 * @ingroup lr-wpan-test
102 * @ingroup tests
103 *
104 * @brief LrWpan header and trailer TestSuite
105 */
107{
108 public:
110};
111
113 : TestSuite("lr-wpan-packet", Type::UNIT)
114{
115 AddTestCase(new LrWpanPacketTestCase, TestCase::Duration::QUICK);
116}
117
118static 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.
Smart pointer class similar to boost::intrusive_ptr.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
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:191
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
static LrWpanPacketTestSuite g_lrWpanPacketTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.