]> git.saurik.com Git - wxWidgets.git/blob - tests/net/socket.cpp
Fix compilation-breaking typo in the previous commit.
[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: wxWindows 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 "wx/url.h"
30 #include "wx/sstream.h"
31 #include "wx/evtloop.h"
32 #include <memory>
33
34 typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
35 typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
36
37 static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
38
39 class SocketTestCase : public CppUnit::TestCase
40 {
41 public:
42 SocketTestCase() { }
43
44 private:
45 // we need to repeat the tests twice as the sockets behave differently when
46 // there is an active event loop and without it
47 #define ALL_SOCKET_TESTS() \
48 CPPUNIT_TEST( BlockingConnect ); \
49 CPPUNIT_TEST( NonblockingConnect ); \
50 CPPUNIT_TEST( ReadNormal ); \
51 CPPUNIT_TEST( ReadBlock ); \
52 CPPUNIT_TEST( ReadNowait ); \
53 CPPUNIT_TEST( ReadWaitall ); \
54 CPPUNIT_TEST( UrlTest )
55
56 CPPUNIT_TEST_SUITE( SocketTestCase );
57 ALL_SOCKET_TESTS();
58 CPPUNIT_TEST( PseudoTest_SetUseEventLoop );
59 ALL_SOCKET_TESTS();
60 CPPUNIT_TEST_SUITE_END();
61
62 // helper event loop class which sets itself as active only if we pass it
63 // true in ctor
64 class SocketTestEventLoop : public wxEventLoop
65 {
66 public:
67 SocketTestEventLoop(bool useLoop)
68 {
69 m_useLoop = useLoop;
70 if ( useLoop )
71 {
72 m_evtLoopOld = wxEventLoopBase::GetActive();
73 SetActive(this);
74 }
75 }
76
77 virtual ~SocketTestEventLoop()
78 {
79 if ( m_useLoop )
80 {
81 wxEventLoopBase::SetActive(m_evtLoopOld);
82 }
83 }
84
85 private:
86 bool m_useLoop;
87 wxEventLoopBase *m_evtLoopOld;
88 };
89
90 // get the address to connect to, if NULL is returned it means that the
91 // test is disabled and shouldn't run at all
92 wxSockAddressPtr GetServer() const;
93
94 // get the socket to read HTTP reply from, returns NULL if the test is
95 // disabled
96 wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
97
98 void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
99
100 void BlockingConnect();
101 void NonblockingConnect();
102 void ReadNormal();
103 void ReadBlock();
104 void ReadNowait();
105 void ReadWaitall();
106
107 void UrlTest();
108
109 static bool ms_useLoop;
110
111 DECLARE_NO_COPY_CLASS(SocketTestCase)
112 };
113
114 bool SocketTestCase::ms_useLoop = false;
115
116 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
117 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
118
119 wxSockAddressPtr SocketTestCase::GetServer() const
120 {
121 if ( gs_serverHost.empty() )
122 return wxSockAddressPtr();
123
124 wxIPV4address *addr = new wxIPV4address;
125 addr->Hostname(gs_serverHost);
126 addr->Service("www");
127
128 return wxSockAddressPtr(addr);
129 }
130
131 wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
132 {
133 wxSockAddressPtr addr = GetServer();
134 if ( !addr.get() )
135 return wxSocketClientPtr();
136
137 wxSocketClient *sock = new wxSocketClient(flags);
138 sock->SetTimeout(1);
139 CPPUNIT_ASSERT( sock->Connect(*addr) );
140
141 const wxString httpGetRoot =
142 "GET / HTTP/1.1\r\n"
143 "Host: " + gs_serverHost + "\r\n"
144 "\r\n";
145
146 sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
147
148 return wxSocketClientPtr(sock);
149 }
150
151 void SocketTestCase::BlockingConnect()
152 {
153 wxSockAddressPtr addr = GetServer();
154 if ( !addr.get() )
155 return;
156
157 wxSocketClient sock;
158 CPPUNIT_ASSERT( sock.Connect(*addr) );
159 }
160
161 void SocketTestCase::NonblockingConnect()
162 {
163 wxSockAddressPtr addr = GetServer();
164 if ( !addr.get() )
165 return;
166
167 SocketTestEventLoop loop(ms_useLoop);
168
169 wxSocketClient sock;
170 sock.Connect(*addr, false);
171 sock.WaitOnConnect(10);
172
173 CPPUNIT_ASSERT( sock.IsConnected() );
174 }
175
176 void SocketTestCase::ReadNormal()
177 {
178 SocketTestEventLoop loop(ms_useLoop);
179
180 wxSocketClientPtr sock(GetHTTPSocket());
181 if ( !sock.get() )
182 return;
183
184 char bufSmall[128];
185 sock->Read(bufSmall, WXSIZEOF(bufSmall));
186
187 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
188 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
189 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
190
191
192 char bufBig[102400];
193 sock->Read(bufBig, WXSIZEOF(bufBig));
194
195 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
196 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
197 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
198 }
199
200 void SocketTestCase::ReadBlock()
201 {
202 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_BLOCK));
203 if ( !sock.get() )
204 return;
205
206 char bufSmall[128];
207 sock->Read(bufSmall, WXSIZEOF(bufSmall));
208
209 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
210 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
211 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() );
212
213
214 char bufBig[102400];
215 sock->Read(bufBig, WXSIZEOF(bufBig));
216
217 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
218 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
219 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() );
220 }
221
222 void SocketTestCase::ReadNowait()
223 {
224 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
225 if ( !sock.get() )
226 return;
227
228 char buf[1024];
229 sock->Read(buf, WXSIZEOF(buf));
230 if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
231 {
232 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
233 }
234 }
235
236 void SocketTestCase::ReadWaitall()
237 {
238 SocketTestEventLoop loop(ms_useLoop);
239
240 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
241 if ( !sock.get() )
242 return;
243
244 char buf[128];
245 sock->Read(buf, WXSIZEOF(buf));
246
247 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
248 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() );
249 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastReadCount() );
250 }
251
252 void SocketTestCase::UrlTest()
253 {
254 if ( gs_serverHost.empty() )
255 return;
256
257 SocketTestEventLoop loop(ms_useLoop);
258
259 wxURL url("http://" + gs_serverHost);
260
261 const std::auto_ptr<wxInputStream> in(url.GetInputStream());
262 CPPUNIT_ASSERT( in.get() );
263
264 wxStringOutputStream out;
265 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() );
266 }
267
268 #endif // wxUSE_SOCKETS