]>
git.saurik.com Git - wxWidgets.git/blob - tests/net/socket.cpp
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"
31 typedef std::auto_ptr
<wxSockAddress
> wxSockAddressPtr
;
32 typedef std::auto_ptr
<wxSocketClient
> wxSocketClientPtr
;
34 static wxString
gs_serverHost(wxGetenv("WX_TEST_SERVER"));
36 class SocketTestCase
: public CppUnit::TestCase
42 CPPUNIT_TEST_SUITE( SocketTestCase
);
43 CPPUNIT_TEST( BlockingConnect
);
44 CPPUNIT_TEST( NonblockingConnect
);
45 CPPUNIT_TEST( ReadNormal
);
46 CPPUNIT_TEST( ReadNowait
);
47 CPPUNIT_TEST( ReadWaitall
);
48 CPPUNIT_TEST_SUITE_END();
50 // get the address to connect to, if NULL is returned it means that the
51 // test is disabled and shouldn't run at all
52 wxSockAddressPtr
GetServer() const;
54 // get the socket to read HTTP reply from, returns NULL if the test is
56 wxSocketClientPtr
GetHTTPSocket(int flags
= wxSOCKET_NONE
) const;
58 void BlockingConnect();
59 void NonblockingConnect();
64 DECLARE_NO_COPY_CLASS(SocketTestCase
)
67 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase
);
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase
, "SocketTestCase" );
70 wxSockAddressPtr
SocketTestCase::GetServer() const
73 if ( !gs_serverHost
.empty() )
75 wxIPV4address
*addr
= new wxIPV4address
;
76 addr
->Hostname(gs_serverHost
);
85 wxSocketClientPtr
SocketTestCase::GetHTTPSocket(int flags
) const
87 wxSocketClientPtr ptr
;
89 wxSockAddressPtr addr
= GetServer();
93 wxSocketClient
*sock
= new wxSocketClient(flags
);
95 CPPUNIT_ASSERT( sock
->Connect(*addr
) );
97 const wxString httpGetRoot
=
99 "Host: " + gs_serverHost
+ "\r\n"
102 sock
->Write(httpGetRoot
.ToAscii(), httpGetRoot
.length());
108 void SocketTestCase::BlockingConnect()
110 wxSockAddressPtr addr
= GetServer();
115 CPPUNIT_ASSERT( sock
.Connect(*addr
) );
118 void SocketTestCase::NonblockingConnect()
120 wxSockAddressPtr addr
= GetServer();
125 sock
.Connect(*addr
, false);
126 sock
.WaitOnConnect(10);
128 CPPUNIT_ASSERT( sock
.IsConnected() );
131 void SocketTestCase::ReadNormal()
133 wxSocketClientPtr
sock(GetHTTPSocket());
138 sock
->Read(bufSmall
, WXSIZEOF(bufSmall
));
140 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
141 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall
), sock
->LastCount() );
145 sock
->Read(bufBig
, WXSIZEOF(bufBig
));
147 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
148 CPPUNIT_ASSERT( WXSIZEOF(bufBig
) >= sock
->LastCount() );
151 void SocketTestCase::ReadNowait()
153 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_NOWAIT
));
158 sock
->Read(buf
, WXSIZEOF(buf
));
159 if ( sock
->LastError() != wxSOCKET_WOULDBLOCK
)
161 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
165 void SocketTestCase::ReadWaitall()
167 wxSocketClientPtr
sock(GetHTTPSocket(wxSOCKET_WAITALL
));
172 sock
->Read(buf
, WXSIZEOF(buf
));
174 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR
, sock
->LastError() );
175 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf
), sock
->LastCount() );
178 #endif // wxUSE_SOCKETS