A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../src/internet/model/tcp-option-winscale.h"
24 #include "../src/internet/model/tcp-option-ts.h"
25 
26 #include <string.h>
27 
28 namespace ns3 {
29 
31 {
32 public:
33  TcpOptionWSTestCase (std::string name, uint8_t scale);
34 
35  void TestSerialize ();
36  void TestDeserialize ();
37 
38 private:
39  virtual void DoRun (void);
40  virtual void DoTeardown (void);
41 
42  uint8_t m_scale;
44 };
45 
46 
47 TcpOptionWSTestCase::TcpOptionWSTestCase (std::string name, uint8_t scale)
48  : TestCase (name)
49 {
50  m_scale = scale;
51 }
52 
53 void
55 {
56  TestSerialize ();
57  TestDeserialize ();
58 }
59 
60 void
62 {
64 
65  opt.SetScale (m_scale);
66  NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Scale isn't saved correctly");
67 
69 
70  opt.Serialize (m_buffer.Begin ());
71 }
72 
73 void
75 {
77 
79  uint8_t kind = start.PeekU8 ();
80 
81  NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::WINSCALE, "Different kind found");
82 
83  opt.Deserialize (start);
84 
85  NS_TEST_EXPECT_MSG_EQ (m_scale, opt.GetScale (), "Different scale found");
86 }
87 
88 void
90 {
91 }
92 
94 {
95 public:
96  TcpOptionTSTestCase (std::string name, uint32_t timestamp, uint32_t echo);
97 
98  void TestSerialize ();
99  void TestDeserialize ();
100 
101 private:
102  virtual void DoRun (void);
103  virtual void DoTeardown (void);
104 
105  uint32_t m_timestamp;
106  uint32_t m_echo;
108 };
109 
110 
111 TcpOptionTSTestCase::TcpOptionTSTestCase (std::string name, uint32_t timestamp,
112  uint32_t echo)
113  : TestCase (name)
114 {
115  m_timestamp = timestamp;
116  m_echo = echo;
117 }
118 
119 void
121 {
122  TestSerialize ();
123  TestDeserialize ();
124 }
125 
126 void
128 {
129  TcpOptionTS opt;
130 
132  opt.SetEcho (m_echo);
133 
134  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "TS isn't saved correctly");
135  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "echo isn't saved correctly");
136 
138 
139  opt.Serialize (m_buffer.Begin ());
140 }
141 
142 void
144 {
145  TcpOptionTS opt;
146 
148  uint8_t kind = start.PeekU8 ();
149 
150  NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::TS, "Different kind found");
151 
152  opt.Deserialize (start);
153 
154  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "Different TS found");
155  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "Different echo found");
156 }
157 
158 void
160 {
161 }
162 
163 static class TcpOptionTestSuite : public TestSuite
164 {
165 public:
167  : TestSuite ("tcp-option", UNIT)
168  {
169  for (uint8_t i = 0; i < 15; ++i)
170  {
171  AddTestCase (new TcpOptionWSTestCase ("Testing window "
172  "scale value", i), TestCase::QUICK);
173  }
174 
175  Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
176 
177  for (uint32_t i = 0; i < 1000; ++i)
178  {
179  AddTestCase (new TcpOptionTSTestCase ("Testing serialization of random "
180  "values for timestamp",
181  x->GetInteger (),
182  x->GetInteger ()), TestCase::QUICK);
183  }
184 
185  }
186 
188 
189 } // namespace ns3
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
virtual void DoRun(void)
Implementation to actually run this TestCase.
uint32_t GetInteger(uint32_t min, uint32_t max)
Returns a random unsigned integer from a uniform distribution over the interval [min,max] including both ends.
A suite of tests to run.
Definition: test.h:1289
automatically resized byte buffer
Definition: buffer.h:92
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
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:265
encapsulates test code
Definition: test.h:1113
void SetEcho(uint32_t ts)
Set the timestamp echo stored in the Option.
iterator in a Buffer instance
Definition: buffer.h:98
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the Option from a buffer iterator.
TcpOptionTSTestCase(std::string name, uint32_t timestamp, uint32_t echo)
uint8_t GetScale(void) const
Get the scale value (uint8_t)
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
TcpOptionWSTestCase(std::string name, uint8_t scale)
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:1076
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
void SetScale(uint8_t scale)
Set the scale option.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
virtual void DoRun(void)
Implementation to actually run this TestCase.
Defines the TCP option of kind 8 (timestamp option) as in RFC 1323
Definition: tcp-option-ts.h:33
Fast test.
Definition: test.h:1121
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
uint8_t PeekU8(void)
Definition: buffer.h:1005
bool AddAtStart(uint32_t start)
Definition: buffer.cc:309
This test suite implements a Unit Test.
Definition: test.h:1299
uint32_t GetTimestamp(void) const
Get the timestamp stored in the Option.
ns3::TcpOptionTestSuite g_TcpOptionTestSuite
uint32_t GetEcho(void) const
Get the timestamp echo stored in the Option.