]> git.saurik.com Git - wxWidgets.git/blob - tests/net/ipc.cpp
PCH-less build fix
[wxWidgets.git] / tests / net / ipc.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/net/ipc.cpp
3 // Purpose: IPC classes unit tests
4 // Author: Vadim Zeitlin
5 // RCS-ID: $Id$
6 // Copyright: (c) 2008 Vadim Zeitlin
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 // and "wx/cppunit.h"
12 #include "testprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // this test needs threads as it runs the test server in a secondary thread
19 #if wxUSE_THREADS
20
21 // for all others, include the necessary headers
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #endif
25
26 #include "wx/ipc.h"
27 #include "wx/thread.h"
28
29 namespace
30 {
31
32 const char *IPC_TEST_PORT = "4242";
33 const char *IPC_TEST_TOPIC = "IPC TEST";
34
35 } // anonymous namespace
36
37 // ----------------------------------------------------------------------------
38 // test connection class used by IPCTestServer
39 // ----------------------------------------------------------------------------
40
41 class IPCTestConnection : public wxConnection
42 {
43 public:
44 IPCTestConnection() { }
45
46 virtual bool OnExec(const wxString& topic, const wxString& data)
47 {
48 if ( topic != IPC_TEST_TOPIC )
49 return false;
50
51 return data == "Date";
52 }
53
54 private:
55 DECLARE_NO_COPY_CLASS(IPCTestConnection)
56 };
57
58 // ----------------------------------------------------------------------------
59 // event dispatching thread class
60 // ----------------------------------------------------------------------------
61
62 class EventThread : public wxThread
63 {
64 public:
65 EventThread()
66 : wxThread(wxTHREAD_JOINABLE)
67 {
68 Create();
69 Run();
70 }
71
72 protected:
73 virtual void *Entry()
74 {
75 wxTheApp->MainLoop();
76
77 return NULL;
78 }
79
80 DECLARE_NO_COPY_CLASS(EventThread)
81 };
82
83 // ----------------------------------------------------------------------------
84 // test server class
85 // ----------------------------------------------------------------------------
86
87 class IPCTestServer : public wxServer
88 {
89 public:
90 IPCTestServer()
91 {
92 m_conn = NULL;
93
94 // we must call this from the main thread
95 wxSocketBase::Initialize();
96
97 // we need event dispatching to work for IPC server to work
98 m_thread = new EventThread;
99
100 Create(IPC_TEST_PORT);
101 }
102
103 virtual ~IPCTestServer()
104 {
105 wxTheApp->ExitMainLoop();
106
107 m_thread->Wait();
108 delete m_thread;
109
110 delete m_conn;
111
112 wxSocketBase::Shutdown();
113 }
114
115 virtual wxConnectionBase *OnAcceptConnection(const wxString& topic)
116 {
117 if ( topic != IPC_TEST_TOPIC )
118 return NULL;
119
120 m_conn = new IPCTestConnection;
121 return m_conn;
122 }
123
124 private:
125 EventThread *m_thread;
126 IPCTestConnection *m_conn;
127
128 DECLARE_NO_COPY_CLASS(IPCTestServer)
129 };
130
131 static IPCTestServer *gs_server = NULL;
132
133 // ----------------------------------------------------------------------------
134 // test client class
135 // ----------------------------------------------------------------------------
136
137 class IPCTestClient : public wxClient
138 {
139 public:
140 IPCTestClient()
141 {
142 m_conn = NULL;
143 }
144
145 virtual ~IPCTestClient()
146 {
147 Disconnect();
148 }
149
150 bool
151 Connect(const wxString& host, const wxString& service, const wxString& topic)
152 {
153 m_conn = MakeConnection(host, service, topic);
154
155 return m_conn != NULL;
156 }
157
158 void Disconnect()
159 {
160 if ( m_conn )
161 {
162 delete m_conn;
163 m_conn = NULL;
164 }
165 }
166
167 wxConnectionBase& GetConn() const
168 {
169 CPPUNIT_ASSERT( m_conn );
170
171 return *m_conn;
172 }
173
174 private:
175 wxConnectionBase *m_conn;
176
177 DECLARE_NO_COPY_CLASS(IPCTestClient)
178 };
179
180 static IPCTestClient gs_client;
181
182 // ----------------------------------------------------------------------------
183 // the test code itself
184 // ----------------------------------------------------------------------------
185
186 class IPCTestCase : public CppUnit::TestCase
187 {
188 public:
189 IPCTestCase() { }
190
191 private:
192 CPPUNIT_TEST_SUITE( IPCTestCase );
193 CPPUNIT_TEST( Connect );
194 CPPUNIT_TEST( Execute );
195 CPPUNIT_TEST( Disconnect );
196 CPPUNIT_TEST_SUITE_END();
197
198 void Connect();
199 void Execute();
200 void Disconnect();
201
202 DECLARE_NO_COPY_CLASS(IPCTestCase)
203 };
204
205 // this test is not enabled by default because it requires an IPC server to run
206 //CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
207 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase, "IPCTestCase" );
208
209 void IPCTestCase::Connect()
210 {
211 gs_server = new IPCTestServer;
212
213 // connecting to the wrong port should fail
214 CPPUNIT_ASSERT( !gs_client.Connect("localhost", "2424", IPC_TEST_TOPIC) );
215
216 // connecting using an unsupported topic should fail (unless the server
217 // expects a ROT-13'd topic name...)
218 CPPUNIT_ASSERT( !gs_client.Connect("localhost", IPC_TEST_PORT, "VCP GRFG") );
219
220 // connecting to the right port on the right topic should succeed
221 CPPUNIT_ASSERT( gs_client.Connect("localhost", IPC_TEST_PORT, IPC_TEST_TOPIC) );
222 }
223
224 void IPCTestCase::Execute()
225 {
226 wxConnectionBase& conn = gs_client.GetConn();
227
228 const wxString s("Date");
229 CPPUNIT_ASSERT( conn.Execute(s) );
230 CPPUNIT_ASSERT( conn.Execute(s.mb_str(), s.length() + 1) );
231
232 char bytes[] = { 1, 2, 3 };
233 CPPUNIT_ASSERT( conn.Execute(bytes, WXSIZEOF(bytes)) );
234 }
235
236 void IPCTestCase::Disconnect()
237 {
238 gs_client.Disconnect();
239
240 if ( gs_server )
241 {
242 delete gs_server;
243 gs_server = NULL;
244 }
245 }
246
247 #endif // wxUSE_THREADS