A Discrete-Event Network Simulator
API
tcp-timestamp-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Natale Patriciello <natale.patriciello@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 #include "tcp-general-test.h"
20 
21 #include "ns3/log.h"
22 #include "ns3/node.h"
23 #include "ns3/tcp-header.h"
24 #include "ns3/tcp-option-ts.h"
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE("TimestampTestSuite");
29 
36 {
37  public:
42  {
46  ENABLED
47  };
48 
54 
55  protected:
56  Ptr<TcpSocketMsgBase> CreateReceiverSocket(Ptr<Node> node) override;
57  Ptr<TcpSocketMsgBase> CreateSenderSocket(Ptr<Node> node) override;
58 
59  void Tx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
60  void Rx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
61 
63 };
64 
66  : TcpGeneralTest("Testing the TCP Timestamp option")
67 {
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),
131  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),
137  true,
138  "timestamp enabled but option disabled");
139  }
140 
141  NS_LOG_INFO(h);
142  if (who == SENDER)
143  {
144  if (h.GetFlags() & TcpHeader::SYN)
145  {
147  {
148  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
149  false,
150  "timestamp disabled but option enabled");
151  }
152  else if (m_configuration == ENABLED_SENDER)
153  {
154  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
155  true,
156  "timestamp enabled but option disabled");
157  }
158  }
159  else
160  {
161  if (m_configuration != ENABLED)
162  {
163  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
164  false,
165  "timestamp disabled but option enabled");
166  }
167  }
168  }
169  else if (who == RECEIVER)
170  {
171  if (h.GetFlags() & TcpHeader::SYN)
172  {
173  // Sender has not sent timestamp, so implementation should disable ts
175  {
176  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
177  false,
178  "sender has not ts, but receiver sent anyway");
179  }
180  else if (m_configuration == ENABLED_SENDER)
181  {
182  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
183  false,
184  "receiver has not ts enabled but sent anyway");
185  }
186  }
187  else
188  {
189  if (m_configuration != ENABLED)
190  {
191  NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
192  false,
193  "timestamp disabled but option enabled");
194  }
195  }
196  }
197 }
198 
199 void
201 {
202  if (who == SENDER)
203  {
204  }
205  else if (who == RECEIVER)
206  {
207  }
208 }
209 
216 {
217  public:
224  TimestampValueTestCase(double startTime, double timeToWait, std::string name);
225 
226  private:
227  void DoRun() override;
228  void DoTeardown() override;
229 
233  void Check();
237  void Init();
238 
239  double m_startTime;
240  double m_timeToWait;
241  double m_initValue;
242 };
243 
245  double timeToWait,
246  std::string name)
247  : TestCase(name)
248 {
249  m_startTime = startTime;
250  m_timeToWait = timeToWait;
251 }
252 
253 void
255 {
256  Simulator::Schedule(Seconds(m_startTime + m_timeToWait), &TimestampValueTestCase::Check, this);
257  Simulator::Schedule(Seconds(m_startTime), &TimestampValueTestCase::Init, this);
258 
259  Simulator::Run();
260 }
261 
262 void
264 {
265  Simulator::Destroy();
266 }
267 
268 void
270 {
271  m_initValue = TcpOptionTS::NowToTsValue();
272 }
273 
274 void
276 {
277  uint32_t lastValue = TcpOptionTS::NowToTsValue();
278 
281  MilliSeconds(1),
282  "Different TS values");
283 
284  NS_TEST_ASSERT_MSG_EQ_TOL(TcpOptionTS::ElapsedTimeFromTsValue(m_initValue),
286  MilliSeconds(1),
287  "Estimating Wrong RTT");
288 }
289 
296 {
297  public:
299  : TestSuite("tcp-timestamp", UNIT)
300  {
305  AddTestCase(new TimestampValueTestCase(0.0, 0.01, "Value Check"), TestCase::QUICK);
306  AddTestCase(new TimestampValueTestCase(3.0, 0.5, "Value Check"), TestCase::QUICK);
307  AddTestCase(new TimestampValueTestCase(5.5, 1.0, "Value Check"), TestCase::QUICK);
308  AddTestCase(new TimestampValueTestCase(6.0, 2.0, "Value Check"), TestCase::QUICK);
309  AddTestCase(new TimestampValueTestCase(2.4, 0.7, "Value Check"), TestCase::QUICK);
310  }
311 };
312 
TCP TimeStamp TestSuite.
TCP TimeStamp enabling Test.
Configuration m_configuration
Test configuration.
void Tx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who) override
Packet transmitted down to IP layer.
TimestampTestCase(TimestampTestCase::Configuration conf)
Constructor.
void Rx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who) override
Packet received from IP layer.
Ptr< TcpSocketMsgBase > CreateSenderSocket(Ptr< Node > node) override
Create and install the socket to install on the sender.
Ptr< TcpSocketMsgBase > CreateReceiverSocket(Ptr< Node > node) override
Create and install the socket to install on the receiver.
Configuration
TimeStamp configuration.
TCP TimeStamp values Test.
void DoRun() override
Implementation to actually run this TestCase.
TimestampValueTestCase(double startTime, double timeToWait, std::string name)
Constructor.
double m_timeToWait
Time to wait (Seconds).
double m_startTime
Start time (Seconds).
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void Check()
Perform the test checks.
void Init()
Test initialization.
double m_initValue
Initialization value (Seconds).
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
General infrastructure for TCP testing.
SocketWho
Used as parameter of methods, specifies on what node the caller is interested (e.g.
@ RECEIVER
Receiver node.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:47
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition: tcp-header.cc:478
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:148
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#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:144
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Definition: conf.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TcpTimestampTestSuite g_tcpTimestampTestSuite
Static variable for test initialization.