]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/socketstream.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/socketstream.cpp
3 // Purpose: Test wxSocketInputStream/wxSocketOutputStream
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
18 // for all others, include the necessary headers
23 #include "wx/socket.h"
24 #include "wx/sckstrm.h"
25 #include "wx/thread.h"
32 const int TEST_PORT_READ
= 0x7778; // arbitrary, chosen because == "wx"
33 const int TEST_PORT_WRITE
= 0x7779; // well, "wy"
35 // these cond and mutex are used to minimize the risk of the main thread
36 // Connect()-ing before this thread starts Accept()-ing connections but
37 // unfortunately we can't make this truly safe, see comment in
38 // SocketServerThread::Entry()
40 wxCondition
gs_cond(gs_mutex
);
41 } // anonymous namespace
43 // return address for the given port on local host
44 static inline wxIPV4address
LocalAddress(int port
)
53 // A thread which creates a listening socket on the specified port and executes
54 // the given function with each socket which connects to it
55 class SocketServerThread
: public wxThread
58 // port is the port to listen on and function will be called on each
60 SocketServerThread(int port
, void (*accept
)(wxSocketBase
&))
61 : wxThread(wxTHREAD_JOINABLE
),
72 wxSocketServer
srv(LocalAddress(m_port
), wxSOCKET_REUSEADDR
);
74 // FIXME: this is still not atomic, of course and the main thread could
75 // call Connect() before we have time to Accept() but there is
76 // no way to fix it with current API
78 wxMutexLocker
lock(gs_mutex
);
82 wxSocketBase
*socket
= srv
.Accept();
93 void (*m_accept
)(wxSocketBase
&);
95 DECLARE_NO_COPY_CLASS(SocketServerThread
)
98 // The test case for socket streams
100 public BaseStreamTestCase
<wxSocketInputStream
, wxSocketOutputStream
>
104 virtual ~socketStream();
106 virtual void setUp();
107 virtual void tearDown();
109 // repeat all socket tests several times with different socket flags, so we
110 // define this macro which is used several times in the test suite
112 // there must be some more elegant way to do this but I didn't find it...
113 #define ALL_SOCKET_TESTS() \
114 CPPUNIT_TEST(Input_GetC); \
115 CPPUNIT_TEST(Input_Eof); \
116 CPPUNIT_TEST(Input_Read); \
117 CPPUNIT_TEST(Input_LastRead); \
118 CPPUNIT_TEST(Input_CanRead); \
119 CPPUNIT_TEST(Input_Peek); \
120 CPPUNIT_TEST(Input_Ungetch); \
122 CPPUNIT_TEST(Output_PutC); \
123 CPPUNIT_TEST(Output_Write); \
124 CPPUNIT_TEST(Output_LastWrite)
126 CPPUNIT_TEST_SUITE(socketStream
);
128 CPPUNIT_TEST( PseudoTest_SetNoWait
);
130 CPPUNIT_TEST( PseudoTest_SetWaitAll
);
132 CPPUNIT_TEST_SUITE_END();
135 // Implement base class functions.
136 virtual wxSocketInputStream
*DoCreateInStream();
137 virtual wxSocketOutputStream
*DoCreateOutStream();
139 // socket thread functions
140 static void WriteSocket(wxSocketBase
& socket
)
142 socket
.Write("hello, world!", 13);
145 static void ReadSocket(wxSocketBase
& socket
)
148 while ( socket
.Read(&ch
, 1).LastCount() == 1 )
152 void PseudoTest_SetNoWait() { m_flags
= wxSOCKET_NOWAIT
; }
153 void PseudoTest_SetWaitAll() { m_flags
= wxSOCKET_WAITALL
; }
155 wxSocketClient
*m_readSocket
,
157 wxThread
*m_writeThread
,
160 wxSocketFlags m_flags
;
163 socketStream::socketStream()
166 m_writeSocket
= NULL
;
171 m_flags
= wxSOCKET_NONE
;
173 wxSocketBase::Initialize();
176 socketStream::~socketStream()
178 wxSocketBase::Shutdown();
181 void socketStream::setUp()
183 // create the socket threads and wait until they are ready to accept
184 // connections (if we called Connect() before this happens, it would fail)
186 wxMutexLocker
lock(gs_mutex
);
189 new SocketServerThread(TEST_PORT_READ
, &socketStream::WriteSocket
);
190 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, gs_cond
.Wait() );
193 new SocketServerThread(TEST_PORT_WRITE
, &socketStream::ReadSocket
);
194 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, gs_cond
.Wait() );
197 m_readSocket
= new wxSocketClient(m_flags
);
198 CPPUNIT_ASSERT( m_readSocket
->Connect(LocalAddress(TEST_PORT_READ
)) );
200 m_writeSocket
= new wxSocketClient(m_flags
);
201 CPPUNIT_ASSERT( m_writeSocket
->Connect(LocalAddress(TEST_PORT_WRITE
)) );
204 void socketStream::tearDown()
206 wxDELETE(m_readSocket
);
207 wxDELETE(m_writeSocket
);
209 m_writeThread
->Wait();
210 wxDELETE(m_writeThread
);
212 m_readThread
->Wait();
213 wxDELETE(m_readThread
);
216 wxSocketInputStream
*socketStream::DoCreateInStream()
218 wxSocketInputStream
*pStrInStream
= new wxSocketInputStream(*m_readSocket
);
219 CPPUNIT_ASSERT(pStrInStream
->IsOk());
223 wxSocketOutputStream
*socketStream::DoCreateOutStream()
225 wxSocketOutputStream
*pStrOutStream
= new wxSocketOutputStream(*m_writeSocket
);
226 CPPUNIT_ASSERT(pStrOutStream
->IsOk());
227 return pStrOutStream
;
230 // Register the stream sub suite, by using some stream helper macro.
231 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(socketStream
)