]> git.saurik.com Git - wxWidgets.git/blame - tests/net/socket.cpp
Fixed centering and right-justification when combined with left indentation (bug...
[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
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"
7b9b06e3 29#include "wx/evtloop.h"
62fb86a5
VZ
30#include <memory>
31
32typedef std::auto_ptr<wxSockAddress> wxSockAddressPtr;
33typedef std::auto_ptr<wxSocketClient> wxSocketClientPtr;
34
35static wxString gs_serverHost(wxGetenv("WX_TEST_SERVER"));
36
37class SocketTestCase : public CppUnit::TestCase
38{
39public:
40 SocketTestCase() { }
41
42private:
43 CPPUNIT_TEST_SUITE( SocketTestCase );
44 CPPUNIT_TEST( BlockingConnect );
45 CPPUNIT_TEST( NonblockingConnect );
46 CPPUNIT_TEST( ReadNormal );
7b9b06e3 47 CPPUNIT_TEST( ReadBlock );
62fb86a5
VZ
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();
7b9b06e3 63 void ReadBlock();
62fb86a5
VZ
64 void ReadNowait();
65 void ReadWaitall();
66
67 DECLARE_NO_COPY_CLASS(SocketTestCase)
68};
69
70CPPUNIT_TEST_SUITE_REGISTRATION( SocketTestCase );
71CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( SocketTestCase, "SocketTestCase" );
72
73wxSockAddressPtr SocketTestCase::GetServer() const
74{
b245cbc5
VZ
75 if ( gs_serverHost.empty() )
76 return wxSockAddressPtr();
62fb86a5 77
b245cbc5
VZ
78 wxIPV4address *addr = new wxIPV4address;
79 addr->Hostname(gs_serverHost);
80 addr->Service("www");
62fb86a5 81
b245cbc5 82 return wxSockAddressPtr(addr);
62fb86a5
VZ
83}
84
85wxSocketClientPtr SocketTestCase::GetHTTPSocket(int flags) const
86{
62fb86a5
VZ
87 wxSockAddressPtr addr = GetServer();
88 if ( !addr.get() )
b245cbc5 89 return wxSocketClientPtr();
62fb86a5
VZ
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
6b6b233a 100 sock->Write(httpGetRoot.ToAscii(), httpGetRoot.length());
62fb86a5 101
b245cbc5 102 return wxSocketClientPtr(sock);
62fb86a5
VZ
103}
104
105void 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
115void SocketTestCase::NonblockingConnect()
116{
117 wxSockAddressPtr addr = GetServer();
118 if ( !addr.get() )
119 return;
120
7b9b06e3
VZ
121 wxEventLoopGuarantor loop;
122
62fb86a5
VZ
123 wxSocketClient sock;
124 sock.Connect(*addr, false);
125 sock.WaitOnConnect(10);
126
127 CPPUNIT_ASSERT( sock.IsConnected() );
128}
129
130void SocketTestCase::ReadNormal()
131{
7b9b06e3
VZ
132 wxEventLoopGuarantor loop;
133
62fb86a5
VZ
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
3afc646c 145 char bufBig[102400];
62fb86a5
VZ
146 sock->Read(bufBig, WXSIZEOF(bufBig));
147
148 CPPUNIT_ASSERT_EQUAL( wxSOCKET_NOERROR, sock->LastError() );
149 CPPUNIT_ASSERT( WXSIZEOF(bufBig) >= sock->LastCount() );
150}
151
7b9b06e3
VZ
152void 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
62fb86a5
VZ
172void 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
186void SocketTestCase::ReadWaitall()
187{
7b9b06e3
VZ
188 wxEventLoopGuarantor loop;
189
62fb86a5
VZ
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