]>
git.saurik.com Git - wxWidgets.git/blob - tests/net/ipc.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/ipc.cpp
3 // Purpose: IPC classes unit tests
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 // this test needs threads as it runs the test server in a secondary thread
21 // for all others, include the necessary headers
27 #include "wx/thread.h"
29 #define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)
34 const char *IPC_TEST_PORT
= "4242";
35 const char *IPC_TEST_TOPIC
= "IPC TEST";
37 } // anonymous namespace
39 // ----------------------------------------------------------------------------
40 // test connection class used by IPCTestServer
41 // ----------------------------------------------------------------------------
43 class IPCTestConnection
: public wxConnection
46 IPCTestConnection() { }
48 virtual bool OnExec(const wxString
& topic
, const wxString
& data
)
50 if ( topic
!= IPC_TEST_TOPIC
)
53 return data
== "Date";
57 DECLARE_NO_COPY_CLASS(IPCTestConnection
)
60 // ----------------------------------------------------------------------------
61 // event dispatching thread class
62 // ----------------------------------------------------------------------------
64 class EventThread
: public wxThread
68 : wxThread(wxTHREAD_JOINABLE
)
82 DECLARE_NO_COPY_CLASS(EventThread
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 class IPCTestServer
: public wxServer
96 #if wxUSE_SOCKETS_FOR_IPC
97 // we must call this from the main thread
98 wxSocketBase::Initialize();
99 #endif // wxUSE_SOCKETS_FOR_IPC
101 // we need event dispatching to work for IPC server to work
102 m_thread
= new EventThread
;
104 Create(IPC_TEST_PORT
);
107 virtual ~IPCTestServer()
109 wxTheApp
->ExitMainLoop();
116 #if wxUSE_SOCKETS_FOR_IPC
117 wxSocketBase::Shutdown();
118 #endif // wxUSE_SOCKETS_FOR_IPC
121 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
)
123 if ( topic
!= IPC_TEST_TOPIC
)
126 m_conn
= new IPCTestConnection
;
131 EventThread
*m_thread
;
132 IPCTestConnection
*m_conn
;
134 DECLARE_NO_COPY_CLASS(IPCTestServer
)
137 static IPCTestServer
*gs_server
= NULL
;
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 class IPCTestClient
: public wxClient
151 virtual ~IPCTestClient()
157 Connect(const wxString
& host
, const wxString
& service
, const wxString
& topic
)
159 m_conn
= MakeConnection(host
, service
, topic
);
161 return m_conn
!= NULL
;
173 wxConnectionBase
& GetConn() const
175 CPPUNIT_ASSERT( m_conn
);
181 wxConnectionBase
*m_conn
;
183 DECLARE_NO_COPY_CLASS(IPCTestClient
)
186 static IPCTestClient gs_client
;
188 // ----------------------------------------------------------------------------
189 // the test code itself
190 // ----------------------------------------------------------------------------
192 class IPCTestCase
: public CppUnit::TestCase
198 CPPUNIT_TEST_SUITE( IPCTestCase
);
199 CPPUNIT_TEST( Connect
);
200 CPPUNIT_TEST( Execute
);
201 CPPUNIT_TEST( Disconnect
);
202 CPPUNIT_TEST_SUITE_END();
208 DECLARE_NO_COPY_CLASS(IPCTestCase
)
211 CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase
);
212 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase
, "IPCTestCase" );
214 void IPCTestCase::Connect()
216 gs_server
= new IPCTestServer
;
218 // connecting to the wrong port should fail
219 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", "2424", IPC_TEST_TOPIC
) );
221 // connecting using an unsupported topic should fail (unless the server
222 // expects a ROT-13'd topic name...)
223 CPPUNIT_ASSERT( !gs_client
.Connect("localhost", IPC_TEST_PORT
, "VCP GRFG") );
225 // connecting to the right port on the right topic should succeed
226 CPPUNIT_ASSERT( gs_client
.Connect("localhost", IPC_TEST_PORT
, IPC_TEST_TOPIC
) );
229 void IPCTestCase::Execute()
231 wxConnectionBase
& conn
= gs_client
.GetConn();
233 const wxString
s("Date");
234 CPPUNIT_ASSERT( conn
.Execute(s
) );
235 CPPUNIT_ASSERT( conn
.Execute(s
.mb_str(), s
.length() + 1) );
237 char bytes
[] = { 1, 2, 3 };
238 CPPUNIT_ASSERT( conn
.Execute(bytes
, WXSIZEOF(bytes
)) );
241 void IPCTestCase::Disconnect()
243 gs_client
.Disconnect();
252 #endif // wxUSE_THREADS