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
40 SocketTestCase() { m_useLoop
= false; }
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() { m_useLoop
= true; }
91 void BlockingConnect();
92 void NonblockingConnect();
100 DECLARE_NO_COPY_CLASS(SocketTestCase
)
103 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase
);
104 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase
, "SocketTestCase" );
106 wxSockAddressPtr
SocketTestCase::GetServer() const
108 if ( gs_serverHost
.empty() )
109 return wxSockAddressPtr();
111 wxIPV4address
*addr
= new wxIPV4address
;
112 addr
->Hostname(gs_serverHost
);
113 addr
->Service("www");
115 return wxSockAddressPtr(addr
);
118 wxSocketClientPtr
SocketTestCase::GetHTTPSocket(int flags
) const
120 wxSockAddressPtr addr
= GetServer();
122 return wxSocketClientPtr();
124 wxSocketClient
*sock
= new wxSocketClient(flags
);
126 CPPUNIT_ASSERT( sock
->Connect(*addr
) );
128 const wxString httpGetRoot
=
130 "Host: " + gs_serverHost
+ "\r\n"
133 sock
->Write(httpGetRoot
.ToAscii(), httpGetRoot
.length());
135 return wxSocketClientPtr(sock
);
138 void SocketTestCase::BlockingConnect()
140 wxSockAddressPtr addr
= GetServer();
145 CPPUNIT_ASSERT( sock
.Connect(*addr
) );
148 void SocketTestCase::NonblockingConnect()
150 wxSockAddressPtr addr
= GetServer();
154 SocketTestEventLoop
loop(m_useLoop
);
157 sock
.Connect(*addr
, false);
158 sock
.WaitOnConnect(10);
160 CPPUNIT_ASSERT( sock
.IsConnected() );
163 void SocketTestCase::ReadNormal()
165 SocketTestEventLoop
loop(m_useLoop
);
167 wxSocketClientPtr
sock(GetHTTPSocket());
172 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
174 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
175 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
179 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
181 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
182 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
185 void SocketTestCase::ReadBlock()
187 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_BLOCK
));
192 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
194 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
195 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
199 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
201 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
202 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
205 void SocketTestCase::ReadNowait()
207 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_NOWAIT
));
212 sock
->Read(buf
, WXSIZEOF(buf
));
213 if ( sock
->LastError() != wxSOCKET_WOULDBLOCK
)
215 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
219 void SocketTestCase::ReadWaitall()
221 SocketTestEventLoop
loop(m_useLoop
);
223 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_WAITALL
));
228 sock
->Read(buf
, WXSIZEOF(buf
));
230 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
231 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf
), sock
->LastCount() );
234 #endif // wxUSE_SOCKETS