A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
24
namespace
ns3
25
{
26
27
/**
28
* Subclass of TestCase class adding the ability to test the serialization and
29
* deserialization of a Header object.
30
*/
31
class
HeaderSerializationTestCase
:
public
TestCase
32
{
33
protected
:
34
/**
35
* @brief Constructor.
36
*
37
* @param [in] name The name of the new TestCase created
38
*/
39
HeaderSerializationTestCase
(std::string name)
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
65
namespace
ns3
66
{
67
68
template
<
typename
T,
typename
... Args>
69
void
70
HeaderSerializationTestCase::TestHeaderSerialization
(
const
T& hdr, Args&&... args)
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 */
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1016
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::GetSize
uint32_t GetSize() const
Definition
buffer.h:1057
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition
buffer.cc:303
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::HeaderSerializationTestCase
Subclass of TestCase class adding the ability to test the serialization and deserialization of a Head...
Definition
header-serialization-test.h:32
ns3::HeaderSerializationTestCase::TestHeaderSerialization
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...
Definition
header-serialization-test.h:70
ns3::HeaderSerializationTestCase::HeaderSerializationTestCase
HeaderSerializationTestCase(std::string name)
Constructor.
Definition
header-serialization-test.h:39
ns3::TestCase
encapsulates test code
Definition
test.h:1050
uint32_t
NS_TEST_ASSERT_MSG_EQ
#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
NS_TEST_EXPECT_MSG_EQ
#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
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
network
utils
header-serialization-test.h
Generated on Mon Jul 14 2025 11:07:02 for ns-3 by
1.11.0