src/common/packet-metadata-test.cc
changeset 3180 b69d00a3d410
parent 3171 e74786aa54ae
child 3202 bb35abea1d2a
equal deleted inserted replaced
3179:1763f7ac8e80 3180:b69d00a3d410
    30 
    30 
    31 using namespace ns3;
    31 using namespace ns3;
    32 
    32 
    33 namespace {
    33 namespace {
    34 
    34 
    35 template <int N>
    35 class HistoryHeaderBase : public Header
    36 class HistoryHeader : public Header
    36 {
       
    37 public:
       
    38   static TypeId GetTypeId (void);
       
    39   HistoryHeaderBase ();
       
    40   bool IsOk (void) const;
       
    41 protected:
       
    42   void ReportError (void);
       
    43 private:
       
    44   bool m_ok;
       
    45 };
       
    46 
       
    47 TypeId 
       
    48 HistoryHeaderBase::GetTypeId (void)
       
    49 {
       
    50   static TypeId tid = TypeId ("ns3::HistoryHeaderBase")
       
    51     .SetParent<Header> ()
       
    52     ;
       
    53   return tid;
       
    54 }
       
    55 
       
    56 HistoryHeaderBase::HistoryHeaderBase ()
       
    57   : m_ok (true)
       
    58 {}
       
    59 
       
    60 bool 
       
    61 HistoryHeaderBase::IsOk (void) const
       
    62 {
       
    63   return m_ok;
       
    64 }
       
    65 void 
       
    66 HistoryHeaderBase::ReportError (void)
       
    67 {
       
    68   m_ok = false;
       
    69 }
       
    70 
       
    71 
       
    72 template <int N>
       
    73 class HistoryHeader : public HistoryHeaderBase
    37 {
    74 {
    38 public:
    75 public:
    39   HistoryHeader ();
    76   HistoryHeader ();
       
    77   static TypeId GetTypeId (void);
       
    78   virtual TypeId GetInstanceTypeId (void) const;
       
    79   virtual void Print (std::ostream &os) const;
       
    80   virtual uint32_t GetSerializedSize (void) const;
       
    81   virtual void Serialize (Buffer::Iterator start) const;
       
    82   virtual uint32_t Deserialize (Buffer::Iterator start);
       
    83 };
       
    84 
       
    85 template <int N>
       
    86 HistoryHeader<N>::HistoryHeader ()
       
    87   : HistoryHeaderBase ()
       
    88 {}
       
    89 
       
    90 template <int N>
       
    91 TypeId
       
    92 HistoryHeader<N>::GetTypeId (void)
       
    93 {
       
    94   std::ostringstream oss;
       
    95   oss << "ns3::HistoryHeader<"<<N<<">";
       
    96   static TypeId tid = TypeId (oss.str ().c_str ())
       
    97     .SetParent<HistoryHeaderBase> ()
       
    98     .AddConstructor<HistoryHeader<N> > ()
       
    99     ;
       
   100   return tid;
       
   101 }
       
   102 
       
   103 template <int N>
       
   104 TypeId 
       
   105 HistoryHeader<N>::GetInstanceTypeId (void) const
       
   106 {
       
   107   return GetTypeId ();
       
   108 }
       
   109 template <int N>
       
   110 void 
       
   111 HistoryHeader<N>::Print (std::ostream &os) const
       
   112 {
       
   113   NS_ASSERT (false);
       
   114 }
       
   115 template <int N>
       
   116 uint32_t 
       
   117 HistoryHeader<N>::GetSerializedSize (void) const
       
   118 {
       
   119   return N;
       
   120 }
       
   121 template <int N>
       
   122 void 
       
   123 HistoryHeader<N>::Serialize (Buffer::Iterator start) const
       
   124 {
       
   125   start.WriteU8 (N, N);
       
   126 }
       
   127 template <int N>
       
   128 uint32_t
       
   129 HistoryHeader<N>::Deserialize (Buffer::Iterator start)
       
   130 {
       
   131   for (int i = 0; i < N; i++)
       
   132     {
       
   133       if (start.ReadU8 () != N)
       
   134         {
       
   135           ReportError ();
       
   136         }
       
   137     }
       
   138   return N;
       
   139 }
       
   140 
       
   141 class HistoryTrailerBase : public Trailer
       
   142 {
       
   143 public:
       
   144   static TypeId GetTypeId (void);
       
   145   HistoryTrailerBase ();
    40   bool IsOk (void) const;
   146   bool IsOk (void) const;
       
   147 protected:
       
   148   void ReportError (void);
       
   149 private:
       
   150   bool m_ok;
       
   151 };
       
   152 
       
   153 TypeId 
       
   154 HistoryTrailerBase::GetTypeId (void)
       
   155 {
       
   156   static TypeId tid = TypeId ("ns3::HistoryTrailerBase")
       
   157     .SetParent<Trailer> ()
       
   158     ;
       
   159   return tid;
       
   160 }
       
   161 HistoryTrailerBase::HistoryTrailerBase ()
       
   162   : m_ok (true)
       
   163 {}
       
   164 bool 
       
   165 HistoryTrailerBase::IsOk (void) const
       
   166 {
       
   167   return m_ok;
       
   168 }
       
   169 void 
       
   170 HistoryTrailerBase::ReportError (void)
       
   171 {
       
   172   m_ok = false;
       
   173 }
       
   174 
       
   175 
       
   176 template <int N>
       
   177 class HistoryTrailer : public HistoryTrailerBase
       
   178 {
       
   179 public:
       
   180   HistoryTrailer ();
       
   181   bool IsOk (void) const;
       
   182 
    41   static TypeId GetTypeId (void);
   183   static TypeId GetTypeId (void);
    42   virtual TypeId GetInstanceTypeId (void) const;
   184   virtual TypeId GetInstanceTypeId (void) const;
    43   virtual void Print (std::ostream &os) const;
   185   virtual void Print (std::ostream &os) const;
    44   virtual uint32_t GetSerializedSize (void) const;
   186   virtual uint32_t GetSerializedSize (void) const;
    45   virtual void Serialize (Buffer::Iterator start) const;
   187   virtual void Serialize (Buffer::Iterator start) const;
    47 private:
   189 private:
    48   bool m_ok;
   190   bool m_ok;
    49 };
   191 };
    50 
   192 
    51 template <int N>
   193 template <int N>
    52 HistoryHeader<N>::HistoryHeader ()
   194 HistoryTrailer<N>::HistoryTrailer ()
    53   : m_ok (false)
       
    54 {}
   195 {}
    55 
       
    56 template <int N>
       
    57 bool 
       
    58 HistoryHeader<N>::IsOk (void) const
       
    59 {
       
    60   return m_ok;
       
    61 }
       
    62 
       
    63 template <int N>
       
    64 TypeId
       
    65 HistoryHeader<N>::GetTypeId (void)
       
    66 {
       
    67   std::ostringstream oss;
       
    68   oss << "ns3::HistoryHeader<"<<N<<">";
       
    69   static TypeId tid = TypeId (oss.str ().c_str ())
       
    70     .SetParent<Header> ()
       
    71     ;
       
    72   return tid;
       
    73 }
       
    74 
       
    75 template <int N>
       
    76 TypeId 
       
    77 HistoryHeader<N>::GetInstanceTypeId (void) const
       
    78 {
       
    79   return GetTypeId ();
       
    80 }
       
    81 template <int N>
       
    82 void 
       
    83 HistoryHeader<N>::Print (std::ostream &os) const
       
    84 {
       
    85   NS_ASSERT (false);
       
    86 }
       
    87 template <int N>
       
    88 uint32_t 
       
    89 HistoryHeader<N>::GetSerializedSize (void) const
       
    90 {
       
    91   return N;
       
    92 }
       
    93 template <int N>
       
    94 void 
       
    95 HistoryHeader<N>::Serialize (Buffer::Iterator start) const
       
    96 {
       
    97   start.WriteU8 (N, N);
       
    98 }
       
    99 template <int N>
       
   100 uint32_t
       
   101 HistoryHeader<N>::Deserialize (Buffer::Iterator start)
       
   102 {
       
   103   m_ok = true;
       
   104   for (int i = 0; i < N; i++)
       
   105     {
       
   106       if (start.ReadU8 () != N)
       
   107         {
       
   108           m_ok = false;
       
   109         }
       
   110     }
       
   111   return N;
       
   112 }
       
   113 
       
   114 template <int N>
       
   115 class HistoryTrailer : public Trailer
       
   116 {
       
   117 public:
       
   118   HistoryTrailer ();
       
   119   bool IsOk (void) const;
       
   120 
       
   121   static TypeId GetTypeId (void);
       
   122   virtual TypeId GetInstanceTypeId (void) const;
       
   123   virtual void Print (std::ostream &os) const;
       
   124   virtual uint32_t GetSerializedSize (void) const;
       
   125   virtual void Serialize (Buffer::Iterator start) const;
       
   126   virtual uint32_t Deserialize (Buffer::Iterator start);
       
   127 private:
       
   128   bool m_ok;
       
   129 };
       
   130 
       
   131 template <int N>
       
   132 HistoryTrailer<N>::HistoryTrailer ()
       
   133   : m_ok (false)
       
   134 {}
       
   135 
       
   136 template <int N>
       
   137 bool
       
   138 HistoryTrailer<N>::IsOk (void) const
       
   139 {
       
   140   return m_ok;
       
   141 }
       
   142 
   196 
   143 template <int N>
   197 template <int N>
   144 TypeId
   198 TypeId
   145 HistoryTrailer<N>::GetTypeId (void)
   199 HistoryTrailer<N>::GetTypeId (void)
   146 {
   200 {
   147   std::ostringstream oss;
   201   std::ostringstream oss;
   148   oss << "ns3::HistoryTrailer<"<<N<<">";
   202   oss << "ns3::HistoryTrailer<"<<N<<">";
   149   static TypeId tid = TypeId (oss.str ().c_str ())
   203   static TypeId tid = TypeId (oss.str ().c_str ())
   150     .SetParent<Trailer> ()
   204     .SetParent<HistoryTrailerBase> ()
       
   205     .AddConstructor<HistoryTrailer<N> > ()
   151     ;
   206     ;
   152   return tid;
   207   return tid;
   153 }
   208 }
   154 
   209 
   155 template <int N>
   210 template <int N>
   179 }
   234 }
   180 template <int N>
   235 template <int N>
   181 uint32_t
   236 uint32_t
   182 HistoryTrailer<N>::Deserialize (Buffer::Iterator start)
   237 HistoryTrailer<N>::Deserialize (Buffer::Iterator start)
   183 {
   238 {
   184   m_ok = true;
       
   185   start.Prev (N);
   239   start.Prev (N);
   186   for (int i = 0; i < N; i++)
   240   for (int i = 0; i < N; i++)
   187     {
   241     {
   188       if (start.ReadU8 () != N)
   242       if (start.ReadU8 () != N)
   189         {
   243         {
   190           m_ok = false;
   244           ReportError ();
   191         }
   245         }
   192     }
   246     }
   193   return N;
   247   return N;
   194 }
   248 }
   195 
   249 
   232   PacketMetadata::ItemIterator k = p->BeginItem ();
   286   PacketMetadata::ItemIterator k = p->BeginItem ();
   233   std::list<int> got;
   287   std::list<int> got;
   234   while (k.HasNext ())
   288   while (k.HasNext ())
   235     {
   289     {
   236       struct PacketMetadata::Item item = k.Next ();
   290       struct PacketMetadata::Item item = k.Next ();
       
   291       if (item.isFragment || item.type == PacketMetadata::Item::PAYLOAD)
       
   292         {
       
   293           got.push_back (item.currentSize);
       
   294           continue;
       
   295         }
       
   296       if (item.type == PacketMetadata::Item::HEADER)
       
   297         {
       
   298           Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
       
   299           HistoryHeaderBase *header = dynamic_cast<HistoryHeaderBase *> (constructor ());
       
   300           if (header == 0)
       
   301             {
       
   302               goto error;
       
   303             }
       
   304           header->Deserialize (item.current);
       
   305           if (!header->IsOk ())
       
   306             {
       
   307               delete header;
       
   308               goto error;
       
   309             }
       
   310           delete header;
       
   311         }
       
   312       else if (item.type == PacketMetadata::Item::TRAILER)
       
   313         {
       
   314           Callback<ObjectBase *> constructor = item.tid.GetConstructor ();
       
   315           HistoryTrailerBase *trailer = dynamic_cast<HistoryTrailerBase *> (constructor ());
       
   316           if (trailer == 0)
       
   317             {
       
   318               goto error;
       
   319             }
       
   320           trailer->Deserialize (item.current);
       
   321           if (!trailer->IsOk ())
       
   322             {
       
   323               delete trailer;
       
   324               goto error;
       
   325             }
       
   326           delete trailer;
       
   327         }
   237       got.push_back (item.currentSize);
   328       got.push_back (item.currentSize);
   238     }
   329     }
   239 
   330 
   240   for (std::list<int>::iterator i = got.begin (),
   331   for (std::list<int>::iterator i = got.begin (),
   241          j = expected.begin (); 
   332          j = expected.begin (); 
   289 #define CHECK_HISTORY(p, ...)                   \
   380 #define CHECK_HISTORY(p, ...)                   \
   290   {                                             \
   381   {                                             \
   291     if (!CheckHistory (p, __FILE__,             \
   382     if (!CheckHistory (p, __FILE__,             \
   292                       __LINE__, __VA_ARGS__))   \
   383                       __LINE__, __VA_ARGS__))   \
   293       {                                         \
   384       {                                         \
   294         ok = false;                             \
   385         result = false;                         \
   295       }                                         \
   386       }                                         \
   296     Buffer buffer;                              \
   387     Buffer buffer;                              \
   297     buffer = p->Serialize ();                   \
   388     buffer = p->Serialize ();                   \
   298     Ptr<Packet> otherPacket = Create<Packet> ();\
   389     Ptr<Packet> otherPacket = Create<Packet> ();\
   299     otherPacket->Deserialize  (buffer);         \
   390     otherPacket->Deserialize  (buffer);         \
   300     if (!CheckHistory (otherPacket, __FILE__,   \
   391     if (!CheckHistory (otherPacket, __FILE__,   \
   301                       __LINE__, __VA_ARGS__))   \
   392                       __LINE__, __VA_ARGS__))   \
   302       {                                         \
   393       {                                         \
   303         ok = false;                             \
   394         result = false;                         \
   304       }                                         \
   395       }                                         \
   305   }
   396   }
   306 
   397 
   307 
   398 
   308 Ptr<Packet>
   399 Ptr<Packet>
   313 }
   404 }
   314 
   405 
   315 bool
   406 bool
   316 PacketMetadataTest::RunTests (void)
   407 PacketMetadataTest::RunTests (void)
   317 {
   408 {
   318   bool ok = true;
   409   bool result = true;
   319 
   410 
   320   PacketMetadata::Enable ();
   411   PacketMetadata::Enable ();
   321 
   412 
   322   Ptr<Packet> p = Create<Packet> (0);
   413   Ptr<Packet> p = Create<Packet> (0);
   323   Ptr<Packet> p1 = Create<Packet> (0);
   414   Ptr<Packet> p1 = Create<Packet> (0);
   622   ADD_HEADER (p3, 8);
   713   ADD_HEADER (p3, 8);
   623   REM_HEADER (p2, 8);
   714   REM_HEADER (p2, 8);
   624   REM_HEADER (p3, 8);
   715   REM_HEADER (p3, 8);
   625   p2->AddAtEnd (p3);
   716   p2->AddAtEnd (p3);
   626 
   717 
   627   return ok;
   718 
       
   719   p = Create<Packet> (1000);
       
   720   ADD_HEADER (p, 10);
       
   721   ADD_TRAILER (p, 5);
       
   722   p1 = p->Copy ();
       
   723   ADD_HEADER (p1, 20);
       
   724   REM_HEADER (p1, 20);
       
   725   REM_TRAILER (p1, 5);
       
   726   NS_TEST_ASSERT_EQUAL (p->GetSize (), 1015);
       
   727 
       
   728   
       
   729   p = Create<Packet> (1510);
       
   730   ADD_HEADER (p, 8);
       
   731   ADD_HEADER (p, 25);
       
   732   REM_HEADER (p, 25);
       
   733   ADD_HEADER (p, 1);
       
   734   p1 = p->CreateFragment (0, 1500);
       
   735   p2 = p1->Copy ();
       
   736   ADD_HEADER (p2, 24);
       
   737   NS_TEST_ASSERT_EQUAL (p->GetSize (), 1519);
       
   738 
       
   739   p = Create<Packet> (1000);
       
   740   ADD_HEADER (p, 2);
       
   741   ADD_TRAILER (p, 3);
       
   742   p1 = p->Copy ();
       
   743   CHECK_HISTORY (p1, 3, 2, 1000, 3);
       
   744   REM_HEADER (p, 2);
       
   745   ADD_HEADER (p, 1);
       
   746   CHECK_HISTORY (p, 3, 1, 1000, 3);
       
   747   CHECK_HISTORY (p1, 3, 2, 1000, 3);
       
   748 
       
   749 
       
   750 
       
   751   return result;
   628 }
   752 }
   629 
   753 
   630 static PacketMetadataTest g_packetHistoryTest;
   754 static PacketMetadataTest g_packetHistoryTest;
   631 
   755 
   632 }//namespace ns3
   756 }//namespace ns3