A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
packet-metadata-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006,2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/header.h"
9#include "ns3/packet-metadata.h"
10#include "ns3/packet.h"
11#include "ns3/test.h"
12#include "ns3/trailer.h"
13
14#include <cstdarg>
15#include <iostream>
16#include <sstream>
17
18using namespace ns3;
19
20namespace
21{
22
23/**
24 * \ingroup network-test
25 * \ingroup tests
26 *
27 * \brief Base header-type class to check the proper header concatenation
28 *
29 * \note Class internal to packet-metadata-test.cc
30 */
32{
33 public:
34 /**
35 * \brief Get the type ID.
36 * \return The object TypeId.
37 */
38 static TypeId GetTypeId();
40 /**
41 * Checks if the header has deserialization errors
42 * \returns True if no error found.
43 */
44 bool IsOk() const;
45
46 protected:
47 /**
48 * Signal that an error has been found in deserialization.
49 */
50 void ReportError();
51
52 private:
53 bool m_ok; //!< True if no error is signalled.
54};
55
58{
59 static TypeId tid = TypeId("ns3::HistoryHeaderBase").SetParent<Header>();
60 return tid;
61}
62
64 : m_ok(true)
65{
66}
67
68bool
70{
71 return m_ok;
72}
73
74void
79
80/**
81 * \ingroup network-test
82 * \ingroup tests
83 *
84 * \brief Template header-type class to check the proper header concatenation
85 *
86 * \note Class internal to packet-metadata-test.cc
87 */
88template <int N>
90{
91 public:
93 /**
94 * \brief Get the type ID.
95 * \return The object TypeId.
96 */
97 static TypeId GetTypeId();
98 TypeId GetInstanceTypeId() const override;
99 void Print(std::ostream& os) const override;
100 uint32_t GetSerializedSize() const override;
101 void Serialize(Buffer::Iterator start) const override;
102 uint32_t Deserialize(Buffer::Iterator start) override;
103};
104
105template <int N>
110
111template <int N>
112TypeId
114{
115 std::ostringstream oss;
116 oss << "ns3::HistoryHeader<" << N << ">";
117 static TypeId tid =
118 TypeId(oss.str()).SetParent<HistoryHeaderBase>().AddConstructor<HistoryHeader<N>>();
119 return tid;
120}
121
122template <int N>
123TypeId
125{
126 return GetTypeId();
127}
128
129template <int N>
130void
131HistoryHeader<N>::Print(std::ostream& os) const
132{
133 NS_ASSERT(false);
134}
135
136template <int N>
139{
140 return N;
141}
142
143template <int N>
144void
146{
147 start.WriteU8(N, N);
148}
149
150template <int N>
153{
154 for (int i = 0; i < N; i++)
155 {
156 if (start.ReadU8() != N)
157 {
158 ReportError();
159 }
160 }
161 return N;
162}
163
164/**
165 * \ingroup network-test
166 * \ingroup tests
167 *
168 * \brief Base trailer-type class to check the proper trailer concatenation
169 *
170 * \note Class internal to packet-metadata-test.cc
171 */
173{
174 public:
175 /**
176 * \brief Get the type ID.
177 * \return The object TypeId.
178 */
179 static TypeId GetTypeId();
181 /**
182 * Checks if the header has deserialization errors
183 * \returns True if no error found.
184 */
185 bool IsOk() const;
186
187 protected:
188 /**
189 * Signal that an error has been found in deserialization.
190 */
191 void ReportError();
192
193 private:
194 bool m_ok; //!< True if no error is signalled.
195};
196
197TypeId
199{
200 static TypeId tid = TypeId("ns3::HistoryTrailerBase").SetParent<Trailer>();
201 return tid;
202}
203
205 : m_ok(true)
206{
207}
208
209bool
211{
212 return m_ok;
213}
214
215void
217{
218 m_ok = false;
219}
220
221/**
222 * \ingroup network-test
223 * \ingroup tests
224 *
225 * \brief Template trailer-type class to check the proper trailer concatenation
226 *
227 * \note Class internal to packet-metadata-test.cc
228 */
229template <int N>
231{
232 public:
234
235 /**
236 * \brief Get the type ID.
237 * \return The object TypeId.
238 */
239 static TypeId GetTypeId();
240 TypeId GetInstanceTypeId() const override;
241 void Print(std::ostream& os) const override;
242 uint32_t GetSerializedSize() const override;
243 void Serialize(Buffer::Iterator start) const override;
244 uint32_t Deserialize(Buffer::Iterator start) override;
245};
246
247template <int N>
251
252template <int N>
253TypeId
255{
256 std::ostringstream oss;
257 oss << "ns3::HistoryTrailer<" << N << ">";
258 static TypeId tid =
259 TypeId(oss.str()).SetParent<HistoryTrailerBase>().AddConstructor<HistoryTrailer<N>>();
260 return tid;
261}
262
263template <int N>
264TypeId
266{
267 return GetTypeId();
268}
269
270template <int N>
271void
272HistoryTrailer<N>::Print(std::ostream& os) const
273{
274 NS_ASSERT(false);
275}
276
277template <int N>
280{
281 return N;
282}
283
284template <int N>
285void
287{
288 start.Prev(N);
289 start.WriteU8(N, N);
290}
291
292template <int N>
295{
296 start.Prev(N);
297 for (int i = 0; i < N; i++)
298 {
299 if (start.ReadU8() != N)
300 {
301 ReportError();
302 }
303 }
304 return N;
305}
306
307} // namespace
308
309/**
310 * \ingroup network-test
311 * \ingroup tests
312 *
313 * Packet Metadata unit tests.
314 */
316{
317 public:
319 ~PacketMetadataTest() override;
320 /**
321 * Checks the packet header and trailer history
322 * \param p The packet
323 * \param n The number of variable arguments
324 * \param ... The variable arguments
325 */
326 void CheckHistory(Ptr<Packet> p, uint32_t n, ...);
327 void DoRun() override;
328
329 private:
330 /**
331 * Adds an header to the packet
332 * \param p The packet
333 * \return The packet with the header added.
334 */
335 Ptr<Packet> DoAddHeader(Ptr<Packet> p);
336};
337
339 : TestCase("Packet metadata")
340{
341}
342
346
347void
349{
350 std::list<int> expected;
351 va_list ap;
352 va_start(ap, n);
353 for (uint32_t j = 0; j < n; j++)
354 {
355 int v = va_arg(ap, int);
356 expected.push_back(v);
357 }
358 va_end(ap);
359
360 PacketMetadata::ItemIterator k = p->BeginItem();
361 std::list<int> got;
362 while (k.HasNext())
363 {
364 PacketMetadata::Item item = k.Next();
366 {
367 got.push_back(item.currentSize);
368 continue;
369 }
371 {
372 Callback<ObjectBase*> constructor = item.tid.GetConstructor();
373 auto header = dynamic_cast<HistoryHeaderBase*>(constructor());
374 if (header == nullptr)
375 {
376 goto error;
377 }
378 header->Deserialize(item.current);
379 if (!header->IsOk())
380 {
381 delete header;
382 goto error;
383 }
384 delete header;
385 }
386 else if (item.type == PacketMetadata::Item::TRAILER)
387 {
388 Callback<ObjectBase*> constructor = item.tid.GetConstructor();
389 auto trailer = dynamic_cast<HistoryTrailerBase*>(constructor());
390 if (trailer == nullptr)
391 {
392 goto error;
393 }
394 trailer->Deserialize(item.current);
395 if (!trailer->IsOk())
396 {
397 delete trailer;
398 goto error;
399 }
400 delete trailer;
401 }
402 got.push_back(item.currentSize);
403 }
404
405 for (auto i = got.begin(), j = expected.begin(); i != got.end(); i++, j++)
406 {
407 NS_ASSERT(j != expected.end());
408 if (*j != *i)
409 {
410 goto error;
411 }
412 }
413 return;
414error:
415 std::ostringstream failure;
416 failure << "PacketMetadata error. Got:\"";
417 for (auto i = got.begin(); i != got.end(); i++)
418 {
419 failure << *i << ", ";
420 }
421 failure << "\", expected: \"";
422 for (auto j = expected.begin(); j != expected.end(); j++)
423 {
424 failure << *j << ", ";
425 }
426 failure << "\"";
427 NS_TEST_ASSERT_MSG_EQ(false, true, failure.str());
428}
429
430#define ADD_HEADER(p, n) \
431 { \
432 HistoryHeader<n> header; \
433 p->AddHeader(header); \
434 }
435#define ADD_TRAILER(p, n) \
436 { \
437 HistoryTrailer<n> trailer; \
438 p->AddTrailer(trailer); \
439 }
440#define REM_HEADER(p, n) \
441 { \
442 HistoryHeader<n> header; \
443 p->RemoveHeader(header); \
444 }
445#define REM_TRAILER(p, n) \
446 { \
447 HistoryTrailer<n> trailer; \
448 p->RemoveTrailer(trailer); \
449 }
450#define CHECK_HISTORY(p, ...) \
451 { \
452 CheckHistory(p, __VA_ARGS__); \
453 uint32_t size = p->GetSerializedSize(); \
454 uint8_t* buffer = new uint8_t[size]; \
455 p->Serialize(buffer, size); \
456 Ptr<Packet> otherPacket = Create<Packet>(buffer, size, true); \
457 delete[] buffer; \
458 CheckHistory(otherPacket, __VA_ARGS__); \
459 }
460
463{
464 ADD_HEADER(p, 10);
465 return p;
466}
467
468void
470{
472
475
476 p = Create<Packet>(10);
477 ADD_TRAILER(p, 100);
478 CHECK_HISTORY(p, 2, 10, 100);
479
480 p = Create<Packet>(10);
481 ADD_HEADER(p, 1);
482 ADD_HEADER(p, 2);
483 ADD_HEADER(p, 3);
484 CHECK_HISTORY(p, 4, 3, 2, 1, 10);
485 ADD_HEADER(p, 5);
486 CHECK_HISTORY(p, 5, 5, 3, 2, 1, 10);
487 ADD_HEADER(p, 6);
488 CHECK_HISTORY(p, 6, 6, 5, 3, 2, 1, 10);
489
490 p = Create<Packet>(10);
491 ADD_HEADER(p, 1);
492 ADD_HEADER(p, 2);
493 ADD_HEADER(p, 3);
494 REM_HEADER(p, 3);
495 CHECK_HISTORY(p, 3, 2, 1, 10);
496
497 p = Create<Packet>(10);
498 ADD_HEADER(p, 1);
499 ADD_HEADER(p, 2);
500 ADD_HEADER(p, 3);
501 REM_HEADER(p, 3);
502 REM_HEADER(p, 2);
503 CHECK_HISTORY(p, 2, 1, 10);
504
505 p = Create<Packet>(10);
506 ADD_HEADER(p, 1);
507 ADD_HEADER(p, 2);
508 ADD_HEADER(p, 3);
509 REM_HEADER(p, 3);
510 REM_HEADER(p, 2);
511 REM_HEADER(p, 1);
512 CHECK_HISTORY(p, 1, 10);
513
514 p = Create<Packet>(10);
515 ADD_HEADER(p, 1);
516 ADD_HEADER(p, 2);
517 ADD_HEADER(p, 3);
518 p1 = p->Copy();
519 REM_HEADER(p1, 3);
520 REM_HEADER(p1, 2);
521 REM_HEADER(p1, 1);
522 CHECK_HISTORY(p1, 1, 10);
523 CHECK_HISTORY(p, 4, 3, 2, 1, 10);
524 ADD_HEADER(p1, 1);
525 ADD_HEADER(p1, 2);
526 CHECK_HISTORY(p1, 3, 2, 1, 10);
527 CHECK_HISTORY(p, 4, 3, 2, 1, 10);
528 ADD_HEADER(p, 3);
529 CHECK_HISTORY(p, 5, 3, 3, 2, 1, 10);
530 ADD_TRAILER(p, 4);
531 CHECK_HISTORY(p, 6, 3, 3, 2, 1, 10, 4);
532 ADD_TRAILER(p, 5);
533 CHECK_HISTORY(p, 7, 3, 3, 2, 1, 10, 4, 5);
534 REM_HEADER(p, 3);
535 CHECK_HISTORY(p, 6, 3, 2, 1, 10, 4, 5);
536 REM_TRAILER(p, 5);
537 CHECK_HISTORY(p, 5, 3, 2, 1, 10, 4);
538 p1 = p->Copy();
539 REM_TRAILER(p, 4);
540 CHECK_HISTORY(p, 4, 3, 2, 1, 10);
541 CHECK_HISTORY(p1, 5, 3, 2, 1, 10, 4);
542 p1->RemoveAtStart(3);
543 CHECK_HISTORY(p1, 4, 2, 1, 10, 4);
544 p1->RemoveAtStart(1);
545 CHECK_HISTORY(p1, 4, 1, 1, 10, 4);
546 p1->RemoveAtStart(1);
547 CHECK_HISTORY(p1, 3, 1, 10, 4);
548 p1->RemoveAtEnd(4);
549 CHECK_HISTORY(p1, 2, 1, 10);
550 p1->RemoveAtStart(1);
551 CHECK_HISTORY(p1, 1, 10);
552
553 p = Create<Packet>(10);
554 ADD_HEADER(p, 8);
555 ADD_TRAILER(p, 8);
556 ADD_TRAILER(p, 8);
557 p->RemoveAtStart(8 + 10 + 8);
558 CHECK_HISTORY(p, 1, 8);
559
560 p = Create<Packet>(10);
561 ADD_HEADER(p, 10);
562 ADD_HEADER(p, 8);
563 ADD_TRAILER(p, 6);
564 ADD_TRAILER(p, 7);
565 ADD_TRAILER(p, 9);
566 p->RemoveAtStart(5);
567 p->RemoveAtEnd(12);
568 CHECK_HISTORY(p, 5, 3, 10, 10, 6, 4);
569
570 p = Create<Packet>(10);
571 ADD_HEADER(p, 10);
572 ADD_TRAILER(p, 6);
573 p->RemoveAtEnd(18);
574 ADD_TRAILER(p, 5);
575 ADD_HEADER(p, 3);
576 CHECK_HISTORY(p, 3, 3, 8, 5);
577 p->RemoveAtStart(12);
578 CHECK_HISTORY(p, 1, 4);
579 p->RemoveAtEnd(2);
580 CHECK_HISTORY(p, 1, 2);
581 ADD_HEADER(p, 10);
582 CHECK_HISTORY(p, 2, 10, 2);
583 p->RemoveAtEnd(5);
584 CHECK_HISTORY(p, 1, 7);
585
588
589 p = Create<Packet>(40);
590 ADD_HEADER(p, 5);
591 ADD_HEADER(p, 8);
592 CHECK_HISTORY(p, 3, 8, 5, 40);
593 p1 = p->CreateFragment(0, 5);
594 p2 = p->CreateFragment(5, 5);
595 p3 = p->CreateFragment(10, 43);
596 CHECK_HISTORY(p1, 1, 5);
597 CHECK_HISTORY(p2, 2, 3, 2);
598 CHECK_HISTORY(p3, 2, 3, 40);
599 p1->AddAtEnd(p2);
600 CHECK_HISTORY(p1, 2, 8, 2);
601 CHECK_HISTORY(p2, 2, 3, 2);
602 p1->AddAtEnd(p3);
603 CHECK_HISTORY(p1, 3, 8, 5, 40);
604 CHECK_HISTORY(p2, 2, 3, 2);
605 CHECK_HISTORY(p3, 2, 3, 40);
606 p1 = p->CreateFragment(0, 5);
607 CHECK_HISTORY(p1, 1, 5);
608
609 p3 = Create<Packet>(50);
610 ADD_HEADER(p3, 8);
611 CHECK_HISTORY(p3, 2, 8, 50);
612 CHECK_HISTORY(p1, 1, 5);
613 p1->AddAtEnd(p3);
614 CHECK_HISTORY(p1, 3, 5, 8, 50);
615 ADD_HEADER(p1, 5);
616 CHECK_HISTORY(p1, 4, 5, 5, 8, 50);
617 ADD_TRAILER(p1, 2);
618 CHECK_HISTORY(p1, 5, 5, 5, 8, 50, 2);
619 REM_HEADER(p1, 5);
620 CHECK_HISTORY(p1, 4, 5, 8, 50, 2);
621 p1->RemoveAtEnd(60);
622 CHECK_HISTORY(p1, 1, 5);
623 p1->AddAtEnd(p2);
624 CHECK_HISTORY(p1, 2, 8, 2);
625 CHECK_HISTORY(p2, 2, 3, 2);
626
627 p3 = Create<Packet>(40);
628 ADD_HEADER(p3, 5);
629 ADD_HEADER(p3, 5);
630 CHECK_HISTORY(p3, 3, 5, 5, 40);
631 p1 = p3->CreateFragment(0, 5);
632 p2 = p3->CreateFragment(5, 5);
633 CHECK_HISTORY(p1, 1, 5);
634 CHECK_HISTORY(p2, 1, 5);
635 p1->AddAtEnd(p2);
636 CHECK_HISTORY(p1, 2, 5, 5);
637
638 p = Create<Packet>(0);
639 CHECK_HISTORY(p, 0);
640
641 p3 = Create<Packet>(0);
642 ADD_HEADER(p3, 5);
643 ADD_HEADER(p3, 5);
644 CHECK_HISTORY(p3, 2, 5, 5);
645 p1 = p3->CreateFragment(0, 4);
646 p2 = p3->CreateFragment(9, 1);
647 CHECK_HISTORY(p1, 1, 4);
648 CHECK_HISTORY(p2, 1, 1);
649 p1->AddAtEnd(p2);
650 CHECK_HISTORY(p1, 2, 4, 1);
651
652 p = Create<Packet>(2000);
653 CHECK_HISTORY(p, 1, 2000);
654
655 p = Create<Packet>();
656 ADD_TRAILER(p, 10);
657 ADD_HEADER(p, 5);
658 p1 = p->CreateFragment(0, 8);
659 p2 = p->CreateFragment(8, 7);
660 p1->AddAtEnd(p2);
661 CHECK_HISTORY(p, 2, 5, 10);
662
663 p = Create<Packet>();
664 ADD_TRAILER(p, 10);
665 REM_TRAILER(p, 10);
666 ADD_TRAILER(p, 10);
667 CHECK_HISTORY(p, 1, 10);
668
669 p = Create<Packet>();
670 ADD_HEADER(p, 10);
671 REM_HEADER(p, 10);
672 ADD_HEADER(p, 10);
673 CHECK_HISTORY(p, 1, 10);
674
675 p = Create<Packet>();
676 ADD_HEADER(p, 10);
677 p = DoAddHeader(p);
678 CHECK_HISTORY(p, 2, 10, 10);
679
680 p = Create<Packet>(10);
681 ADD_HEADER(p, 8);
682 ADD_TRAILER(p, 8);
683 ADD_TRAILER(p, 8);
684 p->RemoveAtStart(8 + 10 + 8);
685 CHECK_HISTORY(p, 1, 8);
686
687 p = Create<Packet>(0);
688 ADD_HEADER(p, 8);
689 REM_HEADER(p, 8);
690 CHECK_HISTORY(p, 0);
691
692 p = Create<Packet>(0);
693 ADD_TRAILER(p, 8);
694 REM_TRAILER(p, 8);
695 CHECK_HISTORY(p, 0);
696
697 p = Create<Packet>(0);
698 ADD_HEADER(p, 8);
699 p->RemoveAtStart(8);
700 CHECK_HISTORY(p, 0);
701
702 p = Create<Packet>(0);
703 ADD_HEADER(p, 8);
704 ADD_TRAILER(p, 8);
705 REM_TRAILER(p, 8);
706 REM_HEADER(p, 8);
707 CHECK_HISTORY(p, 0);
708
709 p = Create<Packet>(0);
710 ADD_HEADER(p, 8);
711 ADD_TRAILER(p, 8);
712 REM_HEADER(p, 8);
713 REM_TRAILER(p, 8);
714 CHECK_HISTORY(p, 0);
715
716 p = Create<Packet>(0);
717 ADD_HEADER(p, 8);
718 ADD_TRAILER(p, 8);
719 REM_TRAILER(p, 8);
720 p->RemoveAtStart(8);
721 CHECK_HISTORY(p, 0);
722
723 p = Create<Packet>(0);
724 ADD_HEADER(p, 8);
725 ADD_TRAILER(p, 8);
726 REM_HEADER(p, 8);
727 p->RemoveAtEnd(8);
728 CHECK_HISTORY(p, 0);
729
730 p = Create<Packet>(0);
731 ADD_HEADER(p, 8);
732 ADD_TRAILER(p, 8);
733 REM_TRAILER(p, 8);
734 p->RemoveAtEnd(8);
735 CHECK_HISTORY(p, 0);
736
737 p = Create<Packet>(0);
738 ADD_HEADER(p, 8);
739 ADD_TRAILER(p, 8);
740 REM_HEADER(p, 8);
741 p->RemoveAtStart(8);
742 CHECK_HISTORY(p, 0);
743
744 p = Create<Packet>(16383);
745 p = Create<Packet>(16384);
746
747 /// \internal
748 /// See \bugid{179}
749 p = Create<Packet>(40);
750 p2 = p->CreateFragment(5, 5);
751 p3 = p->CreateFragment(10, 30);
752 ADD_HEADER(p2, 8);
753 ADD_HEADER(p3, 8);
754 REM_HEADER(p2, 8);
755 REM_HEADER(p3, 8);
756 p2->AddAtEnd(p3);
757
758 p = Create<Packet>(1000);
759 ADD_HEADER(p, 10);
760 ADD_TRAILER(p, 5);
761 p1 = p->Copy();
762 ADD_HEADER(p1, 20);
763 REM_HEADER(p1, 20);
764 REM_TRAILER(p1, 5);
765 NS_TEST_EXPECT_MSG_EQ(p->GetSize(), 1015, "Correct size");
766
767 p = Create<Packet>(1510);
768 ADD_HEADER(p, 8);
769 ADD_HEADER(p, 25);
770 REM_HEADER(p, 25);
771 ADD_HEADER(p, 1);
772 p1 = p->CreateFragment(0, 1500);
773 p2 = p1->Copy();
774 ADD_HEADER(p2, 24);
775 NS_TEST_EXPECT_MSG_EQ(p->GetSize(), 1519, "Correct size");
776
777 p = Create<Packet>(1000);
778 ADD_HEADER(p, 2);
779 ADD_TRAILER(p, 3);
780 p1 = p->Copy();
781 CHECK_HISTORY(p1, 3, 2, 1000, 3);
782 REM_HEADER(p, 2);
783 ADD_HEADER(p, 1);
784 CHECK_HISTORY(p, 3, 1, 1000, 3);
785 CHECK_HISTORY(p1, 3, 2, 1000, 3);
786
787 p = Create<Packet>(200);
788 ADD_HEADER(p, 24);
789 p1 = p->CreateFragment(0, 100);
790 p2 = p->CreateFragment(100, 100);
791 p1->AddAtEnd(p2);
792
793 p = Create<Packet>();
794 ADD_HEADER(p, 10);
795 p1 = Create<Packet>();
796 ADD_HEADER(p1, 11);
797 REM_HEADER(p1, 11);
798 p->AddAtEnd(p1);
799
800 p = Create<Packet>(500);
801 CHECK_HISTORY(p, 1, 500);
802 ADD_HEADER(p, 10);
803 CHECK_HISTORY(p, 2, 10, 500);
804 REM_HEADER(p, 10);
805 CHECK_HISTORY(p, 1, 500);
806 p->RemoveAtEnd(10);
807 CHECK_HISTORY(p, 1, 490);
808
809 p = Create<Packet>(500);
810 CHECK_HISTORY(p, 1, 500);
811 ADD_TRAILER(p, 10);
812 CHECK_HISTORY(p, 2, 500, 10);
813 REM_TRAILER(p, 10);
814 CHECK_HISTORY(p, 1, 500);
815 p->RemoveAtStart(10);
816 CHECK_HISTORY(p, 1, 490);
817
818 /// \internal
819 /// See \bugid{1072}
820 p = Create<Packet>(500);
821 ADD_HEADER(p, 10);
822 ADD_HEADER(p, 20);
823 ADD_HEADER(p, 5);
824 CHECK_HISTORY(p, 4, 5, 20, 10, 500);
825 p1 = p->CreateFragment(0, 6);
826 p2 = p->CreateFragment(6, 535 - 6);
827 p1->AddAtEnd(p2);
828
829 /// \internal
830 /// See \bugid{1072}
831 p = Create<Packet>(reinterpret_cast<const uint8_t*>("hello world"), 11);
832 ADD_HEADER(p, 2);
833 CHECK_HISTORY(p, 2, 2, 11);
834 p1 = p->CreateFragment(0, 5);
835 CHECK_HISTORY(p1, 2, 2, 3);
836 p2 = p->CreateFragment(5, 8);
837 CHECK_HISTORY(p2, 1, 8);
838
839 ADD_HEADER(p1, 8 + 2 + 2 * 6);
840 ADD_TRAILER(p1, 4);
841 CHECK_HISTORY(p1, 4, 22, 2, 3, 4);
842 ADD_HEADER(p2, 8 + 2 + 2 * 6);
843 ADD_TRAILER(p2, 4);
844 CHECK_HISTORY(p2, 3, 22, 8, 4);
845
846 REM_TRAILER(p1, 4);
847 REM_HEADER(p1, 8 + 2 + 2 * 6);
848 CHECK_HISTORY(p1, 2, 2, 3);
849 REM_TRAILER(p2, 4);
850 REM_HEADER(p2, 8 + 2 + 2 * 6);
851 CHECK_HISTORY(p2, 1, 8);
852
853 p3 = p1->Copy();
854 CHECK_HISTORY(p3, 2, 2, 3);
855 p3->AddAtEnd(p2);
856 CHECK_HISTORY(p3, 2, 2, 11);
857
858 CHECK_HISTORY(p, 2, 2, 11);
859 REM_HEADER(p, 2);
860 CHECK_HISTORY(p, 1, 11);
861 REM_HEADER(p3, 2);
862 CHECK_HISTORY(p3, 1, 11);
863
864 auto buf = new uint8_t[p3->GetSize()];
865 p3->CopyData(buf, p3->GetSize());
866 std::string msg = std::string(reinterpret_cast<const char*>(buf), p3->GetSize());
867 delete[] buf;
869 std::string("hello world"),
870 "Could not find original data in received packet");
871}
872
873/**
874 * \ingroup network-test
875 * \ingroup tests
876 *
877 * \brief Packet Metadata TestSuite
878 */
880{
881 public:
883};
884
886 : TestSuite("packet-metadata", Type::UNIT)
887{
888 AddTestCase(new PacketMetadataTest, TestCase::Duration::QUICK);
889}
890
891static PacketMetadataTestSuite g_packetMetadataTest; //!< Static variable for test initialization
Packet Metadata unit tests.
void DoRun() override
Implementation to actually run this TestCase.
Ptr< Packet > DoAddHeader(Ptr< Packet > p)
Adds an header to the packet.
void CheckHistory(Ptr< Packet > p, uint32_t n,...)
Checks the packet header and trailer history.
Packet Metadata TestSuite.
Base header-type class to check the proper header concatenation.
bool IsOk() const
Checks if the header has deserialization errors.
void ReportError()
Signal that an error has been found in deserialization.
Template header-type class to check the proper header concatenation.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Base trailer-type class to check the proper trailer concatenation.
void ReportError()
Signal that an error has been found in deserialization.
bool IsOk() const
Checks if the header has deserialization errors.
Template trailer-type class to check the proper trailer concatenation.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
iterator in a Buffer instance
Definition buffer.h:89
Callback template class.
Definition callback.h:422
Protocol header serialization and deserialization.
Definition header.h:33
uint32_t Deserialize(Buffer::Iterator start) override=0
Iterator class for metadata items.
static void Enable()
Enable the packet metadata.
Smart pointer class similar to boost::intrusive_ptr.
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Protocol trailer serialization and deserialization.
Definition trailer.h:30
uint32_t Deserialize(Buffer::Iterator end) override=0
a unique identifier for an interface.
Definition type-id.h:48
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition type-id.cc:1154
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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.
#define ADD_HEADER(p, n)
#define CHECK_HISTORY(p,...)
#define REM_TRAILER(p, n)
static PacketMetadataTestSuite g_packetMetadataTest
Static variable for test initialization.
#define REM_HEADER(p, n)
#define ADD_TRAILER(p, n)
structure describing a packet metadata item
ItemType type
metadata type
TypeId tid
TypeId of Header or Trailer.
bool isFragment
true: this is a fragmented header, trailer, or, payload.
Buffer::Iterator current
an iterator which can be fed to Deserialize.
uint32_t currentSize
size of item.