A Discrete-Event Network Simulator
API
tcp-option-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 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 "ns3/test.h"
21 #include "ns3/core-module.h"
22 #include "ns3/tcp-option.h"
23 #include "ns3/tcp-option-winscale.h"
24 #include "ns3/tcp-option-ts.h"
25 
26 #include <string.h>
27 
28 using namespace ns3;
29 
37 {
38 public:
44  TcpOptionWSTestCase (std::string name, uint8_t scale);
45 
49  void TestSerialize ();
53  void TestDeserialize ();
54 
55 private:
56  virtual void DoRun (void);
57  virtual void DoTeardown (void);
58 
59  uint8_t m_scale;
61 };
62 
63 
64 TcpOptionWSTestCase::TcpOptionWSTestCase (std::string name, uint8_t scale)
65  : TestCase (name)
66 {
67  m_scale = scale;
68 }
69 
70 void
72 {
73  TestSerialize ();
74  TestDeserialize ();
75 }
76 
77 void
79 {
81 
82  opt.SetScale (m_scale);
83  NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Scale isn't saved correctly");
84 
86 
87  opt.Serialize (m_buffer.Begin ());
88 }
89 
90 void
92 {
94 
96  uint8_t kind = start.PeekU8 ();
97 
98  NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::WINSCALE, "Different kind found");
99 
100  opt.Deserialize (start);
101 
102  NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Different scale found");
103 }
104 
105 void
107 {
108 }
109 
110 
118 {
119 public:
120 
125  TcpOptionTSTestCase (std::string name);
126 
130  void TestSerialize ();
134  void TestDeserialize ();
135 
136 private:
137  virtual void DoRun (void);
138  virtual void DoTeardown (void);
139 
140  uint32_t m_timestamp;
141  uint32_t m_echo;
143 };
144 
145 
147  : TestCase (name)
148 {
149  m_timestamp = 0;
150  m_echo = 0;
151 }
152 
153 void
155 {
156  Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
157 
158  for (uint32_t i = 0; i < 1000; ++i)
159  {
160  m_timestamp = x->GetInteger ();
161  m_echo = x->GetInteger ();
162  TestSerialize ();
163  TestDeserialize ();
164  }
165 }
166 
167 void
169 {
170  TcpOptionTS opt;
171 
173  opt.SetEcho (m_echo);
174 
175  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "TS isn't saved correctly");
176  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "echo isn't saved correctly");
177 
179 
180  opt.Serialize (m_buffer.Begin ());
181 }
182 
183 void
185 {
186  TcpOptionTS opt;
187 
189  uint8_t kind = start.PeekU8 ();
190 
191  NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::TS, "Different kind found");
192 
193  opt.Deserialize (start);
194 
195  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "Different TS found");
196  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "Different echo found");
197 }
198 
199 void
201 {
202 }
203 
211 {
212 public:
214  : TestSuite ("tcp-option", UNIT)
215  {
216  for (uint8_t i = 0; i < 15; ++i)
217  {
218  AddTestCase (new TcpOptionWSTestCase ("Testing window scale value", i), TestCase::QUICK);
219  }
220  AddTestCase (new TcpOptionTSTestCase ("Testing serialization of random values for timestamp"), TestCase::QUICK);
221  }
222 
223 };
224 
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
TcpOptionTSTestCase(std::string name)
Constructor.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
static TcpOptionTestSuite g_TcpOptionTestSuite
Static variable for test initialization.
ns3::StringValue attribute value declarations.
void TestDeserialize()
Deserialization test.
A suite of tests to run.
Definition: test.h:1342
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1806
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
uint32_t m_timestamp
TimeStamp.
void SetTimestamp(uint32_t ts)
Set the timestamp stored in the Option.
Defines the TCP option of kind 3 (window scale option) as in RFC 1323
#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:285
void TestSerialize()
Serialization test.
encapsulates test code
Definition: test.h:1155
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
TcpOptionWSTestCase(std::string name, uint8_t scale)
Constructor.
uint8_t m_scale
Window scaling.
iterator in a Buffer instance
Definition: buffer.h:98
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
TCP Window Scaling option Test.
uint8_t GetScale(void) const
Get the scale value (uint8_t)
TCP options TestSuite.
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
virtual void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Buffer m_buffer
Buffer.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
virtual void DoRun(void)
Implementation to actually run this TestCase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetScale(uint8_t scale)
Set the scale option.
TCP TimeStamp option Test.
Defines the TCP option of kind 8 (timestamp option) as in RFC 1323
Definition: tcp-option-ts.h:35
Buffer m_buffer
Buffer.
uint32_t m_echo
Echoed TimeStamp.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
uint8_t PeekU8(void)
Definition: buffer.h:998
void TestSerialize()
Serialization test.
void TestDeserialize()
Deserialization test.
uint32_t GetTimestamp(void) const
Get the timestamp stored in the Option.
uint32_t GetEcho(void) const
Get the timestamp echo stored in the Option.