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 // we need to repeat the tests twice as the sockets behave differently when
44 // there is an active event loop and without it
45 #define ALL_SOCKET_TESTS() \
46 CPPUNIT_TEST( BlockingConnect ); \
47 CPPUNIT_TEST( NonblockingConnect ); \
48 CPPUNIT_TEST( ReadNormal ); \
49 CPPUNIT_TEST( ReadBlock ); \
50 CPPUNIT_TEST( ReadNowait ); \
51 CPPUNIT_TEST( ReadWaitall )
53 CPPUNIT_TEST_SUITE( SocketTestCase
);
55 CPPUNIT_TEST( PseudoTest_SetUseEventLoop
);
57 CPPUNIT_TEST_SUITE_END();
59 // helper event loop class which sets itself as active only if we pass it
61 class SocketTestEventLoop
: public wxEventLoop
64 SocketTestEventLoop(bool useLoop
)
71 virtual ~SocketTestEventLoop()
81 // get the address to connect to, if NULL is returned it means that the
82 // test is disabled and shouldn't run at all
83 wxSockAddressPtr
GetServer() const;
85 // get the socket to read HTTP reply from, returns NULL if the test is
87 wxSocketClientPtr
GetHTTPSocket(int flags
= wxSOCKET_NONE
) const;
89 void PseudoTest_SetUseEventLoop() { ms_useLoop
= true; }
91 void BlockingConnect();
92 void NonblockingConnect();
98 static bool ms_useLoop
;
100 DECLARE_NO_COPY_CLASS(SocketTestCase
)
103 bool SocketTestCase::ms_useLoop
= false;
105 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase
);
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase
, "SocketTestCase" );
108 wxSockAddressPtr
SocketTestCase::GetServer() const
110 if ( gs_serverHost
.empty() )
111 return wxSockAddressPtr();
113 wxIPV4address
*addr
= new wxIPV4address
;
114 addr
->Hostname(gs_serverHost
);
115 addr
->Service("www");
117 return wxSockAddressPtr(addr
);
120 wxSocketClientPtr
SocketTestCase::GetHTTPSocket(int flags
) const
122 wxSockAddressPtr addr
= GetServer();
124 return wxSocketClientPtr();
126 wxSocketClient
*sock
= new wxSocketClient(flags
);
128 CPPUNIT_ASSERT( sock
->Connect(*addr
) );
130 const wxString httpGetRoot
=
132 "Host: " + gs_serverHost
+ "\r\n"
135 sock
->Write(httpGetRoot
.ToAscii(), httpGetRoot
.length());
137 return wxSocketClientPtr(sock
);
140 void SocketTestCase::BlockingConnect()
142 wxSockAddressPtr addr
= GetServer();
147 CPPUNIT_ASSERT( sock
.Connect(*addr
) );
150 void SocketTestCase::NonblockingConnect()
152 wxSockAddressPtr addr
= GetServer();
156 SocketTestEventLoop
loop(ms_useLoop
);
159 sock
.Connect(*addr
, false);
160 sock
.WaitOnConnect(10);
162 CPPUNIT_ASSERT( sock
.IsConnected() );
165 void SocketTestCase::ReadNormal()
167 SocketTestEventLoop
loop(ms_useLoop
);
169 wxSocketClientPtr
sock(GetHTTPSocket());
174 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
176 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
177 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
181 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
183 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
184 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
187 void SocketTestCase::ReadBlock()
189 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_BLOCK
));
194 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
196 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
197 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
201 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
203 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
204 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
207 void SocketTestCase::ReadNowait()
209 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_NOWAIT
));
214 sock
->Read(buf
, WXSIZEOF(buf
));
215 if ( sock
->LastError() != wxSOCKET_WOULDBLOCK
)
217 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
221 void SocketTestCase::ReadWaitall()
223 SocketTestEventLoop
loop(ms_useLoop
);
225 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_WAITALL
));
230 sock
->Read(buf
, WXSIZEOF(buf
));
232 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
233 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf
), sock
->LastCount() );
236 #endif // wxUSE_SOCKETS