20 #include "ns3/packet.h"
21 #include "ns3/packet-tag-list.h"
60 .SetGroupName (
"Network")
61 .HideFromDocumentation ()
93 std::ostringstream oss;
94 oss <<
"anon::ATestTag<" << N <<
">";
97 .SetGroupName (
"Network")
98 .HideFromDocumentation ()
107 return N +
sizeof(m_data);
111 for (uint32_t i = 0; i < N; ++i)
118 for (uint32_t i = 0; i < N; ++i)
120 uint8_t v = buf.
ReadU8 ();
127 virtual void Print (std::ostream &os)
const {
128 os << N <<
"(" << m_data <<
")";
140 #define LARGE_TAG_BUFFER_SIZE 64
158 m_data.push_back (i);
170 .SetGroupName (
"Network")
171 .HideFromDocumentation ()
180 return (uint32_t) m_size;
184 for (uint8_t i = 0; i < (m_size - 1); ++i)
191 for (uint8_t i = 0; i < (m_size - 1); ++i)
193 uint8_t v = buf.
ReadU8 ();
194 m_data.push_back (v);
197 virtual void Print (std::ostream &os)
const {
198 os <<
"(" << (uint16_t) m_size <<
")";
225 .SetGroupName (
"Network")
226 .HideFromDocumentation ()
251 std::ostringstream oss;
252 oss <<
"anon::ATestHeader<" << N <<
">";
255 .SetGroupName (
"Network")
256 .HideFromDocumentation ()
268 for (uint32_t i = 0; i < N; ++i)
274 for (uint32_t i = 0; i < N; ++i)
276 uint8_t v = iter.
ReadU8 ();
284 virtual void Print (std::ostream &os)
const {
311 .SetGroupName (
"Network")
312 .HideFromDocumentation ()
337 std::ostringstream oss;
338 oss <<
"anon::ATestTrailer<" << N <<
">";
341 .SetGroupName (
"Network")
342 .HideFromDocumentation ()
355 for (uint32_t i = 0; i < N; ++i)
362 for (uint32_t i = 0; i < N; ++i)
364 uint8_t v = iter.
ReadU8 ();
372 virtual void Print (std::ostream &os)
const {
395 Expected (uint32_t n_, uint32_t start_, uint32_t end_)
396 : n (n_),
start (start_), end (end_),
data(0) {}
405 Expected (uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
406 : n (n_),
start (start_), end (end_),
data(data_) {}
417 #define E(name,start,end) name,start,end
420 #define E_DATA(name,start,end,data) name,start,end,data
423 #define CHECK(p, n, ...) \
424 DoCheck (p, n, __VA_ARGS__)
427 #define CHECK_DATA(p, n, ...) \
428 DoCheckData (p, n, __VA_ARGS__)
440 virtual void DoRun (
void);
466 std::vector<struct Expected> expected;
469 for (uint32_t
k = 0;
k < n; ++
k)
471 uint32_t N = va_arg (ap, uint32_t);
472 uint32_t
start = va_arg (ap, uint32_t);
473 uint32_t end = va_arg (ap, uint32_t);
474 expected.push_back (Expected (N,
start, end));
480 while (i.
HasNext () && j < expected.size ())
483 struct Expected e = expected[j];
484 std::ostringstream oss;
485 oss <<
"anon::ATestTag<" << e.n <<
">";
503 std::vector<struct Expected> expected;
506 for (uint32_t
k = 0;
k < n; ++
k)
508 uint32_t N = va_arg (ap, uint32_t);
509 uint32_t
start = va_arg (ap, uint32_t);
510 uint32_t end = va_arg (ap, uint32_t);
511 int data = va_arg (ap,
int);
512 expected.push_back (Expected (N,
start, end,
data));
518 while (i.
HasNext () && j < expected.size ())
521 struct Expected e = expected[j];
522 std::ostringstream oss;
523 oss <<
"anon::ATestTag<" << e.n <<
">";
542 Ptr<Packet> pkt1 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
"hello"), 5);
543 Ptr<Packet> pkt2 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
" world"), 6);
550 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
553 std::string msg = std::string (
reinterpret_cast<const char *
>(buf),
563 CHECK (p, 1,
E (1, 0, 1000));
565 CHECK (copy, 1,
E (1, 0, 1000));
568 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
569 CHECK (copy, 1,
E (1, 0, 1000));
575 CHECK (&c0, 1,
E (1, 0, 1000));
576 CHECK (&c1, 1,
E (1, 0, 1000));
577 CHECK (copy, 1,
E (1, 0, 1000));
579 CHECK (&c0, 2,
E (1, 0, 1000),
E (10, 0, 1000));
580 CHECK (&c1, 1,
E (1, 0, 1000));
581 CHECK (copy, 1,
E (1, 0, 1000));
588 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
590 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
592 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
595 CHECK (frag1, 6,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90),
E (1, 90, 990),
E (2, 90, 990),
E (5, 90, 990));
597 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
600 E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10),
601 E (1, 10, 100),
E (2, 10, 100),
E (4, 10, 100),
602 E (1, 100, 1000),
E (2, 100, 1000),
E (5, 100, 1000));
606 frag0 = Create<Packet> (1000);
610 p = Create<Packet> (1000);
612 CHECK (p, 1,
E (20, 0, 1000));
614 CHECK (p, 1,
E (20, 0, 1000));
615 CHECK (frag0, 1,
E (20, 0, 90));
618 CHECK (frag0, 1,
E (20, 10, 100));
623 CHECK (tmp, 1,
E (20, 0, 100));
625 CHECK (tmp, 1,
E (20, 10, 110));
628 CHECK (tmp, 1,
E (20, 0, 100));
630 CHECK (tmp, 1,
E (20, 10, 110));
632 tmp = Create<Packet> (100);
634 CHECK (tmp, 1,
E (20, 0, 100));
636 CHECK (tmp, 1,
E (20, 0, 100));
639 CHECK (tmp, 1,
E (20, 0, 100));
641 CHECK (tmp, 1,
E (20, 0, 100));
649 CHECK (tmp, 1,
E (20, 0, 156));
651 CHECK (tmp, 1,
E (20, 0, 36));
654 CHECK (a, 1,
E (20, 0, 36));
660 CHECK (tmp, 0,
E (20, 0, 0));
665 CHECK (tmp, 1,
E (20, 0, 1000));
667 CHECK (tmp, 0,
E (0,0,0));
670 CHECK (a, 1,
E (10, 0, 10));
672 CHECK (tmp, 1,
E (10, 0, 10));
718 uint8_t* buffer =
new uint8_t[serializedSize + 16];
721 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
749 CHECK (p1, 3,
E (10, 0, 1000),
E (11, 0, 1000),
E (12, 0, 1000));
752 uint8_t* buffer =
new uint8_t[serializedSize];
755 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
759 CHECK_DATA (p2, 3,
E_DATA (10, 0, 1000, 65),
E_DATA (11, 0, 1000, 66),
E_DATA (12, 0, 1000, 67));
767 CHECK (tmp, 1,
E (20, 0, 1000));
769 CHECK (tmp, 1,
E (20, 2, 1002));
771 CHECK (tmp, 1,
E (20, 1, 1001));
774 CHECK (tmp, 1,
E (20, 1, 1001));
783 CHECK (tmp, 1,
E (25, 0, 100));
785 CHECK (tmp, 1,
E (25, 0, 50));
787 CHECK (tmp, 1,
E (25, 50, 100));
795 CHECK (tmp, 1,
E (25, 0, 100));
797 CHECK (tmp, 1,
E (25, 0, 50));
799 CHECK (tmp, 1,
E (25, 0, 50));
807 CHECK (tmp, 1,
E (25, 0, 100));
809 CHECK (tmp, 1,
E (25, 0, 50));
811 CHECK (tmp, 1,
E (25, 25, 75));
819 CHECK (tmp, 1,
E (25, 0, 100));
821 CHECK (tmp, 1,
E (25, 0, 50));
823 CHECK (tmp, 1,
E (25, 0, 50));
831 CHECK (tmp, 1,
E (25, 0, 100));
833 CHECK (tmp, 1,
E (25, 0, 50));
835 CHECK (tmp, 1,
E (25, 0, 50));
845 CHECK (tmp, 1,
E (25, 0, 100));
847 CHECK (tmp, 1,
E (25, 0, 50));
849 CHECK (tmp, 1,
E (25, 0, 50));
903 const char * msg = 0);
928 int expect = t.GetData ();
929 bool found = ref.
Peek (t);
931 msg <<
": ref contains "
932 << t.GetTypeId ().GetName ());
935 msg <<
": ref " << t.GetTypeId ().GetName ()
941 #define MAKE_TEST_TAGS \
942 ATestTag<1> t1 (1); \
943 ATestTag<2> t2 (1); \
944 ATestTag<3> t3 (1); \
945 ATestTag<4> t4 (1); \
946 ATestTag<5> t5 (1); \
947 ATestTag<6> t6 (1); \
948 ATestTag<7> t7 (1); \
949 [[maybe_unused]] const int tagLast = 7;
971 const int reps = 10000;
972 std::vector< PacketTagList > ptv(reps, ref);
973 int start = clock ();
974 for (
int i = 0; i < reps; ++i) {
978 int delta = stop -
start;
980 std::cout <<
GetName () <<
"remove time: " << msg <<
": " << std::setw (8)
981 << delta <<
" ticks to remove "
991 const int reps = 100000;
994 int start = clock ();
995 for (
int i = 0; i < reps; ++i) {
1000 int delta = stop -
start;
1002 std::cout <<
GetName () <<
"add/remove time: " << std::setw (8)
1003 << delta <<
" ticks to add+remove "
1013 std::cout <<
GetName () <<
"begin" << std::endl;
1027 std::cout <<
GetName () <<
"check Peek (missing tag) returns false"
1034 std::cout <<
GetName () <<
"check copy and assignment" << std::endl;
1046 # define RemoveCheck(n) \
1047 { PacketTagList p ## n = ref; \
1048 p ## n .Remove ( t ## n ); \
1049 CheckRefList (ref, "remove " #n " orig"); \
1050 CheckRefList (p ## n, "remove " #n " copy", n); \
1054 std::cout <<
GetName () <<
"check removal of each tag" << std::endl;
1065 std::cout <<
GetName () <<
"check removal doesn't disturb merge "
1081 const char * msg =
"post merge, short chain";
1093 std::cout <<
GetName () <<
"check replacing each tag" << std::endl;
1095 # define ReplaceCheck(n) \
1096 t ## n .m_data = 2; \
1097 { PacketTagList p ## n = ref; \
1098 p ## n .Replace ( t ## n ); \
1099 CheckRefList (ref, "replace " #n " orig"); \
1100 CheckRef (p ## n, t ## n, "replace " #n " copy"); \
1113 std::cout <<
GetName () <<
"add+remove timing" << std::endl;
1115 const int nIterations = 100;
1116 for (
int i = 0; i < nIterations; ++i) {
1118 if (now < flm) flm = now;
1120 std::cout <<
GetName () <<
"min add+remove time: "
1121 << std::setw (8) << flm <<
" ticks"
1124 std::cout <<
GetName () <<
"remove timing" << std::endl;
1127 for (
int i = 0; i < nIterations; ++i) {
1128 for (
int j = 1; j <= tagLast; ++j) {
1140 if (now < rmn[j]) rmn[j] = now;
1143 for (
int j = tagLast; j > 0; --j) {
1144 std::cout <<
GetName () <<
"min remove time: t"
1146 << std::setw (8) << rmn[j] <<
" ticks"
Packet Tag list unit tests.
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
Checks against a reference PacketTagList.
int AddRemoveTime(const bool verbose=false)
Prints the remove time.
virtual ~PacketTagListTest()
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
void DoRun(void)
Implementation to actually run this TestCase.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
Prints the remove time.
void DoCheckData(Ptr< const Packet > p, uint32_t n,...)
Checks the packet and its data.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void DoCheck(Ptr< const Packet > p, uint32_t n,...)
Checks the packet.
Template class for Large Test tags.
static TypeId GetTypeId(void)
Register this type.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual void Deserialize(TagBuffer buf)
virtual void Print(std::ostream &os) const
uint8_t m_size
Packet size.
virtual uint32_t GetSerializedSize(void) const
std::vector< uint8_t > m_data
Tag data.
virtual void Serialize(TagBuffer buf) const
Base class for Test tags.
static TypeId GetTypeId(void)
Register this type.
bool m_error
Error in the Tag.
int GetData() const
Get the tag data.
ATestTagBase(uint8_t data)
Constructor.
Template class for Test tags.
virtual void Serialize(TagBuffer buf) const
ATestTag(uint8_t data)
Constructor.
virtual void Deserialize(TagBuffer buf)
virtual uint32_t GetSerializedSize(void) const
virtual void Print(std::ostream &os) const
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
static TypeId GetTypeId(void)
Register this type.
Base class for Test trailers.
static TypeId GetTypeId(void)
Register this type.
bool m_error
Error in the Trailer.
Template class for Test trailers.
virtual void Print(std::ostream &os) const
virtual uint32_t GetSerializedSize(void) const
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
virtual void Serialize(Buffer::Iterator iter) const
virtual uint32_t Deserialize(Buffer::Iterator iter)
static TypeId GetTypeId(void)
Register this type.
iterator in a Buffer instance
void WriteU8(uint8_t data)
void Prev(void)
go backward by one byte
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
TypeId GetTypeId(void) const
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
Iterator over the set of byte tags in a packet.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
void AddHeader(const Header &header)
Add header to this packet.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
void RemoveAllPacketTags(void)
Remove all packet tags.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
bool Peek(Tag &tag) const
Find a tag and return its value.
void Add(Tag const &tag) const
Add a tag to the head of this branch.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
TAG_BUFFER_INLINE uint8_t ReadU8(void)
tag a set of bytes in a packet
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
std::string GetName(void) const
Protocol trailer serialization and deserialization.
a unique identifier for an interface.
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
std::string GetName(void) const
Get the name.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define LARGE_TAG_BUFFER_SIZE
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
#define E_DATA(name, start, end, data)
#define E(name, start, end)
#define CHECK_DATA(p, n,...)
Struct to hold the expected data in the packet.
uint8_t data
Optional data.
Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
Constructor.
uint32_t n
Number of elements.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.