A Discrete-Event Network Simulator
API
tcp-timestamp-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 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 "tcp-general-test.h"
21 #include "ns3/node.h"
22 #include "ns3/log.h"
23 #include "ns3/tcp-option-ts.h"
24 
25 using namespace ns3;
26 
27 NS_LOG_COMPONENT_DEFINE ("TimestampTestSuite");
28 
36 {
37 public:
42  {
46  ENABLED
47  };
48 
54 
55 protected:
56  virtual Ptr<TcpSocketMsgBase> CreateReceiverSocket (Ptr<Node> node);
57  virtual Ptr<TcpSocketMsgBase> CreateSenderSocket (Ptr<Node> node);
58 
59  virtual void Tx (const Ptr<const Packet> p, const TcpHeader&h, SocketWho who);
60  virtual void Rx (const Ptr<const Packet> p, const TcpHeader&h, SocketWho who);
61 
63 };
64 
66  : TcpGeneralTest ("Testing the TCP Timestamp option")
67 {
68  m_configuration = conf;
69 }
70 
73 {
74  Ptr<TcpSocketMsgBase> socket = TcpGeneralTest::CreateReceiverSocket (node);
75 
76  switch (m_configuration)
77  {
78  case DISABLED:
79  socket->SetAttribute ("Timestamp", BooleanValue (false));
80  break;
81 
82  case ENABLED_RECEIVER:
83  socket->SetAttribute ("Timestamp", BooleanValue (true));
84  break;
85 
86  case ENABLED_SENDER:
87  socket->SetAttribute ("Timestamp", BooleanValue (false));
88  break;
89 
90  case ENABLED:
91  socket->SetAttribute ("Timestamp", BooleanValue (true));
92  break;
93  }
94 
95  return socket;
96 }
97 
100 {
101  Ptr<TcpSocketMsgBase> socket = TcpGeneralTest::CreateSenderSocket (node);
102 
103  switch (m_configuration)
104  {
105  case DISABLED:
106  socket->SetAttribute ("Timestamp", BooleanValue (false));
107  break;
108 
109  case ENABLED_RECEIVER:
110  socket->SetAttribute ("Timestamp", BooleanValue (false));
111  break;
112 
113  case ENABLED_SENDER:
114  socket->SetAttribute ("Timestamp", BooleanValue (true));
115  break;
116 
117  case ENABLED:
118  socket->SetAttribute ("Timestamp", BooleanValue (true));
119  break;
120  }
121 
122  return socket;
123 }
124 
125 void
127 {
128  if (m_configuration == DISABLED)
129  {
130  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
131  "timestamp disabled but option enabled");
132  }
133  else if (m_configuration == ENABLED)
134  {
135  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), true,
136  "timestamp enabled but option disabled");
137  }
138 
139  NS_LOG_INFO (h);
140  if (who == SENDER)
141  {
142  if (h.GetFlags () & TcpHeader::SYN)
143  {
145  {
146  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
147  "timestamp disabled but option enabled");
148  }
149  else if (m_configuration == ENABLED_SENDER)
150  {
151  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), true,
152  "timestamp enabled but option disabled");
153  }
154  }
155  else
156  {
157  if (m_configuration != ENABLED)
158  {
159  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
160  "timestamp disabled but option enabled");
161  }
162  }
163  }
164  else if (who == RECEIVER)
165  {
166  if (h.GetFlags () & TcpHeader::SYN)
167  {
168  // Sender has not sent timestamp, so implementation should disable ts
170  {
171  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
172  "sender has not ts, but receiver sent anyway");
173  }
174  else if (m_configuration == ENABLED_SENDER)
175  {
176  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
177  "receiver has not ts enabled but sent anyway");
178  }
179  }
180  else
181  {
182  if (m_configuration != ENABLED)
183  {
184  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
185  "timestamp disabled but option enabled");
186  }
187  }
188  }
189 }
190 
191 void
193 {
194  if (who == SENDER)
195  {
196 
197  }
198  else if (who == RECEIVER)
199  {
200 
201  }
202 }
203 
211 {
212 public:
219  TimestampValueTestCase (double startTime, double timeToWait,
220  std::string name);
221 
222 private:
223  virtual void DoRun (void);
224  virtual void DoTeardown (void);
225 
229  void Check ();
233  void Init ();
234 
235  double m_startTime;
236  double m_timeToWait;
237  double m_initValue;
238 };
239 
241  double timeToWait,
242  std::string name)
243  : TestCase (name)
244 {
246  m_timeToWait = timeToWait;
247 }
248 
249 void
251 {
252  Simulator::Schedule (Seconds (m_startTime + m_timeToWait),
254  Simulator::Schedule (Seconds (m_startTime),
256 
257  Simulator::Run ();
258 }
259 
260 void
262 {
263  Simulator::Destroy ();
264 }
265 
266 void
268 {
269  m_initValue = TcpOptionTS::NowToTsValue ();
270 }
271 
272 void
274 {
275  uint32_t lastValue = TcpOptionTS::NowToTsValue ();
276 
278  MilliSeconds (1), "Different TS values");
279 
280  NS_TEST_ASSERT_MSG_EQ_TOL (TcpOptionTS::ElapsedTimeFromTsValue (m_initValue), Seconds (m_timeToWait),
281  MilliSeconds (1), "Estimating Wrong RTT");
282 }
283 
291 {
292 public:
294  : TestSuite ("tcp-timestamp", UNIT)
295  {
300  AddTestCase (new TimestampValueTestCase (0.0, 0.01, "Value Check"), TestCase::QUICK);
301  AddTestCase (new TimestampValueTestCase (3.0, 0.5, "Value Check"), TestCase::QUICK);
302  AddTestCase (new TimestampValueTestCase (5.5, 1.0, "Value Check"), TestCase::QUICK);
303  AddTestCase (new TimestampValueTestCase (6.0, 2.0, "Value Check"), TestCase::QUICK);
304  AddTestCase (new TimestampValueTestCase (2.4, 0.7, "Value Check"), TestCase::QUICK);
305 
306  }
307 
308 };
309 
311 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:36
virtual void Tx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who)
Packet transmitted down to IP layer.
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:173
TCP TimeStamp values Test.
A suite of tests to run.
Definition: test.h:1342
virtual void Rx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who)
Packet received from IP layer.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1001
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Definition: conf.py:1
encapsulates test code
Definition: test.h:1155
This test suite implements a Unit Test.
Definition: test.h:1352
TimestampValueTestCase(double startTime, double timeToWait, std::string name)
Constructor.
double m_timeToWait
Time to wait (Seconds).
static TcpTimestampTestSuite g_tcpTimestampTestSuite
Static variable for test initialization.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
SocketWho
Used as parameter of methods, specifies on what node the caller is interested (e.g.
double m_initValue
Initialization value (Seconds).
double startTime
#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:168
TimestampTestCase(TimestampTestCase::Configuration conf)
Constructor.
virtual Ptr< TcpSocketMsgBase > CreateReceiverSocket(Ptr< Node > node)
Create and install the socket to install on the receiver.
virtual Ptr< TcpSocketMsgBase > CreateSenderSocket(Ptr< Node > node)
Create and install the socket to install on the sender.
Configuration m_configuration
Test configuration.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:380
TCP TimeStamp TestSuite.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
virtual void DoRun(void)
Implementation to actually run this TestCase.
void Check()
Perform the test checks.
void Init()
Test initialization.
General infrastructure for TCP testing.
Configuration
TimeStamp configuration.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition: tcp-header.cc:511
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
double m_startTime
Start time (Seconds).
TCP TimeStamp enabling Test.