A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-extension-header-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Fabian Mauchle <fabian.mauchle@hsr.ch>
5 */
6
7#include "ns3/ipv6-extension-header.h"
8#include "ns3/ipv6-option-header.h"
9#include "ns3/test.h"
10
11using namespace ns3;
12
13// ===========================================================================
14// An empty option field must be filled with pad1 or padN header so theshape
15// extension header's size is a multiple of 8.
16//
17// 0 31
18// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
19// | Extension Destination Header | |
20// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ PadN Header +
21// | |
22// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23// ===========================================================================
24
25/**
26 * @ingroup internet-test
27 *
28 * @brief IPv6 extensions Test: Empty option field.
29 */
31{
32 public:
34 : TestCase("TestEmptyOptionField")
35 {
36 }
37
38 void DoRun() override
39 {
42 0,
43 "length of extension header is not a multiple of 8");
44
45 Buffer buf;
46 buf.AddAtStart(header.GetSerializedSize());
47 header.Serialize(buf.Begin());
48
49 const uint8_t* data = buf.PeekData();
50 NS_TEST_EXPECT_MSG_EQ(*(data + 2), 1, "padding is missing"); // expecting a padN header
51 }
52};
53
54// ===========================================================================
55// An option without alignment requirement must not be padded
56//
57// 0 31
58// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59// | Extension Destination Header | OptionWithoutAlignmentHeader..|
60// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61// |..OptionWithoutAlignmentHeader | PadN Header |
62// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63// ===========================================================================
64
65/**
66 * @ingroup internet-test
67 *
68 * @brief IPv6 extensions Test: Option without alignment.
69 */
71{
72 public:
73 static const uint8_t TYPE = 42; //!< Option type.
74
75 uint32_t GetSerializedSize() const override
76 {
77 return 4;
78 }
79
80 void Serialize(Buffer::Iterator start) const override
81 {
82 start.WriteU8(TYPE);
83 start.WriteU8(GetSerializedSize() - 2);
84 start.WriteU16(0);
85 }
86};
87
88/**
89 * @ingroup internet-test
90 *
91 * @brief IPv6 extensions Test: Test the option without alignment.
92 */
94{
95 public:
97 : TestCase("TestOptionWithoutAlignment")
98 {
99 }
100
101 void DoRun() override
102 {
104 OptionWithoutAlignmentHeader optionHeader;
105 header.AddOption(optionHeader);
106
108 0,
109 "length of extension header is not a multiple of 8");
110
111 Buffer buf;
112 buf.AddAtStart(header.GetSerializedSize());
113 header.Serialize(buf.Begin());
114
115 const uint8_t* data = buf.PeekData();
118 "option without alignment is not first in header field");
119 }
120};
121
122// ===========================================================================
123// An option with alignment requirement must be padded accordingly (padding to
124// a total size multiple of 8 is allowed)
125//
126// 0 31
127// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128// | Extension Destination Header | PadN Header |
129// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130// | OptionWithAlignmentHeader |
131// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132// | PadN Header | |
133// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
134// | Ipv6OptionJumbogramHeader |
135// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136// ===========================================================================
137
138/**
139 * @ingroup internet-test
140 *
141 * @brief IPv6 extensions Test: Option with alignment.
142 */
144{
145 public:
146 static const uint8_t TYPE = 73; //!< Option Type.
147
149 {
150 return 4;
151 }
152
153 void Serialize(Buffer::Iterator start) const override
154 {
155 start.WriteU8(TYPE);
156 start.WriteU8(GetSerializedSize() - 2);
157 start.WriteU16(0);
158 }
159
160 Alignment GetAlignment() const override
161 {
162 return Alignment{4, 0};
163 }
164};
165
166/**
167 * @ingroup internet-test
168 *
169 * @brief IPv6 extensions Test: Test the option with alignment.
170 */
172{
173 public:
175 : TestCase("TestOptionWithAlignment")
176 {
177 }
178
179 void DoRun() override
180 {
182 OptionWithAlignmentHeader optionHeader;
183 header.AddOption(optionHeader);
184 Ipv6OptionJumbogramHeader jumboHeader; // has an alignment of 4n+2
185 header.AddOption(jumboHeader);
186
188 0,
189 "length of extension header is not a multiple of 8");
190
191 Buffer buf;
192 buf.AddAtStart(header.GetSerializedSize());
193 header.Serialize(buf.Begin());
194
195 const uint8_t* data = buf.PeekData();
196 NS_TEST_EXPECT_MSG_EQ(*(data + 2), 1, "padding is missing"); // expecting a padN header
199 "option with alignment is not padded correctly");
200 NS_TEST_EXPECT_MSG_EQ(*(data + 8), 1, "padding is missing"); // expecting a padN header
202 jumboHeader.GetType(),
203 "option with alignment is not padded correctly");
204 }
205};
206
207// ===========================================================================
208// An option with an alignment that exactly matches the gap must not be padded
209// (padding to a total size multiple of 8 is allowed)
210//
211// 0 31
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213// | Extension Destination Header | |
214// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
215// | Ipv6OptionJumbogramHeader |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217// | OptionWithAlignmentHeader |
218// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219// | PadN Header |
220// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221// ===========================================================================
222
223/**
224 * @ingroup internet-test
225 *
226 * @brief IPv6 extensions Test: Test an option already aligned.
227 */
229{
230 public:
232 : TestCase("TestCorrectAlignment")
233 {
234 }
235
236 void DoRun() override
237 {
239 Ipv6OptionJumbogramHeader jumboHeader; // has an alignment of 4n+2
240 header.AddOption(jumboHeader);
241 OptionWithAlignmentHeader optionHeader;
242 header.AddOption(optionHeader);
243
245 0,
246 "length of extension header is not a multiple of 8");
247
248 Buffer buf;
249 buf.AddAtStart(header.GetSerializedSize());
250 header.Serialize(buf.Begin());
251
252 const uint8_t* data = buf.PeekData();
254 jumboHeader.GetType(),
255 "option with fulfilled alignment is padded anyway");
258 "option with fulfilled alignment is padded anyway");
259 }
260};
261
262/**
263 * @ingroup internet-test
264 *
265 * @brief IPv6 extensions TestSuite.
266 */
268{
269 public:
271 : TestSuite("ipv6-extension-header", Type::UNIT)
272 {
273 AddTestCase(new TestEmptyOptionField, TestCase::Duration::QUICK);
274 AddTestCase(new TestOptionWithoutAlignment, TestCase::Duration::QUICK);
275 AddTestCase(new TestOptionWithAlignment, TestCase::Duration::QUICK);
276 AddTestCase(new TestFulfilledAlignment, TestCase::Duration::QUICK);
277 }
278};
279
281 ipv6ExtensionHeaderTestSuite; //!< Static variable for test initialization
IPv6 extensions Test: Option with alignment.
static const uint8_t TYPE
Option Type.
Alignment GetAlignment() const override
Get the Alignment requirement of this option header.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
IPv6 extensions Test: Option without alignment.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
static const uint8_t TYPE
Option type.
IPv6 extensions Test: Empty option field.
void DoRun() override
Implementation to actually run this TestCase.
IPv6 extensions Test: Test an option already aligned.
void DoRun() override
Implementation to actually run this TestCase.
IPv6 extensions Test: Test the option with alignment.
void DoRun() override
Implementation to actually run this TestCase.
IPv6 extensions Test: Test the option without alignment.
void DoRun() override
Implementation to actually run this TestCase.
iterator in a Buffer instance
Definition buffer.h:89
automatically resized byte buffer
Definition buffer.h:83
void AddAtStart(uint32_t start)
Definition buffer.cc:303
Buffer::Iterator Begin() const
Definition buffer.h:1063
const uint8_t * PeekData() const
Definition buffer.cc:692
Header of IPv6 Extension Destination.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header for IPv6 Option.
uint8_t GetType() const
Get the type of the option.
Header of IPv6 Option Jumbogram.
void AddOption(const Ipv6OptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
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
static constexpr auto UNIT
Definition test.h:1291
#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:241
static Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
represents the alignment requirements of an option header