network: Add a function to test header (de)serialization
This commit is contained in:
committed by
Stefano Avallone
parent
badf118289
commit
7193a5f96d
@@ -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
|
||||
****************************
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
98
src/network/test/header-serialization-test.h
Normal file
98
src/network/test/header-serialization-test.h
Normal file
@@ -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 <magrin.davide@gmail.com>
|
||||
* Stefano Avallone <stavallo@unina.it>
|
||||
*/
|
||||
|
||||
#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 <typename T, typename... Args>
|
||||
void TestHeaderSerialization (const T& hdr, Args&&... args);
|
||||
};
|
||||
|
||||
} //namespace ns3
|
||||
|
||||
/***************************************************************
|
||||
* Implementation of the templates declared above.
|
||||
***************************************************************/
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void
|
||||
HeaderSerializationTestCase::TestHeaderSerialization (const T& hdr, Args&&... args)
|
||||
{
|
||||
Buffer buffer;
|
||||
buffer.AddAtStart (hdr.GetSerializedSize ());
|
||||
hdr.Serialize (buffer.Begin ());
|
||||
|
||||
T otherHdr (std::forward<Args> (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 */
|
||||
Reference in New Issue
Block a user