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/private/tcp-option-winscale.h"
24 #include "ns3/private/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);
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 
112  : TestCase (name)
113 {
114 }
115 
116 void
118 {
119  Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
120 
121  for (uint32_t i = 0; i < 1000; ++i)
122  {
123  m_timestamp = x->GetInteger ();
124  m_echo = x->GetInteger ();
125  TestSerialize ();
126  TestDeserialize ();
127  }
128 }
129 
130 void
132 {
133  TcpOptionTS opt;
134 
136  opt.SetEcho (m_echo);
137 
138  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "TS isn't saved correctly");
139  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "echo isn't saved correctly");
140 
142 
143  opt.Serialize (m_buffer.Begin ());
144 }
145 
146 void
148 {
149  TcpOptionTS opt;
150 
152  uint8_t kind = start.PeekU8 ();
153 
154  NS_TEST_EXPECT_MSG_EQ (kind, TcpOption::TS, "Different kind found");
155 
156  opt.Deserialize (start);
157 
158  NS_TEST_EXPECT_MSG_EQ (m_timestamp, opt.GetTimestamp (), "Different TS found");
159  NS_TEST_EXPECT_MSG_EQ (m_echo, opt.GetEcho (), "Different echo found");
160 }
161 
162 void
164 {
165 }
166 
167 static class TcpOptionTestSuite : public TestSuite
168 {
169 public:
171  : TestSuite ("tcp-option", UNIT)
172  {
173  for (uint8_t i = 0; i < 15; ++i)
174  {
175  AddTestCase (new TcpOptionWSTestCase ("Testing window "
176  "scale value", i), TestCase::QUICK);
177  }
178  AddTestCase (new TcpOptionTSTestCase ("Testing serialization of random values for timestamp"), TestCase::QUICK);
179  }
180 
182 
183 } // namespace ns3
virtual uint32_t GetSerializedSize(void) const
Returns number of bytes required for Option serialization.
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
virtual void DoRun(void)
Implementation to actually run this TestCase.
String attribute value declarations.
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
automatically resized byte buffer
Definition: buffer.h:92
def start()
Definition: core.py:1482
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:278
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
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.
uint8_t GetScale(void) const
Get the scale value (uint8_t)
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
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:1068
virtual void Serialize(Buffer::Iterator start) const
Serialize the Option to a buffer iterator.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetScale(uint8_t scale)
Set the scale option.
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:35
virtual void DoTeardown(void)
Implementation to do any local setup required for this TestCase.
uint8_t PeekU8(void)
Definition: buffer.h:997
uint32_t GetTimestamp(void) const
Get the timestamp stored in the Option.
ns3::TcpOptionTestSuite g_TcpOptionTestSuite
TcpOptionTSTestCase(std::string name)
uint32_t GetEcho(void) const
Get the timestamp echo stored in the Option.