20 #include "ns3/packet.h" 21 #include "ns3/packet-tag-list.h" 23 #include "ns3/unused.h" 61 .SetGroupName (
"Network")
62 .HideFromDocumentation ()
70 int result = (int)m_data;
94 std::ostringstream oss;
95 oss <<
"anon::ATestTag<" << N <<
">";
97 .SetParent<ATestTagBase> ()
108 return N +
sizeof(m_data);
112 for (uint32_t i = 0; i < N; ++i)
119 for (uint32_t i = 0; i < N; ++i)
121 uint8_t v = buf.
ReadU8 ();
128 virtual void Print (std::ostream &os)
const {
129 os << N <<
"(" << m_data <<
")";
141 #define LARGE_TAG_BUFFER_SIZE 64 159 m_data.push_back (i);
171 .SetGroupName (
"Network")
172 .HideFromDocumentation ()
181 return (uint32_t) m_size;
185 for (uint8_t i = 0; i < (m_size - 1); ++i)
192 for (uint8_t i = 0; i < (m_size - 1); ++i)
194 uint8_t v = buf.
ReadU8 ();
195 m_data.push_back (v);
198 virtual void Print (std::ostream &os)
const {
199 os <<
"(" << (uint16_t) m_size <<
")";
226 .SetGroupName (
"Network")
227 .HideFromDocumentation ()
252 std::ostringstream oss;
253 oss <<
"anon::ATestHeader<" << N <<
">";
255 .SetParent<ATestHeaderBase> ()
269 for (uint32_t i = 0; i < N; ++i)
275 for (uint32_t i = 0; i < N; ++i)
277 uint8_t v = iter.
ReadU8 ();
285 virtual void Print (std::ostream &os)
const {
312 .SetGroupName (
"Network")
313 .HideFromDocumentation ()
338 std::ostringstream oss;
339 oss <<
"anon::ATestTrailer<" << N <<
">";
341 .SetParent<ATestTrailerBase> ()
356 for (uint32_t i = 0; i < N; ++i)
363 for (uint32_t i = 0; i < N; ++i)
365 uint8_t v = iter.
ReadU8 ();
373 virtual void Print (std::ostream &os)
const {
396 Expected (uint32_t n_, uint32_t start_, uint32_t end_)
397 :
n (n_),
start (start_), end (end_),
data(0) {}
406 Expected (uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
407 :
n (n_),
start (start_), end (end_),
data(data_) {}
418 #define E(name,start,end) name,start,end 421 #define E_DATA(name,start,end,data) name,start,end,data 424 #define CHECK(p, n, ...) \ 425 DoCheck (p, __FILE__, __LINE__, n, __VA_ARGS__) 428 #define CHECK_DATA(p, n, ...) \ 429 DoCheckData (p, __FILE__, __LINE__, n, __VA_ARGS__) 441 virtual void DoRun (
void);
463 std::vector<struct Expected> expected;
466 for (uint32_t k = 0; k <
n; ++k)
468 uint32_t N = va_arg (ap, uint32_t);
469 uint32_t
start = va_arg (ap, uint32_t);
470 uint32_t end = va_arg (ap, uint32_t);
471 expected.push_back (Expected (N,
start, end));
477 while (i.
HasNext () && j < expected.size ())
480 struct Expected e = expected[j];
481 std::ostringstream oss;
482 oss <<
"anon::ATestTag<" << e.n <<
">";
500 std::vector<struct Expected> expected;
503 for (uint32_t k = 0; k <
n; ++k)
505 uint32_t N = va_arg (ap, uint32_t);
506 uint32_t
start = va_arg (ap, uint32_t);
507 uint32_t end = va_arg (ap, uint32_t);
508 int data = va_arg (ap,
int);
509 expected.push_back (Expected (N,
start, end,
data));
515 while (i.
HasNext () && j < expected.size ())
518 struct Expected e = expected[j];
519 std::ostringstream oss;
520 oss <<
"anon::ATestTag<" << e.n <<
">";
539 Ptr<Packet> pkt1 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
"hello"), 5);
540 Ptr<Packet> pkt2 = Create<Packet> (
reinterpret_cast<const uint8_t*
> (
" world"), 6);
547 uint8_t *buf =
new uint8_t[packet->
GetSize ()];
550 std::string msg = std::string (reinterpret_cast<const char *>(buf),
560 CHECK (p, 1,
E (1, 0, 1000));
562 CHECK (copy, 1,
E (1, 0, 1000));
565 CHECK (p, 2,
E (1, 0, 1000),
E (2, 0, 1000));
566 CHECK (copy, 1,
E (1, 0, 1000));
572 CHECK (&c0, 1,
E (1, 0, 1000));
573 CHECK (&c1, 1,
E (1, 0, 1000));
574 CHECK (copy, 1,
E (1, 0, 1000));
576 CHECK (&c0, 2,
E (1, 0, 1000),
E (10, 0, 1000));
577 CHECK (&c1, 1,
E (1, 0, 1000));
578 CHECK (copy, 1,
E (1, 0, 1000));
585 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
587 CHECK (frag1, 3,
E (1, 0, 90),
E (2, 0, 90),
E (4, 0, 90));
589 CHECK (frag2, 3,
E (1, 0, 900),
E (2, 0, 900),
E (5, 0, 900));
592 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));
594 CHECK (frag0, 3,
E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10));
597 E (1, 0, 10),
E (2, 0, 10),
E (3, 0, 10),
598 E (1, 10, 100),
E (2, 10, 100),
E (4, 10, 100),
599 E (1, 100, 1000),
E (2, 100, 1000),
E (5, 100, 1000));
603 frag0 = Create<Packet> (1000);
607 p = Create<Packet> (1000);
609 CHECK (p, 1,
E (20, 0, 1000));
611 CHECK (p, 1,
E (20, 0, 1000));
612 CHECK (frag0, 1,
E (20, 0, 90));
615 CHECK (frag0, 1,
E (20, 10, 100));
620 CHECK (tmp, 1,
E (20, 0, 100));
622 CHECK (tmp, 1,
E (20, 10, 110));
625 CHECK (tmp, 1,
E (20, 0, 100));
627 CHECK (tmp, 1,
E (20, 10, 110));
629 tmp = Create<Packet> (100);
631 CHECK (tmp, 1,
E (20, 0, 100));
633 CHECK (tmp, 1,
E (20, 0, 100));
636 CHECK (tmp, 1,
E (20, 0, 100));
638 CHECK (tmp, 1,
E (20, 0, 100));
646 CHECK (tmp, 1,
E (20, 0, 156));
648 CHECK (tmp, 1,
E (20, 0, 36));
651 CHECK (a, 1,
E (20, 0, 36));
657 CHECK (tmp, 0,
E (20, 0, 0));
662 CHECK (tmp, 1,
E (20, 0, 1000));
664 CHECK (tmp, 0,
E (0,0,0));
667 CHECK (a, 1,
E (10, 0, 10));
669 CHECK (tmp, 1,
E (10, 0, 10));
686 copy.AddPacketTag (c);
691 copy.RemovePacketTag (b);
715 uint8_t* buffer =
new uint8_t[serializedSize + 16];
718 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
746 CHECK (p1, 3,
E (10, 0, 1000),
E (11, 0, 1000),
E (12, 0, 1000));
749 uint8_t* buffer =
new uint8_t[serializedSize];
752 Ptr<Packet> p2 = Create<Packet> (buffer, serializedSize,
true);
756 CHECK_DATA (p2, 3,
E_DATA (10, 0, 1000, 65),
E_DATA (11, 0, 1000, 66),
E_DATA (12, 0, 1000, 67));
764 CHECK (tmp, 1,
E (20, 0, 1000));
766 CHECK (tmp, 1,
E (20, 2, 1002));
768 CHECK (tmp, 1,
E (20, 1, 1001));
771 CHECK (tmp, 1,
E (20, 1, 1001));
780 CHECK (tmp, 1,
E (25, 0, 100));
782 CHECK (tmp, 1,
E (25, 0, 50));
784 CHECK (tmp, 1,
E (25, 50, 100));
792 CHECK (tmp, 1,
E (25, 0, 100));
794 CHECK (tmp, 1,
E (25, 0, 50));
796 CHECK (tmp, 1,
E (25, 0, 50));
804 CHECK (tmp, 1,
E (25, 0, 100));
806 CHECK (tmp, 1,
E (25, 0, 50));
808 CHECK (tmp, 1,
E (25, 25, 75));
816 CHECK (tmp, 1,
E (25, 0, 100));
818 CHECK (tmp, 1,
E (25, 0, 50));
820 CHECK (tmp, 1,
E (25, 0, 50));
828 CHECK (tmp, 1,
E (25, 0, 100));
830 CHECK (tmp, 1,
E (25, 0, 50));
832 CHECK (tmp, 1,
E (25, 0, 50));
842 CHECK (tmp, 1,
E (25, 0, 100));
844 CHECK (tmp, 1,
E (25, 0, 50));
846 CHECK (tmp, 1,
E (25, 0, 50));
900 const char * msg = 0);
925 int expect = t.GetData ();
926 bool found = ref.
Peek (t);
928 msg <<
": ref contains " 929 << t.GetTypeId ().GetName ());
932 msg <<
": ref " << t.GetTypeId ().GetName ()
938 #define MAKE_TEST_TAGS \ 939 ATestTag<1> t1 (1); \ 940 ATestTag<2> t2 (1); \ 941 ATestTag<3> t3 (1); \ 942 ATestTag<4> t4 (1); \ 943 ATestTag<5> t5 (1); \ 944 ATestTag<6> t6 (1); \ 945 ATestTag<7> t7 (1); \ 946 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"
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual void Serialize(TagBuffer buf) const
std::vector< uint8_t > m_data
Tag data.
virtual void DoRun(void)
Implementation to actually run this TestCase.
ATestTagBase(uint8_t data)
Constructor.
bool Remove(Tag &tag)
Remove (the first instance of) tag from the list.
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
TypeId GetTypeId(void) const
std::string GetName(void) const
Get the name.
Packet Tag list unit tests.
TypeId AddConstructor(void)
Record in this TypeId the fact that the default constructor is accessible.
void CheckRefList(const PacketTagList &ref, const char *msg, int miss=0)
Checks against a reference PacketTagList.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
bool m_error
Error in the Tag.
virtual uint32_t GetSerializedSize(void) const
static TypeId GetTypeId(void)
Register this type.
virtual void Serialize(TagBuffer buf) const
virtual uint32_t GetSerializedSize(void) const
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
virtual uint32_t GetSerializedSize(void) const
uint8_t m_size
Packet size.
Template class for Test trailers.
List of the packet tags stored in a packet.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
virtual void Print(std::ostream &os) const
uint32_t GetStart(void) const
The index is an offset from the start of the packet.
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
virtual void Print(std::ostream &os) const
static TypeId GetTypeId(void)
Register this type.
static TypeId GetTypeId(void)
Register this type.
Expected(uint32_t n_, uint32_t start_, uint32_t end_)
Constructor.
TAG_BUFFER_INLINE uint8_t ReadU8(void)
iterator in a Buffer instance
static TypeId GetTypeId(void)
Register this type.
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
void RemoveAllPacketTags(void)
Remove all packet tags.
Identifies a byte tag and a set of bytes within a packet to which the tag applies.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Base class for Test trailers.
#define E_DATA(name, start, end, data)
void Prev(void)
go backward by one byte
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
#define CHECK_DATA(p, n,...)
void DoRun(void)
Implementation to actually run this TestCase.
void AddPaddingAtEnd(uint32_t size)
Add a zero-filled padding to the packet.
static TypeId GetTypeId(void)
Register this type.
TypeId SetGroupName(std::string groupName)
Set the group name.
virtual void Deserialize(TagBuffer buf)
Protocol trailer serialization and deserialization.
tag a set of bytes in a packet
int GetData() const
Get the tag data.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int RemoveTime(const PacketTagList &ref, ATestTagBase &t, const char *msg=0)
Prints the remove time.
void AddTrailer(const Trailer &trailer)
Add trailer to this packet.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Iterator over the set of byte tags in a packet.
uint32_t RemoveTrailer(Trailer &trailer)
Remove a deserialized trailer from the internal buffer.
bool Peek(Tag &tag) const
Find a tag and return its value.
#define NS_TEST_EXPECT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report if not.
Base class for Test tags.
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
#define NS_TEST_EXPECT_MSG_EQ_INTERNAL(actual, limit, msg, file, line)
Test that an actual and expected (limit) value are equal and report if not.
void Add(Tag const &tag) const
Add a tag to the head of this branch.
#define LARGE_TAG_BUFFER_SIZE
void CheckRef(const PacketTagList &ref, ATestTagBase &t, const char *msg, bool miss=false)
Checks against a reference PacketTagList.
void DoCheckData(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
virtual void Deserialize(TagBuffer buf)
uint32_t GetEnd(void) const
The index is an offset from the start of the packet.
ATestTag(uint8_t data)
Constructor.
std::string GetName(void) const
void WriteU8(uint8_t data)
void AddPacketTag(const Tag &tag) const
Add a packet tag.
virtual ~PacketTagListTest()
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
#define E(name, start, end)
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
uint8_t data
Optional data.
static PacketTestSuite g_packetTestSuite
Static variable for test initialization.
virtual void Serialize(Buffer::Iterator iter) const
virtual uint32_t Deserialize(Buffer::Iterator iter)
virtual void Print(std::ostream &os) const
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
void GetTag(Tag &tag) const
Read the requested tag and store it in the user-provided tag instance.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Template class for Test tags.
Template class for Large Test tags.
void DoCheck(Ptr< const Packet > p, const char *file, int line, uint32_t n,...)
Checks the packet.
Struct to hold the expected data in the packet.
Expected(uint32_t n_, uint32_t start_, uint32_t end_, uint8_t data_)
Constructor
a unique identifier for an interface.
bool m_error
Error in the Trailer.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
void AddByteTag(const Tag &tag) const
Tag each byte included in this packet with a new byte tag.
uint32_t n
Number of elements.
void AddHeader(const Header &header)
Add header to this packet.
ByteTagIterator GetByteTagIterator(void) const
Returns an iterator over the set of byte tags included in this packet.
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Serialize a packet, tags, and metadata into a byte buffer.
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
int AddRemoveTime(const bool verbose=false)
Prints the remove time.