24 #include "ns3/socket-factory.h"
25 #include "ns3/tcp-socket-factory.h"
26 #include "ns3/simulator.h"
27 #include "ns3/simple-channel.h"
28 #include "ns3/simple-net-device.h"
29 #include "ns3/drop-tail-queue.h"
30 #include "ns3/config.h"
31 #include "ns3/ipv4-static-routing.h"
32 #include "ns3/ipv4-list-routing.h"
33 #include "ns3/ipv6-static-routing.h"
34 #include "ns3/ipv6-list-routing.h"
36 #include "ns3/inet-socket-address.h"
37 #include "ns3/inet6-socket-address.h"
38 #include "ns3/uinteger.h"
41 #include "ns3/ipv4-end-point.h"
42 #include "ns3/arp-l3-protocol.h"
43 #include "ns3/ipv4-l3-protocol.h"
44 #include "ns3/ipv6-l3-protocol.h"
45 #include "ns3/icmpv4-l4-protocol.h"
46 #include "ns3/icmpv6-l4-protocol.h"
47 #include "ns3/udp-l4-protocol.h"
48 #include "ns3/tcp-l4-protocol.h"
60 uint32_t sourceWriteSize,
61 uint32_t sourceReadSize,
62 uint32_t serverWriteSize,
63 uint32_t serverReadSize,
66 virtual void DoRun (
void);
67 virtual void DoTeardown (
void);
68 void SetupDefaultSim (
void);
69 void SetupDefaultSim6 (
void);
77 void ServerHandleSend (
Ptr<Socket> sock, uint32_t available);
78 void SourceHandleSend (
Ptr<Socket> sock, uint32_t available);
81 uint32_t m_totalBytes;
82 uint32_t m_sourceWriteSize;
83 uint32_t m_sourceReadSize;
84 uint32_t m_serverWriteSize;
85 uint32_t m_serverReadSize;
86 uint32_t m_currentSourceTxBytes;
87 uint32_t m_currentSourceRxBytes;
88 uint32_t m_currentServerRxBytes;
89 uint32_t m_currentServerTxBytes;
90 uint8_t *m_sourceTxPayload;
91 uint8_t *m_sourceRxPayload;
92 uint8_t* m_serverRxPayload;
97 static std::string Name (std::string str, uint32_t totalStreamSize,
98 uint32_t sourceWriteSize,
99 uint32_t serverReadSize,
100 uint32_t serverWriteSize,
101 uint32_t sourceReadSize,
104 std::ostringstream oss;
105 oss << str <<
" total=" << totalStreamSize <<
" sourceWrite=" << sourceWriteSize
106 <<
" sourceRead=" << sourceReadSize <<
" serverRead=" << serverReadSize
107 <<
" serverWrite=" << serverWriteSize <<
" useIpv6=" << useIpv6;
111 #ifdef NS3_LOG_ENABLE
114 std::ostringstream oss;
120 TcpTestCase::TcpTestCase (uint32_t totalStreamSize,
121 uint32_t sourceWriteSize,
122 uint32_t sourceReadSize,
123 uint32_t serverWriteSize,
124 uint32_t serverReadSize,
126 :
TestCase (Name (
"Send string data from client to server and back",
133 m_totalBytes (totalStreamSize),
134 m_sourceWriteSize (sourceWriteSize),
135 m_sourceReadSize (sourceReadSize),
136 m_serverWriteSize (serverWriteSize),
137 m_serverReadSize (serverReadSize),
145 m_currentSourceTxBytes = 0;
146 m_currentSourceRxBytes = 0;
147 m_currentServerRxBytes = 0;
148 m_currentServerTxBytes = 0;
149 m_sourceTxPayload =
new uint8_t [m_totalBytes];
150 m_sourceRxPayload =
new uint8_t [m_totalBytes];
151 m_serverRxPayload =
new uint8_t [m_totalBytes];
152 for(uint32_t i = 0; i < m_totalBytes; ++i)
154 uint8_t m = (uint8_t)(97 + (i % 26));
155 m_sourceTxPayload[i] = m;
157 memset (m_sourceRxPayload, 0, m_totalBytes);
158 memset (m_serverRxPayload, 0, m_totalBytes);
160 if (m_useIpv6 ==
true)
171 NS_TEST_EXPECT_MSG_EQ (m_currentSourceTxBytes, m_totalBytes,
"Source sent all bytes");
172 NS_TEST_EXPECT_MSG_EQ (m_currentServerRxBytes, m_totalBytes,
"Server received all bytes");
173 NS_TEST_EXPECT_MSG_EQ (m_currentSourceRxBytes, m_totalBytes,
"Source received all bytes");
174 NS_TEST_EXPECT_MSG_EQ (memcmp (m_sourceTxPayload, m_serverRxPayload, m_totalBytes), 0,
175 "Server received expected data buffers");
176 NS_TEST_EXPECT_MSG_EQ (memcmp (m_sourceTxPayload, m_sourceRxPayload, m_totalBytes), 0,
177 "Source received back expected data buffers");
182 delete [] m_sourceTxPayload;
183 delete [] m_sourceRxPayload;
184 delete [] m_serverRxPayload;
185 Simulator::Destroy ();
200 uint32_t toRead = std::min (m_serverReadSize, sock->
GetRxAvailable ());
202 if (p == 0 && sock->
GetErrno () != Socket::ERROR_NOTERROR)
204 NS_FATAL_ERROR (
"Server could not read stream at byte " << m_currentServerRxBytes);
206 NS_TEST_EXPECT_MSG_EQ ((m_currentServerRxBytes + p->GetSize () <= m_totalBytes),
true,
207 "Server received too many bytes");
208 NS_LOG_DEBUG (
"Server recv data=\"" << GetString (p) <<
"\"");
209 p->CopyData (&m_serverRxPayload[m_currentServerRxBytes], p->GetSize ());
210 m_currentServerRxBytes += p->GetSize ();
216 TcpTestCase::ServerHandleSend (
Ptr<Socket> sock, uint32_t available)
218 while (sock->
GetTxAvailable () > 0 && m_currentServerTxBytes < m_currentServerRxBytes)
220 uint32_t left = m_currentServerRxBytes - m_currentServerTxBytes;
222 toSend = std::min (toSend, m_serverWriteSize);
223 Ptr<Packet> p = Create<Packet> (&m_serverRxPayload[m_currentServerTxBytes], toSend);
224 NS_LOG_DEBUG (
"Server send data=\"" << GetString (p) <<
"\"");
225 int sent = sock->
Send (p);
226 NS_TEST_EXPECT_MSG_EQ ((sent != -1),
true,
"Server error during send ?");
227 m_currentServerTxBytes += sent;
229 if (m_currentServerTxBytes == m_totalBytes)
236 TcpTestCase::SourceHandleSend (
Ptr<Socket> sock, uint32_t available)
238 while (sock->
GetTxAvailable () > 0 && m_currentSourceTxBytes < m_totalBytes)
240 uint32_t left = m_totalBytes - m_currentSourceTxBytes;
242 toSend = std::min (toSend, m_sourceWriteSize);
243 Ptr<Packet> p = Create<Packet> (&m_sourceTxPayload[m_currentSourceTxBytes], toSend);
244 NS_LOG_DEBUG (
"Source send data=\"" << GetString (p) <<
"\"");
245 int sent = sock->
Send (p);
246 NS_TEST_EXPECT_MSG_EQ ((sent != -1),
true,
"Error during send ?");
247 m_currentSourceTxBytes += sent;
254 while (sock->
GetRxAvailable () > 0 && m_currentSourceRxBytes < m_totalBytes)
256 uint32_t toRead = std::min (m_sourceReadSize, sock->
GetRxAvailable ());
258 if (p == 0 && sock->
GetErrno () != Socket::ERROR_NOTERROR)
260 NS_FATAL_ERROR (
"Source could not read stream at byte " << m_currentSourceRxBytes);
262 NS_TEST_EXPECT_MSG_EQ ((m_currentSourceRxBytes + p->GetSize () <= m_totalBytes),
true,
263 "Source received too many bytes");
264 NS_LOG_DEBUG (
"Source recv data=\"" << GetString (p) <<
"\"");
265 p->CopyData (&m_sourceRxPayload[m_currentSourceRxBytes], p->GetSize ());
266 m_currentSourceRxBytes += p->GetSize ();
268 if (m_currentSourceRxBytes == m_totalBytes)
275 TcpTestCase::CreateInternetNode ()
287 ipv4Routing->AddRoutingProtocol (ipv4staticRouting, 0);
302 TcpTestCase::AddSimpleNetDevice (
Ptr<Node> node,
const char* ipaddr,
const char* netmask)
305 dev->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
316 TcpTestCase::SetupDefaultSim (
void)
318 const char* netmask =
"255.255.255.0";
319 const char* ipaddr0 =
"192.168.1.1";
320 const char* ipaddr1 =
"192.168.1.2";
327 dev0->SetChannel (channel);
328 dev1->SetChannel (channel);
336 uint16_t port = 50000;
340 server->Bind (serverlocaladdr);
343 MakeCallback (&TcpTestCase::ServerHandleConnectionCreated,
this));
348 source->
Connect (serverremoteaddr);
352 TcpTestCase::SetupDefaultSim6 (
void)
357 Ptr<Node> node0 = CreateInternetNode6 ();
358 Ptr<Node> node1 = CreateInternetNode6 ();
363 dev0->SetChannel (channel);
364 dev1->SetChannel (channel);
372 uint16_t port = 50000;
376 server->Bind (serverlocaladdr);
379 MakeCallback (&TcpTestCase::ServerHandleConnectionCreated,
this));
384 source->
Connect (serverremoteaddr);
388 TcpTestCase::CreateInternetNode6 ()
395 ipv6->SetRoutingProtocol (ipv6Routing);
397 ipv6Routing->AddRoutingProtocol (ipv6staticRouting, 0);
403 ipv6->RegisterExtensions ();
404 ipv6->RegisterOptions ();
418 dev->
SetAddress (Mac48Address::ConvertFrom (Mac48Address::Allocate ()));
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Access to the IPv6 forwarding table, interfaces, and configuration.
API to create TCP socket instances.
a class to represent an Ipv4 address mask
virtual void DoTeardown(void)
Implementation to do any local setup required for this test case.
virtual void DoRun(void)
Implementation to actually run this test case.
#define NS_LOG_COMPONENT_DEFINE(name)
IPv6 address associated with an interface.
uint32_t GetSize(void) const
virtual Ptr< Socket > CreateSocket(void)=0
TestSuite(std::string name, Type type=UNIT)
Constuct a new test suite.
virtual enum Socket::SocketErrno GetErrno(void) const =0
#define NS_FATAL_ERROR(msg)
fatal error handling
a polymophic address class
virtual void SetUp(uint32_t interface)=0
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
void AggregateObject(Ptr< Object > other)
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Access to the Ipv4 forwarding table, interfaces, and configuration.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Callback< R > MakeNullCallback(void)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual test case to this test suite.
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
Add a NetDevice interface.
Describes an IPv6 address.
Ipv4 addresses are stored in host order in this class.
uint32_t AddDevice(Ptr< NetDevice > device)
a class to store IPv4 address information on an interface
virtual void SetAddress(Address address)
#define NS_LOG_DEBUG(msg)
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual bool AddAddress(uint32_t interface, Ipv6InterfaceAddress address)=0
Add an address on the specified IPv6 interface.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
virtual uint32_t GetTxAvailable(void) const =0
Returns the number of bytes which can be sent in a single call to Send.
Ptr< T > GetObject(void) const
virtual void SetUp(uint32_t interface)=0
Set the interface into the "up" state.
virtual uint32_t GetRxAvailable(void) const =0
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.