From 7193a5f96daaefe37df37e7f98e598af7a5a07e7 Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Sun, 17 Jul 2022 21:40:45 +0200 Subject: [PATCH] network: Add a function to test header (de)serialization --- doc/manual/source/how-to-write-tests.rst | 35 +++++++ src/network/CMakeLists.txt | 1 + src/network/test/header-serialization-test.h | 98 ++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/network/test/header-serialization-test.h diff --git a/doc/manual/source/how-to-write-tests.rst b/doc/manual/source/how-to-write-tests.rst index 6fa9f7485..4282afef0 100644 --- a/doc/manual/source/how-to-write-tests.rst +++ b/doc/manual/source/how-to-write-tests.rst @@ -214,6 +214,41 @@ keep up-to-date. Output as little as needed for the example and include only behavioral state that is important for determining if the example has run correctly. +Testing (de)serialization of Headers +************************************ +Implementing serialization and deserialization of Headers is often prone to +errors. A generic approach to test these operations is to start from a given +Header, serialize the given header in a buffer, then create a new header by +deserializing from the buffer and serialize the new header into a second buffer. +If everything is correct, the two buffers have the same size and the same content. + +The ``HeaderSerializationTestCase`` class enables to perform such a test in an +easy manner. Test cases willing to exploit such an approach have to inherit from +``HeaderSerializationTestCase`` instead of ``TestCase`` and pass a Header object +to the ``TestHeaderSerialization`` method (along with arguments that may be +needed to construct the new header that is going to be deserialized). + +Note that such an approach is not restricted to Header subclasses, but it is +available for all classes that provide (de)serialization operations, such as +the wifi Information Elements. + +:: + + #include "ns3/header-serialization-test.h" + class BasicMultiLinkElementTest : public HeaderSerializationTestCase + { + ... + }; + void + BasicMultiLinkElementTest::DoRun (void) + { + MultiLinkElement mle (WIFI_MAC_MGT_BEACON); + // Fill in the Multi-Link Element + TestHeaderSerialization (mle, WIFI_MAC_MGT_BEACON); + } + +Examples of this approach are found, e.g., in ``src/wifi/test/wifi-eht-info-elems-test.cc`` + Testing for boolean outcomes **************************** diff --git a/src/network/CMakeLists.txt b/src/network/CMakeLists.txt index f0566b9fb..88e357fd7 100644 --- a/src/network/CMakeLists.txt +++ b/src/network/CMakeLists.txt @@ -98,6 +98,7 @@ set(header_files model/tag-buffer.h model/tag.h model/trailer.h + test/header-serialization-test.h utils/address-utils.h utils/bit-deserializer.h utils/bit-serializer.h diff --git a/src/network/test/header-serialization-test.h b/src/network/test/header-serialization-test.h new file mode 100644 index 000000000..dd52f2153 --- /dev/null +++ b/src/network/test/header-serialization-test.h @@ -0,0 +1,98 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2022 Universita' degli Studi di Napoli Federico II + * + * 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 + * + * Authors: Davide Magrin + * Stefano Avallone + */ + +#ifndef NS3_TEST_HDR_SERIALIZE_H +#define NS3_TEST_HDR_SERIALIZE_H + +#include "ns3/buffer.h" +#include "ns3/test.h" + +namespace ns3 { + +/** + * Subclass of TestCase class adding the ability to test the serialization and + * deserialization of a Header object. + */ +class HeaderSerializationTestCase : public TestCase +{ +protected: + /** + * \brief Constructor. + * + * \param [in] name The name of the new TestCase created + */ + HeaderSerializationTestCase (std::string name) + : TestCase (name) {} + +public: + /** + * Serialize the given header in a buffer, then create a new header by + * deserializing from the buffer and serialize the new header into a new buffer. + * Verify that the two buffers have the same size and the same content. + * + * \tparam T \deduced Type of the given header + * \tparam Args \deduced Type of arguments to pass to the constructor of the header + * \param [in] hdr the header to test + * \param [in] args the arguments to construct the new header + */ + template + void TestHeaderSerialization (const T& hdr, Args&&... args); +}; + +} //namespace ns3 + +/*************************************************************** + * Implementation of the templates declared above. + ***************************************************************/ + +namespace ns3 { + +template +void +HeaderSerializationTestCase::TestHeaderSerialization (const T& hdr, Args&&... args) +{ + Buffer buffer; + buffer.AddAtStart (hdr.GetSerializedSize ()); + hdr.Serialize (buffer.Begin ()); + + T otherHdr (std::forward (args)...); + otherHdr.Deserialize (buffer.Begin ()); + Buffer otherBuffer; + otherBuffer.AddAtStart (otherHdr.GetSerializedSize ()); + otherHdr.Serialize (otherBuffer.Begin ()); + + NS_TEST_ASSERT_MSG_EQ (buffer.GetSize (), otherBuffer.GetSize (), "Size of buffers differs"); + + Buffer::Iterator bufferIterator = buffer.Begin (); + Buffer::Iterator otherBufferIterator = otherBuffer.Begin (); + for (uint32_t j = 0; j < buffer.GetSize (); j++) + { + uint8_t bufferVal = bufferIterator.ReadU8 (); + uint8_t otherBufferVal = otherBufferIterator.ReadU8 (); + NS_TEST_EXPECT_MSG_EQ (+bufferVal, +otherBufferVal, + "Serialization -> Deserialization -> Serialization " + << "is different than Serialization at byte " << j); + } +} + +} // namespace ns3 + +#endif /* NS3_TEST_HDR_SERIALIZE_H */