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
75 if ( gs_serverHost
.empty() )
76 return wxSockAddressPtr();
78 wxIPV4address
*addr
= new wxIPV4address
;
79 addr
->Hostname(gs_serverHost
);
82 return wxSockAddressPtr(addr
);
85 wxSocketClientPtr
SocketTestCase::GetHTTPSocket(int flags
) const
87 wxSockAddressPtr addr
= GetServer();
89 return wxSocketClientPtr();
91 wxSocketClient
*sock
= new wxSocketClient(flags
);
93 CPPUNIT_ASSERT( sock
->Connect(*addr
) );
95 const wxString httpGetRoot
=
97 "Host: " + gs_serverHost
+ "\r\n"
100 sock
->Write(httpGetRoot
.ToAscii(), httpGetRoot
.length());
102 return wxSocketClientPtr(sock
);
105 void SocketTestCase::BlockingConnect()
107 wxSockAddressPtr addr
= GetServer();
112 CPPUNIT_ASSERT( sock
.Connect(*addr
) );
115 void SocketTestCase::NonblockingConnect()
117 wxSockAddressPtr addr
= GetServer();
121 wxEventLoopGuarantor loop
;
124 sock
.Connect(*addr
, false);
125 sock
.WaitOnConnect(10);
127 CPPUNIT_ASSERT( sock
.IsConnected() );
130 void SocketTestCase::ReadNormal()
132 wxEventLoopGuarantor loop
;
134 wxSocketClientPtr
sock(GetHTTPSocket());
139 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
141 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
142 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
146 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
148 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
149 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
152 void SocketTestCase::ReadBlock()
154 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_BLOCK
));
159 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
161 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
162 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
166 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
168 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
169 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
172 void SocketTestCase::ReadNowait()
174 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_NOWAIT
));
179 sock
->Read(buf
, WXSIZEOF(buf
));
180 if ( sock
->LastError() != wxSOCKET_WOULDBLOCK
)
182 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
186 void SocketTestCase::ReadWaitall()
188 wxEventLoopGuarantor loop
;
190 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_WAITALL
));
195 sock
->Read(buf
, WXSIZEOF(buf
));
197 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
198 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf
), sock
->LastCount() );
201 #endif // wxUSE_SOCKETS