barrier/src/test/unittests/client/CServerProxyTests.cpp

118 lines
3.4 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Bolton Software Ltd.
* Copyright (C) 2011 Nick Bolton
2012-06-10 16:50:54 +00:00
*
* 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/>.
*/
#define TEST_ENV
#include <gtest/gtest.h>
2012-06-10 16:50:54 +00:00
#include "CServerProxy.h"
#include "CMockClient.h"
#include "CMockStream.h"
#include "CMockEventQueue.h"
#include "ProtocolTypes.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::AnyNumber;
using ::testing::ReturnRef;
2012-06-10 16:50:54 +00:00
const UInt8 g_mouseMove_bufferLen = 16;
UInt8 g_mouseMove_buffer[g_mouseMove_bufferLen];
UInt32 g_mouseMove_bufferIndex;
UInt32 mouseMove_mockRead(void* buffer, UInt32 n);
2012-06-10 16:50:54 +00:00
const UInt8 g_readCryptoIv_bufferLen = 20;
UInt8 g_readCryptoIv_buffer[g_readCryptoIv_bufferLen];
UInt32 g_readCryptoIv_bufferIndex;
CString g_readCryptoIv_result;
2013-05-01 15:46:15 +00:00
UInt32 readCryptoIv_mockRead(void* buffer, UInt32 n);
void readCryptoIv_setDecryptIv(const UInt8*);
2012-06-10 16:50:54 +00:00
TEST(CServerProxyTests, mouseMove)
2012-06-10 16:50:54 +00:00
{
g_mouseMove_bufferIndex = 0;
2012-06-10 16:50:54 +00:00
NiceMock<CMockEventQueue> eventQueue;
NiceMock<CMockStream> stream;
NiceMock<CMockClient> client;
IStreamEvents streamEvents;
streamEvents.setEvents(&eventQueue);
ON_CALL(eventQueue, forIStream()).WillByDefault(ReturnRef(streamEvents));
ON_CALL(stream, read(_, _)).WillByDefault(Invoke(mouseMove_mockRead));
EXPECT_CALL(client, mouseMove(1, 2)).Times(1);
const char data[] = "DSOP\0\0\0\0DMMV\0\1\0\2";
memcpy(g_mouseMove_buffer, data, g_mouseMove_bufferLen);
CServerProxy serverProxy(&client, &stream, &eventQueue);
serverProxy.handleDataForTest();
}
2013-05-01 15:46:15 +00:00
TEST(CServerProxyTests, readCryptoIv)
{
g_readCryptoIv_bufferIndex = 0;
NiceMock<CMockEventQueue> eventQueue;
NiceMock<CMockClient> client;
NiceMock<CMockStream> stream;
IStreamEvents streamEvents;
streamEvents.setEvents(&eventQueue);
ON_CALL(eventQueue, forIStream()).WillByDefault(ReturnRef(streamEvents));
2013-05-01 15:46:15 +00:00
ON_CALL(stream, read(_, _)).WillByDefault(Invoke(readCryptoIv_mockRead));
ON_CALL(client, setDecryptIv(_)).WillByDefault(Invoke(readCryptoIv_setDecryptIv));
2012-06-10 16:50:54 +00:00
const char data[] = "DSOP\0\0\0\0DCIV\0\0\0\4mock";
memcpy(g_readCryptoIv_buffer, data, g_readCryptoIv_bufferLen);
2012-06-10 16:50:54 +00:00
CServerProxy serverProxy(&client, &stream, &eventQueue);
serverProxy.handleDataForTest();
2012-06-10 16:50:54 +00:00
EXPECT_EQ("mock", g_readCryptoIv_result);
2012-06-10 16:50:54 +00:00
}
UInt32
mouseMove_mockRead(void* buffer, UInt32 n)
2012-06-10 16:50:54 +00:00
{
if (g_mouseMove_bufferIndex >= g_mouseMove_bufferLen) {
return 0;
2012-06-10 16:50:54 +00:00
}
memcpy(buffer, &g_mouseMove_buffer[g_mouseMove_bufferIndex], n);
g_mouseMove_bufferIndex += n;
return n;
}
UInt32
2013-05-01 15:46:15 +00:00
readCryptoIv_mockRead(void* buffer, UInt32 n)
{
if (g_readCryptoIv_bufferIndex >= g_readCryptoIv_bufferLen) {
return 0;
2012-06-10 16:50:54 +00:00
}
memcpy(buffer, &g_readCryptoIv_buffer[g_readCryptoIv_bufferIndex], n);
g_readCryptoIv_bufferIndex += n;
return n;
}
2012-06-10 16:50:54 +00:00
void
2013-05-01 15:46:15 +00:00
readCryptoIv_setDecryptIv(const UInt8* data)
{
g_readCryptoIv_result = reinterpret_cast<const char*>(data);
2012-06-10 16:50:54 +00:00
}