A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
22
using 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
42
class
TestEmptyOptionField
:
public
TestCase
43
{
44
public
:
45
TestEmptyOptionField
()
46
:
TestCase
(
"TestEmptyOptionField"
)
47
{
48
}
49
50
void
DoRun
()
override
51
{
52
Ipv6ExtensionDestinationHeader
header;
53
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
83
class
OptionWithoutAlignmentHeader
:
public
Ipv6OptionHeader
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
107
class
TestOptionWithoutAlignment
:
public
TestCase
108
{
109
public
:
110
TestOptionWithoutAlignment
()
111
:
TestCase
(
"TestOptionWithoutAlignment"
)
112
{
113
}
114
115
void
DoRun
()
override
116
{
117
Ipv6ExtensionDestinationHeader
header;
118
OptionWithoutAlignmentHeader
optionHeader;
119
header.
AddOption
(optionHeader);
120
121
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
();
130
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 2),
131
OptionWithoutAlignmentHeader::TYPE
,
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
158
class
OptionWithAlignmentHeader
:
public
Ipv6OptionHeader
159
{
160
public
:
161
static
const
uint8_t
TYPE
= 73;
162
163
uint32_t
GetSerializedSize
()
const override
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
187
class
TestOptionWithAlignment
:
public
TestCase
188
{
189
public
:
190
TestOptionWithAlignment
()
191
:
TestCase
(
"TestOptionWithAlignment"
)
192
{
193
}
194
195
void
DoRun
()
override
196
{
197
Ipv6ExtensionDestinationHeader
header;
198
OptionWithAlignmentHeader
optionHeader;
199
header.
AddOption
(optionHeader);
200
Ipv6OptionJumbogramHeader
jumboHeader;
// has an alignment of 4n+2
201
header.
AddOption
(jumboHeader);
202
203
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
213
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 4),
214
OptionWithAlignmentHeader::TYPE
,
215
"option with alignment is not padded correctly"
);
216
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 8), 1,
"padding is missing"
);
// expecting a padN header
217
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 10),
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
245
class
TestFulfilledAlignment
:
public
TestCase
246
{
247
public
:
248
TestFulfilledAlignment
()
249
:
TestCase
(
"TestCorrectAlignment"
)
250
{
251
}
252
253
void
DoRun
()
override
254
{
255
Ipv6ExtensionDestinationHeader
header;
256
Ipv6OptionJumbogramHeader
jumboHeader;
// has an alignment of 4n+2
257
header.
AddOption
(jumboHeader);
258
OptionWithAlignmentHeader
optionHeader;
259
header.
AddOption
(optionHeader);
260
261
NS_TEST_EXPECT_MSG_EQ
(header.
GetSerializedSize
() % 8,
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
();
270
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 2),
271
jumboHeader.
GetType
(),
272
"option with fulfilled alignment is padded anyway"
);
273
NS_TEST_EXPECT_MSG_EQ
(*(
data
+ 8),
274
OptionWithAlignmentHeader::TYPE
,
275
"option with fulfilled alignment is padded anyway"
);
276
}
277
};
278
285
class
Ipv6ExtensionHeaderTestSuite
:
public
TestSuite
286
{
287
public
:
288
Ipv6ExtensionHeaderTestSuite
()
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
298
static
Ipv6ExtensionHeaderTestSuite
299
ipv6ExtensionHeaderTestSuite
;
Ipv6ExtensionHeaderTestSuite
IPv6 extensions TestSuite.
Definition:
ipv6-extension-header-test-suite.cc:286
Ipv6ExtensionHeaderTestSuite::Ipv6ExtensionHeaderTestSuite
Ipv6ExtensionHeaderTestSuite()
Definition:
ipv6-extension-header-test-suite.cc:288
OptionWithAlignmentHeader
IPv6 extensions Test: Option with alignment.
Definition:
ipv6-extension-header-test-suite.cc:159
OptionWithAlignmentHeader::TYPE
static const uint8_t TYPE
Option Type.
Definition:
ipv6-extension-header-test-suite.cc:161
OptionWithAlignmentHeader::GetAlignment
Alignment GetAlignment() const override
Get the Alignment requirement of this option header.
Definition:
ipv6-extension-header-test-suite.cc:175
OptionWithAlignmentHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition:
ipv6-extension-header-test-suite.cc:163
OptionWithAlignmentHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition:
ipv6-extension-header-test-suite.cc:168
OptionWithoutAlignmentHeader
IPv6 extensions Test: Option without alignment.
Definition:
ipv6-extension-header-test-suite.cc:84
OptionWithoutAlignmentHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition:
ipv6-extension-header-test-suite.cc:93
OptionWithoutAlignmentHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition:
ipv6-extension-header-test-suite.cc:88
OptionWithoutAlignmentHeader::TYPE
static const uint8_t TYPE
Option type.
Definition:
ipv6-extension-header-test-suite.cc:86
TestEmptyOptionField
IPv6 extensions Test: Empty option field.
Definition:
ipv6-extension-header-test-suite.cc:43
TestEmptyOptionField::TestEmptyOptionField
TestEmptyOptionField()
Definition:
ipv6-extension-header-test-suite.cc:45
TestEmptyOptionField::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
ipv6-extension-header-test-suite.cc:50
TestFulfilledAlignment
IPv6 extensions Test: Test an option already aligned.
Definition:
ipv6-extension-header-test-suite.cc:246
TestFulfilledAlignment::TestFulfilledAlignment
TestFulfilledAlignment()
Definition:
ipv6-extension-header-test-suite.cc:248
TestFulfilledAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
ipv6-extension-header-test-suite.cc:253
TestOptionWithAlignment
IPv6 extensions Test: Test the option with alignment.
Definition:
ipv6-extension-header-test-suite.cc:188
TestOptionWithAlignment::TestOptionWithAlignment
TestOptionWithAlignment()
Definition:
ipv6-extension-header-test-suite.cc:190
TestOptionWithAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
ipv6-extension-header-test-suite.cc:195
TestOptionWithoutAlignment
IPv6 extensions Test: Test the option without alignment.
Definition:
ipv6-extension-header-test-suite.cc:108
TestOptionWithoutAlignment::TestOptionWithoutAlignment
TestOptionWithoutAlignment()
Definition:
ipv6-extension-header-test-suite.cc:110
TestOptionWithoutAlignment::DoRun
void DoRun() override
Implementation to actually run this TestCase.
Definition:
ipv6-extension-header-test-suite.cc:115
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:100
ns3::Buffer
automatically resized byte buffer
Definition:
buffer.h:94
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition:
buffer.cc:311
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition:
buffer.h:1074
ns3::Buffer::PeekData
const uint8_t * PeekData() const
Definition:
buffer.cc:706
ns3::Ipv6ExtensionDestinationHeader
Header of IPv6 Extension Destination.
Definition:
ipv6-extension-header.h:278
ns3::Ipv6ExtensionDestinationHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition:
ipv6-extension-header.cc:327
ns3::Ipv6ExtensionDestinationHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition:
ipv6-extension-header.cc:333
ns3::Ipv6OptionHeader
Header for IPv6 Option.
Definition:
ipv6-option-header.h:36
ns3::Ipv6OptionHeader::GetType
uint8_t GetType() const
Get the type of the option.
Definition:
ipv6-option-header.cc:66
ns3::Ipv6OptionJumbogramHeader
Header of IPv6 Option Jumbogram.
Definition:
ipv6-option-header.h:267
ns3::OptionField::AddOption
void AddOption(const Ipv6OptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
Definition:
ipv6-extension-header.cc:188
ns3::TestCase
encapsulates test code
Definition:
test.h:1060
ns3::TestCase::AddTestCase
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition:
test.cc:305
ns3::TestSuite
A suite of tests to run.
Definition:
test.h:1256
ns3::TestSuite::UNIT
@ UNIT
This test suite implements a Unit Test.
Definition:
test.h:1265
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:251
ipv6ExtensionHeaderTestSuite
static Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite
Static variable for test initialization.
Definition:
ipv6-extension-header-test-suite.cc:299
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
visualizer.core.start
def start()
Definition:
core.py:1861
data
uint8_t data[writeSize]
Definition:
socket-bound-tcp-static-routing.cc:52
ns3::Ipv6OptionHeader::Alignment
represents the alignment requirements of an option header
Definition:
ipv6-option-header.h:46
src
internet
test
ipv6-extension-header-test-suite.cc
Generated on Tue Nov 1 2022 23:00:00 for ns-3 by
1.9.3