1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/socket.cpp
3 // Purpose: wxSocket unit tests
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
11 IMPORTANT NOTE: the environment variable WX_TEST_SERVER must be set to the
12 hostname of the server to use for the tests below, if it is not set all
13 tests are silently skipped (rationale: this makes it possible to run the
14 test in the restricted environments (e.g. sandboxes) without any network
18 // For compilers that support precompilation, includes "wx/wx.h".
28 #include "wx/socket.h"
29 #include "wx/evtloop.h"
32 typedef std::auto_ptr
<wxSockAddress
> wxSockAddressPtr
;
33 typedef std::auto_ptr
<wxSocketClient
> wxSocketClientPtr
;
35 static wxString
gs_serverHost(wxGetenv("WX_TEST_SERVER"));
37 class SocketTestCase
: public CppUnit::TestCase
43 CPPUNIT_TEST_SUITE( SocketTestCase
);
44 CPPUNIT_TEST( BlockingConnect
);
45 CPPUNIT_TEST( NonblockingConnect
);
46 CPPUNIT_TEST( ReadNormal
);
47 CPPUNIT_TEST( ReadBlock
);
48 CPPUNIT_TEST( ReadNowait
);
49 CPPUNIT_TEST( ReadWaitall
);
50 CPPUNIT_TEST_SUITE_END();
52 // get the address to connect to, if NULL is returned it means that the
53 // test is disabled and shouldn't run at all
54 wxSockAddressPtr
GetServer() const;
56 // get the socket to read HTTP reply from, returns NULL if the test is
58 wxSocketClientPtr
GetHTTPSocket(int flags
= wxSOCKET_NONE
) const;
60 void BlockingConnect();
61 void NonblockingConnect();
67 DECLARE_NO_COPY_CLASS(SocketTestCase
)
70 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase
);
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase
, "SocketTestCase" );
73 wxSockAddressPtr
SocketTestCase::GetServer() const
76 if ( !gs_serverHost
.empty() )
78 wxIPV4address
*addr
= new wxIPV4address
;
79 addr
->Hostname(gs_serverHost
);
82 ptr
= wxSockAddressPtr(addr
);
88 wxSocketClientPtr
SocketTestCase::GetHTTPSocket(int flags
) const
90 wxSocketClientPtr ptr
;
92 wxSockAddressPtr addr
= GetServer();
96 wxSocketClient
*sock
= new wxSocketClient(flags
);
98 CPPUNIT_ASSERT( sock
->Connect(*addr
) );
100 const wxString httpGetRoot
=
102 "Host: " + gs_serverHost
+ "\r\n"
105 sock
->Write(httpGetRoot
.ToAscii(), httpGetRoot
.length());
107 ptr
= wxSocketClientPtr(sock
);
111 void SocketTestCase::BlockingConnect()
113 wxSockAddressPtr addr
= GetServer();
118 CPPUNIT_ASSERT( sock
.Connect(*addr
) );
121 void SocketTestCase::NonblockingConnect()
123 wxSockAddressPtr addr
= GetServer();
127 wxEventLoopGuarantor loop
;
130 sock
.Connect(*addr
, false);
131 sock
.WaitOnConnect(10);
133 CPPUNIT_ASSERT( sock
.IsConnected() );
136 void SocketTestCase::ReadNormal()
138 wxEventLoopGuarantor loop
;
140 wxSocketClientPtr
sock(GetHTTPSocket());
145 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
147 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
148 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
152 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
154 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
155 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
158 void SocketTestCase::ReadBlock()
160 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_BLOCK
));
165 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
167 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
168 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
172 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
174 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
175 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
178 void SocketTestCase::ReadNowait()
180 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_NOWAIT
));
185 sock
->Read(buf
, WXSIZEOF(buf
));
186 if ( sock
->LastError() != wxSOCKET_WOULDBLOCK
)
188 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
192 void SocketTestCase::ReadWaitall()
194 wxEventLoopGuarantor loop
;
196 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_WAITALL
));
201 sock
->Read(buf
, WXSIZEOF(buf
));
203 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
204 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf
), sock
->LastCount() );
207 #endif // wxUSE_SOCKETS