]> git.saurik.com Git - wxWidgets.git/blob - tests/net/socket.cpp
remove m_endModalCalled flag, it seems to be unnecessary
[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 "wx/evtloop.h"
30 #include <memory>
31
32 typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
33 typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
34
35 static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
36
37 class SocketTestCase : public CppUnit::TestCase
38 {
39 public:
40 SocketTestCase() { }
41
42 private:
43 CPPUNIT_TEST_SUITE( SocketTestCase );
44 CPPUNIT_TEST( BlockingConnect );
45 CPPUNIT_TEST( NonblockingConnect );
46 CPPUNIT_TEST( ReadNormal );
47 CPPUNIT_TEST( ReadBlock );
48 CPPUNIT_TEST( ReadNowait );
49 CPPUNIT_TEST( ReadWaitall );
50 CPPUNIT_TEST_SUITE_END();
51
52 // get the address to connect to, if NULL is returned it means that the
53 // test is disabled and shouldn't run at all
54 wxSockAddressPtr GetServer() const;
55
56 // get the socket to read HTTP reply from, returns NULL if the test is
57 // disabled
58 wxSocketClientPtr GetHTTPSocket(int flags = wxSOCKET_NONE) const;
59
60 void BlockingConnect();
61 void NonblockingConnect();
62 void ReadNormal();
63 void ReadBlock();
64 void ReadNowait();
65 void ReadWaitall();
66
67 DECLARE_NO_COPY_CLASS(SocketTestCase)
68 };
69
70 CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
71 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
72
73 wxSockAddressPtr SocketTestCase::GetServer() const
74 {
75 if ( gs_serverHost.empty() )
76 return wxSockAddressPtr();
77
78 wxIPV4address *addr = new wxIPV4address;
79 addr->Hostname(gs_serverHost);
80 addr->Service("www");
81
82 return wxSockAddressPtr(addr);
83 }
84
85 wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
86 {
87 wxSockAddressPtr addr = GetServer();
88 if ( !addr.get() )
89 return wxSocketClientPtr();
90
91 wxSocketClient *sock = new wxSocketClient(flags);
92 sock->SetTimeout(1);
93 CPPUNIT_ASSERT( sock->Connect(*addr) );
94
95 const wxString httpGetRoot =
96 "GET / HTTP/1.1\r\n"
97 "Host: " + gs_serverHost + "\r\n"
98 "\r\n";
99
100 sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
101
102 return wxSocketClientPtr(sock);
103 }
104
105 void SocketTestCase::BlockingConnect()
106 {
107 wxSockAddressPtr addr = GetServer();
108 if ( !addr.get() )
109 return;
110
111 wxSocketClient sock;
112 CPPUNIT_ASSERT( sock.Connect(*addr) );
113 }
114
115 void SocketTestCase::NonblockingConnect()
116 {
117 wxSockAddressPtr addr = GetServer();
118 if ( !addr.get() )
119 return;
120
121 wxEventLoopGuarantor loop;
122
123 wxSocketClient sock;
124 sock.Connect(*addr, false);
125 sock.WaitOnConnect(10);
126
127 CPPUNIT_ASSERT( sock.IsConnected() );
128 }
129
130 void SocketTestCase::ReadNormal()
131 {
132 wxEventLoopGuarantor loop;
133
134 wxSocketClientPtr sock(GetHTTPSocket());
135 if ( !sock.get() )
136 return;
137
138 char bufSmall[128];
139 sock->Read(bufSmall, WXSIZEOF(bufSmall));
140
141 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
142 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
143
144
145 char bufBig[102400];
146 sock->Read(bufBig, WXSIZEOF(bufBig));
147
148 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
149 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
150 }
151
152 void SocketTestCase::ReadBlock()
153 {
154 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_BLOCK));
155 if ( !sock.get() )
156 return;
157
158 char bufSmall[128];
159 sock->Read(bufSmall, WXSIZEOF(bufSmall));
160
161 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
162 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(bufSmall), sock->LastCount() );
163
164
165 char bufBig[102400];
166 sock->Read(bufBig, WXSIZEOF(bufBig));
167
168 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
169 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
170 }
171
172 void SocketTestCase::ReadNowait()
173 {
174 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_NOWAIT));
175 if ( !sock.get() )
176 return;
177
178 char buf[1024];
179 sock->Read(buf, WXSIZEOF(buf));
180 if ( sock->LastError() != wxSOCKET_WOULDBLOCK )
181 {
182 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
183 }
184 }
185
186 void SocketTestCase::ReadWaitall()
187 {
188 wxEventLoopGuarantor loop;
189
190 wxSocketClientPtr sock(GetHTTPSocket(wxSOCKET_WAITALL));
191 if ( !sock.get() )
192 return;
193
194 char buf[128];
195 sock->Read(buf, WXSIZEOF(buf));
196
197 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
198 CPPUNIT_ASSERT_EQUAL( WXSIZEOF(buf), sock->LastCount() );
199 }
200
201 #endif // wxUSE_SOCKETS