A Discrete-Event Network Simulator
API
ipv6-extension-header-test-suite.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 * Author: Fabian Mauchle <fabian.mauchle@hsr.ch>
17 */
18
19#include "ns3/test.h"
20#include "ns3/ipv6-extension-header.h"
21#include "ns3/ipv6-option-header.h"
22
23using namespace ns3;
24
25// ===========================================================================
26// An empty option field must be filled with pad1 or padN header so theshape
27// extension header's size is a multiple of 8.
28//
29// 0 31
30// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31// | Extension Destination Header | |
32// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ PadN Header +
33// | |
34// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35// ===========================================================================
36
44{
45public:
46 TestEmptyOptionField () : TestCase ("TestEmptyOptionField") {}
47
48 virtual void DoRun ()
49 {
51 NS_TEST_EXPECT_MSG_EQ (header.GetSerializedSize () % 8, 0, "length of extension header is not a multiple of 8");
52
53 Buffer buf;
54 buf.AddAtStart (header.GetSerializedSize ());
55 header.Serialize (buf.Begin ());
56
57 const uint8_t* data = buf.PeekData ();
58 NS_TEST_EXPECT_MSG_EQ (*(data+2), 1, "padding is missing"); //expecting a padN header
59 }
60};
61
62// ===========================================================================
63// An option without alignment requirement must not be padded
64//
65// 0 31
66// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67// | Extension Destination Header | OptionWithoutAlignmentHeader..|
68// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69// |..OptionWithoutAlignmentHeader | PadN Header |
70// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71// ===========================================================================
72
80{
81public:
82 static const uint8_t TYPE = 42;
83
84 virtual uint32_t GetSerializedSize () const
85 {
86 return 4;
87 }
88
89 virtual void Serialize (Buffer::Iterator start) const
90 {
91 start.WriteU8 (TYPE);
92 start.WriteU8 (GetSerializedSize ()-2);
93 start.WriteU16 (0);
94 }
95};
96
97
105{
106public:
107 TestOptionWithoutAlignment () : TestCase ("TestOptionWithoutAlignment") {}
108
109 virtual void DoRun ()
110 {
112 OptionWithoutAlignmentHeader optionHeader;
113 header.AddOption (optionHeader);
114
115
116 NS_TEST_EXPECT_MSG_EQ (header.GetSerializedSize () % 8, 0, "length of extension header is not a multiple of 8");
117
118 Buffer buf;
119 buf.AddAtStart (header.GetSerializedSize ());
120 header.Serialize (buf.Begin ());
121
122 const uint8_t* data = buf.PeekData ();
123 NS_TEST_EXPECT_MSG_EQ (*(data+2), OptionWithoutAlignmentHeader::TYPE, "option without alignment is not first in header field");
124 }
125};
126
127// ===========================================================================
128// An option with alignment requirement must be padded accordingly (padding to
129// a total size multiple of 8 is allowed)
130//
131// 0 31
132// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133// | Extension Destination Header | PadN Header |
134// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135// | OptionWithAlignmentHeader |
136// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137// | PadN Header | |
138// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
139// | Ipv6OptionJumbogramHeader |
140// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141// ===========================================================================
142
150{
151public:
152 static const uint8_t TYPE = 73;
153
155 {
156 return 4;
157 }
158
159 virtual void Serialize (Buffer::Iterator start) const
160 {
161 start.WriteU8 (TYPE);
162 start.WriteU8 (GetSerializedSize ()-2);
163 start.WriteU16 (0);
164 }
165
166 virtual Alignment GetAlignment () const
167 {
168 return (Alignment){ 4,0};
169 }
170};
171
172
180{
181public:
182 TestOptionWithAlignment () : TestCase ("TestOptionWithAlignment") {}
183
184 virtual void DoRun ()
185 {
187 OptionWithAlignmentHeader optionHeader;
188 header.AddOption (optionHeader);
189 Ipv6OptionJumbogramHeader jumboHeader; //has an alignment of 4n+2
190 header.AddOption (jumboHeader);
191
192 NS_TEST_EXPECT_MSG_EQ (header.GetSerializedSize () % 8, 0, "length of extension header is not a multiple of 8");
193
194 Buffer buf;
195 buf.AddAtStart (header.GetSerializedSize ());
196 header.Serialize (buf.Begin ());
197
198 const uint8_t* data = buf.PeekData ();
199 NS_TEST_EXPECT_MSG_EQ (*(data+2), 1, "padding is missing"); //expecting a padN header
200 NS_TEST_EXPECT_MSG_EQ (*(data+4), OptionWithAlignmentHeader::TYPE, "option with alignment is not padded correctly");
201 NS_TEST_EXPECT_MSG_EQ (*(data+8), 1, "padding is missing"); //expecting a padN header
202 NS_TEST_EXPECT_MSG_EQ (*(data+10), jumboHeader.GetType (), "option with alignment is not padded correctly");
203 }
204};
205
206// ===========================================================================
207// An option with an alignment that exactly matches the gap must not be padded
208// (padding to a total size multiple of 8 is allowed)
209//
210// 0 31
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212// | Extension Destination Header | |
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
214// | Ipv6OptionJumbogramHeader |
215// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216// | OptionWithAlignmentHeader |
217// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218// | PadN Header |
219// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
220// ===========================================================================
221
229{
230public:
231 TestFulfilledAlignment () : TestCase ("TestCorrectAlignment") {}
232
233 virtual void DoRun ()
234 {
236 Ipv6OptionJumbogramHeader jumboHeader; //has an alignment of 4n+2
237 header.AddOption (jumboHeader);
238 OptionWithAlignmentHeader optionHeader;
239 header.AddOption (optionHeader);
240
241 NS_TEST_EXPECT_MSG_EQ (header.GetSerializedSize () % 8, 0, "length of extension header is not a multiple of 8");
242
243 Buffer buf;
244 buf.AddAtStart (header.GetSerializedSize ());
245 header.Serialize (buf.Begin ());
246
247 const uint8_t* data = buf.PeekData ();
248 NS_TEST_EXPECT_MSG_EQ (*(data+2), jumboHeader.GetType (), "option with fulfilled alignment is padded anyway");
249 NS_TEST_EXPECT_MSG_EQ (*(data+8), OptionWithAlignmentHeader::TYPE, "option with fulfilled alignment is padded anyway");
250 }
251};
252
260{
261public:
263 : TestSuite ("ipv6-extension-header", UNIT)
264 {
265 AddTestCase (new TestEmptyOptionField, TestCase::QUICK);
266 AddTestCase (new TestOptionWithoutAlignment, TestCase::QUICK);
267 AddTestCase (new TestOptionWithAlignment, TestCase::QUICK);
268 AddTestCase (new TestFulfilledAlignment, TestCase::QUICK);
269
270 }
271};
272
IPv6 extensions Test: Option with alignment.
static const uint8_t TYPE
Option Type.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
IPv6 extensions Test: Option without alignment.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
static const uint8_t TYPE
Option type.
IPv6 extensions Test: Empty option field.
virtual void DoRun()
Implementation to actually run this TestCase.
IPv6 extensions Test: Test an option already aligned.
virtual void DoRun()
Implementation to actually run this TestCase.
IPv6 extensions Test: Test the option with alignment.
virtual void DoRun()
Implementation to actually run this TestCase.
IPv6 extensions Test: Test the option without alignment.
virtual void DoRun()
Implementation to actually run this TestCase.
iterator in a Buffer instance
Definition: buffer.h:99
automatically resized byte buffer
Definition: buffer.h:93
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
uint8_t const * PeekData(void) const
Definition: buffer.cc:710
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
Header of IPv6 Extension Destination.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Header for IPv6 Option.
uint8_t GetType() const
Get the type of the option.
Header of IPv6 Option Jumbogram.
void AddOption(Ipv6OptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
#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
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:1853
uint8_t data[writeSize]
represents the alignment requirements of an option header