A Discrete-Event Network Simulator
API
tcp-datasentcb-test.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 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 "tcp-general-test.h"
21 #include "ns3/node.h"
22 #include "ns3/log.h"
23 #include "ns3/tcp-header.h"
24 
25 using namespace ns3;
26 
27 NS_LOG_COMPONENT_DEFINE ("TcpDatSentCbTest");
28 
37 {
38 public:
43  static TypeId GetTypeId (void);
44 
46  {
47  }
48 
49 protected:
50  virtual Ptr<TcpSocketBase> Fork ();
51  virtual void ReceivedData (Ptr<Packet> packet, const TcpHeader& tcpHeader);
52 };
53 
55 
56 TypeId
58 {
59  static TypeId tid = TypeId ("ns3::TcpSocketHalfAck")
61  .SetGroupName ("Internet")
62  .AddConstructor<TcpSocketHalfAck> ()
63  ;
64  return tid;
65 }
66 
69 {
70  return CopyObject<TcpSocketHalfAck> (this);
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION (this << packet << tcpHeader);
77  static uint32_t times = 1;
78 
79  Ptr<Packet> halved = packet->Copy ();
80 
81  if (times % 2 == 0)
82  halved->RemoveAtEnd (packet->GetSize () / 2);
83 
84  times++;
85 
86  TcpSocketMsgBase::ReceivedData (halved, tcpHeader);
87 }
88 
89 
103 {
104 public:
105 
112  TcpDataSentCbTestCase (const std::string &desc, uint32_t size, uint32_t packets) :
113  TcpGeneralTest (desc),
114  m_pktSize (size),
115  m_pktCount (packets),
116  m_notifiedData (0)
117  { }
118 
119 protected:
120  virtual Ptr<TcpSocketMsgBase> CreateReceiverSocket (Ptr<Node> node);
121 
122  virtual void DataSent (uint32_t size, SocketWho who);
123  virtual void ConfigureEnvironment ();
124  virtual void FinalChecks ();
125 
126 private:
127  uint32_t m_pktSize;
128  uint32_t m_pktCount;
129  uint32_t m_notifiedData;
130 };
131 
132 void
134 {
136  SetAppPktCount (m_pktCount);
137  SetAppPktSize (m_pktSize);
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this << who << size);
144 
145  m_notifiedData += size;
146 }
147 
148 void
150 {
151  NS_TEST_ASSERT_MSG_EQ (m_notifiedData, GetPktSize () * GetPktCount (),
152  "Notified more data than application sent");
153 }
154 
157 {
158  NS_LOG_FUNCTION (this);
159 
160  return CreateSocket (node, TcpSocketHalfAck::GetTypeId (), m_congControlTypeId);
161 }
162 
170 {
171 public:
173  : TestSuite ("tcp-datasentcb", UNIT)
174  {
175  AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 500, 10), TestCase::QUICK);
176  AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 100, 100), TestCase::QUICK);
177  AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 1000, 50), TestCase::QUICK);
178  AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 855, 18), TestCase::QUICK);
179  AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 1243, 59), TestCase::QUICK);
180  }
181 
182 };
183 
Data Sent callback test.
virtual void DataSent(uint32_t size, SocketWho who)
uint32_t m_notifiedData
Amount of data notified.
TcpDataSentCbTestCase(const std::string &desc, uint32_t size, uint32_t packets)
Constructor.
virtual void FinalChecks()
Performs the (eventual) final checks through test asserts.
uint32_t m_pktCount
Number of packets sent.
virtual void ConfigureEnvironment()
Change the configuration of the environment.
virtual Ptr< TcpSocketMsgBase > CreateReceiverSocket(Ptr< Node > node)
Create and install the socket to install on the receiver.
uint32_t m_pktSize
Packet size.
TestSuite: Data Sent callback.
Socket that the 50% of the times saves the entire packet in the buffer, while in the other 50% saves ...
static TypeId GetTypeId(void)
Get the type ID.
virtual Ptr< TcpSocketBase > Fork()
Call CopyObject<> to clone me.
virtual void ReceivedData(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Recv of a data, put into buffer, call L7 to get it if necessary.
void RemoveAtEnd(uint32_t size)
Remove size bytes from the end of the current packet.
Definition: packet.cc:355
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
General infrastructure for TCP testing.
SocketWho
Used as parameter of methods, specifies on what node the caller is interested (e.g.
virtual void ConfigureEnvironment(void)
Change the configuration of the environment.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:45
virtual void ReceivedData(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Recv of a data, put into buffer, call L7 to get it if necessary.
Class for inserting callbacks special points of the flow of TCP sockets.
@ QUICK
Fast test.
Definition: test.h:999
A suite of tests to run.
Definition: test.h:1188
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:141
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TcpDataSentCbTestSuite g_tcpDataSentCbTestSuite
Static variable for test initialization.