A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
11
using 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
*/
30
class
TestEmptyOptionField
:
public
TestCase
31
{
32
public
:
33
TestEmptyOptionField
()
34
:
TestCase
(
"TestEmptyOptionField"
)
35
{
36
}
37
38
void
DoRun
()
override
39
{
40
Ipv6ExtensionDestinationHeader
header;
41
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
*/
70
class
OptionWithoutAlignmentHeader
:
public
Ipv6OptionHeader
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
*/
93
class
TestOptionWithoutAlignment
:
public
TestCase
94
{
95
public
:
96
TestOptionWithoutAlignment
()
97
:
TestCase
(
"TestOptionWithoutAlignment"
)
98
{
99
}
100
101
void
DoRun
()
override
102
{
103
Ipv6ExtensionDestinationHeader
header;
104
OptionWithoutAlignmentHeader
optionHeader;
105
header.
AddOption
(optionHeader);
106
107
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
();
116
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 2),
117
OptionWithoutAlignmentHeader::TYPE
,
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
*/
143
class
OptionWithAlignmentHeader
:
public
Ipv6OptionHeader
144
{
145
public
:
146
static
const
uint8_t
TYPE
= 73;
//!< Option Type.
147
148
uint32_t
GetSerializedSize
()
const override
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
*/
171
class
TestOptionWithAlignment
:
public
TestCase
172
{
173
public
:
174
TestOptionWithAlignment
()
175
:
TestCase
(
"TestOptionWithAlignment"
)
176
{
177
}
178
179
void
DoRun
()
override
180
{
181
Ipv6ExtensionDestinationHeader
header;
182
OptionWithAlignmentHeader
optionHeader;
183
header.
AddOption
(optionHeader);
184
Ipv6OptionJumbogramHeader
jumboHeader;
// has an alignment of 4n+2
185
header.
AddOption
(jumboHeader);
186
187
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
197
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 4),
198
OptionWithAlignmentHeader::TYPE
,
199
"option with alignment is not padded correctly"
);
200
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 8), 1,
"padding is missing"
);
// expecting a padN header
201
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 10),
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
*/
228
class
TestFulfilledAlignment
:
public
TestCase
229
{
230
public
:
231
TestFulfilledAlignment
()
232
:
TestCase
(
"TestCorrectAlignment"
)
233
{
234
}
235
236
void
DoRun
()
override
237
{
238
Ipv6ExtensionDestinationHeader
header;
239
Ipv6OptionJumbogramHeader
jumboHeader;
// has an alignment of 4n+2
240
header.
AddOption
(jumboHeader);
241
OptionWithAlignmentHeader
optionHeader;
242
header.
AddOption
(optionHeader);
243
244
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
();
253
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 2),
254
jumboHeader.
GetType
(),
255
"option with fulfilled alignment is padded anyway"
);
256
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 8),
257
OptionWithAlignmentHeader::TYPE
,
258
"option with fulfilled alignment is padded anyway"
);
259
}
260
};
261
262
/**
263
* \ingroup internet-test
264
*
265
* \brief IPv6 extensions TestSuite.
266
*/
267
class
Ipv6ExtensionHeaderTestSuite
:
public
TestSuite
268
{
269
public
:
270
Ipv6ExtensionHeaderTestSuite
()
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
280
static
Ipv6ExtensionHeaderTestSuite
281
ipv6ExtensionHeaderTestSuite
;
//!< Static variable for test initialization
Ipv6ExtensionHeaderTestSuite
IPv6 extensions TestSuite.
Definition
ipv6-extension-header-test-suite.cc:268
Ipv6ExtensionHeaderTestSuite::Ipv6ExtensionHeaderTestSuite
Ipv6ExtensionHeaderTestSuite()
Definition
ipv6-extension-header-test-suite.cc:270
OptionWithAlignmentHeader
IPv6 extensions Test: Option with alignment.
Definition
ipv6-extension-header-test-suite.cc:144
OptionWithAlignmentHeader::TYPE
static const uint8_t TYPE
Option Type.
Definition
ipv6-extension-header-test-suite.cc:146
OptionWithAlignmentHeader::GetAlignment
Alignment GetAlignment() const override
Get the Alignment requirement of this option header.
Definition
ipv6-extension-header-test-suite.cc:160
OptionWithAlignmentHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
ipv6-extension-header-test-suite.cc:148
OptionWithAlignmentHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
ipv6-extension-header-test-suite.cc:153
OptionWithoutAlignmentHeader
IPv6 extensions Test: Option without alignment.
Definition
ipv6-extension-header-test-suite.cc:71
OptionWithoutAlignmentHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
ipv6-extension-header-test-suite.cc:80
OptionWithoutAlignmentHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
ipv6-extension-header-test-suite.cc:75
OptionWithoutAlignmentHeader::TYPE
static const uint8_t TYPE
Option type.
Definition
ipv6-extension-header-test-suite.cc:73
TestEmptyOptionField
IPv6 extensions Test: Empty option field.
Definition
ipv6-extension-header-test-suite.cc:31
TestEmptyOptionField::TestEmptyOptionField
TestEmptyOptionField()
Definition
ipv6-extension-header-test-suite.cc:33
TestEmptyOptionField::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv6-extension-header-test-suite.cc:38
TestFulfilledAlignment
IPv6 extensions Test: Test an option already aligned.
Definition
ipv6-extension-header-test-suite.cc:229
TestFulfilledAlignment::TestFulfilledAlignment
TestFulfilledAlignment()
Definition
ipv6-extension-header-test-suite.cc:231
TestFulfilledAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv6-extension-header-test-suite.cc:236
TestOptionWithAlignment
IPv6 extensions Test: Test the option with alignment.
Definition
ipv6-extension-header-test-suite.cc:172
TestOptionWithAlignment::TestOptionWithAlignment
TestOptionWithAlignment()
Definition
ipv6-extension-header-test-suite.cc:174
TestOptionWithAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv6-extension-header-test-suite.cc:179
TestOptionWithoutAlignment
IPv6 extensions Test: Test the option without alignment.
Definition
ipv6-extension-header-test-suite.cc:94
TestOptionWithoutAlignment::TestOptionWithoutAlignment
TestOptionWithoutAlignment()
Definition
ipv6-extension-header-test-suite.cc:96
TestOptionWithoutAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition
ipv6-extension-header-test-suite.cc:101
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition
buffer.cc:303
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::Buffer::PeekData
const uint8_t * PeekData() const
Definition
buffer.cc:692
ns3::Ipv6ExtensionDestinationHeader
Header of IPv6 Extension Destination.
Definition
ipv6-extension-header.h:267
ns3::Ipv6ExtensionDestinationHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
ipv6-extension-header.cc:316
ns3::Ipv6ExtensionDestinationHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
ipv6-extension-header.cc:322
ns3::Ipv6OptionHeader
Header for IPv6 Option.
Definition
ipv6-option-header.h:25
ns3::Ipv6OptionHeader::GetType
uint8_t GetType() const
Get the type of the option.
Definition
ipv6-option-header.cc:55
ns3::Ipv6OptionJumbogramHeader
Header of IPv6 Option Jumbogram.
Definition
ipv6-option-header.h:256
ns3::OptionField::AddOption
void AddOption(const Ipv6OptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
Definition
ipv6-extension-header.cc:177
ns3::TestCase
encapsulates test code
Definition
test.h:1050
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition
test.cc:292
ns3::TestSuite
A suite of tests to run.
Definition
test.h:1267
ns3::TestSuite::Type
Type
Type of test.
Definition
test.h:1274
ns3::TestSuite::UNIT
static constexpr auto UNIT
Definition
test.h:1291
uint32_t
NS_TEST_EXPECT_MSG_EQ
#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
ipv6ExtensionHeaderTestSuite
static Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite
Static variable for test initialization.
Definition
ipv6-extension-header-test-suite.cc:281
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
data
uint8_t data[writeSize]
Definition
socket-bound-tcp-static-routing.cc:41
ns3::Ipv6OptionHeader::Alignment
represents the alignment requirements of an option header
Definition
ipv6-option-header.h:35
src
internet
test
ipv6-extension-header-test-suite.cc
Generated on Fri Nov 8 2024 13:59:01 for ns-3 by
1.11.0