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 
TCP TimeStamp option Test.
void TestSerialize()
Serialization test.
uint32_t m_timestamp
TimeStamp.
uint32_t m_echo
Echoed TimeStamp.
virtual void DoRun(void)
Implementation to actually run this TestCase.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
Buffer m_buffer
Buffer.
void TestDeserialize()
Deserialization test.
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.
Buffer m_buffer
Buffer.
TcpOptionWSTestCase(std::string name, uint8_t scale)
Constructor.
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
virtual void DoRun(void)
Implementation to actually run this TestCase.
iterator in a Buffer instance
Definition: buffer.h:99
automatically resized byte buffer
Definition: buffer.h:93
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
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.
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
uint32_t GetEcho(void) const
Get the timestamp echo stored in the Option.
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
uint32_t GetTimestamp(void) const
Get the timestamp stored in the Option.
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
Defines the TCP option of kind 3 (window scale option) as in RFC 1323
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
void SetScale(uint8_t scale)
Set the scale option.
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
uint8_t GetScale(void) const
Get the scale value (uint8_t)
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1197
#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:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
list x
Random number samples.
def start()
Definition: core.py:1853
ns3::StringValue attribute value declarations.
static TcpOptionTestSuite g_TcpOptionTestSuite
Static variable for test initialization.