A Discrete-Event Network Simulator
API
tcp-option-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 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 "ns3/core-module.h"
20#include "ns3/tcp-option-ts.h"
21#include "ns3/tcp-option-winscale.h"
22#include "ns3/tcp-option.h"
23#include "ns3/test.h"
24
25#include <string.h>
26
27using namespace ns3;
28
36{
37 public:
43 TcpOptionWSTestCase(std::string name, uint8_t scale);
44
48 void TestSerialize();
52 void TestDeserialize();
53
54 private:
55 void DoRun() override;
56 void DoTeardown() override;
57
58 uint8_t m_scale;
60};
61
62TcpOptionWSTestCase::TcpOptionWSTestCase(std::string name, uint8_t scale)
63 : TestCase(name)
64{
65 m_scale = scale;
66}
67
68void
70{
73}
74
75void
77{
79
80 opt.SetScale(m_scale);
81 NS_TEST_EXPECT_MSG_EQ(m_scale, opt.GetScale(), "Scale isn't saved correctly");
82
84
86}
87
88void
90{
92
94 uint8_t kind = start.PeekU8();
95
96 NS_TEST_EXPECT_MSG_EQ(kind, TcpOption::WINSCALE, "Different kind found");
97
98 opt.Deserialize(start);
99
100 NS_TEST_EXPECT_MSG_EQ(m_scale, opt.GetScale(), "Different scale found");
101}
102
103void
105{
106}
107
115{
116 public:
121 TcpOptionTSTestCase(std::string name);
122
126 void TestSerialize();
130 void TestDeserialize();
131
132 private:
133 void DoRun() override;
134 void DoTeardown() override;
135
139};
140
142 : TestCase(name)
143{
144 m_timestamp = 0;
145 m_echo = 0;
146}
147
148void
150{
151 Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable>();
152
153 for (uint32_t i = 0; i < 1000; ++i)
154 {
155 m_timestamp = x->GetInteger();
156 m_echo = x->GetInteger();
159 }
160}
161
162void
164{
165 TcpOptionTS opt;
166
168 opt.SetEcho(m_echo);
169
170 NS_TEST_EXPECT_MSG_EQ(m_timestamp, opt.GetTimestamp(), "TS isn't saved correctly");
171 NS_TEST_EXPECT_MSG_EQ(m_echo, opt.GetEcho(), "echo isn't saved correctly");
172
174
175 opt.Serialize(m_buffer.Begin());
176}
177
178void
180{
181 TcpOptionTS opt;
182
184 uint8_t kind = start.PeekU8();
185
186 NS_TEST_EXPECT_MSG_EQ(kind, TcpOption::TS, "Different kind found");
187
188 opt.Deserialize(start);
189
190 NS_TEST_EXPECT_MSG_EQ(m_timestamp, opt.GetTimestamp(), "Different TS found");
191 NS_TEST_EXPECT_MSG_EQ(m_echo, opt.GetEcho(), "Different echo found");
192}
193
194void
196{
197}
198
206{
207 public:
209 : TestSuite("tcp-option", UNIT)
210 {
211 for (uint8_t i = 0; i < 15; ++i)
212 {
213 AddTestCase(new TcpOptionWSTestCase("Testing window scale value", i), TestCase::QUICK);
214 }
215 AddTestCase(new TcpOptionTSTestCase("Testing serialization of random values for timestamp"),
216 TestCase::QUICK);
217 }
218};
219
TCP TimeStamp option Test.
void TestSerialize()
Serialization test.
uint32_t m_timestamp
TimeStamp.
uint32_t m_echo
Echoed TimeStamp.
Buffer m_buffer
Buffer.
void TestDeserialize()
Deserialization test.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
void DoRun() override
Implementation to actually run this TestCase.
TcpOptionTSTestCase(std::string name)
Constructor.
TCP options TestSuite.
TCP Window Scaling option Test.
void TestSerialize()
Serialization test.
void TestDeserialize()
Deserialization test.
uint8_t m_scale
Window scaling.
void DoTeardown() override
Implementation to do any local setup required for this TestCase.
Buffer m_buffer
Buffer.
void DoRun() override
Implementation to actually run this TestCase.
TcpOptionWSTestCase(std::string name, uint8_t scale)
Constructor.
iterator in a Buffer instance
Definition: buffer.h:100
automatically resized byte buffer
Definition: buffer.h:94
void AddAtStart(uint32_t start)
Definition: buffer.cc:311
Buffer::Iterator Begin() const
Definition: buffer.h:1074
Defines the TCP option of kind 8 (timestamp option) as in RFC 1323
Definition: tcp-option-ts.h:36
void SetTimestamp(uint32_t ts)
Set the timestamp stored in the Option.
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
uint32_t GetTimestamp() const
Get the timestamp stored in the Option.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
uint32_t GetEcho() const
Get the timestamp echo stored in the Option.
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
Defines the TCP option of kind 3 (window scale option) as in RFC 1323
uint8_t GetScale() const
Get the scale value (uint8_t)
uint32_t GetSerializedSize() const override
Returns number of bytes required for Option serialization.
void Serialize(Buffer::Iterator start) const override
Serialize the Option to a buffer iterator.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the Option from a buffer iterator.
void SetScale(uint8_t scale)
Set the scale option.
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
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:251
Every class exported by the ns3 library is enclosed in the ns3 namespace.
def start()
Definition: core.py:1861
ns3::StringValue attribute value declarations.
static TcpOptionTestSuite g_TcpOptionTestSuite
Static variable for test initialization.