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
26using namespace ns3;
27
28NS_LOG_COMPONENT_DEFINE("TimestampTestSuite");
29
37{
38 public:
43 {
48 };
49
55
56 protected:
59
60 void Tx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
61 void Rx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
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
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
126void
128{
130 {
131 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
132 false,
133 "timestamp disabled but option enabled");
134 }
135 else if (m_configuration == ENABLED)
136 {
137 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
138 true,
139 "timestamp enabled but option disabled");
140 }
141
142 NS_LOG_INFO(h);
143 if (who == SENDER)
144 {
145 if (h.GetFlags() & TcpHeader::SYN)
146 {
148 {
149 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
150 false,
151 "timestamp disabled but option enabled");
152 }
154 {
155 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
156 true,
157 "timestamp enabled but option disabled");
158 }
159 }
160 else
161 {
163 {
164 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
165 false,
166 "timestamp disabled but option enabled");
167 }
168 }
169 }
170 else if (who == RECEIVER)
171 {
172 if (h.GetFlags() & TcpHeader::SYN)
173 {
174 // Sender has not sent timestamp, so implementation should disable ts
176 {
177 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
178 false,
179 "sender has not ts, but receiver sent anyway");
180 }
182 {
183 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
184 false,
185 "receiver has not ts enabled but sent anyway");
186 }
187 }
188 else
189 {
191 {
192 NS_TEST_ASSERT_MSG_EQ(h.HasOption(TcpOption::TS),
193 false,
194 "timestamp disabled but option enabled");
195 }
196 }
197 }
198}
199
200void
202{
203 if (who == SENDER)
204 {
205 }
206 else if (who == RECEIVER)
207 {
208 }
209}
210
218{
219 public:
226 TimestampValueTestCase(double startTime, double timeToWait, std::string name);
227
228 private:
229 void DoRun() override;
230 void DoTeardown() override;
231
235 void Check();
239 void Init();
240
241 double m_startTime;
243 double m_initValue;
244};
245
247 double timeToWait,
248 std::string name)
249 : TestCase(name)
250{
251 m_startTime = startTime;
252 m_timeToWait = timeToWait;
253}
254
255void
257{
258 Simulator::Schedule(Seconds(m_startTime + m_timeToWait), &TimestampValueTestCase::Check, this);
259 Simulator::Schedule(Seconds(m_startTime), &TimestampValueTestCase::Init, this);
260
261 Simulator::Run();
262}
263
264void
266{
267 Simulator::Destroy();
268}
269
270void
272{
273 m_initValue = TcpOptionTS::NowToTsValue();
274}
275
276void
278{
279 uint32_t lastValue = TcpOptionTS::NowToTsValue();
280
283 MilliSeconds(1),
284 "Different TS values");
285
286 NS_TEST_ASSERT_MSG_EQ_TOL(TcpOptionTS::ElapsedTimeFromTsValue(m_initValue),
288 MilliSeconds(1),
289 "Estimating Wrong RTT");
290}
291
299{
300 public:
302 : TestSuite("tcp-timestamp", UNIT)
303 {
308 AddTestCase(new TimestampValueTestCase(0.0, 0.01, "Value Check"), TestCase::QUICK);
309 AddTestCase(new TimestampValueTestCase(3.0, 0.5, "Value Check"), TestCase::QUICK);
310 AddTestCase(new TimestampValueTestCase(5.5, 1.0, "Value Check"), TestCase::QUICK);
311 AddTestCase(new TimestampValueTestCase(6.0, 2.0, "Value Check"), TestCase::QUICK);
312 AddTestCase(new TimestampValueTestCase(2.4, 0.7, "Value Check"), TestCase::QUICK);
313 }
314};
315
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).
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:46
bool HasOption(uint8_t kind) const
Check if the header has the option specified.
Definition: tcp-header.cc:502
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:167
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:305
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:1338
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
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.