]> git.saurik.com Git - wxWidgets.git/blame - tests/net/socket.cpp
Replace define for OLECMDID_OPTICAL_ZOOM with an enum to avoid errors in compilers...
[wxWidgets.git] / tests / net / socket.cpp
CommitLineData
62fb86a5
VZ
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
526954c5 7// Licence: wxWindows licence
62fb86a5
VZ
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"
6799c5e2
VZ
29#include "wx/url.h"
30#include "wx/sstream.h"
7b9b06e3 31#include "wx/evtloop.h"
62fb86a5
VZ
32#include <memory>
33
34typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
35typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
36
37static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
38
39class SocketTestCase : public CppUnit::TestCase
40{
41public:
8d8087fc 42 SocketTestCase() { }
62fb86a5
VZ
43
44private:
6e7fd3ca
VZ
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 ); \
16685812
VZ
53 CPPUNIT_TEST( ReadWaitall ); \
54 CPPUNIT_TEST( UrlTest )
6e7fd3ca 55
62fb86a5 56 CPPUNIT_TEST_SUITE( SocketTestCase );
6e7fd3ca
VZ
57 ALL_SOCKET_TESTS();
58 CPPUNIT_TEST( PseudoTest_SetUseEventLoop );
59 ALL_SOCKET_TESTS();
62fb86a5
VZ
60 CPPUNIT_TEST_SUITE_END();
61
6e7fd3ca
VZ
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 )
232fdc63
VZ
71 {
72 m_evtLoopOld = wxEventLoopBase::GetActive();
6e7fd3ca 73 SetActive(this);
232fdc63 74 }
6e7fd3ca
VZ
75 }
76
77 virtual ~SocketTestEventLoop()
78 {
79 if ( m_useLoop )
232fdc63
VZ
80 {
81 wxEventLoopBase::SetActive(m_evtLoopOld);
82 }
6e7fd3ca
VZ
83 }
84
85 private:
86 bool m_useLoop;
232fdc63 87 wxEventLoopBase *m_evtLoopOld;
6e7fd3ca
VZ
88 };
89
62fb86a5
VZ
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
8d8087fc 98 void PseudoTest_SetUseEventLoop() { ms_useLoop = true; }
6e7fd3ca 99
62fb86a5
VZ
100 void BlockingConnect();
101 void NonblockingConnect();
102 void ReadNormal();
7b9b06e3 103 void ReadBlock();
62fb86a5
VZ
104 void ReadNowait();
105 void ReadWaitall();
106
6799c5e2
VZ
107 void UrlTest();
108
8d8087fc 109 static bool ms_useLoop;
6e7fd3ca 110
62fb86a5
VZ
111 DECLARE_NO_COPY_CLASS(SocketTestCase)
112};
113
8d8087fc
VZ
114bool SocketTestCase::ms_useLoop = false;
115
62fb86a5
VZ
116CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
117CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
118
119wxSockAddressPtr SocketTestCase::GetServer() const
120{
b245cbc5
VZ
121 if ( gs_serverHost.empty() )
122 return wxSockAddressPtr();
62fb86a5 123
b245cbc5
VZ
124 wxIPV4address *addr = new wxIPV4address;
125 addr->Hostname(gs_serverHost);
126 addr->Service("www");
62fb86a5 127
b245cbc5 128 return wxSockAddressPtr(addr);
62fb86a5
VZ
129}
130
131wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
132{
62fb86a5
VZ
133 wxSockAddressPtr addr = GetServer();
134 if ( !addr.get() )
b245cbc5 135 return wxSocketClientPtr();
62fb86a5
VZ
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
6b6b233a 146 sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
62fb86a5 147
b245cbc5 148 return wxSocketClientPtr(sock);
62fb86a5
VZ
149}
150
151void 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
161void SocketTestCase::NonblockingConnect()
162{
163 wxSockAddressPtr addr = GetServer();
164 if ( !addr.get() )
165 return;
166
8d8087fc 167 SocketTestEventLoop loop(ms_useLoop);
7b9b06e3 168
62fb86a5
VZ
169 wxSocketClient sock;
170 sock.Connect(*addr, false);
171 sock.WaitOnConnect(10);
172
173 CPPUNIT_ASSERT( sock.IsConnected() );
174}
175
176void SocketTestCase::ReadNormal()
177{
8d8087fc 178 SocketTestEventLoop loop(ms_useLoop);
7b9b06e3 179
62fb86a5
VZ
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() );
68888eea 188 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
62fb86a5
VZ
189
190
3afc646c 191 char bufBig[102400];
62fb86a5
VZ
192 sock->Read(bufBig, WXSIZEOF(bufBig));
193
194 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
195 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
196}
197
7b9b06e3
VZ
198void SocketTestCase::ReadBlock()
199{
200 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_BLOCK));
201 if ( !sock.get() )
202 return;
203
204 char bufSmall[128];
205 sock->Read(bufSmall, WXSIZEOF(bufSmall));
206
207 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
68888eea 208 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() );
7b9b06e3
VZ
209
210
211 char bufBig[102400];
212 sock->Read(bufBig, WXSIZEOF(bufBig));
213
214 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
215 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
216}
217
62fb86a5
VZ
218void SocketTestCase::ReadNowait()
219{
220 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
221 if ( !sock.get() )
222 return;
223
224 char buf[1024];
225 sock->Read(buf, WXSIZEOF(buf));
226 if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
227 {
228 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
229 }
230}
231
232void SocketTestCase::ReadWaitall()
233{
8d8087fc 234 SocketTestEventLoop loop(ms_useLoop);
7b9b06e3 235
62fb86a5
VZ
236 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
237 if ( !sock.get() )
238 return;
239
240 char buf[128];
241 sock->Read(buf, WXSIZEOF(buf));
242
243 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
68888eea 244 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() );
62fb86a5
VZ
245}
246
6799c5e2
VZ
247void SocketTestCase::UrlTest()
248{
249 if ( gs_serverHost.empty() )
250 return;
251
252 SocketTestEventLoop loop(ms_useLoop);
253
254 wxURL url("http://" + gs_serverHost);
1f30c75e
VZ
255
256 const std::auto_ptr<wxInputStream> in(url.GetInputStream());
257 CPPUNIT_ASSERT( in.get() );
6799c5e2
VZ
258
259 wxStringOutputStream out;
260 CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() );
261}
262
62fb86a5 263#endif // wxUSE_SOCKETS