|
|
|
|
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 |
|
| 24 |
namespace ns3 { |
| 25 |
|
| 26 |
NS_LOG_COMPONENT_DEFINE ("TcpDatSentCbTest"); |
| 27 |
|
| 28 |
/** |
| 29 |
* \brief Socket that the 50% of the times saves the entire packet in the buffer, |
| 30 |
* while in the other 50% saves only half the packet. |
| 31 |
*/ |
| 32 |
class TcpSocketHalfAck : public TcpSocketMsgBase |
| 33 |
{ |
| 34 |
public: |
| 35 |
static TypeId GetTypeId (void); |
| 36 |
|
| 37 |
TcpSocketHalfAck () : TcpSocketMsgBase () |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
TcpSocketHalfAck (const TcpSocketHalfAck &other) : TcpSocketMsgBase (other) |
| 42 |
{ |
| 43 |
} |
| 44 |
protected: |
| 45 |
virtual Ptr<TcpSocketBase> Fork (); |
| 46 |
virtual void ReceivedData (Ptr<Packet> packet, const TcpHeader& tcpHeader); |
| 47 |
}; |
| 48 |
|
| 49 |
NS_OBJECT_ENSURE_REGISTERED (TcpSocketHalfAck); |
| 50 |
|
| 51 |
TypeId |
| 52 |
TcpSocketHalfAck::GetTypeId (void) |
| 53 |
{ |
| 54 |
static TypeId tid = TypeId ("ns3::TcpSocketHalfAck") |
| 55 |
.SetParent<TcpSocketMsgBase> () |
| 56 |
.SetGroupName ("Internet") |
| 57 |
.AddConstructor<TcpSocketHalfAck> () |
| 58 |
; |
| 59 |
return tid; |
| 60 |
} |
| 61 |
|
| 62 |
Ptr<TcpSocketBase> |
| 63 |
TcpSocketHalfAck::Fork (void) |
| 64 |
{ |
| 65 |
return CopyObject<TcpSocketHalfAck> (this); |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
TcpSocketHalfAck::ReceivedData(Ptr<Packet> packet, const TcpHeader &tcpHeader) |
| 70 |
{ |
| 71 |
NS_LOG_FUNCTION (this << packet << tcpHeader); |
| 72 |
static uint32_t times = 1; |
| 73 |
|
| 74 |
Ptr<Packet> halved = packet->Copy (); |
| 75 |
|
| 76 |
if (times % 2 == 0) |
| 77 |
halved->RemoveAtEnd (packet->GetSize() / 2); |
| 78 |
|
| 79 |
times++; |
| 80 |
|
| 81 |
TcpSocketMsgBase::ReceivedData (halved, tcpHeader); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* \brief Data Sent callback test |
| 87 |
* |
| 88 |
* The rationale of this test is to check if the dataSent callback advertises |
| 89 |
* to the application all the transmitted bytes. We know in advance how many |
| 90 |
* bytes are being transmitted, and we check if the amount of data notified |
| 91 |
* equals this value. |
| 92 |
* |
| 93 |
*/ |
| 94 |
class TcpDataSentCbTestCase : public TcpGeneralTest |
| 95 |
{ |
| 96 |
public: |
| 97 |
TcpDataSentCbTestCase (const std::string &desc, uint32_t size, uint32_t packets) : |
| 98 |
TcpGeneralTest (desc, size, packets), |
| 99 |
m_notifiedData (0) |
| 100 |
{ } |
| 101 |
|
| 102 |
protected: |
| 103 |
virtual Ptr<TcpSocketMsgBase> CreateReceiverSocket (Ptr<Node> node); |
| 104 |
|
| 105 |
virtual void DataSent (uint32_t size, SocketWho who); |
| 106 |
|
| 107 |
virtual void FinalChecks (); |
| 108 |
|
| 109 |
private: |
| 110 |
uint32_t m_notifiedData; |
| 111 |
}; |
| 112 |
|
| 113 |
void |
| 114 |
TcpDataSentCbTestCase::DataSent (uint32_t size, SocketWho who) |
| 115 |
{ |
| 116 |
NS_LOG_FUNCTION (this << who << size); |
| 117 |
|
| 118 |
m_notifiedData += size; |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
TcpDataSentCbTestCase::FinalChecks() |
| 123 |
{ |
| 124 |
NS_TEST_ASSERT_MSG_EQ (m_notifiedData, GetPktSize () * GetPktCount () , |
| 125 |
"Notified more data than application sent"); |
| 126 |
} |
| 127 |
|
| 128 |
Ptr<TcpSocketMsgBase> |
| 129 |
TcpDataSentCbTestCase::CreateReceiverSocket (Ptr<Node> node) |
| 130 |
{ |
| 131 |
NS_LOG_FUNCTION (this); |
| 132 |
|
| 133 |
return CreateSocket (node, TcpSocketHalfAck::GetTypeId (), m_congControlTypeId); |
| 134 |
} |
| 135 |
|
| 136 |
static class TcpDataSentCbTestSuite : public TestSuite |
| 137 |
{ |
| 138 |
public: |
| 139 |
TcpDataSentCbTestSuite () |
| 140 |
: TestSuite ("tcp-datasentcb", UNIT) |
| 141 |
{ |
| 142 |
AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 500, 10), TestCase::QUICK); |
| 143 |
AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 100, 100), TestCase::QUICK); |
| 144 |
AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 1000, 50), TestCase::QUICK); |
| 145 |
AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 855, 18), TestCase::QUICK); |
| 146 |
AddTestCase (new TcpDataSentCbTestCase ("Check the data sent callback", 1243, 59), TestCase::QUICK); |
| 147 |
} |
| 148 |
|
| 149 |
} g_tcpDataSentCbTestSuite; |
| 150 |
|
| 151 |
} // namespace ns3 |