]>
git.saurik.com Git - wxWidgets.git/blob - tests/streams/socketstream.cpp
7a4002163a09416fb70a4bcb8036ac11c71b7d75
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/gsocket.h"
24 #include "wx/socket.h"
25 #include "wx/sckstrm.h"
26 #include "wx/thread.h"
33 const int TEST_PORT_READ
= 0x7778; // arbitrary, chosen because == "wx"
34 const int TEST_PORT_WRITE
= 0x7779; // well, "wy"
36 // these cond and mutex are used to minimize the risk of the main thread
37 // Connect()-ing before this thread starts Accept()-ing connections but
38 // unfortunately we can't make this truly safe, see comment in
39 // SocketServerThread::Entry()
41 wxCondition
gs_cond(gs_mutex
);
42 } // anonymous namespace
44 // return address for the given port on local host
45 static inline wxIPV4address
LocalAddress(int port
)
54 // A thread which creates a listening socket on the specified port and executes
55 // the given function with each socket which connects to it
56 class SocketServerThread
: public wxThread
59 // port is the port to listen on and function will be called on each
61 SocketServerThread(int port
, void (*accept
)(wxSocketBase
&))
62 : wxThread(wxTHREAD_JOINABLE
),
73 wxSocketServer
srv(LocalAddress(m_port
), wxSOCKET_REUSEADDR
);
75 // FIXME: this is still not atomic, of course and the main thread could
76 // call Connect() before we have time to Accept() but there is
77 // no way to fix it with current API
79 wxMutexLocker
lock(gs_mutex
);
83 wxSocketBase
*socket
= srv
.Accept();
91 void (*m_accept
)(wxSocketBase
&);
93 DECLARE_NO_COPY_CLASS(SocketServerThread
)
96 // The test case for socket streams
98 public BaseStreamTestCase
<wxSocketInputStream
, wxSocketOutputStream
>
102 virtual ~socketStream();
104 virtual void setUp();
105 virtual void tearDown();
107 CPPUNIT_TEST_SUITE(socketStream
);
108 // Base class stream tests the socketStream supports.
109 CPPUNIT_TEST(Input_GetC
);
111 // This one fails because wxSocketInputStream::Eof() is not implemented
113 //CPPUNIT_TEST(Input_Read);
115 // The other ones untested yet
117 CPPUNIT_TEST(Input_Eof
);
118 CPPUNIT_TEST(Input_LastRead
);
119 CPPUNIT_TEST(Input_CanRead
);
120 CPPUNIT_TEST(Input_Peek
);
121 CPPUNIT_TEST(Input_Ungetch
);
123 CPPUNIT_TEST(Output_PutC
);
124 CPPUNIT_TEST(Output_Write
);
125 CPPUNIT_TEST(Output_LastWrite
);
127 CPPUNIT_TEST_SUITE_END();
130 // Implement base class functions.
131 virtual wxSocketInputStream
*DoCreateInStream();
132 virtual wxSocketOutputStream
*DoCreateOutStream();
134 // socket thread functions
135 static void WriteSocket(wxSocketBase
& socket
)
137 socket
.Write("hello, world!", 13);
140 static void ReadSocket(wxSocketBase
& socket
)
143 while ( socket
.Read(&ch
, 1).LastCount() == 1 )
147 wxSocketClient
*m_readSocket
,
149 wxThread
*m_writeThread
,
153 socketStream::socketStream()
156 m_writeSocket
= NULL
;
164 socketStream::~socketStream()
169 void socketStream::setUp()
171 // create the socket threads and wait until they are ready to accept
172 // connections (if we called Connect() before this happens, it would fail)
174 wxMutexLocker
lock(gs_mutex
);
177 new SocketServerThread(TEST_PORT_READ
, &socketStream::WriteSocket
);
178 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, gs_cond
.Wait() );
181 new SocketServerThread(TEST_PORT_WRITE
, &socketStream::ReadSocket
);
182 CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR
, gs_cond
.Wait() );
185 m_readSocket
= new wxSocketClient
;
186 m_readSocket
->SetTimeout(3);
187 CPPUNIT_ASSERT( m_readSocket
->Connect(LocalAddress(TEST_PORT_READ
)) );
189 m_writeSocket
= new wxSocketClient
;
190 m_writeSocket
->SetTimeout(3);
191 CPPUNIT_ASSERT( m_writeSocket
->Connect(LocalAddress(TEST_PORT_WRITE
)) );
194 void socketStream::tearDown()
196 wxDELETE(m_readSocket
);
197 wxDELETE(m_writeSocket
);
199 m_writeThread
->Wait();
200 wxDELETE(m_writeThread
);
202 m_readThread
->Wait();
203 wxDELETE(m_readThread
);
206 wxSocketInputStream
*socketStream::DoCreateInStream()
208 wxSocketInputStream
*pStrInStream
= new wxSocketInputStream(*m_readSocket
);
209 CPPUNIT_ASSERT(pStrInStream
->IsOk());
213 wxSocketOutputStream
*socketStream::DoCreateOutStream()
215 wxSocketOutputStream
*pStrOutStream
= new wxSocketOutputStream(*m_writeSocket
);
216 CPPUNIT_ASSERT(pStrOutStream
->IsOk());
217 return pStrOutStream
;
220 // Register the stream sub suite, by using some stream helper macro.
221 STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(socketStream
)