A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
header-serialization-test.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Universita' degli Studi di Napoli Federico II
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Davide Magrin <magrin.davide@gmail.com>
7 * Stefano Avallone <stavallo@unina.it>
8 */
9
10#ifndef NS3_TEST_HDR_SERIALIZE_H
11#define NS3_TEST_HDR_SERIALIZE_H
12
13#include "ns3/buffer.h"
14#include "ns3/test.h"
15
16/**
17 * @file
18 * @ingroup network
19 * Contains the implementation of the HeaderSerializationTestCase base class.
20 * It is used to generate tests that verify whether a header, when encoded,
21 * then decoded, then re-encoded, matches the original encoded header byte-for-byte.
22 */
23
24namespace ns3
25{
26
27/**
28 * Subclass of TestCase class adding the ability to test the serialization and
29 * deserialization of a Header object.
30 */
32{
33 protected:
34 /**
35 * @brief Constructor.
36 *
37 * @param [in] name The name of the new TestCase created
38 */
40 : TestCase(name)
41 {
42 }
43
44 public:
45 /**
46 * Serialize the given header in a buffer, then create a new header by
47 * deserializing from the buffer and serialize the new header into a new buffer.
48 * Verify that the two buffers have the same size and the same content.
49 *
50 * @tparam T \deduced Type of the given header
51 * @tparam Args \deduced Type of arguments to pass to the constructor of the header
52 * @param [in] hdr the header to test
53 * @param [in] args the arguments to construct the new header
54 */
55 template <typename T, typename... Args>
56 void TestHeaderSerialization(const T& hdr, Args&&... args);
57};
58
59} // namespace ns3
60
61/***************************************************************
62 * Implementation of the templates declared above.
63 ***************************************************************/
64
65namespace ns3
66{
67
68template <typename T, typename... Args>
69void
71{
72 Buffer buffer;
73 buffer.AddAtStart(hdr.GetSerializedSize());
74 hdr.Serialize(buffer.Begin());
75
76 T otherHdr(std::forward<Args>(args)...);
77 otherHdr.Deserialize(buffer.Begin());
78 Buffer otherBuffer;
79 otherBuffer.AddAtStart(otherHdr.GetSerializedSize());
80 otherHdr.Serialize(otherBuffer.Begin());
81
82 NS_TEST_ASSERT_MSG_EQ(buffer.GetSize(), otherBuffer.GetSize(), "Size of buffers differs");
83
84 Buffer::Iterator bufferIterator = buffer.Begin();
85 Buffer::Iterator otherBufferIterator = otherBuffer.Begin();
86 for (uint32_t j = 0; j < buffer.GetSize(); j++)
87 {
88 uint8_t bufferVal = bufferIterator.ReadU8();
89 uint8_t otherBufferVal = otherBufferIterator.ReadU8();
90 NS_TEST_EXPECT_MSG_EQ(+bufferVal,
91 +otherBufferVal,
92 "Serialization -> Deserialization -> Serialization "
93 << "is different than Serialization at byte " << j);
94 }
95}
96
97} // namespace ns3
98
99#endif /* NS3_TEST_HDR_SERIALIZE_H */
iterator in a Buffer instance
Definition buffer.h:89
automatically resized byte buffer
Definition buffer.h:83
uint32_t GetSize() const
Definition buffer.h:1057
void AddAtStart(uint32_t start)
Definition buffer.cc:303
Buffer::Iterator Begin() const
Definition buffer.h:1063
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
void TestHeaderSerialization(const T &hdr, Args &&... args)
Serialize the given header in a buffer, then create a new header by deserializing from the buffer and...
HeaderSerializationTestCase(std::string name)
Constructor.
encapsulates test code
Definition test.h:1050
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:134
#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:241
Every class exported by the ns3 library is enclosed in the ns3 namespace.