]>
Commit | Line | Data |
---|---|---|
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 | ||
190 | ||
191 | char bufBig[102400]; | |
192 | sock->Read(bufBig, WXSIZEOF(bufBig)); | |
193 | ||
194 | CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() ); | |
195 | CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() ); | |
196 | } | |
197 | ||
198 | void 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() ); | |
208 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), (size_t)sock->LastCount() ); | |
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 | ||
218 | void 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 | ||
232 | void SocketTestCase::ReadWaitall() | |
233 | { | |
234 | SocketTestEventLoop loop(ms_useLoop); | |
235 | ||
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() ); | |
244 | CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), (size_t)sock->LastCount() ); | |
245 | } | |
246 | ||
247 | void SocketTestCase::UrlTest() | |
248 | { | |
249 | if ( gs_serverHost.empty() ) | |
250 | return; | |
251 | ||
252 | SocketTestEventLoop loop(ms_useLoop); | |
253 | ||
254 | wxURL url("http://" + gs_serverHost); | |
255 | ||
256 | const std::auto_ptr<wxInputStream> in(url.GetInputStream()); | |
257 | CPPUNIT_ASSERT( in.get() ); | |
258 | ||
259 | wxStringOutputStream out; | |
260 | CPPUNIT_ASSERT_EQUAL( wxSTREAM_EOF, in->Read(out).GetLastError() ); | |
261 | } | |
262 | ||
263 | #endif // wxUSE_SOCKETS |