]>
Commit | Line | Data |
---|---|---|
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 | ||
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: | |
8d8087fc | 42 | SocketTestCase() { } |
62fb86a5 VZ |
43 | |
44 | private: | |
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 |
114 | bool SocketTestCase::ms_useLoop = false; |
115 | ||
62fb86a5 VZ |
116 | CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase ); |
117 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" ); | |
118 | ||
119 | wxSockAddressPtr 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 | ||
131 | wxSocketClientPtr 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 | ||
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 | ||
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 | ||
176 | void 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() ); |
294a09aa | 189 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() ); |
62fb86a5 VZ |
190 | |
191 | ||
3afc646c | 192 | char bufBig[102400]; |
62fb86a5 VZ |
193 | sock->Read(bufBig, WXSIZEOF(bufBig)); |
194 | ||
195 | CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() ); | |
196 | CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() ); | |
294a09aa | 197 | CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() ); |
62fb86a5 VZ |
198 | } |
199 | ||
7b9b06e3 VZ |
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() ); | |
68888eea | 210 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() ); |
294a09aa | 211 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastReadCount() ); |
7b9b06e3 VZ |
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() ); | |
294a09aa | 219 | CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastReadCount() ); |
7b9b06e3 VZ |
220 | } |
221 | ||
62fb86a5 VZ |
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 | { | |
8d8087fc | 238 | SocketTestEventLoop loop(ms_useLoop); |
7b9b06e3 | 239 | |
62fb86a5 VZ |
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() ); | |
68888eea | 248 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() ); |
294a09aa | 249 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastReadCount() ); |
62fb86a5 VZ |
250 | } |
251 | ||
6799c5e2 VZ |
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); | |
1f30c75e VZ |
260 | |
261 | const std::auto_ptr<wxInputStream> in(url.GetInputStream()); | |
262 | CPPUNIT_ASSERT( in.get() ); | |
6799c5e2 VZ |
263 | |
264 | wxStringOutputStream out; | |
265 | CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() ); | |
266 | } | |
267 | ||
62fb86a5 | 268 | #endif // wxUSE_SOCKETS |