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 #include "ns3/tcp-header.h"
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE ("TimestampTestSuite");
29 
37 {
38 public:
43  {
47  ENABLED
48  };
49 
55 
56 protected:
57  virtual Ptr<TcpSocketMsgBase> CreateReceiverSocket (Ptr<Node> node);
58  virtual Ptr<TcpSocketMsgBase> CreateSenderSocket (Ptr<Node> node);
59 
60  virtual void Tx (const Ptr<const Packet> p, const TcpHeader&h, SocketWho who);
61  virtual void Rx (const Ptr<const Packet> p, const TcpHeader&h, SocketWho who);
62 
64 };
65 
67  : TcpGeneralTest ("Testing the TCP Timestamp option")
68 {
70 }
71 
74 {
75  Ptr<TcpSocketMsgBase> socket = TcpGeneralTest::CreateReceiverSocket (node);
76 
77  switch (m_configuration)
78  {
79  case DISABLED:
80  socket->SetAttribute ("Timestamp", BooleanValue (false));
81  break;
82 
83  case ENABLED_RECEIVER:
84  socket->SetAttribute ("Timestamp", BooleanValue (true));
85  break;
86 
87  case ENABLED_SENDER:
88  socket->SetAttribute ("Timestamp", BooleanValue (false));
89  break;
90 
91  case ENABLED:
92  socket->SetAttribute ("Timestamp", BooleanValue (true));
93  break;
94  }
95 
96  return socket;
97 }
98 
101 {
102  Ptr<TcpSocketMsgBase> socket = TcpGeneralTest::CreateSenderSocket (node);
103 
104  switch (m_configuration)
105  {
106  case DISABLED:
107  socket->SetAttribute ("Timestamp", BooleanValue (false));
108  break;
109 
110  case ENABLED_RECEIVER:
111  socket->SetAttribute ("Timestamp", BooleanValue (false));
112  break;
113 
114  case ENABLED_SENDER:
115  socket->SetAttribute ("Timestamp", BooleanValue (true));
116  break;
117 
118  case ENABLED:
119  socket->SetAttribute ("Timestamp", BooleanValue (true));
120  break;
121  }
122 
123  return socket;
124 }
125 
126 void
128 {
129  if (m_configuration == DISABLED)
130  {
131  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
132  "timestamp disabled but option enabled");
133  }
134  else if (m_configuration == ENABLED)
135  {
136  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), true,
137  "timestamp enabled but option disabled");
138  }
139 
140  NS_LOG_INFO (h);
141  if (who == SENDER)
142  {
143  if (h.GetFlags () & TcpHeader::SYN)
144  {
146  {
147  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
148  "timestamp disabled but option enabled");
149  }
150  else if (m_configuration == ENABLED_SENDER)
151  {
152  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), true,
153  "timestamp enabled but option disabled");
154  }
155  }
156  else
157  {
158  if (m_configuration != ENABLED)
159  {
160  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
161  "timestamp disabled but option enabled");
162  }
163  }
164  }
165  else if (who == RECEIVER)
166  {
167  if (h.GetFlags () & TcpHeader::SYN)
168  {
169  // Sender has not sent timestamp, so implementation should disable ts
171  {
172  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
173  "sender has not ts, but receiver sent anyway");
174  }
175  else if (m_configuration == ENABLED_SENDER)
176  {
177  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
178  "receiver has not ts enabled but sent anyway");
179  }
180  }
181  else
182  {
183  if (m_configuration != ENABLED)
184  {
185  NS_TEST_ASSERT_MSG_EQ (h.HasOption (TcpOption::TS), false,
186  "timestamp disabled but option enabled");
187  }
188  }
189  }
190 }
191 
192 void
194 {
195  if (who == SENDER)
196  {
197 
198  }
199  else if (who == RECEIVER)
200  {
201 
202  }
203 }
204 
212 {
213 public:
220  TimestampValueTestCase (double startTime, double timeToWait,
221  std::string name);
222 
223 private:
224  virtual void DoRun (void);
225  virtual void DoTeardown (void);
226 
230  void Check ();
234  void Init ();
235 
236  double m_startTime;
237  double m_timeToWait;
238  double m_initValue;
239 };
240 
242  double timeToWait,
243  std::string name)
244  : TestCase (name)
245 {
247  m_timeToWait = timeToWait;
248 }
249 
250 void
252 {
253  Simulator::Schedule (Seconds (m_startTime + m_timeToWait),
255  Simulator::Schedule (Seconds (m_startTime),
257 
258  Simulator::Run ();
259 }
260 
261 void
263 {
264  Simulator::Destroy ();
265 }
266 
267 void
269 {
270  m_initValue = TcpOptionTS::NowToTsValue ();
271 }
272 
273 void
275 {
276  uint32_t lastValue = TcpOptionTS::NowToTsValue ();
277 
279  MilliSeconds (1), "Different TS values");
280 
281  NS_TEST_ASSERT_MSG_EQ_TOL (TcpOptionTS::ElapsedTimeFromTsValue (m_initValue), Seconds (m_timeToWait),
282  MilliSeconds (1), "Estimating Wrong RTT");
283 }
284 
292 {
293 public:
295  : TestSuite ("tcp-timestamp", UNIT)
296  {
301  AddTestCase (new TimestampValueTestCase (0.0, 0.01, "Value Check"), TestCase::QUICK);
302  AddTestCase (new TimestampValueTestCase (3.0, 0.5, "Value Check"), TestCase::QUICK);
303  AddTestCase (new TimestampValueTestCase (5.5, 1.0, "Value Check"), TestCase::QUICK);
304  AddTestCase (new TimestampValueTestCase (6.0, 2.0, "Value Check"), TestCase::QUICK);
305  AddTestCase (new TimestampValueTestCase (2.4, 0.7, "Value Check"), TestCase::QUICK);
306 
307  }
308 
309 };
310 
312 
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.
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:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Configuration
TimeStamp configuration.
Definition: conf.py:1
encapsulates test code
Definition: test.h:1155
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
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.
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition: tcp-header.cc:511
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.
SocketWho
Used as parameter of methods, specifies on what node the caller is interested (e.g.
General infrastructure for TCP testing.
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:173
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
This test suite implements a Unit Test.
Definition: test.h:1352
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.