2008-07-13 22:19:34 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/net/ipc.cpp
|
|
|
|
// Purpose: IPC classes unit tests
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin
|
|
|
|
// Licence: wxWidgets licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
// and "wx/cppunit.h"
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2008-07-14 11:12:58 -04:00
|
|
|
// this test needs threads as it runs the test server in a secondary thread
|
|
|
|
#if wxUSE_THREADS
|
|
|
|
|
2008-07-13 22:19:34 -04:00
|
|
|
// for all others, include the necessary headers
|
|
|
|
#ifndef WX_PRECOMP
|
2008-07-14 11:12:58 -04:00
|
|
|
#include "wx/app.h"
|
2008-07-13 22:19:34 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/ipc.h"
|
|
|
|
#include "wx/thread.h"
|
|
|
|
|
2008-07-14 17:01:41 -04:00
|
|
|
#define wxUSE_SOCKETS_FOR_IPC (!wxUSE_DDE_FOR_IPC)
|
|
|
|
|
2008-07-13 22:19:34 -04:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
const char *IPC_TEST_PORT = "4242";
|
|
|
|
const char *IPC_TEST_TOPIC = "IPC TEST";
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test connection class used by IPCTestServer
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class IPCTestConnection : public wxConnection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IPCTestConnection() { }
|
|
|
|
|
|
|
|
virtual bool OnExec(const wxString& topic, const wxString& data)
|
|
|
|
{
|
|
|
|
if ( topic != IPC_TEST_TOPIC )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return data == "Date";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DECLARE_NO_COPY_CLASS(IPCTestConnection)
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// event dispatching thread class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class EventThread : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventThread()
|
|
|
|
: wxThread(wxTHREAD_JOINABLE)
|
|
|
|
{
|
|
|
|
Create();
|
|
|
|
Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void *Entry()
|
|
|
|
{
|
|
|
|
wxTheApp->MainLoop();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(EventThread)
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test server class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class IPCTestServer : public wxServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IPCTestServer()
|
|
|
|
{
|
|
|
|
m_conn = NULL;
|
|
|
|
|
2008-07-14 17:01:41 -04:00
|
|
|
#if wxUSE_SOCKETS_FOR_IPC
|
2008-07-13 22:19:34 -04:00
|
|
|
// we must call this from the main thread
|
|
|
|
wxSocketBase::Initialize();
|
2008-07-14 17:01:41 -04:00
|
|
|
#endif // wxUSE_SOCKETS_FOR_IPC
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
// we need event dispatching to work for IPC server to work
|
|
|
|
m_thread = new EventThread;
|
|
|
|
|
|
|
|
Create(IPC_TEST_PORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~IPCTestServer()
|
|
|
|
{
|
|
|
|
wxTheApp->ExitMainLoop();
|
|
|
|
|
|
|
|
m_thread->Wait();
|
|
|
|
delete m_thread;
|
2008-07-14 11:12:58 -04:00
|
|
|
|
|
|
|
delete m_conn;
|
2008-07-13 22:19:34 -04:00
|
|
|
|
2008-07-14 17:01:41 -04:00
|
|
|
#if wxUSE_SOCKETS_FOR_IPC
|
2008-07-13 22:19:34 -04:00
|
|
|
wxSocketBase::Shutdown();
|
2008-07-14 17:01:41 -04:00
|
|
|
#endif // wxUSE_SOCKETS_FOR_IPC
|
2008-07-13 22:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic)
|
|
|
|
{
|
|
|
|
if ( topic != IPC_TEST_TOPIC )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m_conn = new IPCTestConnection;
|
|
|
|
return m_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EventThread *m_thread;
|
|
|
|
IPCTestConnection *m_conn;
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(IPCTestServer)
|
|
|
|
};
|
|
|
|
|
|
|
|
static IPCTestServer *gs_server = NULL;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test client class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class IPCTestClient : public wxClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IPCTestClient()
|
|
|
|
{
|
|
|
|
m_conn = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~IPCTestClient()
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Connect(const wxString& host, const wxString& service, const wxString& topic)
|
|
|
|
{
|
|
|
|
m_conn = MakeConnection(host, service, topic);
|
|
|
|
|
|
|
|
return m_conn != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect()
|
|
|
|
{
|
|
|
|
if ( m_conn )
|
|
|
|
{
|
|
|
|
delete m_conn;
|
|
|
|
m_conn = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxConnectionBase& GetConn() const
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT( m_conn );
|
|
|
|
|
|
|
|
return *m_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxConnectionBase *m_conn;
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(IPCTestClient)
|
|
|
|
};
|
|
|
|
|
2008-09-11 21:36:09 -04:00
|
|
|
static IPCTestClient *gs_client = NULL;
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the test code itself
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class IPCTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IPCTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( IPCTestCase );
|
|
|
|
CPPUNIT_TEST( Connect );
|
|
|
|
CPPUNIT_TEST( Execute );
|
|
|
|
CPPUNIT_TEST( Disconnect );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void Connect();
|
|
|
|
void Execute();
|
|
|
|
void Disconnect();
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(IPCTestCase)
|
|
|
|
};
|
|
|
|
|
2008-07-14 17:01:41 -04:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( IPCTestCase );
|
2008-07-13 22:19:34 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( IPCTestCase, "IPCTestCase" );
|
|
|
|
|
|
|
|
void IPCTestCase::Connect()
|
|
|
|
{
|
|
|
|
gs_server = new IPCTestServer;
|
2008-09-11 21:36:09 -04:00
|
|
|
gs_client = new IPCTestClient;
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
// connecting to the wrong port should fail
|
2008-09-11 21:36:09 -04:00
|
|
|
CPPUNIT_ASSERT( !gs_client->Connect("localhost", "2424", IPC_TEST_TOPIC) );
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
// connecting using an unsupported topic should fail (unless the server
|
|
|
|
// expects a ROT-13'd topic name...)
|
2008-09-11 21:36:09 -04:00
|
|
|
CPPUNIT_ASSERT( !gs_client->Connect("localhost", IPC_TEST_PORT, "VCP GRFG") );
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
// connecting to the right port on the right topic should succeed
|
2008-09-11 21:36:09 -04:00
|
|
|
CPPUNIT_ASSERT( gs_client->Connect("localhost", IPC_TEST_PORT, IPC_TEST_TOPIC) );
|
2008-07-13 22:19:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void IPCTestCase::Execute()
|
|
|
|
{
|
2008-09-11 21:36:09 -04:00
|
|
|
wxConnectionBase& conn = gs_client->GetConn();
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
const wxString s("Date");
|
|
|
|
CPPUNIT_ASSERT( conn.Execute(s) );
|
|
|
|
CPPUNIT_ASSERT( conn.Execute(s.mb_str(), s.length() + 1) );
|
|
|
|
|
|
|
|
char bytes[] = { 1, 2, 3 };
|
|
|
|
CPPUNIT_ASSERT( conn.Execute(bytes, WXSIZEOF(bytes)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCTestCase::Disconnect()
|
|
|
|
{
|
2008-09-11 21:36:09 -04:00
|
|
|
if ( gs_client )
|
|
|
|
{
|
|
|
|
gs_client->Disconnect();
|
|
|
|
delete gs_client;
|
|
|
|
gs_client = NULL;
|
|
|
|
}
|
2008-07-13 22:19:34 -04:00
|
|
|
|
|
|
|
if ( gs_server )
|
|
|
|
{
|
|
|
|
delete gs_server;
|
|
|
|
gs_server = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // wxUSE_THREADS
|