1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Natale Patriciello <natale.patriciello@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19 
20 #include "ns3/test.h"
21 #include "ns3/core-module.h"
22 #include "ns3/tcp-option.h"
23 #include "ns3/tcp-option-winscale.h"
24 #include "ns3/tcp-option-ts.h"
25 
26 #include <string.h>
27 
28 using namespace ns3;
29 
30 /**
31  * \ingroup internet-test
32  * \ingroup tests
33  *
34  * \brief TCP Window Scaling option Test
35  */
36 class TcpOptionWSTestCase : public TestCase
37 {
38 public:
39   /**
40    * \brief Constructor.
41    * \param name Test description.
42    * \param scale Window scaling.
43    */
44   TcpOptionWSTestCase (std::string name, uint8_t scale);
45 
46   /**
47    * \brief Serialization test.
48    */
49   void TestSerialize ();
50   /**
51    * \brief Deserialization test.
52    */
53   void TestDeserialize ();
54 
55 private:
56   virtual void DoRun (void);
57   virtual void DoTeardown (void);
58 
59   uint8_t m_scale;  //!< Window scaling.
60   Buffer m_buffer;  //!< Buffer.
61 };
62 
63 
TcpOptionWSTestCase(std::string name,uint8_t scale)64 TcpOptionWSTestCase::TcpOptionWSTestCase (std::string name, uint8_t scale)
65   : TestCase (name)
66 {
67   m_scale = scale;
68 }
69 
70 void
DoRun()71 TcpOptionWSTestCase::DoRun ()
72 {
73   TestSerialize ();
74   TestDeserialize ();
75 }
76 
77 void
TestSerialize()78 TcpOptionWSTestCase::TestSerialize ()
79 {
80   TcpOptionWinScale opt;
81 
82   opt.SetScale (m_scale);
83   NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Scale isn't saved correctly");
84 
85   m_buffer.AddAtStart (opt.GetSerializedSize ());
86 
87   opt.Serialize (m_buffer.Begin ());
88 }
89 
90 void
TestDeserialize()91 TcpOptionWSTestCase::TestDeserialize ()
92 {
93   TcpOptionWinScale opt;
94 
95   Buffer::Iterator start = m_buffer.Begin ();
96   uint8_t kind = start.PeekU8 ();
97 
98   NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::WINSCALE, "Different kind found");
99 
100   opt.Deserialize (start);
101 
102   NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Different scale found");
103 }
104 
105 void
DoTeardown()106 TcpOptionWSTestCase::DoTeardown ()
107 {
108 }
109 
110 
111 /**
112  * \ingroup internet-test
113  * \ingroup tests
114  *
115  * \brief TCP TimeStamp option Test
116  */
117 class TcpOptionTSTestCase : public TestCase
118 {
119 public:
120 
121   /**
122    * \brief Constructor.
123    * \param name Test description.
124    */
125   TcpOptionTSTestCase (std::string name);
126 
127   /**
128    * \brief Serialization test.
129    */
130   void TestSerialize ();
131   /**
132    * \brief Deserialization test.
133    */
134   void TestDeserialize ();
135 
136 private:
137   virtual void DoRun (void);
138   virtual void DoTeardown (void);
139 
140   uint32_t m_timestamp; //!< TimeStamp.
141   uint32_t m_echo;      //!< Echoed TimeStamp.
142   Buffer m_buffer;      //!< Buffer.
143 };
144 
145 
TcpOptionTSTestCase(std::string name)146 TcpOptionTSTestCase::TcpOptionTSTestCase (std::string name)
147   : TestCase (name)
148 {
149   m_timestamp = 0;
150   m_echo = 0;
151 }
152 
153 void
DoRun()154 TcpOptionTSTestCase::DoRun ()
155 {
156   Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
157 
158   for (uint32_t i = 0; i < 1000; ++i)
159     {
160       m_timestamp = x->GetInteger ();
161       m_echo = x->GetInteger ();
162       TestSerialize ();
163       TestDeserialize ();
164     }
165 }
166 
167 void
TestSerialize()168 TcpOptionTSTestCase::TestSerialize ()
169 {
170   TcpOptionTS opt;
171 
172   opt.SetTimestamp (m_timestamp);
173   opt.SetEcho (m_echo);
174 
175   NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "TS isn't saved correctly");
176   NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "echo isn't saved correctly");
177 
178   m_buffer.AddAtStart (opt.GetSerializedSize ());
179 
180   opt.Serialize (m_buffer.Begin ());
181 }
182 
183 void
TestDeserialize()184 TcpOptionTSTestCase::TestDeserialize ()
185 {
186   TcpOptionTS opt;
187 
188   Buffer::Iterator start = m_buffer.Begin ();
189   uint8_t kind = start.PeekU8 ();
190 
191   NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::TS, "Different kind found");
192 
193   opt.Deserialize (start);
194 
195   NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "Different TS found");
196   NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "Different echo found");
197 }
198 
199 void
DoTeardown()200 TcpOptionTSTestCase::DoTeardown ()
201 {
202 }
203 
204 /**
205  * \ingroup internet-test
206  * \ingroup tests
207  *
208  * \brief TCP options TestSuite
209  */
210 class TcpOptionTestSuite : public TestSuite
211 {
212 public:
TcpOptionTestSuite()213   TcpOptionTestSuite ()
214     : TestSuite ("tcp-option", UNIT)
215   {
216     for (uint8_t i = 0; i < 15; ++i)
217       {
218         AddTestCase (new TcpOptionWSTestCase ("Testing window scale value", i), TestCase::QUICK);
219       }
220     AddTestCase (new TcpOptionTSTestCase ("Testing serialization of random values for timestamp"), TestCase::QUICK);
221   }
222 
223 };
224 
225 static TcpOptionTestSuite g_TcpOptionTestSuite; //!< Static variable for test initialization
226