A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-nd-packet-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Boh Jie Qi <jieqiboh5836@gmail.com>
7 */
8
9#include "ns3/boolean.h"
10#include "ns3/inet6-socket-address.h"
11#include "ns3/internet-stack-helper.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/simple-channel.h"
15#include "ns3/simple-net-device.h"
16#include "ns3/simulator.h"
17#include "ns3/sixlowpan-header.h"
18#include "ns3/sixlowpan-nd-prefix.h"
19#include "ns3/sixlowpan-nd-protocol.h"
20#include "ns3/sixlowpan-net-device.h"
21#include "ns3/socket-factory.h"
22#include "ns3/socket.h"
23#include "ns3/test.h"
24#include "ns3/udp-socket-factory.h"
25
26#include <limits>
27#include <string>
28
29namespace ns3
30{
31/**
32 * @ingroup sixlowpan-nd-packet-tests
33 *
34 * @brief 6LoWPAN-ND test case for NS(EARO) packet creation and parsing
35 */
37{
38 public:
40 : TestCase("Make and Parse NS(EARO) Packet")
41 {
42 }
43
44 void DoRun() override
45 {
46 Ipv6Address src("fe80::1");
47 Ipv6Address dst("fe80::2");
48
49 Icmpv6NS nsHdr(src);
50 Mac48Address mac("00:11:22:33:44:55");
51 Icmpv6OptionLinkLayerAddress slla(true, mac);
52 Icmpv6OptionLinkLayerAddress tlla(false, mac);
53 std::vector<uint8_t> rovr(16, 0xAB);
55
56 Ptr<Packet> p = SixLowPanNdProtocol::MakeNsEaroPacket(src, dst, nsHdr, slla, tlla, earo);
57
58 Icmpv6NS parsedNs;
59 Icmpv6OptionLinkLayerAddress parsedSlla(true);
60 Icmpv6OptionLinkLayerAddress parsedTlla(false);
62 bool hasEaro = false;
63
65 parsedNs,
66 parsedSlla,
67 parsedTlla,
68 parsedEaro,
69 hasEaro);
70
71 NS_TEST_EXPECT_MSG_EQ(result, true, "NS(EARO) should be parsed successfully");
72 // Validate parsed NS header
73 NS_TEST_EXPECT_MSG_EQ(parsedNs.GetIpv6Target(), src, "Target address in NS should match");
74
75 // Validate parsed SLLAO
76 NS_TEST_EXPECT_MSG_EQ(parsedSlla.GetType(),
78 "SLLAO type should be source");
80 parsedTlla.GetAddress(),
81 "SLLAO MAC should match TLLAO MAC");
82
83 // Validate parsed TLLAO
84 NS_TEST_EXPECT_MSG_EQ(parsedTlla.GetType(),
86 "TLLAO type should be target");
87
88 // Validate parsed EARO
89 NS_TEST_EXPECT_MSG_EQ(parsedEaro.GetRegTime(), 20, "EARO lifetime should match");
90 NS_TEST_EXPECT_MSG_EQ(parsedEaro.GetTransactionId(), 5, "Transaction ID should match");
91
92 NS_TEST_EXPECT_MSG_EQ(hasEaro, 1, "hasEaro should be true");
93
94 Ipv6Address target("fe80::1");
95
96 // Create a bare NS (no options)
97 p = Create<Packet>();
98 p->AddHeader(nsHdr);
99
100 // Run parser
102 parsedNs,
103 parsedSlla,
104 parsedTlla,
105 parsedEaro,
106 hasEaro);
107
108 // Expectations
109 NS_TEST_EXPECT_MSG_EQ(result, true, "Bare NS should be parsed successfully");
110 NS_TEST_EXPECT_MSG_EQ(parsedNs.GetIpv6Target(), target, "NS target should match");
111 NS_TEST_EXPECT_MSG_EQ(hasEaro, 0, "hasEaro should be false");
112 }
113};
114
115/**
116 * @ingroup sixlowpan-nd-packet-tests
117 *
118 * @brief 6LoWPAN-ND test case for NA(EARO) packet creation and parsing
119 */
121{
122 public:
124 : TestCase("Make and Parse NA(EARO) Packet")
125 {
126 }
127
128 void DoRun() override
129 {
130 Ipv6Address src("fe80::1");
131 Ipv6Address dst("fe80::2");
132 Ipv6Address target("fe80::ff:fe00:2");
133 Icmpv6NA naHdr;
134 naHdr.SetIpv6Target(target);
135 naHdr.SetFlagR(true);
136 naHdr.SetFlagS(true);
137 naHdr.SetFlagO(false);
138 std::vector<uint8_t> rovr(16, 0xCD);
140
141 Ptr<Packet> p = SixLowPanNdProtocol::MakeNaEaroPacket(src, dst, naHdr, earo);
142
143 Icmpv6NA parsedNa;
144 Icmpv6OptionLinkLayerAddress parsedTlla(false);
146 bool hasEaro = false;
148 parsedNa,
149 parsedTlla,
150 parsedEaro,
151 hasEaro);
152
153 NS_TEST_EXPECT_MSG_EQ(result, true, "NA(EARO) should be parsed successfully");
154
155 // Validate parsed NA header
157 target,
158 "Target address in NA should match");
159 NS_TEST_EXPECT_MSG_EQ(parsedNa.GetFlagR(), true, "Solicited flag should match");
160 NS_TEST_EXPECT_MSG_EQ(parsedNa.GetFlagS(), true, "Solicited flag should match");
161 NS_TEST_EXPECT_MSG_EQ(parsedNa.GetFlagO(), false, "Override flag should match");
162
163 // Validate parsed EARO option
164 NS_TEST_EXPECT_MSG_EQ(parsedEaro.GetStatus(), 0, "Status should match");
165 NS_TEST_EXPECT_MSG_EQ(parsedEaro.GetRegTime(), 20, "EARO lifetime should match");
166 NS_TEST_EXPECT_MSG_EQ(parsedEaro.GetTransactionId(), 7, "Transaction ID should match");
167
168 NS_TEST_EXPECT_MSG_EQ(hasEaro, 1, "hasEaro should be true");
169 }
170};
171
172/**
173 * @ingroup sixlowpan-nd-packet-tests
174 *
175 * @brief 6LoWPAN-ND test case for RA packet creation and parsing
176 */
178{
179 public:
181 : TestCase("Make and Parse RA Packet")
182 {
183 }
184
185 void DoRun() override
186 {
187 Ipv6Address src("fe80::1");
188 Ipv6Address dst("fe80::2");
189
192 raEntry->SetManagedFlag(false);
193 raEntry->SetHomeAgentFlag(false);
194 raEntry->SetOtherConfigFlag(false);
195 raEntry->SetOtherConfigFlag(false);
196 raEntry->SetCurHopLimit(0); // unspecified by this router
197 raEntry->SetRetransTimer(0); // unspecified by this router
198 raEntry->SetReachableTime(0); // unspecified by this router
199 raEntry->SetRouterLifeTime(60);
200 raEntry->SetAbroBorderRouterAddress("2001::ff:fe00:1");
201 raEntry->SetAbroVersion(0x66);
202 raEntry->SetAbroValidLifeTime(600);
203 Ipv6Prefix prefix = Ipv6Prefix("2001::", 64);
205 prefix.GetPrefixLength(),
206 Time("10min"),
207 Time("10min"));
208 raEntry->AddPrefix(newPrefix);
209
210 Icmpv6OptionLinkLayerAddress slla(true, Mac48Address("00:11:22:33:44:55"));
211
215
216 Ptr<Packet> p = SixLowPanNdProtocol::MakeRaPacket(src, dst, slla, cio, raEntry);
217
218 Icmpv6RA ra;
220 Icmpv6OptionLinkLayerAddress parsedSlla(true);
222 std::list<Icmpv6OptionPrefixInformation> pios;
223 std::list<Icmpv6OptionSixLowPanContext> contexts;
224
226 ra,
227 pios,
228 abro,
229 parsedSlla,
230 parsedCio,
231 contexts);
232 NS_TEST_EXPECT_MSG_EQ(result, true, "RA packet should be parsed successfully");
233 NS_TEST_EXPECT_MSG_EQ(abro.GetVersion(), 0x66, "ABRO version should match");
234 // Validate specific bits
236 true,
237 "E bit should be set");
239 true,
240 "B bit should be set");
242 false,
243 "L bit should not be set");
244 }
245};
246
247/**
248 * @ingroup sixlowpan-nd-packet-tests
249 *
250 * @brief 6LoWPAN-ND test case for RS packet creation and parsing
251 */
253{
254 public:
256 : TestCase("Make and Parse RS Packet")
257 {
258 }
259
260 void DoRun() override
261 {
262 Ipv6Address src("fe80::1");
263 Ipv6Address dst("ff02::2");
264 Icmpv6RS rs;
265 Icmpv6OptionLinkLayerAddress slla(true, Mac48Address("00:11:22:33:44:55"));
269
271 p->AddHeader(cio);
272 p->AddHeader(slla);
273 p->AddHeader(rs);
274
275 Icmpv6RS parsedRs;
276 Icmpv6OptionLinkLayerAddress parsedSlla(true);
278
279 bool result =
280 SixLowPanNdProtocol::ParseAndValidateRsPacket(p, parsedRs, parsedSlla, parsedCio);
281 NS_TEST_EXPECT_MSG_EQ(result, true, "RS packet should be parsed successfully");
282
283 // Validate parsed SLLAO
284 NS_TEST_EXPECT_MSG_EQ(parsedSlla.GetType(),
286 "SLLAO type should be source");
287
288 // Validate specific bits
290 true,
291 "E bit should be set");
293 true,
294 "B bit should be set");
296 false,
297 "L bit should not be set");
298 }
299};
300
301/**
302 * @ingroup sixlowpan-nd-packet-tests
303 *
304 * @brief 6LoWPAN-ND TestSuite
305 */
318
319static SixlowpanNdTestSuite g_sixlowpanNdTestSuite; //!< Static variable for test initialization
320} // namespace ns3
return result
ICMPv6 Neighbor Advertisement header.
bool GetFlagS() const
Get the S flag.
void SetFlagS(bool s)
Set the S flag.
void SetIpv6Target(Ipv6Address target)
Set the IPv6 target field.
void SetFlagR(bool r)
Set the R flag.
Ipv6Address GetIpv6Target() const
Get the IPv6 target field.
bool GetFlagR() const
Get the R flag.
bool GetFlagO() const
Get the O flag.
void SetFlagO(bool o)
Set the O flag.
ICMPv6 Neighbor Solicitation header.
Ipv6Address GetIpv6Target() const
Get the IPv6 target field.
uint8_t GetType() const
Get the type of the option.
ICMPv6 Authoritative Border Router Option header (see RFC 8505).
uint32_t GetVersion() const
Get the version field.
6LoWPAN Capability Indication Option - see RFC 7400.
void SetOption(SixLowPanCapability_e option)
Set an option.
bool CheckOption(SixLowPanCapability_e option) const
Checks an option.
@ B
The node is a 6LBR (see RFC 8505).
@ E
The node is an IPv6 ND Registrar (see RFC 8505).
ICMPv6 Extended Address Registration Option header RFC 8505.
uint8_t GetTransactionId() const
Get the transaction ID field.
uint16_t GetRegTime() const
Get the registration lifetime field.
ICMPv6 Router Advertisement header.
ICMPv6 Router Solicitation header.
Describes an IPv6 address.
Describes an IPv6 prefix.
uint8_t GetPrefixLength() const
Get prefix length.
Ipv6Address ConvertToIpv6Address() const
Convert the Prefix into an IPv6 Address.
an EUI-48 address
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
6LoWPAN-ND test case for NA(EARO) packet creation and parsing
void DoRun() override
Implementation to actually run this TestCase.
6LoWPAN-ND test case for NS(EARO) packet creation and parsing
void DoRun() override
Implementation to actually run this TestCase.
static Ptr< Packet > MakeRaPacket(Ipv6Address src, Ipv6Address dst, Icmpv6OptionLinkLayerAddress &slla, Icmpv6OptionSixLowPanCapabilityIndication &cio, Ptr< SixLowPanRaEntry > raEntry)
Constructs a RA packet (raEntry contains info for raHdr, pios, abro and contexts).
static bool ParseAndValidateRaPacket(Ptr< Packet > p, Icmpv6RA &raHdr, std::list< Icmpv6OptionPrefixInformation > &pios, Icmpv6OptionSixLowPanAuthoritativeBorderRouter &abro, Icmpv6OptionLinkLayerAddress &slla, Icmpv6OptionSixLowPanCapabilityIndication &cio, std::list< Icmpv6OptionSixLowPanContext > &contexts)
Parses RA packet and populates params, returning true if packet is valid.
static Ptr< Packet > MakeNaEaroPacket(Ipv6Address src, Ipv6Address dst, Icmpv6NA &naHdr, Icmpv6OptionSixLowPanExtendedAddressRegistration &earo)
Construct NA (EARO) packet.
static bool ParseAndValidateNsEaroPacket(Ptr< Packet > p, Icmpv6NS &nsHdr, Icmpv6OptionLinkLayerAddress &slla, Icmpv6OptionLinkLayerAddress &tlla, Icmpv6OptionSixLowPanExtendedAddressRegistration &earo, bool &hasEaro)
Parses NS packet and populates params, returning true if packet is a valid NS/NS(EARO) packet.
static bool ParseAndValidateNaEaroPacket(Ptr< Packet > p, Icmpv6NA &naHdr, Icmpv6OptionLinkLayerAddress &tlla, Icmpv6OptionSixLowPanExtendedAddressRegistration &earo, bool &hasEaro)
Parses NA packet and populates params, returning true if packet is valid.
static bool ParseAndValidateRsPacket(Ptr< Packet > p, Icmpv6RS &rsHdr, Icmpv6OptionLinkLayerAddress &slla, Icmpv6OptionSixLowPanCapabilityIndication &cio)
Parses RS packet and populates params, returning true if packet is valid.
static Ptr< Packet > MakeNsEaroPacket(Ipv6Address src, Ipv6Address dst, Icmpv6NS &nsHdr, Icmpv6OptionLinkLayerAddress &slla, Icmpv6OptionLinkLayerAddress &tlla, Icmpv6OptionSixLowPanExtendedAddressRegistration &earo)
Construct NS (EARO) packet.
6LoWPAN-ND test case for RA packet creation and parsing
void DoRun() override
Implementation to actually run this TestCase.
6LoWPAN-ND test case for RS packet creation and parsing
void DoRun() override
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static SixlowpanNdTestSuite g_sixlowpanNdTestSuite
Static variable for test initialization.