]> git.saurik.com Git - wxWidgets.git/blob - tests/net/socket.cpp
use the currently active event loop for the event dispatching instead of wxYield...
[wxWidgets.git] / tests / net / socket.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/socket.cpp
3 // Purpose: wxSocket unit tests
4 // Author: Vadim Zeitlin
5 // RCS-ID: $Id$
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 /*
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
15 connectivity).
16 */
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 // and "wx/cppunit.h"
20 #include "testprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_SOCKETS
27
28 #include "wx/socket.h"
29 #include <memory>
30
31 typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
32 typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
33
34 static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
35
36 class SocketTestCase : public CppUnit::TestCase
37 {
38 public:
39 SocketTestCase() { }
40
41 private:
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();
49
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;
53
54 // get the socket to read HTTP reply from, returns NULL if the test is
55 // disabled
56 wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
57
58 void BlockingConnect();
59 void NonblockingConnect();
60 void ReadNormal();
61 void ReadNowait();
62 void ReadWaitall();
63
64 DECLARE_NO_COPY_CLASS(SocketTestCase)
65 };
66
67 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
68 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
69
70 wxSockAddressPtr SocketTestCase::GetServer() const
71 {
72 wxSockAddressPtr ptr;
73 if ( !gs_serverHost.empty() )
74 {
75 wxIPV4address *addr = new wxIPV4address;
76 addr->Hostname(gs_serverHost);
77 addr->Service("www");
78
79 ptr.reset(addr);
80 }
81
82 return ptr;
83 }
84
85 wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
86 {
87 wxSocketClientPtr ptr;
88
89 wxSockAddressPtr addr = GetServer();
90 if ( !addr.get() )
91 return ptr;
92
93 wxSocketClient *sock = new wxSocketClient(flags);
94 sock->SetTimeout(1);
95 CPPUNIT_ASSERT( sock->Connect(*addr) );
96
97 const wxString httpGetRoot =
98 "GET / HTTP/1.1\r\n"
99 "Host: " + gs_serverHost + "\r\n"
100 "\r\n";
101
102 sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
103
104 ptr.reset(sock);
105 return ptr;
106 }
107
108 void SocketTestCase::BlockingConnect()
109 {
110 wxSockAddressPtr addr = GetServer();
111 if ( !addr.get() )
112 return;
113
114 wxSocketClient sock;
115 CPPUNIT_ASSERT( sock.Connect(*addr) );
116 }
117
118 void SocketTestCase::NonblockingConnect()
119 {
120 wxSockAddressPtr addr = GetServer();
121 if ( !addr.get() )
122 return;
123
124 wxSocketClient sock;
125 sock.Connect(*addr, false);
126 sock.WaitOnConnect(10);
127
128 CPPUNIT_ASSERT( sock.IsConnected() );
129 }
130
131 void SocketTestCase::ReadNormal()
132 {
133 wxSocketClientPtr sock(GetHTTPSocket());
134 if ( !sock.get() )
135 return;
136
137 char bufSmall[128];
138 sock->Read(bufSmall, WXSIZEOF(bufSmall));
139
140 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
141 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
142
143
144 char bufBig[102400];
145 sock->Read(bufBig, WXSIZEOF(bufBig));
146
147 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
148 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
149 }
150
151 void SocketTestCase::ReadNowait()
152 {
153 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
154 if ( !sock.get() )
155 return;
156
157 char buf[1024];
158 sock->Read(buf, WXSIZEOF(buf));
159 if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
160 {
161 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
162 }
163 }
164
165 void SocketTestCase::ReadWaitall()
166 {
167 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
168 if ( !sock.get() )
169 return;
170
171 char buf[128];
172 sock->Read(buf, WXSIZEOF(buf));
173
174 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
175 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), sock->LastCount() );
176 }
177
178 #endif // wxUSE_SOCKETS