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 
23 using 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 {
45 public:
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 {
81 public:
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 {
106 public:
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 {
151 public:
152  static const uint8_t TYPE = 73;
153 
154  virtual uint32_t GetSerializedSize () const
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 {
181 public:
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 {
230 public:
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 {
261 public:
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 
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Header for IPv6 Option.
A suite of tests to run.
Definition: test.h:1343
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1855
#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:283
static const uint8_t TYPE
Option type.
encapsulates test code
Definition: test.h:1153
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
static Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite
Static variable for test initialization.
IPv6 extensions Test: Option with alignment.
represents the alignment requirements of an option header
iterator in a Buffer instance
Definition: buffer.h:98
uint8_t data[writeSize]
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
virtual void DoRun()
Implementation to actually run this TestCase.
Header of IPv6 Option Jumbogram.
virtual void DoRun()
Implementation to actually run this TestCase.
uint8_t GetType() const
Get the type of the option.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void AddOption(Ipv6OptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
virtual void DoRun()
Implementation to actually run this TestCase.
Fast test.
Definition: test.h:1159
IPv6 extensions Test: Test the option with alignment.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Header of IPv6 Extension Destination.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
static const uint8_t TYPE
Option Type.
IPv6 extensions Test: Option without alignment.
IPv6 extensions Test: Test an option already aligned.
virtual void DoRun()
Implementation to actually run this TestCase.
IPv6 extensions Test: Empty option field.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
IPv6 extensions Test: Test the option without alignment.