A Discrete-Event Network Simulator
API
ipv6-extension-header-test-suite.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Fabian Mauchle <fabian.mauchle@hsr.ch>
16 */
17
18#include "ns3/ipv6-extension-header.h"
19#include "ns3/ipv6-option-header.h"
20#include "ns3/test.h"
21
22using namespace ns3;
23
24// ===========================================================================
25// An empty option field must be filled with pad1 or padN header so theshape
26// extension header's size is a multiple of 8.
27//
28// 0 31
29// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30// | Extension Destination Header | |
31// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ PadN Header +
32// | |
33// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34// ===========================================================================
35
43{
44 public:
46 : TestCase("TestEmptyOptionField")
47 {
48 }
49
50 void DoRun() override
51 {
54 0,
55 "length of extension header is not a multiple of 8");
56
57 Buffer buf;
58 buf.AddAtStart(header.GetSerializedSize());
59 header.Serialize(buf.Begin());
60
61 const uint8_t* data = buf.PeekData();
62 NS_TEST_EXPECT_MSG_EQ(*(data + 2), 1, "padding is missing"); // expecting a padN header
63 }
64};
65
66// ===========================================================================
67// An option without alignment requirement must not be padded
68//
69// 0 31
70// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71// | Extension Destination Header | OptionWithoutAlignmentHeader..|
72// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73// |..OptionWithoutAlignmentHeader | PadN Header |
74// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75// ===========================================================================
76
84{
85 public:
86 static const uint8_t TYPE = 42;
87
88 uint32_t GetSerializedSize() const override
89 {
90 return 4;
91 }
92
93 void Serialize(Buffer::Iterator start) const override
94 {
95 start.WriteU8(TYPE);
96 start.WriteU8(GetSerializedSize() - 2);
97 start.WriteU16(0);
98 }
99};
100
108{
109 public:
111 : TestCase("TestOptionWithoutAlignment")
112 {
113 }
114
115 void DoRun() override
116 {
118 OptionWithoutAlignmentHeader optionHeader;
119 header.AddOption(optionHeader);
120
122 0,
123 "length of extension header is not a multiple of 8");
124
125 Buffer buf;
126 buf.AddAtStart(header.GetSerializedSize());
127 header.Serialize(buf.Begin());
128
129 const uint8_t* data = buf.PeekData();
132 "option without alignment is not first in header field");
133 }
134};
135
136// ===========================================================================
137// An option with alignment requirement must be padded accordingly (padding to
138// a total size multiple of 8 is allowed)
139//
140// 0 31
141// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142// | Extension Destination Header | PadN Header |
143// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144// | OptionWithAlignmentHeader |
145// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146// | PadN Header | |
147// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
148// | Ipv6OptionJumbogramHeader |
149// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150// ===========================================================================
151
159{
160 public:
161 static const uint8_t TYPE = 73;
162
164 {
165 return 4;
166 }
167
168 void Serialize(Buffer::Iterator start) const override
169 {
170 start.WriteU8(TYPE);
171 start.WriteU8(GetSerializedSize() - 2);
172 start.WriteU16(0);
173 }
174
175 Alignment GetAlignment() const override
176 {
177 return (Alignment){4, 0};
178 }
179};
180
188{
189 public:
191 : TestCase("TestOptionWithAlignment")
192 {
193 }
194
195 void DoRun() override
196 {
198 OptionWithAlignmentHeader optionHeader;
199 header.AddOption(optionHeader);
200 Ipv6OptionJumbogramHeader jumboHeader; // has an alignment of 4n+2
201 header.AddOption(jumboHeader);
202
204 0,
205 "length of extension header is not a multiple of 8");
206
207 Buffer buf;
208 buf.AddAtStart(header.GetSerializedSize());
209 header.Serialize(buf.Begin());
210
211 const uint8_t* data = buf.PeekData();
212 NS_TEST_EXPECT_MSG_EQ(*(data + 2), 1, "padding is missing"); // expecting a padN header
215 "option with alignment is not padded correctly");
216 NS_TEST_EXPECT_MSG_EQ(*(data + 8), 1, "padding is missing"); // expecting a padN header
218 jumboHeader.GetType(),
219 "option with alignment is not padded correctly");
220 }
221};
222
223// ===========================================================================
224// An option with an alignment that exactly matches the gap must not be padded
225// (padding to a total size multiple of 8 is allowed)
226//
227// 0 31
228// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229// | Extension Destination Header | |
230// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
231// | Ipv6OptionJumbogramHeader |
232// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
233// | OptionWithAlignmentHeader |
234// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
235// | PadN Header |
236// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
237// ===========================================================================
238
246{
247 public:
249 : TestCase("TestCorrectAlignment")
250 {
251 }
252
253 void DoRun() override
254 {
256 Ipv6OptionJumbogramHeader jumboHeader; // has an alignment of 4n+2
257 header.AddOption(jumboHeader);
258 OptionWithAlignmentHeader optionHeader;
259 header.AddOption(optionHeader);
260
262 0,
263 "length of extension header is not a multiple of 8");
264
265 Buffer buf;
266 buf.AddAtStart(header.GetSerializedSize());
267 header.Serialize(buf.Begin());
268
269 const uint8_t* data = buf.PeekData();
271 jumboHeader.GetType(),
272 "option with fulfilled alignment is padded anyway");
275 "option with fulfilled alignment is padded anyway");
276 }
277};
278
286{
287 public:
289 : TestSuite("ipv6-extension-header", UNIT)
290 {
291 AddTestCase(new TestEmptyOptionField, TestCase::QUICK);
292 AddTestCase(new TestOptionWithoutAlignment, TestCase::QUICK);
293 AddTestCase(new TestOptionWithAlignment, TestCase::QUICK);
294 AddTestCase(new TestFulfilledAlignment, TestCase::QUICK);
295 }
296};
297
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:100
automatically resized byte buffer
Definition: buffer.h:94
void AddAtStart(uint32_t start)
Definition: buffer.cc:311
Buffer::Iterator Begin() const
Definition: buffer.h:1074
const uint8_t * PeekData() const
Definition: buffer.cc:706
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:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:305
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
#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:251
static Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite
Static variable for test initialization.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
def start()
Definition: core.py:1861
uint8_t data[writeSize]
represents the alignment requirements of an option header