From 7b94c5ddab587baf619bd7d32f1665c7953e8562 Mon Sep 17 00:00:00 2001 From: Sebastien Vincent Date: Tue, 24 Nov 2009 20:37:29 +0100 Subject: [PATCH] Add Ipv6ExtensionHeader tests (from Fabian Mauchle). --- .../ipv6-extension-header-test-suite.cc | 234 ++++++++++++++++++ src/internet-stack/wscript | 3 +- 2 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 src/internet-stack/ipv6-extension-header-test-suite.cc diff --git a/src/internet-stack/ipv6-extension-header-test-suite.cc b/src/internet-stack/ipv6-extension-header-test-suite.cc new file mode 100644 index 000000000..5a282b1a3 --- /dev/null +++ b/src/internet-stack/ipv6-extension-header-test-suite.cc @@ -0,0 +1,234 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Fabian Mauchle + */ + +#include "ns3/test.h" +#include "ipv6-extension-header.h" +#include "ipv6-option-header.h" + +using namespace ns3; + +// =========================================================================== +// An empty option field must be filled with pad1 or padN header so the +// extension header's size is a multiple of 8. +// +// 0 31 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | Extension Destination Header | | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ PadN Header + +// | | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// =========================================================================== +class TestEmptyOptionField : public TestCase +{ +public: + TestEmptyOptionField () : TestCase("TestEmptyOptionField") {} + + virtual bool DoRun() + { + Ipv6ExtensionDestinationHeader header; + NS_TEST_EXPECT_MSG_EQ(header.GetSerializedSize() % 8, 0, "length of extension header is not a multiple of 8"); + + Buffer buf; + buf.AddAtStart(header.GetSerializedSize()); + header.Serialize(buf.Begin()); + + const uint8_t* data = buf.PeekData(); + NS_TEST_EXPECT_MSG_EQ(*(data+2), 1, "padding is missing"); //expecting a padN header + + return GetErrorStatus(); + } +}; + +// =========================================================================== +// An option without alignment requirement must not be padded +// +// 0 31 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | Extension Destination Header | OptionWithoutAlignmentHeader..| +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// |..OptionWithoutAlignmentHeader | PadN Header | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// =========================================================================== +class OptionWithoutAlignmentHeader : public Ipv6OptionHeader +{ +public: + static const uint8_t TYPE = 42; + virtual uint32_t GetSerializedSize () const + { + return 4; + } + + virtual void Serialize (Buffer::Iterator start) const + { + start.WriteU8(TYPE); + start.WriteU8(GetSerializedSize()-2); + start.WriteU16(0); + } +}; + + +class TestOptionWithoutAlignment : public TestCase +{ +public: + TestOptionWithoutAlignment () : TestCase("TestOptionWithoutAlignment") {} + + virtual bool DoRun() + { + Ipv6ExtensionDestinationHeader header; + OptionWithoutAlignmentHeader optionHeader; + header.AddOption(optionHeader); + + + NS_TEST_EXPECT_MSG_EQ(header.GetSerializedSize() % 8, 0, "length of extension header is not a multiple of 8"); + + Buffer buf; + buf.AddAtStart(header.GetSerializedSize()); + header.Serialize(buf.Begin()); + + const uint8_t* data = buf.PeekData(); + NS_TEST_EXPECT_MSG_EQ(*(data+2), OptionWithoutAlignmentHeader::TYPE, "option without alignemt is not first in header field"); + + return GetErrorStatus(); + } +}; + +// =========================================================================== +// An option with alignment requirement must be padded accordingly (padding to +// a total size multiple of 8 is allowed) +// +// 0 31 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | Extension Destination Header | PadN Header | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | OptionWithAlignmentHeader | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | PadN Header | | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +// | Ipv6OptionJumbogramHeader | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// =========================================================================== +class OptionWithAlignmentHeader : public Ipv6OptionHeader +{ +public: + static const uint8_t TYPE = 73; + virtual uint32_t GetSerializedSize () const + { + return 4; + } + + virtual void Serialize (Buffer::Iterator start) const + { + start.WriteU8(TYPE); + start.WriteU8(GetSerializedSize()-2); + start.WriteU16(0); + } + + virtual Alignment GetAlignment() const + { + return (Alignment){4,0}; + } +}; + + +class TestOptionWithAlignment : public TestCase +{ +public: + TestOptionWithAlignment () : TestCase("TestOptionWithAlignment") {} + + virtual bool DoRun() + { + Ipv6ExtensionDestinationHeader header; + OptionWithAlignmentHeader optionHeader; + header.AddOption(optionHeader); + Ipv6OptionJumbogramHeader jumboHeader; //has an alignment of 4n+2 + header.AddOption(jumboHeader); + + NS_TEST_EXPECT_MSG_EQ(header.GetSerializedSize() % 8, 0, "length of extension header is not a multiple of 8"); + + Buffer buf; + buf.AddAtStart(header.GetSerializedSize()); + header.Serialize(buf.Begin()); + + const uint8_t* data = buf.PeekData(); + NS_TEST_EXPECT_MSG_EQ(*(data+2), 1, "padding is missing"); //expecting a padN header + NS_TEST_EXPECT_MSG_EQ(*(data+4), OptionWithAlignmentHeader::TYPE, "option with alignment is not padded correctly"); + NS_TEST_EXPECT_MSG_EQ(*(data+8), 1, "padding is missing"); //expecting a padN header + NS_TEST_EXPECT_MSG_EQ(*(data+10), jumboHeader.GetType(), "option with alignment is not padded correctly"); + + return GetErrorStatus(); + } +}; + +// =========================================================================== +// An option with an alignment that exactly matches the gap must not be padded +// (padding to a total size multiple of 8 is allowed) +// +// 0 31 +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | Extension Destination Header | | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +// | Ipv6OptionJumbogramHeader | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | OptionWithAlignmentHeader | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// | PadN Header | +// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +// =========================================================================== + +class TestFulfilledAlignment : public TestCase +{ +public: + TestFulfilledAlignment () : TestCase("TestCorrectAlignment") {} + + virtual bool DoRun() + { + Ipv6ExtensionDestinationHeader header; + Ipv6OptionJumbogramHeader jumboHeader; //has an alignment of 4n+2 + header.AddOption(jumboHeader); + OptionWithAlignmentHeader optionHeader; + header.AddOption(optionHeader); + + NS_TEST_EXPECT_MSG_EQ(header.GetSerializedSize() % 8, 0, "length of extension header is not a multiple of 8"); + + Buffer buf; + buf.AddAtStart(header.GetSerializedSize()); + header.Serialize(buf.Begin()); + + const uint8_t* data = buf.PeekData(); + NS_TEST_EXPECT_MSG_EQ(*(data+2), jumboHeader.GetType(), "option with fulfilled alignment is padded anyway"); + NS_TEST_EXPECT_MSG_EQ(*(data+8), OptionWithAlignmentHeader::TYPE, "option with fulfilled alignment is padded anyway"); + + return GetErrorStatus(); + } +}; + +class Ipv6ExtensionHeaderTestSuite : public TestSuite +{ +public: + Ipv6ExtensionHeaderTestSuite () + : TestSuite ("ipv6-extension-header", UNIT) + { + AddTestCase (new TestEmptyOptionField); + AddTestCase (new TestOptionWithoutAlignment); + AddTestCase (new TestOptionWithAlignment); + AddTestCase (new TestFulfilledAlignment); + + } +}; + +Ipv6ExtensionHeaderTestSuite ipv6ExtensionHeaderTestSuite; diff --git a/src/internet-stack/wscript b/src/internet-stack/wscript index 05f512b58..099ee4113 100644 --- a/src/internet-stack/wscript +++ b/src/internet-stack/wscript @@ -116,7 +116,8 @@ def build(bld): 'ipv6-option-header.cc', 'ipv6-option-demux.cc', 'icmpv6-l4-protocol.cc', - 'ipv6-test.cc' + 'ipv6-test.cc', + 'ipv6-extension-header-test-suite.cc', ] headers = bld.new_task_gen('ns3header')