2008-10-12 16:45:29 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/net/socket.cpp
|
|
|
|
// Purpose: wxSocket unit tests
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin
|
2010-07-13 09:29:13 -04:00
|
|
|
// Licence: wxWindows licence
|
2008-10-12 16:45:29 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
|
|
|
IMPORTANT NOTE: the environment variable WX_TEST_SERVER must be set to the
|
|
|
|
hostname of the server to use for the tests below, if it is not set all
|
|
|
|
tests are silently skipped (rationale: this makes it possible to run the
|
|
|
|
test in the restricted environments (e.g. sandboxes) without any network
|
|
|
|
connectivity).
|
|
|
|
*/
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
// and "wx/cppunit.h"
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#if wxUSE_SOCKETS
|
|
|
|
|
|
|
|
#include "wx/socket.h"
|
2008-12-30 07:53:04 -05:00
|
|
|
#include "wx/url.h"
|
2017-10-31 16:14:05 -04:00
|
|
|
#include "wx/scopedptr.h"
|
2008-12-30 07:53:04 -05:00
|
|
|
#include "wx/sstream.h"
|
2008-10-12 18:55:56 -04:00
|
|
|
#include "wx/evtloop.h"
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2017-10-31 16:14:05 -04:00
|
|
|
typedef wxScopedPtr<wxSockAddress> wxSockAddressPtr;
|
|
|
|
typedef wxScopedPtr<wxSocketClient> wxSocketClientPtr;
|
2008-10-12 16:45:29 -04:00
|
|
|
|
|
|
|
static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
|
|
|
|
|
|
|
|
class SocketTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
2008-12-27 06:12:47 -05:00
|
|
|
SocketTestCase() { }
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2019-11-01 10:35:41 -04:00
|
|
|
// get the address to connect to, if NULL is returned it means that the
|
|
|
|
// test is disabled and shouldn't run at all
|
|
|
|
static wxSockAddress* GetServer();
|
|
|
|
|
|
|
|
// get the socket to read HTTP reply from, returns NULL if the test is
|
|
|
|
// disabled
|
|
|
|
static wxSocketClient* GetHTTPSocket(int flags = wxSOCKET_NONE);
|
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
private:
|
2008-12-26 17:58:20 -05:00
|
|
|
// we need to repeat the tests twice as the sockets behave differently when
|
|
|
|
// there is an active event loop and without it
|
|
|
|
#define ALL_SOCKET_TESTS() \
|
|
|
|
CPPUNIT_TEST( BlockingConnect ); \
|
|
|
|
CPPUNIT_TEST( NonblockingConnect ); \
|
|
|
|
CPPUNIT_TEST( ReadNormal ); \
|
|
|
|
CPPUNIT_TEST( ReadBlock ); \
|
|
|
|
CPPUNIT_TEST( ReadNowait ); \
|
2008-12-30 07:53:44 -05:00
|
|
|
CPPUNIT_TEST( ReadWaitall ); \
|
2019-11-01 10:35:41 -04:00
|
|
|
CPPUNIT_TEST( ReadAnotherThread ); \
|
2008-12-30 07:53:44 -05:00
|
|
|
CPPUNIT_TEST( UrlTest )
|
2008-12-26 17:58:20 -05:00
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
CPPUNIT_TEST_SUITE( SocketTestCase );
|
2008-12-26 17:58:20 -05:00
|
|
|
ALL_SOCKET_TESTS();
|
|
|
|
CPPUNIT_TEST( PseudoTest_SetUseEventLoop );
|
|
|
|
ALL_SOCKET_TESTS();
|
2008-10-12 16:45:29 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2008-12-26 17:58:20 -05:00
|
|
|
// helper event loop class which sets itself as active only if we pass it
|
|
|
|
// true in ctor
|
|
|
|
class SocketTestEventLoop : public wxEventLoop
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketTestEventLoop(bool useLoop)
|
|
|
|
{
|
|
|
|
m_useLoop = useLoop;
|
|
|
|
if ( useLoop )
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
m_evtLoopOld = wxEventLoopBase::GetActive();
|
2008-12-26 17:58:20 -05:00
|
|
|
SetActive(this);
|
2010-08-22 18:16:05 -04:00
|
|
|
}
|
2008-12-26 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~SocketTestEventLoop()
|
|
|
|
{
|
|
|
|
if ( m_useLoop )
|
2010-08-22 18:16:05 -04:00
|
|
|
{
|
|
|
|
wxEventLoopBase::SetActive(m_evtLoopOld);
|
|
|
|
}
|
2008-12-26 17:58:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_useLoop;
|
2010-08-22 18:16:05 -04:00
|
|
|
wxEventLoopBase *m_evtLoopOld;
|
2008-12-26 17:58:20 -05:00
|
|
|
};
|
|
|
|
|
2008-12-27 06:12:47 -05:00
|
|
|
void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
|
2008-12-26 17:58:20 -05:00
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
void BlockingConnect();
|
|
|
|
void NonblockingConnect();
|
|
|
|
void ReadNormal();
|
2008-10-12 18:55:56 -04:00
|
|
|
void ReadBlock();
|
2008-10-12 16:45:29 -04:00
|
|
|
void ReadNowait();
|
|
|
|
void ReadWaitall();
|
2019-11-01 10:35:41 -04:00
|
|
|
void ReadAnotherThread();
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2008-12-30 07:53:04 -05:00
|
|
|
void UrlTest();
|
|
|
|
|
2008-12-27 06:12:47 -05:00
|
|
|
static bool ms_useLoop;
|
2008-12-26 17:58:20 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(SocketTestCase);
|
2008-10-12 16:45:29 -04:00
|
|
|
};
|
|
|
|
|
2008-12-27 06:12:47 -05:00
|
|
|
bool SocketTestCase::ms_useLoop = false;
|
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
|
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
|
|
|
|
|
2019-11-01 10:35:41 -04:00
|
|
|
wxSockAddress* SocketTestCase::GetServer()
|
2008-10-12 16:45:29 -04:00
|
|
|
{
|
2008-11-22 19:09:34 -05:00
|
|
|
if ( gs_serverHost.empty() )
|
2017-10-31 16:14:05 -04:00
|
|
|
return NULL;
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2008-11-22 19:09:34 -05:00
|
|
|
wxIPV4address *addr = new wxIPV4address;
|
|
|
|
addr->Hostname(gs_serverHost);
|
|
|
|
addr->Service("www");
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2017-10-31 16:14:05 -04:00
|
|
|
return addr;
|
2008-10-12 16:45:29 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 10:35:41 -04:00
|
|
|
wxSocketClient* SocketTestCase::GetHTTPSocket(int flags)
|
2008-10-12 16:45:29 -04:00
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxSockAddress *addr = GetServer();
|
|
|
|
if ( !addr )
|
|
|
|
return NULL;
|
2008-10-12 16:45:29 -04:00
|
|
|
|
|
|
|
wxSocketClient *sock = new wxSocketClient(flags);
|
|
|
|
sock->SetTimeout(1);
|
|
|
|
CPPUNIT_ASSERT( sock->Connect(*addr) );
|
|
|
|
|
|
|
|
const wxString httpGetRoot =
|
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
"Host: " + gs_serverHost + "\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
2008-10-12 18:27:49 -04:00
|
|
|
sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
|
2008-10-12 16:45:29 -04:00
|
|
|
|
2017-10-31 16:14:05 -04:00
|
|
|
return sock;
|
2008-10-12 16:45:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SocketTestCase::BlockingConnect()
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxSockAddressPtr addr(GetServer());
|
2008-10-12 16:45:29 -04:00
|
|
|
if ( !addr.get() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
wxSocketClient sock;
|
|
|
|
CPPUNIT_ASSERT( sock.Connect(*addr) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketTestCase::NonblockingConnect()
|
|
|
|
{
|
2017-10-31 16:14:05 -04:00
|
|
|
wxSockAddressPtr addr(GetServer());
|
2008-10-12 16:45:29 -04:00
|
|
|
if ( !addr.get() )
|
|
|
|
return;
|
|
|
|
|
2008-12-27 06:12:47 -05:00
|
|
|
SocketTestEventLoop loop(ms_useLoop);
|
2008-10-12 18:55:56 -04:00
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
wxSocketClient sock;
|
|
|
|
sock.Connect(*addr, false);
|
|
|
|
sock.WaitOnConnect(10);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT( sock.IsConnected() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketTestCase::ReadNormal()
|
|
|
|
{
|
2008-12-27 06:12:47 -05:00
|
|
|
SocketTestEventLoop loop(ms_useLoop);
|
2008-10-12 18:55:56 -04:00
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
wxSocketClientPtr sock(GetHTTPSocket());
|
|
|
|
if ( !sock.get() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
char bufSmall[128];
|
|
|
|
sock->Read(bufSmall, WXSIZEOF(bufSmall));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
2009-01-16 12:41:25 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
|
2012-09-30 18:21:44 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
|
2008-10-12 16:45:29 -04:00
|
|
|
|
|
|
|
|
2008-10-12 16:57:19 -04:00
|
|
|
char bufBig[102400];
|
2008-10-12 16:45:29 -04:00
|
|
|
sock->Read(bufBig, WXSIZEOF(bufBig));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
|
|
|
CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
|
2012-09-30 18:21:44 -04:00
|
|
|
CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
|
2008-10-12 16:45:29 -04:00
|
|
|
}
|
|
|
|
|
2008-10-12 18:55:56 -04:00
|
|
|
void SocketTestCase::ReadBlock()
|
|
|
|
{
|
|
|
|
wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_BLOCK));
|
|
|
|
if ( !sock.get() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
char bufSmall[128];
|
|
|
|
sock->Read(bufSmall, WXSIZEOF(bufSmall));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
2009-01-16 12:41:25 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
|
2012-09-30 18:21:44 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
|
2008-10-12 18:55:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
char bufBig[102400];
|
|
|
|
sock->Read(bufBig, WXSIZEOF(bufBig));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
|
|
|
CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
|
2012-09-30 18:21:44 -04:00
|
|
|
CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
|
2008-10-12 18:55:56 -04:00
|
|
|
}
|
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
void SocketTestCase::ReadNowait()
|
|
|
|
{
|
|
|
|
wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
|
|
|
|
if ( !sock.get() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[1024];
|
|
|
|
sock->Read(buf, WXSIZEOF(buf));
|
|
|
|
if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SocketTestCase::ReadWaitall()
|
|
|
|
{
|
2008-12-27 06:12:47 -05:00
|
|
|
SocketTestEventLoop loop(ms_useLoop);
|
2008-10-12 18:55:56 -04:00
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
|
|
|
|
if ( !sock.get() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
sock->Read(buf, WXSIZEOF(buf));
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
|
2009-01-16 12:41:25 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() );
|
2012-09-30 18:21:44 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastReadCount() );
|
2008-10-12 16:45:29 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 10:35:41 -04:00
|
|
|
void SocketTestCase::ReadAnotherThread()
|
|
|
|
{
|
|
|
|
class SocketThread : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketThread()
|
|
|
|
: wxThread(wxTHREAD_JOINABLE)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void* Entry() wxOVERRIDE
|
|
|
|
{
|
|
|
|
wxSocketClientPtr sock(SocketTestCase::GetHTTPSocket(wxSOCKET_BLOCK));
|
|
|
|
if ( !sock )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char bufSmall[128];
|
|
|
|
sock->Read(bufSmall, WXSIZEOF(bufSmall));
|
|
|
|
|
|
|
|
REQUIRE( sock->LastError() == wxSOCKET_NOERROR );
|
|
|
|
CHECK( sock->LastCount() == WXSIZEOF(bufSmall) );
|
|
|
|
CHECK( sock->LastReadCount() == WXSIZEOF(bufSmall) );
|
|
|
|
|
|
|
|
REQUIRE_NOTHROW( sock.reset() );
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SocketThread thr;
|
|
|
|
|
|
|
|
SocketTestEventLoop loop(ms_useLoop);
|
|
|
|
|
|
|
|
thr.Run();
|
|
|
|
|
|
|
|
CHECK( thr.Wait() == NULL );
|
|
|
|
}
|
|
|
|
|
2008-12-30 07:53:04 -05:00
|
|
|
void SocketTestCase::UrlTest()
|
|
|
|
{
|
|
|
|
if ( gs_serverHost.empty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
SocketTestEventLoop loop(ms_useLoop);
|
|
|
|
|
|
|
|
wxURL url("http://" + gs_serverHost);
|
2008-12-30 08:18:22 -05:00
|
|
|
|
2017-10-31 16:14:05 -04:00
|
|
|
const wxScopedPtr<wxInputStream> in(url.GetInputStream());
|
2008-12-30 08:18:22 -05:00
|
|
|
CPPUNIT_ASSERT( in.get() );
|
2008-12-30 07:53:04 -05:00
|
|
|
|
|
|
|
wxStringOutputStream out;
|
|
|
|
CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() );
|
|
|
|
}
|
|
|
|
|
2023-06-03 15:29:22 -04:00
|
|
|
TEST_CASE("wxDatagramSocket::ShortRead", "[socket][dgram]")
|
|
|
|
{
|
|
|
|
// Check that reading fewer bytes than are present in a
|
|
|
|
// datagram does not leave the socket in an error state
|
|
|
|
|
|
|
|
wxIPV4address addr;
|
|
|
|
addr.LocalHost();
|
|
|
|
addr.Service(19898);// Arbitrary port number
|
|
|
|
wxDatagramSocket sock(addr);
|
|
|
|
|
|
|
|
// Send ourselves a datagram
|
|
|
|
unsigned int sendbuf[4] = {1, 2, 3, 4};
|
|
|
|
sock.SendTo(addr, sendbuf, sizeof(sendbuf));
|
|
|
|
|
|
|
|
// Read less than we know we sent
|
|
|
|
unsigned int recvbuf[1] = {0};
|
|
|
|
sock.Read(recvbuf, sizeof(recvbuf));
|
|
|
|
CHECK(!sock.Error());
|
|
|
|
CHECK(sock.LastReadCount() == sizeof(recvbuf));
|
|
|
|
CHECK(recvbuf[0] == sendbuf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("wxDatagramSocket::ShortPeek", "[socket][dgram]")
|
|
|
|
{
|
|
|
|
// Check that peeking fewer bytes than are present in a datagram
|
|
|
|
// does not lose the rest of the data in that datagram (#23594)
|
|
|
|
|
|
|
|
wxIPV4address addr;
|
|
|
|
addr.LocalHost();
|
|
|
|
addr.Service(27384);// Arbitrary port number
|
|
|
|
wxDatagramSocket sock(addr);
|
|
|
|
|
|
|
|
// Send ourselves 2 datagrams
|
|
|
|
unsigned int sendbuf1[2] = {1, 2};
|
|
|
|
sock.SendTo(addr, sendbuf1, sizeof(sendbuf1));
|
|
|
|
unsigned int sendbuf2[2] = {3, 4};
|
|
|
|
sock.SendTo(addr, sendbuf2, sizeof(sendbuf2));
|
|
|
|
|
|
|
|
long timeout_s = 1;
|
|
|
|
if ( !sock.WaitForRead(timeout_s) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Peek the first word
|
|
|
|
unsigned int peekbuf[1] = {0};
|
|
|
|
sock.Peek(peekbuf, sizeof(peekbuf));
|
|
|
|
CHECK(sock.LastCount() == sizeof(peekbuf));
|
|
|
|
CHECK(peekbuf[0] == sendbuf1[0]);
|
|
|
|
|
|
|
|
// Read the whole of the first datagram
|
|
|
|
unsigned int recvbuf[2] = {0};
|
|
|
|
sock.Read(recvbuf, sizeof(recvbuf));
|
|
|
|
CHECK(sock.LastReadCount() == sizeof(recvbuf));
|
|
|
|
CHECK(recvbuf[0] == sendbuf1[0]);
|
|
|
|
CHECK(recvbuf[1] == sendbuf1[1]);
|
|
|
|
}
|
|
|
|
|
2008-10-12 16:45:29 -04:00
|
|
|
#endif // wxUSE_SOCKETS
|