A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/packet-tag-list.h"
9#include "ns3/packet.h"
10#include "ns3/test.h"
11
12#include <cstdarg>
13#include <ctime>
14#include <iomanip>
15#include <iostream>
16#include <limits> // std:numeric_limits
17#include <string>
18#include <vector>
19
20using namespace ns3;
21
22//-----------------------------------------------------------------------------
23// Unit tests
24//-----------------------------------------------------------------------------
25namespace
26{
27
28/**
29 * @ingroup network-test
30 * @ingroup tests
31 *
32 * @brief Base class for Test tags
33 *
34 * @note Class internal to packet-test-suite.cc
35 */
36class ATestTagBase : public Tag
37{
38 public:
40 : m_error(false),
41 m_data(0)
42 {
43 }
44
45 /// Constructor
46 /// @param data Tag data
48 : m_error(false),
50 {
51 }
52
53 /**
54 * Register this type.
55 * @return The TypeId.
56 */
58 {
59 static TypeId tid =
60 TypeId("ATestTagBase").SetParent<Tag>().SetGroupName("Network").HideFromDocumentation()
61 // No AddConstructor because this is an abstract class.
62 ;
63 return tid;
64 }
65
66 /// Get the tag data.
67 /// @return the tag data.
68 int GetData() const
69 {
70 int result = (int)m_data;
71 return result;
72 }
73
74 bool m_error; //!< Error in the Tag
75 uint8_t m_data; //!< Tag data
76};
77
78/**
79 * @ingroup network-test
80 * @ingroup tests
81 *
82 * @brief Template class for Test tags
83 *
84 * @note Class internal to packet-test-suite.cc
85 */
86template <int N>
87class ATestTag : public ATestTagBase
88{
89 public:
90 /**
91 * Register this type.
92 * @return The TypeId.
93 */
95 {
96 std::ostringstream oss;
97 oss << "anon::ATestTag<" << N << ">";
98 static TypeId tid = TypeId(oss.str())
100 .SetGroupName("Network")
101 .HideFromDocumentation()
102 .AddConstructor<ATestTag<N>>();
103 return tid;
104 }
105
106 TypeId GetInstanceTypeId() const override
107 {
108 return GetTypeId();
109 }
110
112 {
113 return N + sizeof(m_data);
114 }
115
116 void Serialize(TagBuffer buf) const override
117 {
118 buf.WriteU8(m_data);
119 for (uint32_t i = 0; i < N; ++i)
120 {
121 buf.WriteU8(N);
122 }
123 }
124
125 void Deserialize(TagBuffer buf) override
126 {
127 m_data = buf.ReadU8();
128 for (uint32_t i = 0; i < N; ++i)
129 {
130 uint8_t v = buf.ReadU8();
131 if (v != N)
132 {
133 m_error = true;
134 }
135 }
136 }
137
138 void Print(std::ostream& os) const override
139 {
140 os << N << "(" << m_data << ")";
141 }
142
144 : ATestTagBase()
145 {
146 }
147
148 /// Constructor
149 /// @param data Tag data
150 ATestTag(uint8_t data)
152 {
153 }
154};
155
156// Previous versions of ns-3 limited the tag size to 20 bytes or less
157// static const uint8_t LARGE_TAG_BUFFER_SIZE = 64;
158#define LARGE_TAG_BUFFER_SIZE 64
159
160/**
161 * @ingroup network-test
162 * @ingroup tests
163 *
164 * @brief Template class for Large Test tags
165 *
166 * @see Bug 2221: Expanding packet tag maximum size
167 *
168 * @note Class internal to packet-test-suite.cc
169 */
170class ALargeTestTag : public Tag
171{
172 public:
174 {
175 for (uint8_t i = 0; i < (LARGE_TAG_BUFFER_SIZE - 1); i++)
176 {
177 m_data.push_back(i);
178 }
180 }
181
182 /**
183 * Register this type.
184 * @return The TypeId.
185 */
187 {
188 static TypeId tid = TypeId("ALargeTestTag")
189 .SetParent<Tag>()
190 .SetGroupName("Network")
191 .HideFromDocumentation()
192 .AddConstructor<ALargeTestTag>();
193 return tid;
194 }
195
196 TypeId GetInstanceTypeId() const override
197 {
198 return GetTypeId();
199 }
200
202 {
203 return (uint32_t)m_size;
204 }
205
206 void Serialize(TagBuffer buf) const override
207 {
208 buf.WriteU8(m_size);
209 for (uint8_t i = 0; i < (m_size - 1); ++i)
210 {
211 buf.WriteU8(m_data[i]);
212 }
213 }
214
215 void Deserialize(TagBuffer buf) override
216 {
217 m_size = buf.ReadU8();
218 for (uint8_t i = 0; i < (m_size - 1); ++i)
219 {
220 uint8_t v = buf.ReadU8();
221 m_data.push_back(v);
222 }
223 }
224
225 void Print(std::ostream& os) const override
226 {
227 os << "(" << (uint16_t)m_size << ")";
228 }
229
230 private:
231 uint8_t m_size; //!< Packet size
232 std::vector<uint8_t> m_data; //!< Tag data
233};
234
235/**
236 * @ingroup network-test
237 * @ingroup tests
238 *
239 * @brief Base class for Test headers
240 *
241 * @note Class internal to packet-test-suite.cc
242 */
244{
245 public:
247 : Header(),
248 m_error(false)
249 {
250 }
251
252 /**
253 * Register this type.
254 * @return The TypeId.
255 */
257 {
258 static TypeId tid = TypeId("ATestHeaderBase")
259 .SetParent<Header>()
260 .SetGroupName("Network")
261 .HideFromDocumentation()
262 // No AddConstructor because this is an abstract class.
263 ;
264 return tid;
265 }
266
267 bool m_error; //!< Error in the Header
268};
269
270/**
271 * @ingroup network-test
272 * @ingroup tests
273 *
274 * @brief Template class for Test headers
275 *
276 * @note Class internal to packet-test-suite.cc
277 */
278template <int N>
280{
281 public:
282 /**
283 * Register this type.
284 * @return The TypeId.
285 */
287 {
288 std::ostringstream oss;
289 oss << "anon::ATestHeader<" << N << ">";
290 static TypeId tid = TypeId(oss.str())
292 .SetGroupName("Network")
293 .HideFromDocumentation()
294 .AddConstructor<ATestHeader<N>>();
295 return tid;
296 }
297
298 TypeId GetInstanceTypeId() const override
299 {
300 return GetTypeId();
301 }
302
304 {
305 return N;
306 }
307
308 void Serialize(Buffer::Iterator iter) const override
309 {
310 for (uint32_t i = 0; i < N; ++i)
311 {
312 iter.WriteU8(N);
313 }
314 }
315
317 {
318 for (uint32_t i = 0; i < N; ++i)
319 {
320 uint8_t v = iter.ReadU8();
321 if (v != N)
322 {
323 m_error = true;
324 }
325 }
326 return N;
327 }
328
329 void Print(std::ostream& os) const override
330 {
331 }
332
335 {
336 }
337};
338
339/**
340 * @ingroup network-test
341 * @ingroup tests
342 *
343 * @brief Base class for Test trailers
344 *
345 * @note Class internal to packet-test-suite.cc
346 */
348{
349 public:
351 : Trailer(),
352 m_error(false)
353 {
354 }
355
356 /**
357 * Register this type.
358 * @return The TypeId.
359 */
361 {
362 static TypeId tid = TypeId("ATestTrailerBase")
364 .SetGroupName("Network")
365 .HideFromDocumentation()
366 // No AddConstructor because this is an abstract class.
367 ;
368 return tid;
369 }
370
371 bool m_error; //!< Error in the Trailer
372};
373
374/**
375 * @ingroup network-test
376 * @ingroup tests
377 *
378 * @brief Template class for Test trailers
379 *
380 * @note Class internal to packet-test-suite.cc
381 */
382template <int N>
384{
385 public:
386 /**
387 * Register this type.
388 * @return The TypeId.
389 */
391 {
392 std::ostringstream oss;
393 oss << "anon::ATestTrailer<" << N << ">";
394 static TypeId tid = TypeId(oss.str())
396 .SetGroupName("Network")
397 .HideFromDocumentation()
398 .AddConstructor<ATestTrailer<N>>();
399 return tid;
400 }
401
402 TypeId GetInstanceTypeId() const override
403 {
404 return GetTypeId();
405 }
406
408 {
409 return N;
410 }
411
412 void Serialize(Buffer::Iterator iter) const override
413 {
414 iter.Prev(N);
415 for (uint32_t i = 0; i < N; ++i)
416 {
417 iter.WriteU8(N);
418 }
419 }
420
422 {
423 iter.Prev(N);
424 for (uint32_t i = 0; i < N; ++i)
425 {
426 uint8_t v = iter.ReadU8();
427 if (v != N)
428 {
429 m_error = true;
430 }
431 }
432 return N;
433 }
434
435 void Print(std::ostream& os) const override
436 {
437 }
438
441 {
442 }
443};
444
445/**
446 * @ingroup network-test
447 * @ingroup tests
448 *
449 * @brief Struct to hold the expected data in the packet
450 *
451 * @note Class internal to packet-test-suite.cc
452 */
454{
455 /**
456 * Constructor
457 * @param n_ Number of elements
458 * @param start_ Start
459 * @param end_ End
460 */
462 : n(n_),
463 start(start_),
464 end(end_),
465 data(0)
466 {
467 }
468
469 /**
470 * Constructor
471 * @param n_ Number of elements
472 * @param start_ Start
473 * @param end_ End
474 * @param data_ Data stored in tag
475 */
476 Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
477 : n(n_),
478 start(start_),
479 end(end_),
480 data(data_)
481 {
482 }
483
484 uint32_t n; //!< Number of elements
485 uint32_t start; //!< Start
486 uint32_t end; //!< End
487 uint8_t data; //!< Optional data
488};
489
490} // namespace
491
492// tag name, start, end
493#define E(name, start, end) name, start, end
494
495// tag name, start, end, data
496#define E_DATA(name, start, end, data) name, start, end, data
497
498// Check byte tags on a packet, checks name, start, end
499#define CHECK(p, n, ...) DoCheck(p, n, __VA_ARGS__)
500
501// Check byte tags on a packet, checks name, start, end, data
502#define CHECK_DATA(p, n, ...) DoCheckData(p, n, __VA_ARGS__)
503
504/**
505 * @ingroup network-test
506 * @ingroup tests
507 *
508 * Packet unit tests.
509 */
510class PacketTest : public TestCase
511{
512 public:
513 PacketTest();
514 void DoRun() override;
515
516 private:
517 /**
518 * Checks the packet
519 * @param p The packet
520 * @param n The number of variable arguments
521 * @param ... The variable arguments
522 */
523 void DoCheck(Ptr<const Packet> p, uint32_t n, ...);
524 /**
525 * Checks the packet and its data
526 * @param p The packet
527 * @param n The number of variable arguments
528 * @param ... The variable arguments
529 */
530 void DoCheckData(Ptr<const Packet> p, uint32_t n, ...);
531};
532
534 : TestCase("Packet")
535{
536}
537
538void
540{
541 std::vector<Expected> expected;
542 va_list ap;
543 va_start(ap, n);
544 for (uint32_t k = 0; k < n; ++k)
545 {
546 uint32_t N = va_arg(ap, uint32_t);
547 uint32_t start = va_arg(ap, uint32_t);
548 uint32_t end = va_arg(ap, uint32_t);
549 expected.emplace_back(N, start, end);
550 }
551 va_end(ap);
552
553 ByteTagIterator i = p->GetByteTagIterator();
554 uint32_t j = 0;
555 while (i.HasNext() && j < expected.size())
556 {
557 ByteTagIterator::Item item = i.Next();
558 Expected e = expected[j];
559 std::ostringstream oss;
560 oss << "anon::ATestTag<" << e.n << ">";
561 NS_TEST_EXPECT_MSG_EQ(item.GetTypeId().GetName(), oss.str(), "trivial");
562 NS_TEST_EXPECT_MSG_EQ(item.GetStart(), e.start, "trivial");
563 NS_TEST_EXPECT_MSG_EQ(item.GetEnd(), e.end, "trivial");
564 ATestTagBase* tag = dynamic_cast<ATestTagBase*>(item.GetTypeId().GetConstructor()());
565 NS_TEST_EXPECT_MSG_NE(tag, 0, "trivial");
566 item.GetTag(*tag);
567 NS_TEST_EXPECT_MSG_EQ(tag->m_error, false, "trivial");
568 delete tag;
569 j++;
570 }
571 NS_TEST_EXPECT_MSG_EQ(i.HasNext(), false, "Nothing left");
572 NS_TEST_EXPECT_MSG_EQ(j, expected.size(), "Size match");
573}
574
575void
577{
578 std::vector<Expected> expected;
579 va_list ap;
580 va_start(ap, n);
581 for (uint32_t k = 0; k < n; ++k)
582 {
583 uint32_t N = va_arg(ap, uint32_t);
584 uint32_t start = va_arg(ap, uint32_t);
585 uint32_t end = va_arg(ap, uint32_t);
586 int data = va_arg(ap, int);
587 expected.emplace_back(N, start, end, data);
588 }
589 va_end(ap);
590
591 ByteTagIterator i = p->GetByteTagIterator();
592 uint32_t j = 0;
593 while (i.HasNext() && j < expected.size())
594 {
595 ByteTagIterator::Item item = i.Next();
596 Expected e = expected[j];
597 std::ostringstream oss;
598 oss << "anon::ATestTag<" << e.n << ">";
599 NS_TEST_EXPECT_MSG_EQ(item.GetTypeId().GetName(), oss.str(), "trivial");
600 NS_TEST_EXPECT_MSG_EQ(item.GetStart(), e.start, "trivial");
601 NS_TEST_EXPECT_MSG_EQ(item.GetEnd(), e.end, "trivial");
602 ATestTagBase* tag = dynamic_cast<ATestTagBase*>(item.GetTypeId().GetConstructor()());
603 NS_TEST_EXPECT_MSG_NE(tag, 0, "trivial");
604 item.GetTag(*tag);
605 NS_TEST_EXPECT_MSG_EQ(tag->m_error, false, "trivial");
606 NS_TEST_EXPECT_MSG_EQ(tag->GetData(), e.data, "trivial");
607 delete tag;
608 j++;
609 }
610 NS_TEST_EXPECT_MSG_EQ(i.HasNext(), false, "Nothing left");
611 NS_TEST_EXPECT_MSG_EQ(j, expected.size(), "Size match");
612}
613
614void
616{
617 Ptr<Packet> pkt1 = Create<Packet>(reinterpret_cast<const uint8_t*>("hello"), 5);
618 Ptr<Packet> pkt2 = Create<Packet>(reinterpret_cast<const uint8_t*>(" world"), 6);
619 Ptr<Packet> packet = Create<Packet>();
620 packet->AddAtEnd(pkt1);
621 packet->AddAtEnd(pkt2);
622
623 NS_TEST_EXPECT_MSG_EQ(packet->GetSize(), 11, "trivial");
624
625 auto buf = new uint8_t[packet->GetSize()];
626 packet->CopyData(buf, packet->GetSize());
627
628 std::string msg = std::string(reinterpret_cast<const char*>(buf), packet->GetSize());
629 delete[] buf;
630
631 NS_TEST_EXPECT_MSG_EQ(msg, "hello world", "trivial");
632
634
635 p->AddByteTag(ATestTag<1>());
636 CHECK(p, 1, E(1, 0, 1000));
637 Ptr<const Packet> copy = p->Copy();
638 CHECK(copy, 1, E(1, 0, 1000));
639
640 p->AddByteTag(ATestTag<2>());
641 CHECK(p, 2, E(1, 0, 1000), E(2, 0, 1000));
642 CHECK(copy, 1, E(1, 0, 1000));
643
644 {
645 Packet c0 = *copy;
646 Packet c1 = *copy; // NOLINT(performance-unnecessary-copy-initialization)
647 c0 = c1;
648 CHECK(&c0, 1, E(1, 0, 1000));
649 CHECK(&c1, 1, E(1, 0, 1000));
650 CHECK(copy, 1, E(1, 0, 1000));
652 CHECK(&c0, 2, E(1, 0, 1000), E(10, 0, 1000));
653 CHECK(&c1, 1, E(1, 0, 1000));
654 CHECK(copy, 1, E(1, 0, 1000));
655 }
656
657 Ptr<Packet> frag0 = p->CreateFragment(0, 10);
658 Ptr<Packet> frag1 = p->CreateFragment(10, 90);
659 Ptr<const Packet> frag2 = p->CreateFragment(100, 900);
660 frag0->AddByteTag(ATestTag<3>());
661 CHECK(frag0, 3, E(1, 0, 10), E(2, 0, 10), E(3, 0, 10));
662 frag1->AddByteTag(ATestTag<4>());
663 CHECK(frag1, 3, E(1, 0, 90), E(2, 0, 90), E(4, 0, 90));
664 frag2->AddByteTag(ATestTag<5>());
665 CHECK(frag2, 3, E(1, 0, 900), E(2, 0, 900), E(5, 0, 900));
666
667 frag1->AddAtEnd(frag2);
668 CHECK(frag1,
669 6,
670 E(1, 0, 90),
671 E(2, 0, 90),
672 E(4, 0, 90),
673 E(1, 90, 990),
674 E(2, 90, 990),
675 E(5, 90, 990));
676
677 CHECK(frag0, 3, E(1, 0, 10), E(2, 0, 10), E(3, 0, 10));
678 frag0->AddAtEnd(frag1);
679 CHECK(frag0,
680 9,
681 E(1, 0, 10),
682 E(2, 0, 10),
683 E(3, 0, 10),
684 E(1, 10, 100),
685 E(2, 10, 100),
686 E(4, 10, 100),
687 E(1, 100, 1000),
688 E(2, 100, 1000),
689 E(5, 100, 1000));
690
691 // force caching a buffer of the right size.
692 frag0 = Create<Packet>(1000);
693 frag0->AddHeader(ATestHeader<10>());
694 frag0 = nullptr;
695
696 p = Create<Packet>(1000);
697 p->AddByteTag(ATestTag<20>());
698 CHECK(p, 1, E(20, 0, 1000));
699 frag0 = p->CreateFragment(10, 90);
700 CHECK(p, 1, E(20, 0, 1000));
701 CHECK(frag0, 1, E(20, 0, 90));
702 p = nullptr;
703 frag0->AddHeader(ATestHeader<10>());
704 CHECK(frag0, 1, E(20, 10, 100));
705
706 {
707 Ptr<Packet> tmp = Create<Packet>(100);
708 tmp->AddByteTag(ATestTag<20>());
709 CHECK(tmp, 1, E(20, 0, 100));
710 tmp->AddHeader(ATestHeader<10>());
711 CHECK(tmp, 1, E(20, 10, 110));
713 tmp->RemoveHeader(h);
714 CHECK(tmp, 1, E(20, 0, 100));
715 tmp->AddHeader(ATestHeader<10>());
716 CHECK(tmp, 1, E(20, 10, 110));
717
718 tmp = Create<Packet>(100);
719 tmp->AddByteTag(ATestTag<20>());
720 CHECK(tmp, 1, E(20, 0, 100));
721 tmp->AddTrailer(ATestTrailer<10>());
722 CHECK(tmp, 1, E(20, 0, 100));
724 tmp->RemoveTrailer(t);
725 CHECK(tmp, 1, E(20, 0, 100));
726 tmp->AddTrailer(ATestTrailer<10>());
727 CHECK(tmp, 1, E(20, 0, 100));
728 }
729
730 {
732 tmp->AddHeader(ATestHeader<156>());
733 tmp->AddByteTag(ATestTag<20>());
734 CHECK(tmp, 1, E(20, 0, 156));
735 tmp->RemoveAtStart(120);
736 CHECK(tmp, 1, E(20, 0, 36));
738 a->AddAtEnd(tmp);
739 CHECK(a, 1, E(20, 0, 36));
740 }
741
742 {
744 tmp->AddByteTag(ATestTag<20>());
745 CHECK(tmp, 0, E(20, 0, 0));
746 }
747 {
748 Ptr<Packet> tmp = Create<Packet>(1000);
749 tmp->AddByteTag(ATestTag<20>());
750 CHECK(tmp, 1, E(20, 0, 1000));
751 tmp->RemoveAtStart(1000);
752 CHECK(tmp, 0, E(0, 0, 0));
754 a->AddByteTag(ATestTag<10>());
755 CHECK(a, 1, E(10, 0, 10));
756 tmp->AddAtEnd(a);
757 CHECK(tmp, 1, E(10, 0, 10));
758 }
759
760 {
761 Packet p;
762 ATestTag<10> a;
763 p.AddPacketTag(a);
764 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(a), true, "trivial");
765 ATestTag<11> b;
766 p.AddPacketTag(b);
767 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(b), true, "trivial");
768 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(a), true, "trivial");
769 Packet copy = p;
770 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(b), true, "trivial");
771 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(a), true, "trivial");
772 ATestTag<12> c;
773 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(c), false, "trivial");
774 copy.AddPacketTag(c);
775 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(c), true, "trivial");
776 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(b), true, "trivial");
777 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(a), true, "trivial");
778 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(c), false, "trivial");
779 copy.RemovePacketTag(b);
780 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(b), false, "trivial");
781 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(b), true, "trivial");
782 p.RemovePacketTag(a);
783 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(a), false, "trivial");
784 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(a), true, "trivial");
785 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(c), false, "trivial");
786 NS_TEST_EXPECT_MSG_EQ(copy.PeekPacketTag(c), true, "trivial");
788 NS_TEST_EXPECT_MSG_EQ(p.PeekPacketTag(b), false, "trivial");
789 }
790
791 /* Test Serialization and Deserialization of Packet with PacketTag data */
792 {
793 Ptr<Packet> p1 = Create<Packet>(1000);
794 ;
795 ATestTag<10> a1(65);
796 ATestTag<11> b1(66);
797 ATestTag<12> c1(67);
798
799 p1->AddPacketTag(a1);
800 p1->AddPacketTag(b1);
801 p1->AddPacketTag(c1);
802
803 uint32_t serializedSize = p1->GetSerializedSize();
804 auto buffer = new uint8_t[serializedSize + 16];
805 p1->Serialize(buffer, serializedSize);
806
807 Ptr<Packet> p2 = Create<Packet>(buffer, serializedSize, true);
808
809 delete[] buffer;
810
811 ATestTag<10> a2;
812 ATestTag<11> b2;
813 ATestTag<12> c2;
814
815 NS_TEST_EXPECT_MSG_EQ(p2->PeekPacketTag(a2), true, "trivial");
816 NS_TEST_EXPECT_MSG_EQ(a2.GetData(), 65, "trivial");
817 NS_TEST_EXPECT_MSG_EQ(p2->PeekPacketTag(b2), true, "trivial");
818 NS_TEST_EXPECT_MSG_EQ(b2.GetData(), 66, "trivial");
819 NS_TEST_EXPECT_MSG_EQ(p2->PeekPacketTag(c2), true, "trivial");
820 NS_TEST_EXPECT_MSG_EQ(c2.GetData(), 67, "trivial");
821 }
822
823 /* Test Serialization and Deserialization of Packet with NixVector */
824 {
825 Ptr<Packet> p1 = Create<Packet>(1000);
827 nix1->AddNeighborIndex(3, 2);
828 nix1->AddNeighborIndex(5, 3);
829 p1->SetNixVector(nix1);
830
831 uint32_t serializedSize = p1->GetSerializedSize();
832 std::vector<uint8_t> buffer(serializedSize);
833 NS_TEST_EXPECT_MSG_EQ(p1->Serialize(buffer.data(), serializedSize),
834 serializedSize,
835 "serialized size mismatch");
836
837 Ptr<Packet> p2 = Create<Packet>(buffer.data(), serializedSize, true);
838
839 Ptr<NixVector> nix2 = p2->GetNixVector();
840 NS_TEST_ASSERT_MSG_NE(nix2, nullptr, "nix-vector not deserialized");
841 NS_TEST_EXPECT_MSG_EQ(nix2->GetRemainingBits(), 5, "trivial");
842 NS_TEST_EXPECT_MSG_EQ(nix2->ExtractNeighborIndex(3), 5, "trivial");
843 NS_TEST_EXPECT_MSG_EQ(nix2->ExtractNeighborIndex(2), 3, "trivial");
844 }
845
846 /* Test Serialization and Deserialization of Packet with ByteTag data */
847 {
848 Ptr<Packet> p1 = Create<Packet>(1000);
849 ;
850
851 ATestTag<10> a1(65);
852 ATestTag<11> b1(66);
853 ATestTag<12> c1(67);
854
855 p1->AddByteTag(a1);
856 p1->AddByteTag(b1);
857 p1->AddByteTag(c1);
858
859 CHECK(p1, 3, E(10, 0, 1000), E(11, 0, 1000), E(12, 0, 1000));
860
861 uint32_t serializedSize = p1->GetSerializedSize();
862 auto buffer = new uint8_t[serializedSize];
863 p1->Serialize(buffer, serializedSize);
864
865 Ptr<Packet> p2 = Create<Packet>(buffer, serializedSize, true);
866
867 delete[] buffer;
868
869 CHECK_DATA(p2,
870 3,
871 E_DATA(10, 0, 1000, 65),
872 E_DATA(11, 0, 1000, 66),
873 E_DATA(12, 0, 1000, 67));
874 }
875
876 {
877 /// @internal
878 /// See \bugid{572}
879 Ptr<Packet> tmp = Create<Packet>(1000);
880 tmp->AddByteTag(ATestTag<20>());
881 CHECK(tmp, 1, E(20, 0, 1000));
882 tmp->AddHeader(ATestHeader<2>());
883 CHECK(tmp, 1, E(20, 2, 1002));
884 tmp->RemoveAtStart(1);
885 CHECK(tmp, 1, E(20, 1, 1001));
886#if 0
887 tmp->PeekData ();
888 CHECK (tmp, 1, E (20, 1, 1001));
889#endif
890 }
891
892 /* Test reducing tagged packet size and increasing it back. */
893 {
895 tmp->AddHeader(ATestHeader<100>());
896 tmp->AddByteTag(ATestTag<25>());
897 CHECK(tmp, 1, E(25, 0, 100));
898 tmp->RemoveAtStart(50);
899 CHECK(tmp, 1, E(25, 0, 50));
900 tmp->AddHeader(ATestHeader<50>());
901 CHECK(tmp, 1, E(25, 50, 100));
902 }
903
904 /* Similar test case, but using trailer instead of header. */
905 {
907 tmp->AddTrailer(ATestTrailer<100>());
908 tmp->AddByteTag(ATestTag<25>());
909 CHECK(tmp, 1, E(25, 0, 100));
910 tmp->RemoveAtEnd(50);
911 CHECK(tmp, 1, E(25, 0, 50));
912 tmp->AddTrailer(ATestTrailer<50>());
913 CHECK(tmp, 1, E(25, 0, 50));
914 }
915
916 /* Test reducing tagged packet size and increasing it by half. */
917 {
919 tmp->AddHeader(ATestHeader<100>());
920 tmp->AddByteTag(ATestTag<25>());
921 CHECK(tmp, 1, E(25, 0, 100));
922 tmp->RemoveAtStart(50);
923 CHECK(tmp, 1, E(25, 0, 50));
924 tmp->AddHeader(ATestHeader<25>());
925 CHECK(tmp, 1, E(25, 25, 75));
926 }
927
928 /* Similar test case, but using trailer instead of header. */
929 {
931 tmp->AddTrailer(ATestTrailer<100>());
932 tmp->AddByteTag(ATestTag<25>());
933 CHECK(tmp, 1, E(25, 0, 100));
934 tmp->RemoveAtEnd(50);
935 CHECK(tmp, 1, E(25, 0, 50));
936 tmp->AddTrailer(ATestTrailer<25>());
937 CHECK(tmp, 1, E(25, 0, 50));
938 }
939
940 /* Test AddPaddingAtEnd. */
941 {
943 tmp->AddTrailer(ATestTrailer<100>());
944 tmp->AddByteTag(ATestTag<25>());
945 CHECK(tmp, 1, E(25, 0, 100));
946 tmp->RemoveAtEnd(50);
947 CHECK(tmp, 1, E(25, 0, 50));
948 tmp->AddPaddingAtEnd(50);
949 CHECK(tmp, 1, E(25, 0, 50));
950 }
951
952 /* Test reducing tagged packet size and increasing it back,
953 * now using padding bytes to avoid triggering dirty state
954 * in virtual buffer
955 */
956 {
957 Ptr<Packet> tmp = Create<Packet>(100);
958 tmp->AddByteTag(ATestTag<25>());
959 CHECK(tmp, 1, E(25, 0, 100));
960 tmp->RemoveAtEnd(50);
961 CHECK(tmp, 1, E(25, 0, 50));
962 tmp->AddPaddingAtEnd(50);
963 CHECK(tmp, 1, E(25, 0, 50));
964 }
965
966 /* Test ALargeTestTag */
967 {
970 tmp->AddPacketTag(a);
971 }
972}
973
974/**
975 * @ingroup network-test
976 * @ingroup tests
977 *
978 * Packet Tag list unit tests.
979 */
981{
982 public:
984 ~PacketTagListTest() override;
985
986 private:
987 void DoRun() override;
988 /**
989 * Checks against a reference PacketTagList
990 * @param ref Reference
991 * @param t List to test
992 * @param msg Message
993 * @param miss Expected miss/hit
994 */
995 void CheckRef(const PacketTagList& ref, ATestTagBase& t, const char* msg, bool miss = false);
996 /**
997 * Checks against a reference PacketTagList
998 * @param ref Reference
999 * @param msg Message
1000 * @param miss Expected miss/hit
1001 */
1002 void CheckRefList(const PacketTagList& ref, const char* msg, int miss = 0);
1003
1004 /**
1005 * Prints the remove time
1006 * @param ref Reference.
1007 * @param t List to test.
1008 * @param msg Message - prints on cout if msg is not null.
1009 * @return the ticks to remove the tags.
1010 */
1011 int RemoveTime(const PacketTagList& ref, ATestTagBase& t, const char* msg = nullptr);
1012
1013 /**
1014 * Prints the remove time
1015 * @param verbose prints on cout if verbose is true.
1016 * @return the ticks to remove the tags.
1017 */
1018 int AddRemoveTime(const bool verbose = false);
1019};
1020
1022 : TestCase("PacketTagListTest: ")
1023{
1024}
1025
1029
1030void
1031PacketTagListTest::CheckRef(const PacketTagList& ref, ATestTagBase& t, const char* msg, bool miss)
1032{
1033 int expect = t.GetData(); // the value we should find
1034 bool found = ref.Peek(t); // rewrites t with actual value
1035 NS_TEST_EXPECT_MSG_EQ(found, !miss, msg << ": ref contains " << t.GetTypeId().GetName());
1036 if (found)
1037 {
1039 expect,
1040 msg << ": ref " << t.GetTypeId().GetName() << " = " << expect);
1041 }
1042}
1043
1044// A set of tags with data value 1, to check COW
1045#define MAKE_TEST_TAGS \
1046 ATestTag<1> t1(1); \
1047 ATestTag<2> t2(1); \
1048 ATestTag<3> t3(1); \
1049 ATestTag<4> t4(1); \
1050 ATestTag<5> t5(1); \
1051 ATestTag<6> t6(1); \
1052 ATestTag<7> t7(1); \
1053 constexpr int TAG_LAST [[maybe_unused]] = 7; /* length of ref PacketTagList */
1054
1055void
1056PacketTagListTest::CheckRefList(const PacketTagList& ptl, const char* msg, int miss /* = 0 */)
1057{
1059 CheckRef(ptl, t1, msg, miss == 1);
1060 CheckRef(ptl, t2, msg, miss == 2);
1061 CheckRef(ptl, t3, msg, miss == 3);
1062 CheckRef(ptl, t4, msg, miss == 4);
1063 CheckRef(ptl, t5, msg, miss == 5);
1064 CheckRef(ptl, t6, msg, miss == 6);
1065 CheckRef(ptl, t7, msg, miss == 7);
1066}
1067
1068int
1069PacketTagListTest::RemoveTime(const PacketTagList& ref, ATestTagBase& t, const char* msg /* = 0 */)
1070{
1071 const int reps = 10000;
1072 std::vector<PacketTagList> ptv(reps, ref);
1073 int start = clock();
1074 for (int i = 0; i < reps; ++i)
1075 {
1076 ptv[i].Remove(t);
1077 }
1078 int stop = clock();
1079 int delta = stop - start;
1080 if (msg)
1081 {
1082 std::cout << GetName() << "remove time: " << msg << ": " << std::setw(8) << delta
1083 << " ticks to remove " << reps << " times" << std::endl;
1084 }
1085 return delta;
1086}
1087
1088int
1090{
1091 const int reps = 100000;
1092 PacketTagList ptl;
1093 ATestTag<2> t(2);
1094 int start = clock();
1095 for (int i = 0; i < reps; ++i)
1096 {
1097 ptl.Add(t);
1098 ptl.Remove(t);
1099 }
1100 int stop = clock();
1101 int delta = stop - start;
1102 if (verbose)
1103 {
1104 std::cout << GetName() << "add/remove time: " << std::setw(8) << delta
1105 << " ticks to add+remove " << reps << " times" << std::endl;
1106 }
1107 return delta;
1108}
1109
1110void
1112{
1113 std::cout << GetName() << "begin" << std::endl;
1114
1116
1117 PacketTagList ref; // empty list
1118 ref.Add(t1); // last
1119 ref.Add(t2); // post merge
1120 ref.Add(t3); // merge successor
1121 ref.Add(t4); // merge
1122 ref.Add(t5); // merge precursor
1123 ref.Add(t6); // pre-merge
1124 ref.Add(t7); // first
1125
1126 // Peek
1127 {
1128 std::cout << GetName() << "check Peek (missing tag) returns false" << std::endl;
1129 ATestTag<10> t10;
1130 NS_TEST_EXPECT_MSG_EQ(ref.Peek(t10), false, "missing tag");
1131 }
1132
1133 // Copy ctor, assignment
1134 {
1135 std::cout << GetName() << "check copy and assignment" << std::endl;
1136 {
1137 // Test copy constructor
1138 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
1139 PacketTagList ptl(ref);
1140 CheckRefList(ref, "copy ctor orig");
1141 CheckRefList(ptl, "copy ctor copy");
1142 }
1143 {
1144 // Test copy constructor
1145 // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
1146 PacketTagList ptl = ref;
1147 CheckRefList(ref, "assignment orig");
1148 CheckRefList(ptl, "assignment copy");
1149 }
1150 }
1151
1152 // Removal
1153 {
1154#define RemoveCheck(n) \
1155 PacketTagList p##n = ref; \
1156 p##n.Remove(t##n); \
1157 CheckRefList(ref, "remove " #n " orig"); \
1158 CheckRefList(p##n, "remove " #n " copy", n);
1159
1160 // Remove single tags from list
1161 {
1162 std::cout << GetName() << "check removal of each tag" << std::endl;
1163 RemoveCheck(1);
1164 RemoveCheck(2);
1165 RemoveCheck(3);
1166 RemoveCheck(4);
1167 RemoveCheck(5);
1168 RemoveCheck(6);
1169 RemoveCheck(7);
1170 }
1171
1172 // Remove in the presence of a merge
1173 {
1174 std::cout << GetName() << "check removal doesn't disturb merge " << std::endl;
1175 PacketTagList ptl = ref;
1176 ptl.Remove(t7);
1177 ptl.Remove(t6);
1178 ptl.Remove(t5);
1179
1180 PacketTagList mrg = ptl; // merged list
1181 ATestTag<8> m5(1);
1182 mrg.Add(m5); // ptl and mrg differ
1183 ptl.Add(t5);
1184 ptl.Add(t6);
1185 ptl.Add(t7);
1186
1187 CheckRefList(ref, "post merge, orig");
1188 CheckRefList(ptl, "post merge, long chain");
1189 const char* msg = "post merge, short chain";
1190 CheckRef(mrg, t1, msg, false);
1191 CheckRef(mrg, t2, msg, false);
1192 CheckRef(mrg, t3, msg, false);
1193 CheckRef(mrg, t4, msg, false);
1194 CheckRef(mrg, m5, msg, false);
1195 }
1196#undef RemoveCheck
1197 }
1198
1199 // Replace
1200 {
1201 std::cout << GetName() << "check replacing each tag" << std::endl;
1202
1203#define ReplaceCheck(n) \
1204 t##n.m_data = 2; \
1205 { \
1206 PacketTagList p##n = ref; \
1207 p##n.Replace(t##n); \
1208 CheckRefList(ref, "replace " #n " orig"); \
1209 CheckRef(p##n, t##n, "replace " #n " copy"); \
1210 }
1211
1212 ReplaceCheck(1);
1213 ReplaceCheck(2);
1214 ReplaceCheck(3);
1215 ReplaceCheck(4);
1216 ReplaceCheck(5);
1217 ReplaceCheck(6);
1218 ReplaceCheck(7);
1219 }
1220
1221 // Timing
1222 {
1223 std::cout << GetName() << "add+remove timing" << std::endl;
1224 int flm = std::numeric_limits<int>::max();
1225 const int nIterations = 100;
1226 for (int i = 0; i < nIterations; ++i)
1227 {
1228 int now = AddRemoveTime();
1229 if (now < flm)
1230 {
1231 flm = now;
1232 }
1233 }
1234 std::cout << GetName() << "min add+remove time: " << std::setw(8) << flm << " ticks"
1235 << std::endl;
1236
1237 std::cout << GetName() << "remove timing" << std::endl;
1238 // tags numbered from 1, so add one for (unused) entry at 0
1239 std::vector<int> rmn(TAG_LAST + 1, std::numeric_limits<int>::max());
1240 for (int i = 0; i < nIterations; ++i)
1241 {
1242 for (int j = 1; j <= TAG_LAST; ++j)
1243 {
1244 int now = 0;
1245 switch (j)
1246 {
1247 case 7:
1248 now = RemoveTime(ref, t7);
1249 break;
1250 case 6:
1251 now = RemoveTime(ref, t6);
1252 break;
1253 case 5:
1254 now = RemoveTime(ref, t5);
1255 break;
1256 case 4:
1257 now = RemoveTime(ref, t4);
1258 break;
1259 case 3:
1260 now = RemoveTime(ref, t3);
1261 break;
1262 case 2:
1263 now = RemoveTime(ref, t2);
1264 break;
1265 case 1:
1266 now = RemoveTime(ref, t1);
1267 break;
1268 }
1269
1270 if (now < rmn[j])
1271 {
1272 rmn[j] = now;
1273 }
1274 }
1275 }
1276 for (int j = TAG_LAST; j > 0; --j)
1277 {
1278 std::cout << GetName() << "min remove time: t" << j << ": " << std::setw(8) << rmn[j]
1279 << " ticks" << std::endl;
1280 }
1281 }
1282}
1283
1284/**
1285 * @ingroup network-test
1286 * @ingroup tests
1287 *
1288 * @brief Packet TestSuite
1289 */
1291{
1292 public:
1294};
1295
1302
1303static PacketTestSuite g_packetTestSuite; //!< Static variable for test initialization
uint32_t v
return result
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.
void DoRun() override
Implementation to actually run this TestCase.
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=nullptr)
Prints the remove time.
Packet unit tests.
void DoCheckData(Ptr< const Packet > p, uint32_t n,...)
Checks the packet and its data.
void DoRun() override
Implementation to actually run this TestCase.
void DoCheck(Ptr< const Packet > p, uint32_t n,...)
Checks the packet.
Packet TestSuite.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint32_t Deserialize(Buffer::Iterator iter) override
void Serialize(Buffer::Iterator iter) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint32_t Deserialize(Buffer::Iterator iter) override
void Serialize(Buffer::Iterator iter) const override
iterator in a Buffer instance
Definition buffer.h:109
void WriteU8(uint8_t data)
Definition buffer.h:944
void Prev()
go backward by one byte
Definition buffer.h:923
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
Definition packet.h:52
uint32_t GetEnd() const
The index is an offset from the start of the packet.
Definition packet.cc:37
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
Definition packet.cc:43
uint32_t GetStart() const
The index is an offset from the start of the packet.
Definition packet.cc:31
TypeId GetTypeId() const
Definition packet.cc:25
Iterator over the set of byte tags in a packet.
Definition packet.h:45
bool HasNext() const
Definition packet.cc:61
Protocol header serialization and deserialization.
Definition header.h:36
network packets
Definition packet.h:228
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition packet.cc:966
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition packet.cc:959
void RemoveAllPacketTags()
Remove all packet tags.
Definition packet.cc:989
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
Definition packet.cc:914
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition packet.cc:982
List of the packet tags stored in a packet.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
void Add(const Tag &tag) const
Add a tag to the head of this branch.
bool Peek(Tag &tag) const
Find a tag and return its value.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
read and write tag data
Definition tag-buffer.h:41
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition tag-buffer.h:161
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition tag-buffer.h:185
tag a set of bytes in a packet
Definition tag.h:28
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
std::string GetName() const
Definition test.cc:371
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Protocol trailer serialization and deserialization.
Definition trailer.h:30
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
std::string GetName() const
Get the name.
Definition type-id.cc:1063
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Definition test.h:655
#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:240
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
Definition test.h:553
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool verbose
#define LARGE_TAG_BUFFER_SIZE
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
#define CHECK(p, n,...)
#define E_DATA(name, start, end, data)
#define ReplaceCheck(n)
#define E(name, start, end)
#define CHECK_DATA(p, n,...)
#define RemoveCheck(n)
#define MAKE_TEST_TAGS
uint8_t data[writeSize]
Struct to hold the expected data in the packet.
Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
Constructor.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.