Patch by Jerry for issue 46:

- Unit test for sending file data from server to client.
- Removed singleton pattern from CSocketMultiplexer for easier unit testing.
- Incremented protocol version from 1.4 to 1.5 (new file chunk message).
- Storing pointer to CConfig instead of copying in CServer (so we can mock it).
- Created a common event queue for testing (timeout, quit event, etc).
- Fixed code style.
This commit is contained in:
Nick Bolton
2013-07-16 19:02:30 +00:00
parent 6f97f1d186
commit c368013f13
56 changed files with 830 additions and 165 deletions

View File

@@ -35,7 +35,7 @@
#include "CString.h"
#include "CIpcServerProxy.h"
#include "CIpcMessage.h"
#include "CSimpleEventQueueBuffer.h"
#include "CTestEventQueue.h"
#define TEST_IPC_PORT 24802
@@ -49,18 +49,9 @@ public:
void sendMessageToServer_serverHandleMessageReceived(const CEvent&, void*);
void sendMessageToClient_serverHandleClientConnected(const CEvent&, void*);
void sendMessageToClient_clientHandleMessageReceived(const CEvent&, void*);
void handleQuitTimeout(const CEvent&, void* vclient);
void raiseQuitEvent();
void initQuitTimeout(double timeout);
void cleanupQuitTimeout();
private:
void timeoutThread(void*);
public:
CSocketMultiplexer m_multiplexer;
CEventQueue m_events;
CEventQueueTimer* m_quitTimeoutTimer;
bool m_connectToServer_helloMessageReceived;
bool m_connectToServer_hasClientNode;
CIpcServer* m_connectToServer_server;
@@ -68,12 +59,14 @@ public:
CString m_sendMessageToClient_receivedString;
CIpcClient* m_sendMessageToServer_client;
CIpcServer* m_sendMessageToClient_server;
CTestEventQueue m_events;
};
TEST_F(CIpcTests, connectToServer)
{
CIpcServer server(&m_events, TEST_IPC_PORT);
CSocketMultiplexer socketMultiplexer;
CIpcServer server(&m_events, &socketMultiplexer, TEST_IPC_PORT);
server.listen();
m_connectToServer_server = &server;
@@ -82,13 +75,13 @@ TEST_F(CIpcTests, connectToServer)
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::connectToServer_handleMessageReceived));
CIpcClient client(&m_events, TEST_IPC_PORT);
CIpcClient client(&m_events, &socketMultiplexer, TEST_IPC_PORT);
client.connect();
initQuitTimeout(5);
m_events.initQuitTimeout(5);
m_events.loop();
m_events.removeHandler(m_events.forCIpcServer().messageReceived(), &server);
cleanupQuitTimeout();
m_events.cleanupQuitTimeout();
EXPECT_EQ(true, m_connectToServer_helloMessageReceived);
EXPECT_EQ(true, m_connectToServer_hasClientNode);
@@ -96,7 +89,8 @@ TEST_F(CIpcTests, connectToServer)
TEST_F(CIpcTests, sendMessageToServer)
{
CIpcServer server(&m_events, TEST_IPC_PORT);
CSocketMultiplexer socketMultiplexer;
CIpcServer server(&m_events, &socketMultiplexer, TEST_IPC_PORT);
server.listen();
// event handler sends "test" command to server.
@@ -105,21 +99,22 @@ TEST_F(CIpcTests, sendMessageToServer)
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::sendMessageToServer_serverHandleMessageReceived));
CIpcClient client(&m_events, TEST_IPC_PORT);
CIpcClient client(&m_events, &socketMultiplexer, TEST_IPC_PORT);
client.connect();
m_sendMessageToServer_client = &client;
initQuitTimeout(5);
m_events.initQuitTimeout(5);
m_events.loop();
m_events.removeHandler(m_events.forCIpcServer().messageReceived(), &server);
cleanupQuitTimeout();
m_events.cleanupQuitTimeout();
EXPECT_EQ("test", m_sendMessageToServer_receivedString);
}
TEST_F(CIpcTests, sendMessageToClient)
{
CIpcServer server(&m_events, TEST_IPC_PORT);
CSocketMultiplexer socketMultiplexer;
CIpcServer server(&m_events, &socketMultiplexer, TEST_IPC_PORT);
server.listen();
m_sendMessageToClient_server = &server;
@@ -129,7 +124,7 @@ TEST_F(CIpcTests, sendMessageToClient)
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::sendMessageToClient_serverHandleClientConnected));
CIpcClient client(&m_events, TEST_IPC_PORT);
CIpcClient client(&m_events, &socketMultiplexer, TEST_IPC_PORT);
client.connect();
m_events.adoptHandler(
@@ -137,17 +132,16 @@ TEST_F(CIpcTests, sendMessageToClient)
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::sendMessageToClient_clientHandleMessageReceived));
initQuitTimeout(5);
m_events.initQuitTimeout(5);
m_events.loop();
m_events.removeHandler(m_events.forCIpcServer().messageReceived(), &server);
m_events.removeHandler(m_events.forCIpcClient().messageReceived(), &client);
cleanupQuitTimeout();
m_events.cleanupQuitTimeout();
EXPECT_EQ("test", m_sendMessageToClient_receivedString);
}
CIpcTests::CIpcTests() :
m_quitTimeoutTimer(nullptr),
m_connectToServer_helloMessageReceived(false),
m_connectToServer_hasClientNode(false),
m_connectToServer_server(nullptr),
@@ -168,7 +162,7 @@ CIpcTests::connectToServer_handleMessageReceived(const CEvent& e, void*)
m_connectToServer_hasClientNode =
m_connectToServer_server->hasClients(kIpcClientNode);
m_connectToServer_helloMessageReceived = true;
raiseQuitEvent();
m_events.raiseQuitEvent();
}
}
@@ -185,7 +179,7 @@ CIpcTests::sendMessageToServer_serverHandleMessageReceived(const CEvent& e, void
CIpcCommandMessage* cm = static_cast<CIpcCommandMessage*>(m);
LOG((CLOG_DEBUG "got ipc command message, %d", cm->command().c_str()));
m_sendMessageToServer_receivedString = cm->command();
raiseQuitEvent();
m_events.raiseQuitEvent();
}
}
@@ -208,37 +202,6 @@ CIpcTests::sendMessageToClient_clientHandleMessageReceived(const CEvent& e, void
CIpcLogLineMessage* llm = static_cast<CIpcLogLineMessage*>(m);
LOG((CLOG_DEBUG "got ipc log message, %d", llm->logLine().c_str()));
m_sendMessageToClient_receivedString = llm->logLine();
raiseQuitEvent();
m_events.raiseQuitEvent();
}
}
void
CIpcTests::raiseQuitEvent()
{
m_events.addEvent(CEvent(CEvent::kQuit));
}
void
CIpcTests::initQuitTimeout(double timeout)
{
assert(m_quitTimeoutTimer == nullptr);
m_quitTimeoutTimer = m_events.newOneShotTimer(timeout, NULL);
m_events.adoptHandler(CEvent::kTimer, m_quitTimeoutTimer,
new TMethodEventJob<CIpcTests>(
this, &CIpcTests::handleQuitTimeout));
}
void
CIpcTests::cleanupQuitTimeout()
{
m_events.removeHandler(CEvent::kTimer, m_quitTimeoutTimer);
delete m_quitTimeoutTimer;
m_quitTimeoutTimer = nullptr;
}
void
CIpcTests::handleQuitTimeout(const CEvent&, void* vclient)
{
LOG((CLOG_ERR "timeout"));
raiseQuitEvent();
}

View File

@@ -14,9 +14,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(h
CTestEventQueue.h
)
set(src
${h}
Main.cpp
CIpcTests.cpp
NetworkTests.cpp
CTestEventQueue.cpp
)
if (WIN32)
@@ -50,11 +57,11 @@ set(inc
../../lib/mt
../../lib/net
../../lib/platform
../../lib/server
../../lib/synergy
../../../tools/gtest-1.6.0/include
../../../tools/gmock-1.6.0/include
../unittests
../unittests/synergy
)
if (UNIX)
@@ -72,4 +79,4 @@ endif()
include_directories(${inc})
add_executable(integtests ${src})
target_link_libraries(integtests
arch base client common io ipc mt net platform server synergy gtest gmock ${libs})
arch base client common io ipc mt net platform server synergy gtest gmock cryptopp ${libs})

View File

@@ -0,0 +1,52 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CTestEventQueue.h"
#include "CLog.h"
#include "TMethodEventJob.h"
#include "CSimpleEventQueueBuffer.h"
void
CTestEventQueue::raiseQuitEvent()
{
addEvent(CEvent(CEvent::kQuit));
}
void
CTestEventQueue::initQuitTimeout(double timeout)
{
assert(m_quitTimeoutTimer == nullptr);
m_quitTimeoutTimer = newOneShotTimer(timeout, NULL);
adoptHandler(CEvent::kTimer, m_quitTimeoutTimer,
new TMethodEventJob<CTestEventQueue>(
this, &CTestEventQueue::handleQuitTimeout));
}
void
CTestEventQueue::cleanupQuitTimeout()
{
removeHandler(CEvent::kTimer, m_quitTimeoutTimer);
delete m_quitTimeoutTimer;
m_quitTimeoutTimer = nullptr;
}
void
CTestEventQueue::handleQuitTimeout(const CEvent&, void* vclient)
{
LOG((CLOG_ERR "timeout"));
raiseQuitEvent();
}

View File

@@ -0,0 +1,38 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "CEventQueue.h"
class CEventQueueTimer;
class CTestEventQueue : public CEventQueue {
public:
CTestEventQueue() : m_quitTimeoutTimer(nullptr) { }
void handleQuitTimeout(const CEvent&, void* vclient);
void raiseQuitEvent();
void initQuitTimeout(double timeout);
void cleanupQuitTimeout();
private:
void timeoutThread(void*);
private:
CEventQueueTimer* m_quitTimeoutTimer;
};

View File

@@ -0,0 +1,188 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <iostream>
#include <fstream>
#define TEST_ENV
#include "CLog.h"
#include "CServer.h"
#include "CClient.h"
#include "TMethodEventJob.h"
#include "server/CMockConfig.h"
#include "server/CMockPrimaryClient.h"
#include "synergy/CMockScreen.h"
#include "CClientListener.h"
#include "CNetworkAddress.h"
#include "CTCPSocketFactory.h"
#include "CCryptoOptions.h"
#include "CSocketMultiplexer.h"
#include "CMSWindowsScreen.h"
#include "CGameDevice.h"
#include "CThread.h"
#include "TMethodJob.h"
#include "CTestEventQueue.h"
#include "server/CMockInputFilter.h"
using ::testing::_;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::Invoke;
#define TEST_PORT 24803
#define TEST_HOST "localhost"
const int klargeDataSize = 512;
char g_largeData[klargeDataSize] = "large data:head.1221412312341244213123fdsfasdawdwadwadacwdd.12321412312341244213123fdsfasdawdwadwadacwdawddawdwacawdawd232141231awddawdwacawdawd2321412312341244213123fdsfasdawdwadacwdawddawdwacawdtrtetawdawdwaewe1213412321412312341244213123fdsfasdawdwadacwdawddawdwacawdawdawdwaewe121341awdwaewedacwdawddawdwacawdawd2321412312341244213123fdsfasdawdwadacwdawddawdwacawdtrtetawdawdwaewe1213412321412312341244213123fdsfasdawdwadacwdawddawdwacawdawdawdwaewe121341awdwaewe12134123njk1u31i2nm3e123hu23oi132213njk.tail";
void sendFileToClient_getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h);
void sendFileToClient_getCursorPos(SInt32& x, SInt32& y);
class NetworkTests : public ::testing::Test
{
public:
NetworkTests() { }
void sendData(CServer* server);
void sendFileToClient_handleClientConnected(const CEvent&, void* vlistener);
void sendFileToClient_fileRecieveComplete(const CEvent&, void*);
public:
CTestEventQueue m_events;
};
TEST_F(NetworkTests, sendFileToClient)
{
// server and client
CNetworkAddress serverAddress(TEST_HOST, TEST_PORT);
CCryptoOptions cryptoOptions;
serverAddress.resolve();
// server
CSocketMultiplexer serverSocketMultiplexer;
CTCPSocketFactory* serverSocketFactory = new CTCPSocketFactory(&m_events, &serverSocketMultiplexer);
CClientListener listener(serverAddress, serverSocketFactory, NULL, cryptoOptions, &m_events);
NiceMock<CMockScreen> serverScreen;
NiceMock<CMockPrimaryClient> primaryClient;
NiceMock<CMockConfig> serverConfig;
NiceMock<CMockInputFilter> serverInputFilter;
m_events.adoptHandler(
m_events.forCClientListener().connected(), &listener,
new TMethodEventJob<NetworkTests>(
this, &NetworkTests::sendFileToClient_handleClientConnected, &listener));
ON_CALL(serverConfig, isScreen(_)).WillByDefault(Return(true));
ON_CALL(serverConfig, getInputFilter()).WillByDefault(Return(&serverInputFilter));
CServer server(serverConfig, &primaryClient, &serverScreen, &m_events);
server.m_mock = true;
listener.setServer(&server);
// client
NiceMock<CMockScreen> clientScreen;
CSocketMultiplexer clientSocketMultiplexer;
CTCPSocketFactory* clientSocketFactory = new CTCPSocketFactory(&m_events, &clientSocketMultiplexer);
ON_CALL(clientScreen, getShape(_, _, _, _)).WillByDefault(Invoke(sendFileToClient_getShape));
ON_CALL(clientScreen, getCursorPos(_, _)).WillByDefault(Invoke(sendFileToClient_getCursorPos));
CClient client(&m_events, "stub", serverAddress, clientSocketFactory, NULL, &clientScreen, cryptoOptions);
m_events.adoptHandler(
m_events.forIScreen().fileRecieveComplete(), &client,
new TMethodEventJob<NetworkTests>(
this, &NetworkTests::sendFileToClient_fileRecieveComplete));
client.connect();
m_events.initQuitTimeout(10);
m_events.loop();
m_events.cleanupQuitTimeout();
}
void
NetworkTests::sendFileToClient_handleClientConnected(const CEvent&, void* vlistener)
{
CClientListener* listener = reinterpret_cast<CClientListener*>(vlistener);
CServer* server = listener->getServer();
CClientProxy* client = listener->getNextClient();
if (client == NULL) {
throw std::exception("client is null");
}
CBaseClientProxy* bcp = reinterpret_cast<CBaseClientProxy*>(client);
server->adoptClient(bcp);
server->setActive(bcp);
sendData(server);
}
void
NetworkTests::sendFileToClient_fileRecieveComplete(const CEvent& event, void*)
{
CClient* client = reinterpret_cast<CClient*>(event.getTarget());
EXPECT_TRUE(client->isReceivedFileSizeValid());
m_events.raiseQuitEvent();
}
void
NetworkTests::sendData(CServer* server)
{
UInt8* largeDataSize = new UInt8[5];
largeDataSize[0] = '0';
largeDataSize[1] = '5';
largeDataSize[2] = '1';
largeDataSize[3] = '1';
largeDataSize[4] = '\0';
// transfer data from server -> client
m_events.addEvent(CEvent(m_events.forIScreen().fileChunkSending(), server, largeDataSize));
UInt8* largeData = new UInt8[klargeDataSize + 1];
largeData[0] = '1';
memcpy(&largeData[1], g_largeData, klargeDataSize);
m_events.addEvent(CEvent(m_events.forIScreen().fileChunkSending(), server, (UInt8*)largeData));
UInt8* transferFinished = new UInt8[2];
transferFinished[0] = '2';
transferFinished[1] = '\0';
m_events.addEvent(CEvent(m_events.forIScreen().fileChunkSending(), server, transferFinished));
}
void
sendFileToClient_getShape(SInt32& x, SInt32& y, SInt32& w, SInt32& h)
{
x = 0;
y = 0;
w = 1;
h = 1;
}
void
sendFileToClient_getCursorPos(SInt32& x, SInt32& y)
{
x = 0;
y = 0;
}

View File

@@ -27,8 +27,8 @@
#include "CMSWindowsScreen.h"
#include "CMSWindowsScreenSaver.h"
#include "TMethodJob.h"
#include "CMockEventQueue.h"
#include "CMockKeyMap.h"
#include "synergy/CMockEventQueue.h"
#include "synergy/CMockKeyMap.h"
// wParam = flags, HIBYTE(lParam) = virtual key, LOBYTE(lParam) = scan code
#define SYNERGY_MSG_FAKE_KEY SYNERGY_HOOK_LAST_MSG + 4

View File

@@ -22,6 +22,10 @@ set(h
io/CMockStream.h
server/CMockServer.h
io/CMockCryptoStream.h
synergy/CMockScreen.h
server/CMockConfig.h
server/CMockPrimaryClient.h
server/CMockInputFilter.h
)
set(src

View File

@@ -26,7 +26,6 @@ class CMockCryptoStream : public CCryptoStream
public:
CMockCryptoStream(IEventQueue* eventQueue, IStream* stream) :
CCryptoStream(eventQueue, stream, CCryptoOptions("gcm", "stub"), false) { }
MOCK_METHOD2(read, UInt32(void*, UInt32));
MOCK_METHOD2(write, void(const void*, UInt32));
};

View File

@@ -0,0 +1,31 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#define TEST_ENV
#include "CConfig.h"
class CMockConfig : public CConfig
{
public:
CMockConfig() : CConfig() { }
MOCK_METHOD0(getInputFilter, CInputFilter*());
MOCK_CONST_METHOD1(isScreen, bool(const CString&));
};

View File

@@ -0,0 +1,29 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#define TEST_ENV
#include "CInputFilter.h"
class CMockInputFilter : public CInputFilter
{
public:
MOCK_METHOD1(setPrimaryClient, void(CPrimaryClient*));
};

View File

@@ -0,0 +1,40 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#define TEST_ENV
#include "CPrimaryClient.h"
#include "CString.h"
class CMockPrimaryClient : public CPrimaryClient
{
public:
MOCK_CONST_METHOD0(getEventTarget, void*());
MOCK_CONST_METHOD2(getCursorPos, void(SInt32&, SInt32&));
MOCK_CONST_METHOD2(setJumpCursorPos, void(SInt32, SInt32));
MOCK_METHOD1(reconfigure, void(UInt32));
MOCK_METHOD0(resetOptions, void());
MOCK_METHOD1(setOptions, void(const COptionsList&));
MOCK_METHOD0(enable, void());
MOCK_METHOD0(disable, void());
MOCK_METHOD2(registerHotKey, UInt32(KeyID, KeyModifierMask));
MOCK_CONST_METHOD0(getToggleMask, KeyModifierMask());
MOCK_METHOD1(unregisterHotKey, void(UInt32));
};

View File

@@ -0,0 +1,35 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2013 Bolton Software Ltd.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gmock/gmock.h>
#define TEST_ENV
#include "CScreen.h"
class CMockScreen : public CScreen
{
public:
CMockScreen() : CScreen() { }
MOCK_METHOD0(disable, void());
MOCK_CONST_METHOD4(getShape, void(SInt32&, SInt32&, SInt32&, SInt32&));
MOCK_CONST_METHOD2(getCursorPos, void(SInt32&, SInt32&));
MOCK_METHOD0(resetOptions, void());
MOCK_METHOD1(setOptions, void(const COptionsList&));
MOCK_METHOD0(enable, void());
};