diff options
Diffstat (limited to 'src/test/unittests')
| -rw-r--r-- | src/test/unittests/CMakeLists.txt | 71 | ||||
| -rw-r--r-- | src/test/unittests/Main.cpp | 50 | ||||
| -rw-r--r-- | src/test/unittests/barrier/ArgParserTests.cpp | 207 | ||||
| -rw-r--r-- | src/test/unittests/barrier/ClientArgsParsingTests.cpp | 95 | ||||
| -rw-r--r-- | src/test/unittests/barrier/ClipboardChunkTests.cpp | 128 | ||||
| -rw-r--r-- | src/test/unittests/barrier/ClipboardTests.cpp | 404 | ||||
| -rw-r--r-- | src/test/unittests/barrier/DeprecatedArgsParsingTests.cpp | 50 | ||||
| -rw-r--r-- | src/test/unittests/barrier/GenericArgsParsingTests.cpp | 315 | ||||
| -rw-r--r-- | src/test/unittests/barrier/KeyMapTests.cpp | 216 | ||||
| -rw-r--r-- | src/test/unittests/barrier/KeyStateTests.cpp | 488 | ||||
| -rw-r--r-- | src/test/unittests/barrier/ServerArgsParsingTests.cpp | 66 | ||||
| -rw-r--r-- | src/test/unittests/base/StringTests.cpp | 177 | ||||
| -rw-r--r-- | src/test/unittests/ipc/IpcLogOutputterTests.cpp | 165 | ||||
| -rw-r--r-- | src/test/unittests/platform/OSXKeyStateTests.cpp | 57 |
14 files changed, 2489 insertions, 0 deletions
diff --git a/src/test/unittests/CMakeLists.txt b/src/test/unittests/CMakeLists.txt new file mode 100644 index 0000000..3d21a2b --- /dev/null +++ b/src/test/unittests/CMakeLists.txt @@ -0,0 +1,71 @@ +# barrier -- mouse and keyboard sharing utility +# Copyright (C) 2012-2016 Symless Ltd. +# Copyright (C) 2009 Nick Bolton +# +# 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 LICENSE 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/>. + +file(GLOB_RECURSE headers "*.h") +file(GLOB_RECURSE sources "*.cpp") + +file(GLOB_RECURSE remove_platform "platform/*") +list(REMOVE_ITEM headers ${remove_platform}) +list(REMOVE_ITEM sources ${remove_platform}) + +file(GLOB_RECURSE global_headers "../../test/global/*.h") +file(GLOB_RECURSE global_sources "../../test/global/*.cpp") + +list(APPEND headers ${global_headers}) +list(APPEND sources ${global_sources}) + +file(GLOB_RECURSE mock_headers "../../test/mock/*.h") +file(GLOB_RECURSE mock_sources "../../test/mock/*.cpp") + +list(APPEND headers ${mock_headers}) +list(APPEND sources ${mock_sources}) + +# platform +if (WIN32) + file(GLOB platform_sources "platform/MSWindows*.cpp") + file(GLOB platform_headers "platform/MSWindows*.h") +elseif (APPLE) + file(GLOB platform_sources "platform/OSX*.cpp") + file(GLOB platform_headers "platform/OSX*.h") +elseif (UNIX) + file(GLOB platform_sources "platform/XWindows*.cpp") + file(GLOB platform_headers "platform/XWindows*.h") +endif() + +list(APPEND sources ${platform_headers}) +list(APPEND headers ${platform_sources}) + +include_directories( + ../../ + ../../lib/ + ../../../ext/gtest/include + ../../../ext/gmock/include + ../../../ext +) + +if (UNIX) + include_directories( + ../../.. + ) +endif() + +if (BARRIER_ADD_HEADERS) + list(APPEND sources ${headers}) +endif() + +add_executable(unittests ${sources}) +target_link_libraries(unittests + arch base client server common io net platform server barrier mt ipc gtest gmock ${libs} ${OPENSSL_LIBS}) diff --git a/src/test/unittests/Main.cpp b/src/test/unittests/Main.cpp new file mode 100644 index 0000000..7f0d0fe --- /dev/null +++ b/src/test/unittests/Main.cpp @@ -0,0 +1,50 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2011 Nick Bolton + * + * 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 LICENSE 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 "arch/Arch.h" +#include "base/Log.h" + +#if SYSAPI_WIN32 +#include "arch/win32/ArchMiscWindows.h" +#endif + +#include "test/global/gtest.h" + +int +main(int argc, char **argv) +{ +#if SYSAPI_WIN32 + // HACK: shouldn't be needed, but logging fails without this. + ArchMiscWindows::setInstanceWin32(GetModuleHandle(NULL)); +#endif + + Arch arch; + arch.init(); + + Log log; + log.setFilter(kDEBUG4); + + testing::InitGoogleTest(&argc, argv); + + // gtest seems to randomly finish with error codes (e.g. -1, -1073741819) + // even when no tests have failed. not sure what causes this, but it + // happens on all platforms and keeps leading to false positives. + // according to the documentation, 1 is a failure, so we should be + // able to trust that code. + return (RUN_ALL_TESTS() == 1) ? 1 : 0; +} diff --git a/src/test/unittests/barrier/ArgParserTests.cpp b/src/test/unittests/barrier/ArgParserTests.cpp new file mode 100644 index 0000000..e14877e --- /dev/null +++ b/src/test/unittests/barrier/ArgParserTests.cpp @@ -0,0 +1,207 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2014-2016 Symless 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 LICENSE 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 "barrier/ArgParser.h" +#include "barrier/ArgsBase.h" + +#include "test/global/gtest.h" + +TEST(ArgParserTests, isArg_abbreviationsArg_returnTrue) +{ + int i = 1; + const int argc = 2; + const char* argv[argc] = { "stub", "-t" }; + bool result = ArgParser::isArg(i, argc, argv, "-t", NULL); + + EXPECT_EQ(true, result); +} + +TEST(ArgParserTests, isArg_fullArg_returnTrue) +{ + int i = 1; + const int argc = 2; + const char* argv[argc] = { "stub", "--test" }; + bool result = ArgParser::isArg(i, argc, argv, NULL, "--test"); + + EXPECT_EQ(true, result); +} + +TEST(ArgParserTests, isArg_missingArgs_returnFalse) +{ + int i = 1; + const int argc = 2; + const char* argv[argc] = { "stub", "-t" }; + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + bool result = ArgParser::isArg(i, argc, argv, "-t", NULL, 1); + + EXPECT_FALSE(result); + EXPECT_EQ(true, argsBase.m_shouldExit); +} + +TEST(ArgParserTests, searchDoubleQuotes_doubleQuotedArg_returnTrue) +{ + String command("\"stub\""); + size_t left = 0; + size_t right = 0; + + bool result = ArgParser::searchDoubleQuotes(command, left, right); + + EXPECT_EQ(true, result); + EXPECT_EQ(0, left); + EXPECT_EQ(5, right); +} + +TEST(ArgParserTests, searchDoubleQuotes_noDoubleQuotedArg_returnfalse) +{ + String command("stub"); + size_t left = 0; + size_t right = 0; + + bool result = ArgParser::searchDoubleQuotes(command, left, right); + + EXPECT_FALSE(result); + EXPECT_EQ(0, left); + EXPECT_EQ(0, right); +} + +TEST(ArgParserTests, searchDoubleQuotes_oneDoubleQuoteArg_returnfalse) +{ + String command("\"stub"); + size_t left = 0; + size_t right = 0; + + bool result = ArgParser::searchDoubleQuotes(command, left, right); + + EXPECT_FALSE(result); + EXPECT_EQ(0, left); + EXPECT_EQ(0, right); +} + +TEST(ArgParserTests, splitCommandString_oneArg_returnArgv) +{ + String command("stub"); + std::vector<String> argv; + + ArgParser::splitCommandString(command, argv); + + EXPECT_EQ(1, argv.size()); + EXPECT_EQ("stub", argv.at(0)); +} + +TEST(ArgParserTests, splitCommandString_twoArgs_returnArgv) +{ + String command("stub1 stub2"); + std::vector<String> argv; + + ArgParser::splitCommandString(command, argv); + + EXPECT_EQ(2, argv.size()); + EXPECT_EQ("stub1", argv.at(0)); + EXPECT_EQ("stub2", argv.at(1)); +} + +TEST(ArgParserTests, splitCommandString_doubleQuotedArgs_returnArgv) +{ + String command("\"stub1\" stub2 \"stub3\""); + std::vector<String> argv; + + ArgParser::splitCommandString(command, argv); + + EXPECT_EQ(3, argv.size()); + EXPECT_EQ("stub1", argv.at(0)); + EXPECT_EQ("stub2", argv.at(1)); + EXPECT_EQ("stub3", argv.at(2)); +} + +TEST(ArgParserTests, splitCommandString_spaceDoubleQuotedArgs_returnArgv) +{ + String command("\"stub1\" stub2 \"stub3 space\""); + std::vector<String> argv; + + ArgParser::splitCommandString(command, argv); + + EXPECT_EQ(3, argv.size()); + EXPECT_EQ("stub1", argv.at(0)); + EXPECT_EQ("stub2", argv.at(1)); + EXPECT_EQ("stub3 space", argv.at(2)); +} + +TEST(ArgParserTests, getArgv_stringArray_return2DArray) +{ + std::vector<String> argArray; + argArray.push_back("stub1"); + argArray.push_back("stub2"); + argArray.push_back("stub3 space"); + const char** argv = ArgParser::getArgv(argArray); + + String row1(argv[0]); + String row2(argv[1]); + String row3(argv[2]); + + EXPECT_EQ("stub1", row1); + EXPECT_EQ("stub2", row2); + EXPECT_EQ("stub3 space", row3); + + delete[] argv; +} + +TEST(ArgParserTests, assembleCommand_stringArray_returnCommand) +{ + std::vector<String> argArray; + argArray.push_back("stub1"); + argArray.push_back("stub2"); + String command = ArgParser::assembleCommand(argArray); + + EXPECT_EQ("stub1 stub2", command); +} + +TEST(ArgParserTests, assembleCommand_ignoreSecondArg_returnCommand) +{ + std::vector<String> argArray; + argArray.push_back("stub1"); + argArray.push_back("stub2"); + String command = ArgParser::assembleCommand(argArray, "stub2"); + + EXPECT_EQ("stub1", command); +} + +TEST(ArgParserTests, assembleCommand_ignoreSecondArgWithOneParameter_returnCommand) +{ + std::vector<String> argArray; + argArray.push_back("stub1"); + argArray.push_back("stub2"); + argArray.push_back("stub3"); + argArray.push_back("stub4"); + String command = ArgParser::assembleCommand(argArray, "stub2", 1); + + EXPECT_EQ("stub1 stub4", command); +} + +TEST(ArgParserTests, assembleCommand_stringArrayWithSpace_returnCommand) +{ + std::vector<String> argArray; + argArray.push_back("stub1 space"); + argArray.push_back("stub2"); + argArray.push_back("stub3 space"); + String command = ArgParser::assembleCommand(argArray); + + EXPECT_EQ("\"stub1 space\" stub2 \"stub3 space\"", command); +} + diff --git a/src/test/unittests/barrier/ClientArgsParsingTests.cpp b/src/test/unittests/barrier/ClientArgsParsingTests.cpp new file mode 100644 index 0000000..5a1e7d0 --- /dev/null +++ b/src/test/unittests/barrier/ClientArgsParsingTests.cpp @@ -0,0 +1,95 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2014-2016 Symless 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 LICENSE 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 "barrier/ArgParser.h" +#include "barrier/ClientArgs.h" +#include "test/mock/barrier/MockArgParser.h" + +#include "test/global/gtest.h" + +using ::testing::_; +using ::testing::Invoke; +using ::testing::NiceMock; + +bool +client_stubParseGenericArgs(int, const char* const*, int&) +{ + return false; +} + +bool +client_stubCheckUnexpectedArgs() +{ + return false; +} + +TEST(ClientArgsParsingTests, parseClientArgs_yScrollArg_setYScroll) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs)); + ClientArgs clientArgs; + const int argc = 3; + const char* kYScrollCmd[argc] = { "stub", "--yscroll", "1" }; + + argParser.parseClientArgs(clientArgs, argc, kYScrollCmd); + + EXPECT_EQ(1, clientArgs.m_yscroll); +} + +TEST(ClientArgsParsingTests, parseClientArgs_addressArg_setBarrierAddress) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs)); + ClientArgs clientArgs; + const int argc = 2; + const char* kAddressCmd[argc] = { "stub", "mock_address" }; + + bool result = argParser.parseClientArgs(clientArgs, argc, kAddressCmd); + + EXPECT_EQ("mock_address", clientArgs.m_barrierAddress); + EXPECT_EQ(true, result); +} + +TEST(ClientArgsParsingTests, parseClientArgs_noAddressArg_returnFalse) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs)); + ClientArgs clientArgs; + const int argc = 1; + const char* kNoAddressCmd[argc] = { "stub" }; + + bool result = argParser.parseClientArgs(clientArgs, argc, kNoAddressCmd); + + EXPECT_FALSE(result); +} + +TEST(ClientArgsParsingTests, parseClientArgs_unrecognizedArg_returnFalse) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(client_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(client_stubCheckUnexpectedArgs)); + ClientArgs clientArgs; + const int argc = 3; + const char* kUnrecognizedCmd[argc] = { "stub", "mock_arg", "mock_address"}; + + bool result = argParser.parseClientArgs(clientArgs, argc, kUnrecognizedCmd); + + EXPECT_FALSE(result); +} diff --git a/src/test/unittests/barrier/ClipboardChunkTests.cpp b/src/test/unittests/barrier/ClipboardChunkTests.cpp new file mode 100644 index 0000000..9dc4a5e --- /dev/null +++ b/src/test/unittests/barrier/ClipboardChunkTests.cpp @@ -0,0 +1,128 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2015-2016 Symless 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 LICENSE 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 "barrier/ClipboardChunk.h" +#include "barrier/protocol_types.h" + +#include "test/global/gtest.h" + +TEST(ClipboardChunkTests, start_formatStartChunk) +{ +<<<<<<< HEAD + ClipboardID id = 0; + UInt32 sequence = 0; + String mockDataSize("10"); + ClipboardChunk* chunk = ClipboardChunk::start(id, sequence, mockDataSize); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, (UInt32)chunk->m_chunk[1]); + EXPECT_EQ(kDataStart, chunk->m_chunk[5]); + EXPECT_EQ('1', chunk->m_chunk[6]); + EXPECT_EQ('0', chunk->m_chunk[7]); + EXPECT_EQ('\0', chunk->m_chunk[8]); +======= + ClipboardID id = 0; + UInt32 sequence = 0; + String mockDataSize("10"); + ClipboardChunk* chunk = ClipboardChunk::start(id, sequence, mockDataSize); + UInt32 temp_m_chunk; + memcpy(&temp_m_chunk, &(chunk->m_chunk[1]), 4); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, temp_m_chunk); + EXPECT_EQ(kDataStart, chunk->m_chunk[5]); + EXPECT_EQ('1', chunk->m_chunk[6]); + EXPECT_EQ('0', chunk->m_chunk[7]); + EXPECT_EQ('\0', chunk->m_chunk[8]); +>>>>>>> master + + delete chunk; +} + +TEST(ClipboardChunkTests, data_formatDataChunk) +{ +<<<<<<< HEAD + ClipboardID id = 0; + UInt32 sequence = 1; + String mockData("mock data"); + ClipboardChunk* chunk = ClipboardChunk::data(id, sequence, mockData); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, (UInt32)chunk->m_chunk[1]); + EXPECT_EQ(kDataChunk, chunk->m_chunk[5]); + EXPECT_EQ('m', chunk->m_chunk[6]); + EXPECT_EQ('o', chunk->m_chunk[7]); + EXPECT_EQ('c', chunk->m_chunk[8]); + EXPECT_EQ('k', chunk->m_chunk[9]); + EXPECT_EQ(' ', chunk->m_chunk[10]); + EXPECT_EQ('d', chunk->m_chunk[11]); + EXPECT_EQ('a', chunk->m_chunk[12]); + EXPECT_EQ('t', chunk->m_chunk[13]); + EXPECT_EQ('a', chunk->m_chunk[14]); + EXPECT_EQ('\0', chunk->m_chunk[15]); +======= + ClipboardID id = 0; + UInt32 sequence = 1; + String mockData("mock data"); + ClipboardChunk* chunk = ClipboardChunk::data(id, sequence, mockData); + UInt32 temp_m_chunk; + memcpy(&temp_m_chunk, &(chunk->m_chunk[1]), 4); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, temp_m_chunk); + EXPECT_EQ(kDataChunk, chunk->m_chunk[5]); + EXPECT_EQ('m', chunk->m_chunk[6]); + EXPECT_EQ('o', chunk->m_chunk[7]); + EXPECT_EQ('c', chunk->m_chunk[8]); + EXPECT_EQ('k', chunk->m_chunk[9]); + EXPECT_EQ(' ', chunk->m_chunk[10]); + EXPECT_EQ('d', chunk->m_chunk[11]); + EXPECT_EQ('a', chunk->m_chunk[12]); + EXPECT_EQ('t', chunk->m_chunk[13]); + EXPECT_EQ('a', chunk->m_chunk[14]); + EXPECT_EQ('\0', chunk->m_chunk[15]); +>>>>>>> master + + delete chunk; +} + +TEST(ClipboardChunkTests, end_formatDataChunk) +{ +<<<<<<< HEAD + ClipboardID id = 1; + UInt32 sequence = 1; + ClipboardChunk* chunk = ClipboardChunk::end(id, sequence); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, (UInt32)chunk->m_chunk[1]); + EXPECT_EQ(kDataEnd, chunk->m_chunk[5]); + EXPECT_EQ('\0', chunk->m_chunk[6]); +======= + ClipboardID id = 1; + UInt32 sequence = 1; + ClipboardChunk* chunk = ClipboardChunk::end(id, sequence); + UInt32 temp_m_chunk; + memcpy(&temp_m_chunk, &(chunk->m_chunk[1]), 4); + + EXPECT_EQ(id, chunk->m_chunk[0]); + EXPECT_EQ(sequence, temp_m_chunk); + EXPECT_EQ(kDataEnd, chunk->m_chunk[5]); + EXPECT_EQ('\0', chunk->m_chunk[6]); +>>>>>>> master + + delete chunk; +} diff --git a/src/test/unittests/barrier/ClipboardTests.cpp b/src/test/unittests/barrier/ClipboardTests.cpp new file mode 100644 index 0000000..f710751 --- /dev/null +++ b/src/test/unittests/barrier/ClipboardTests.cpp @@ -0,0 +1,404 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2011 Nick Bolton + * + * 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 LICENSE 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 "barrier/Clipboard.h" + +#include "test/global/gtest.h" + +TEST(ClipboardTests, empty_openCalled_returnsTrue) +{ + Clipboard clipboard; + clipboard.open(0); + + bool actual = clipboard.empty(); + + EXPECT_EQ(true, actual); +} + +TEST(ClipboardTests, empty_singleFormat_hasReturnsFalse) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(Clipboard::kText, "barrier rocks!"); + + clipboard.empty(); + + bool actual = clipboard.has(Clipboard::kText); + EXPECT_FALSE(actual); +} + +TEST(ClipboardTests, add_newValue_valueWasStored) +{ + Clipboard clipboard; + clipboard.open(0); + + clipboard.add(IClipboard::kText, "barrier rocks!"); + + String actual = clipboard.get(IClipboard::kText); + EXPECT_EQ("barrier rocks!", actual); +} + +TEST(ClipboardTests, add_replaceValue_valueWasReplaced) +{ + Clipboard clipboard; + clipboard.open(0); + + clipboard.add(IClipboard::kText, "barrier rocks!"); + clipboard.add(IClipboard::kText, "maxivista sucks"); // haha, just kidding. + + String actual = clipboard.get(IClipboard::kText); + EXPECT_EQ("maxivista sucks", actual); +} + +TEST(ClipboardTests, open_timeIsZero_returnsTrue) +{ + Clipboard clipboard; + + bool actual = clipboard.open(0); + + EXPECT_EQ(true, actual); +} + +TEST(ClipboardTests, open_timeIsOne_returnsTrue) +{ + Clipboard clipboard; + + bool actual = clipboard.open(1); + + EXPECT_EQ(true, actual); +} + +TEST(ClipboardTests, close_isOpen_noErrors) +{ + Clipboard clipboard; + clipboard.open(0); + + clipboard.close(); + + // can't assert anything +} + +TEST(ClipboardTests, getTime_openWithNoEmpty_returnsZero) +{ + Clipboard clipboard; + clipboard.open(1); + + Clipboard::Time actual = clipboard.getTime(); + + EXPECT_EQ(0, actual); +} + +TEST(ClipboardTests, getTime_openAndEmpty_returnsOne) +{ + Clipboard clipboard; + clipboard.open(1); + clipboard.empty(); + + Clipboard::Time actual = clipboard.getTime(); + + EXPECT_EQ(1, actual); +} + +TEST(ClipboardTests, has_withFormatAdded_returnsTrue) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks!"); + + bool actual = clipboard.has(IClipboard::kText); + + EXPECT_EQ(true, actual); +} + +TEST(ClipboardTests, has_withNoFormats_returnsFalse) +{ + Clipboard clipboard; + clipboard.open(0); + + bool actual = clipboard.has(IClipboard::kText); + + EXPECT_FALSE(actual); +} + +TEST(ClipboardTests, get_withNoFormats_returnsEmpty) +{ + Clipboard clipboard; + clipboard.open(0); + + String actual = clipboard.get(IClipboard::kText); + + EXPECT_EQ("", actual); +} + +TEST(ClipboardTests, get_withFormatAdded_returnsExpected) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks!"); + + String actual = clipboard.get(IClipboard::kText); + + EXPECT_EQ("barrier rocks!", actual); +} + +TEST(ClipboardTests, marshall_addNotCalled_firstCharIsZero) +{ + Clipboard clipboard; + + String actual = clipboard.marshall(); + + // seems to return "\0\0\0\0" but EXPECT_EQ can't assert this, + // so instead, just assert that first char is '\0'. + EXPECT_EQ(0, (int)actual[0]); +} + +TEST(ClipboardTests, marshall_withTextAdded_typeCharIsText) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks!"); + clipboard.close(); + + String actual = clipboard.marshall(); + + // string contains other data, but 8th char should be kText. + EXPECT_EQ(IClipboard::kText, (int)actual[7]); +} + +TEST(ClipboardTests, marshall_withTextAdded_lastSizeCharIs14) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks!"); // 14 chars + clipboard.close(); + + String actual = clipboard.marshall(); + + EXPECT_EQ(14, (int)actual[11]); +} + +// TODO: there's some integer -> char encoding going on here. i find it +// hard to believe that the clipboard is the only thing doing this. maybe +// we should refactor this stuff out of the clipboard. +TEST(ClipboardTests, marshall_withTextSize285_sizeCharsValid) +{ + // 285 chars + String data; + data.append("Barrier is Free and Open Source Software that lets you "); + data.append("easily share your mouse and keyboard between multiple "); + data.append("computers, where each computer has it's own display. No "); + data.append("special hardware is required, all you need is a local area "); + data.append("network. Barrier is supported on Windows, Mac OS X and Linux."); + + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, data); + clipboard.close(); + + String actual = clipboard.marshall(); + + // 4 asserts here, but that's ok because we're really just asserting 1 + // thing. the 32-bit size value is split into 4 chars. if the size is 285 + // (29 more than the 8-bit max size), the last char "rolls over" to 29 + // (this is caused by a bit-wise & on 0xff and 8-bit truncation). each + // char before the last stores a bit-shifted version of the number, each + // 1 more power than the last, which is done by bit-shifting [0] by 24, + // [1] by 16, [2] by 8 ([3] is not bit-shifted). + EXPECT_EQ(0, actual[8]); // 285 >> 24 = 285 / (256^3) = 0 + EXPECT_EQ(0, actual[9]); // 285 >> 16 = 285 / (256^2) = 0 + EXPECT_EQ(1, actual[10]); // 285 >> 8 = 285 / (256^1) = 1(.11328125) + EXPECT_EQ(29, actual[11]); // 285 - 256 = 29 +} + +TEST(ClipboardTests, marshall_withHtmlAdded_typeCharIsHtml) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kHTML, "html sucks"); + clipboard.close(); + + String actual = clipboard.marshall(); + + // string contains other data, but 8th char should be kHTML. + EXPECT_EQ(IClipboard::kHTML, (int)actual[7]); +} + +TEST(ClipboardTests, marshall_withHtmlAndText_has2Formats) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks"); + clipboard.add(IClipboard::kHTML, "html sucks"); + clipboard.close(); + + String actual = clipboard.marshall(); + + // the number of formats is stored inside the first 4 chars. + // the writeUInt32 function right-aligns numbers in 4 chars, + // so if you right align 2, it will be "\0\0\0\2" in a string. + // we assert that the char at the 4th index is 2 (the number of + // formats that we've added). + EXPECT_EQ(2, (int)actual[3]); +} + +TEST(ClipboardTests, marshall_withTextAdded_endsWithAdded) +{ + Clipboard clipboard; + clipboard.open(0); + clipboard.add(IClipboard::kText, "barrier rocks!"); + clipboard.close(); + + String actual = clipboard.marshall(); + + // string contains other data, but should end in the string we added. + EXPECT_EQ("barrier rocks!", actual.substr(12)); +} + +TEST(ClipboardTests, unmarshall_emptyData_hasTextIsFalse) +{ + Clipboard clipboard; + + String data; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)0; // 0 formats added + + clipboard.unmarshall(data, 0); + + clipboard.open(0); + bool actual = clipboard.has(IClipboard::kText); + EXPECT_FALSE(actual); +} + +TEST(ClipboardTests, unmarshall_withTextSize285_getTextIsValid) +{ + Clipboard clipboard; + + // 285 chars + String text; + text.append("Barrier is Free and Open Source Software that lets you "); + text.append("easily share your mouse and keyboard between multiple "); + text.append("computers, where each computer has it's own display. No "); + text.append("special hardware is required, all you need is a local area "); + text.append("network. Barrier is supported on Windows, Mac OS X and Linux."); + + String data; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)1; // 1 format added + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)IClipboard::kText; + data += (char)0; // 285 >> 24 = 285 / (256^3) = 0 + data += (char)0; // 285 >> 16 = 285 / (256^2) = 0 + data += (char)1; // 285 >> 8 = 285 / (256^1) = 1(.11328125) + data += (char)29; // 285 - 256 = 29 + data += text; + + clipboard.unmarshall(data, 0); + + clipboard.open(0); + String actual = clipboard.get(IClipboard::kText); + EXPECT_EQ(text, actual); +} + +TEST(ClipboardTests, unmarshall_withTextAndHtml_getTextIsValid) +{ + Clipboard clipboard; + String data; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)2; // 2 formats added + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)IClipboard::kText; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)14; + data += "barrier rocks!"; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)IClipboard::kHTML; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)10; + data += "html sucks"; + + clipboard.unmarshall(data, 0); + + clipboard.open(0); + String actual = clipboard.get(IClipboard::kText); + EXPECT_EQ("barrier rocks!", actual); +} + +TEST(ClipboardTests, unmarshall_withTextAndHtml_getHtmlIsValid) +{ + Clipboard clipboard; + String data; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)2; // 2 formats added + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)IClipboard::kText; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)14; + data += "barrier rocks!"; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)IClipboard::kHTML; + data += (char)0; + data += (char)0; + data += (char)0; + data += (char)10; + data += "html sucks"; + + clipboard.unmarshall(data, 0); + + clipboard.open(0); + String actual = clipboard.get(IClipboard::kHTML); + EXPECT_EQ("html sucks", actual); +} + +TEST(ClipboardTests, copy_withSingleText_clipboardsAreEqual) +{ + Clipboard clipboard1; + clipboard1.open(0); + clipboard1.add(Clipboard::kText, "barrier rocks!"); + clipboard1.close(); + + Clipboard clipboard2; + Clipboard::copy(&clipboard2, &clipboard1); + + clipboard2.open(0); + String actual = clipboard2.get(Clipboard::kText); + EXPECT_EQ("barrier rocks!", actual); +} diff --git a/src/test/unittests/barrier/DeprecatedArgsParsingTests.cpp b/src/test/unittests/barrier/DeprecatedArgsParsingTests.cpp new file mode 100644 index 0000000..2856166 --- /dev/null +++ b/src/test/unittests/barrier/DeprecatedArgsParsingTests.cpp @@ -0,0 +1,50 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2015-2016 Symless 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 LICENSE 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 "barrier/ArgParser.h" + +#include "test/global/gtest.h" + +using namespace barrier; + +TEST(DeprecatedArgsParsingTests, parseDeprecatedArgs_cryptoPass_returnTrue) +{ + int i = 1; + const int argc = 3; + const char* kCryptoPassCmd[argc] = { "stub", "--crypto-pass", "mock_pass" }; + + ArgParser argParser(NULL); + + bool result = argParser.parseDeprecatedArgs(argc, kCryptoPassCmd, i); + + EXPECT_EQ(true, result); + EXPECT_EQ(2, i); +} + +TEST(DeprecatedArgsParsingTests, parseDeprecatedArgs_cryptoPass_returnFalse) +{ + int i = 1; + const int argc = 3; + const char* kCryptoPassCmd[argc] = { "stub", "--mock-arg", "mock_value" }; + + ArgParser argParser(NULL); + + bool result = argParser.parseDeprecatedArgs(argc, kCryptoPassCmd, i); + + EXPECT_FALSE(result); + EXPECT_EQ(1, i); +} diff --git a/src/test/unittests/barrier/GenericArgsParsingTests.cpp b/src/test/unittests/barrier/GenericArgsParsingTests.cpp new file mode 100644 index 0000000..f43070b --- /dev/null +++ b/src/test/unittests/barrier/GenericArgsParsingTests.cpp @@ -0,0 +1,315 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2014-2016 Symless 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 LICENSE 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 "barrier/ArgParser.h" +#include "barrier/ArgsBase.h" +#include "test/mock/barrier/MockApp.h" + +#include "test/global/gtest.h" + +using namespace barrier; +using ::testing::_; +using ::testing::Invoke; +using ::testing::NiceMock; + +bool g_helpShowed = false; +bool g_versionShowed = false; + +void +showMockHelp() +{ + g_helpShowed = true; +} + +void +showMockVersion() +{ + g_versionShowed = true; +} + +TEST(GenericArgsParsingTests, parseGenericArgs_logLevelCmd_setLogLevel) +{ + int i = 1; + const int argc = 3; + const char* kLogLevelCmd[argc] = { "stub", "--debug", "DEBUG" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kLogLevelCmd, i); + + String logFilter(argsBase.m_logFilter); + + EXPECT_EQ("DEBUG", logFilter); + EXPECT_EQ(2, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_logFileCmd_saveLogFilename) +{ + int i = 1; + const int argc = 3; + const char* kLogFileCmd[argc] = { "stub", "--log", "mock_filename" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kLogFileCmd, i); + + String logFile(argsBase.m_logFile); + + EXPECT_EQ("mock_filename", logFile); + EXPECT_EQ(2, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_logFileCmdWithSpace_saveLogFilename) +{ + int i = 1; + const int argc = 3; + const char* kLogFileCmdWithSpace[argc] = { "stub", "--log", "mo ck_filename" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kLogFileCmdWithSpace, i); + + String logFile(argsBase.m_logFile); + + EXPECT_EQ("mo ck_filename", logFile); + EXPECT_EQ(2, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_noDeamonCmd_daemonFalse) +{ + int i = 1; + const int argc = 2; + const char* kNoDeamonCmd[argc] = { "stub", "-f" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kNoDeamonCmd, i); + + EXPECT_FALSE(argsBase.m_daemon); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_deamonCmd_daemonTrue) +{ + int i = 1; + const int argc = 2; + const char* kDeamonCmd[argc] = { "stub", "--daemon" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kDeamonCmd, i); + + EXPECT_EQ(true, argsBase.m_daemon); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_nameCmd_saveName) +{ + int i = 1; + const int argc = 3; + const char* kNameCmd[argc] = { "stub", "--name", "mock" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kNameCmd, i); + + EXPECT_EQ("mock", argsBase.m_name); + EXPECT_EQ(2, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_noRestartCmd_restartFalse) +{ + int i = 1; + const int argc = 2; + const char* kNoRestartCmd[argc] = { "stub", "--no-restart" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kNoRestartCmd, i); + + EXPECT_FALSE(argsBase.m_restartable); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_restartCmd_restartTrue) +{ + int i = 1; + const int argc = 2; + const char* kRestartCmd[argc] = { "stub", "--restart" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kRestartCmd, i); + + EXPECT_EQ(true, argsBase.m_restartable); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_backendCmd_backendTrue) +{ + int i = 1; + const int argc = 2; + const char* kBackendCmd[argc] = { "stub", "-z" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kBackendCmd, i); + + EXPECT_EQ(true, argsBase.m_backend); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_noHookCmd_noHookTrue) +{ + int i = 1; + const int argc = 2; + const char* kNoHookCmd[argc] = { "stub", "--no-hooks" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kNoHookCmd, i); + + EXPECT_EQ(true, argsBase.m_noHooks); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_helpCmd_showHelp) +{ + g_helpShowed = false; + int i = 1; + const int argc = 2; + const char* kHelpCmd[argc] = { "stub", "--help" }; + + NiceMock<MockApp> app; + ArgParser argParser(&app); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + ON_CALL(app, help()).WillByDefault(Invoke(showMockHelp)); + + argParser.parseGenericArgs(argc, kHelpCmd, i); + + EXPECT_EQ(true, g_helpShowed); + EXPECT_EQ(1, i); +} + + +TEST(GenericArgsParsingTests, parseGenericArgs_versionCmd_showVersion) +{ + g_versionShowed = false; + int i = 1; + const int argc = 2; + const char* kVersionCmd[argc] = { "stub", "--version" }; + + NiceMock<MockApp> app; + ArgParser argParser(&app); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + ON_CALL(app, version()).WillByDefault(Invoke(showMockVersion)); + + argParser.parseGenericArgs(argc, kVersionCmd, i); + + EXPECT_EQ(true, g_versionShowed); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_noTrayCmd_disableTrayTrue) +{ + int i = 1; + const int argc = 2; + const char* kNoTrayCmd[argc] = { "stub", "--no-tray" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kNoTrayCmd, i); + + EXPECT_EQ(true, argsBase.m_disableTray); + EXPECT_EQ(1, i); +} + +TEST(GenericArgsParsingTests, parseGenericArgs_ipcCmd_enableIpcTrue) +{ + int i = 1; + const int argc = 2; + const char* kIpcCmd[argc] = { "stub", "--ipc" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kIpcCmd, i); + + EXPECT_EQ(true, argsBase.m_enableIpc); + EXPECT_EQ(1, i); +} + +#ifndef WINAPI_XWINDOWS +TEST(GenericArgsParsingTests, parseGenericArgs_dragDropCmdOnNonLinux_enableDragDropTrue) +{ + int i = 1; + const int argc = 2; + const char* kDragDropCmd[argc] = { "stub", "--enable-drag-drop" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kDragDropCmd, i); + + EXPECT_EQ(true, argsBase.m_enableDragDrop); + EXPECT_EQ(1, i); +} +#endif + +#ifdef WINAPI_XWINDOWS +TEST(GenericArgsParsingTests, parseGenericArgs_dragDropCmdOnLinux_enableDragDropFalse) +{ + int i = 1; + const int argc = 2; + const char* kDragDropCmd[argc] = { "stub", "--enable-drag-drop" }; + + ArgParser argParser(NULL); + ArgsBase argsBase; + argParser.setArgsBase(argsBase); + + argParser.parseGenericArgs(argc, kDragDropCmd, i); + + EXPECT_FALSE(argsBase.m_enableDragDrop); + EXPECT_EQ(1, i); +} +#endif diff --git a/src/test/unittests/barrier/KeyMapTests.cpp b/src/test/unittests/barrier/KeyMapTests.cpp new file mode 100644 index 0000000..5980633 --- /dev/null +++ b/src/test/unittests/barrier/KeyMapTests.cpp @@ -0,0 +1,216 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2016 Symless + * + * 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 LICENSE 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 "barrier/KeyMap.h" + +#include "test/global/gtest.h" +#include "test/global/gmock.h" + +using ::testing::_; +using ::testing::NiceMock; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::SaveArg; + +namespace barrier { + +TEST(KeyMapTests, findBestKey_requiredDown_matchExactFirstItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList; + KeyMap::KeyItem item; + item.m_required = KeyModifierShift; + item.m_sensitive = KeyModifierShift; + KeyModifierMask currentState = KeyModifierShift; + KeyModifierMask desiredState = KeyModifierShift; + itemList.push_back(item); + entryList.push_back(itemList); + + EXPECT_EQ(0, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_requiredAndExtraSensitiveDown_matchExactFirstItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList; + KeyMap::KeyItem item; + item.m_required = KeyModifierShift; + item.m_sensitive = KeyModifierShift | KeyModifierAlt; + KeyModifierMask currentState = KeyModifierShift; + KeyModifierMask desiredState = KeyModifierShift; + itemList.push_back(item); + entryList.push_back(itemList); + + EXPECT_EQ(0, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_requiredAndExtraSensitiveDown_matchExactSecondItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList1; + KeyMap::KeyItem item1; + item1.m_required = KeyModifierAlt; + item1.m_sensitive = KeyModifierShift | KeyModifierAlt; + KeyMap::KeyItemList itemList2; + KeyMap::KeyItem item2; + item2.m_required = KeyModifierShift; + item2.m_sensitive = KeyModifierShift | KeyModifierAlt; + KeyModifierMask currentState = KeyModifierShift; + KeyModifierMask desiredState = KeyModifierShift; + itemList1.push_back(item1); + itemList2.push_back(item2); + entryList.push_back(itemList1); + entryList.push_back(itemList2); + + EXPECT_EQ(1, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_extraSensitiveDown_matchExactSecondItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList1; + KeyMap::KeyItem item1; + item1.m_required = 0; + item1.m_sensitive = KeyModifierAlt; + KeyMap::KeyItemList itemList2; + KeyMap::KeyItem item2; + item2.m_required = 0; + item2.m_sensitive = KeyModifierShift; + KeyModifierMask currentState = KeyModifierAlt; + KeyModifierMask desiredState = KeyModifierAlt; + itemList1.push_back(item1); + itemList2.push_back(item2); + entryList.push_back(itemList1); + entryList.push_back(itemList2); + + EXPECT_EQ(1, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_noRequiredDown_matchOneRequiredChangeItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList1; + KeyMap::KeyItem item1; + item1.m_required = KeyModifierShift | KeyModifierAlt; + item1.m_sensitive = KeyModifierShift | KeyModifierAlt; + KeyMap::KeyItemList itemList2; + KeyMap::KeyItem item2; + item2.m_required = KeyModifierShift; + item2.m_sensitive = KeyModifierShift | KeyModifierAlt; + KeyModifierMask currentState = 0; + KeyModifierMask desiredState = 0; + itemList1.push_back(item1); + itemList2.push_back(item2); + entryList.push_back(itemList1); + entryList.push_back(itemList2); + + EXPECT_EQ(1, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_onlyOneRequiredDown_matchTwoRequiredChangesItem) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList1; + KeyMap::KeyItem item1; + item1.m_required = KeyModifierShift | KeyModifierAlt | KeyModifierControl; + item1.m_sensitive = KeyModifierShift | KeyModifierAlt | KeyModifierControl; + KeyMap::KeyItemList itemList2; + KeyMap::KeyItem item2; + item2.m_required = KeyModifierShift| KeyModifierAlt; + item2.m_sensitive = KeyModifierShift | KeyModifierAlt | KeyModifierControl; + KeyModifierMask currentState = 0; + KeyModifierMask desiredState = 0; + itemList1.push_back(item1); + itemList2.push_back(item2); + entryList.push_back(itemList1); + entryList.push_back(itemList2); + + EXPECT_EQ(1, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, findBestKey_noRequiredDown_cannotMatch) +{ + KeyMap keyMap; + KeyMap::KeyEntryList entryList; + KeyMap::KeyItemList itemList; + KeyMap::KeyItem item; + item.m_required = 0xffffffff; + item.m_sensitive = 0xffffffff; + KeyModifierMask currentState = 0; + KeyModifierMask desiredState = 0; + itemList.push_back(item); + entryList.push_back(itemList); + + EXPECT_EQ(-1, keyMap.findBestKey(entryList, currentState, desiredState)); +} + +TEST(KeyMapTests, isCommand_shiftMask_returnFalse) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierShift; + + EXPECT_FALSE(keyMap.isCommand(mask)); +} + +TEST(KeyMapTests, isCommand_controlMask_returnTrue) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierControl; + + EXPECT_EQ(true, keyMap.isCommand(mask)); +} + +TEST(KeyMapTests, isCommand_alternateMask_returnTrue) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierAlt; + + EXPECT_EQ(true, keyMap.isCommand(mask)); +} + +TEST(KeyMapTests, isCommand_alternateGraphicMask_returnTrue) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierAltGr; + + EXPECT_EQ(true, keyMap.isCommand(mask)); +} + +TEST(KeyMapTests, isCommand_metaMask_returnTrue) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierMeta; + + EXPECT_EQ(true, keyMap.isCommand(mask)); +} + +TEST(KeyMapTests, isCommand_superMask_returnTrue) +{ + KeyMap keyMap; + KeyModifierMask mask= KeyModifierSuper; + + EXPECT_EQ(true, keyMap.isCommand(mask)); +} + +} diff --git a/src/test/unittests/barrier/KeyStateTests.cpp b/src/test/unittests/barrier/KeyStateTests.cpp new file mode 100644 index 0000000..d4154d8 --- /dev/null +++ b/src/test/unittests/barrier/KeyStateTests.cpp @@ -0,0 +1,488 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2011 Nick Bolton + * + * 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 LICENSE 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 "test/mock/barrier/MockKeyState.h" +#include "test/mock/barrier/MockEventQueue.h" +#include "test/mock/barrier/MockKeyMap.h" + +#include "test/global/gtest.h" +#include "test/global/gmock.h" + +using ::testing::_; +using ::testing::NiceMock; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::SaveArg; + +void +stubPollPressedKeys(IKeyState::KeyButtonSet& pressedKeys); + +void +assertMaskIsOne(ForeachKeyCallback cb, void* userData); + +const barrier::KeyMap::KeyItem* +stubMapKey( + barrier::KeyMap::Keystrokes& keys, KeyID id, SInt32 group, + barrier::KeyMap::ModifierToKeys& activeModifiers, + KeyModifierMask& currentState, + KeyModifierMask desiredMask, + bool isAutoRepeat); + +barrier::KeyMap::Keystroke s_stubKeystroke(1, false, false); +barrier::KeyMap::KeyItem s_stubKeyItem; + +TEST(CKeyStateTests, onKey_aKeyDown_keyStateOne) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + keyState.onKey(1, true, KeyModifierAlt); + + EXPECT_EQ(1, keyState.getKeyState(1)); +} + +TEST(KeyStateTests, onKey_aKeyUp_keyStateZero) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + keyState.onKey(1, false, KeyModifierAlt); + + EXPECT_EQ(0, keyState.getKeyState(1)); +} + +TEST(KeyStateTests, onKey_invalidKey_keyStateZero) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + keyState.onKey(0, true, KeyModifierAlt); + + EXPECT_EQ(0, keyState.getKeyState(0)); +} + +TEST(KeyStateTests, sendKeyEvent_halfDuplexAndRepeat_addEventNotCalled) +{ + NiceMock<MockKeyMap> keyMap; + NiceMock<MockEventQueue> eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + ON_CALL(keyMap, isHalfDuplex(_, _)).WillByDefault(Return(true)); + + EXPECT_CALL(eventQueue, addEvent(_)).Times(0); + + keyState.sendKeyEvent(NULL, false, true, kKeyCapsLock, 0, 0, 0); +} + +TEST(KeyStateTests, sendKeyEvent_halfDuplex_addEventCalledTwice) +{ + NiceMock<MockKeyMap> keyMap; + NiceMock<MockEventQueue> eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + IKeyStateEvents keyStateEvents; + keyStateEvents.setEvents(&eventQueue); + + ON_CALL(keyMap, isHalfDuplex(_, _)).WillByDefault(Return(true)); + ON_CALL(eventQueue, forIKeyState()).WillByDefault(ReturnRef(keyStateEvents)); + + EXPECT_CALL(eventQueue, addEvent(_)).Times(2); + + keyState.sendKeyEvent(NULL, false, false, kKeyCapsLock, 0, 0, 0); +} + +TEST(KeyStateTests, sendKeyEvent_keyRepeat_addEventCalledOnce) +{ + NiceMock<MockKeyMap> keyMap; + NiceMock<MockEventQueue> eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + IKeyStateEvents keyStateEvents; + keyStateEvents.setEvents(&eventQueue); + + ON_CALL(eventQueue, forIKeyState()).WillByDefault(ReturnRef(keyStateEvents)); + + EXPECT_CALL(eventQueue, addEvent(_)).Times(1); + + keyState.sendKeyEvent(NULL, false, true, 1, 0, 0, 0); +} + +TEST(KeyStateTests, sendKeyEvent_keyDown_addEventCalledOnce) +{ + NiceMock<MockKeyMap> keyMap; + NiceMock<MockEventQueue> eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + IKeyStateEvents keyStateEvents; + keyStateEvents.setEvents(&eventQueue); + + ON_CALL(eventQueue, forIKeyState()).WillByDefault(ReturnRef(keyStateEvents)); + + EXPECT_CALL(eventQueue, addEvent(_)).Times(1); + + keyState.sendKeyEvent(NULL, true, false, 1, 0, 0, 0); +} + +TEST(KeyStateTests, sendKeyEvent_keyUp_addEventCalledOnce) +{ + NiceMock<MockKeyMap> keyMap; + NiceMock<MockEventQueue> eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + IKeyStateEvents keyStateEvents; + keyStateEvents.setEvents(&eventQueue); + + ON_CALL(eventQueue, forIKeyState()).WillByDefault(ReturnRef(keyStateEvents)); + + EXPECT_CALL(eventQueue, addEvent(_)).Times(1); + + keyState.sendKeyEvent(NULL, false, false, 1, 0, 0, 0); +} + +TEST(KeyStateTests, updateKeyMap_mockKeyMap_keyMapGotMock) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // key map member gets a new key map via swap() + EXPECT_CALL(keyMap, swap(_)); + + keyState.updateKeyMap(); +} + +TEST(KeyStateTests, updateKeyState_pollInsertsSingleKey_keyIsDown) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + ON_CALL(keyState, pollPressedKeys(_)).WillByDefault(Invoke(stubPollPressedKeys)); + + keyState.updateKeyState(); + + bool actual = keyState.isKeyDown(1); + ASSERT_TRUE(actual); +} + +TEST(KeyStateTests, updateKeyState_pollDoesNothing_keyNotSet) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + keyState.updateKeyState(); + + bool actual = keyState.isKeyDown(1); + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, updateKeyState_activeModifiers_maskSet) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + ON_CALL(keyState, pollActiveModifiers()).WillByDefault(Return(KeyModifierAlt)); + + keyState.updateKeyState(); + + KeyModifierMask actual = keyState.getActiveModifiers(); + ASSERT_EQ(KeyModifierAlt, actual); +} + +TEST(KeyStateTests, updateKeyState_activeModifiers_maskNotSet) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + keyState.updateKeyState(); + + KeyModifierMask actual = keyState.getActiveModifiers(); + ASSERT_EQ(0, actual); +} + +TEST(KeyStateTests, updateKeyState_activeModifiers_keyMapGotModifers) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + ON_CALL(keyState, pollActiveModifiers()).WillByDefault(Return(1)); + ON_CALL(keyMap, foreachKey(_, _)).WillByDefault(Invoke(assertMaskIsOne)); + + // key map gets new modifiers via foreachKey() + EXPECT_CALL(keyMap, foreachKey(_, _)); + + keyState.updateKeyState(); +} + +TEST(KeyStateTests, setHalfDuplexMask_capsLock_halfDuplexCapsLockAdded) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + EXPECT_CALL(keyMap, addHalfDuplexModifier(kKeyCapsLock)); + + keyState.setHalfDuplexMask(KeyModifierCapsLock); +} + +TEST(KeyStateTests, setHalfDuplexMask_numLock_halfDuplexNumLockAdded) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + EXPECT_CALL(keyMap, addHalfDuplexModifier(kKeyNumLock)); + + keyState.setHalfDuplexMask(KeyModifierNumLock); +} + +TEST(KeyStateTests, setHalfDuplexMask_scrollLock_halfDuplexScollLockAdded) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + EXPECT_CALL(keyMap, addHalfDuplexModifier(kKeyScrollLock)); + + keyState.setHalfDuplexMask(KeyModifierScrollLock); +} + +TEST(KeyStateTests, fakeKeyDown_serverKeyAlreadyDown_fakeKeyCalledTwice) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + s_stubKeyItem.m_client = 0; + s_stubKeyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + + // 2 calls to fakeKeyDown should still call fakeKey, even though + // repeated keys are handled differently. + EXPECT_CALL(keyState, fakeKey(_)).Times(2); + + // call twice to simulate server key already down (a misreported autorepeat). + keyState.fakeKeyDown(1, 0, 0); + keyState.fakeKeyDown(1, 0, 0); +} + +TEST(KeyStateTests, fakeKeyDown_isIgnoredKey_fakeKeyNotCalled) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + EXPECT_CALL(keyState, fakeKey(_)).Times(0); + + keyState.fakeKeyDown(kKeyCapsLock, 0, 0); +} + +TEST(KeyStateTests, fakeKeyDown_mapReturnsKeystrokes_fakeKeyCalled) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + s_stubKeyItem.m_button = 0; + s_stubKeyItem.m_client = 0; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + + EXPECT_CALL(keyState, fakeKey(_)).Times(1); + + keyState.fakeKeyDown(1, 0, 0); +} + +TEST(KeyStateTests, fakeKeyRepeat_invalidKey_returnsFalse) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + bool actual = keyState.fakeKeyRepeat(0, 0, 0, 0); + + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, fakeKeyRepeat_nullKey_returnsFalse) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // set the key to down (we need to make mapKey return a valid key to do this). + barrier::KeyMap::KeyItem keyItem; + keyItem.m_client = 0; + keyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Return(&keyItem)); + keyState.fakeKeyDown(1, 0, 0); + + // change mapKey to return NULL so that fakeKeyRepeat exits early. + barrier::KeyMap::KeyItem* nullKeyItem = NULL; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Return(nullKeyItem)); + + bool actual = keyState.fakeKeyRepeat(1, 0, 0, 0); + + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, fakeKeyRepeat_invalidButton_returnsFalse) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // set the key to down (we need to make mapKey return a valid key to do this). + barrier::KeyMap::KeyItem keyItem; + keyItem.m_client = 0; + keyItem.m_button = 1; // set to 1 to make fakeKeyDown work. + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Return(&keyItem)); + keyState.fakeKeyDown(1, 0, 0); + + // change button to 0 so that fakeKeyRepeat will return early. + keyItem.m_button = 0; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Return(&keyItem)); + + bool actual = keyState.fakeKeyRepeat(1, 0, 0, 0); + + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, fakeKeyRepeat_validKey_returnsTrue) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + s_stubKeyItem.m_client = 0; + s_stubKeystroke.m_type = barrier::KeyMap::Keystroke::kButton; + s_stubKeystroke.m_data.m_button.m_button = 2; + + // set the button to 1 for fakeKeyDown call + s_stubKeyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + keyState.fakeKeyDown(1, 0, 0); + + // change the button to 2 + s_stubKeyItem.m_button = 2; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + + bool actual = keyState.fakeKeyRepeat(1, 0, 0, 0); + + ASSERT_TRUE(actual); +} + +TEST(KeyStateTests, fakeKeyUp_buttonNotDown_returnsFalse) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + bool actual = keyState.fakeKeyUp(0); + + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, fakeKeyUp_buttonAlreadyDown_returnsTrue) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // press alt down so we get full coverage. + ON_CALL(keyState, pollActiveModifiers()).WillByDefault(Return(KeyModifierAlt)); + keyState.updateKeyState(); + + // press button 1 down. + s_stubKeyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + keyState.fakeKeyDown(1, 0, 1); + + // this takes the button id, which is the 3rd arg of fakeKeyDown + bool actual = keyState.fakeKeyUp(1); + + ASSERT_TRUE(actual); +} + +TEST(KeyStateTests, fakeAllKeysUp_keysWereDown_keysAreUp) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // press button 1 down. + s_stubKeyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + keyState.fakeKeyDown(1, 0, 1); + + // method under test + keyState.fakeAllKeysUp(); + + bool actual = keyState.isKeyDown(1); + ASSERT_FALSE(actual); +} + +TEST(KeyStateTests, isKeyDown_keyDown_returnsTrue) +{ + NiceMock<MockKeyMap> keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // press button 1 down. + s_stubKeyItem.m_button = 1; + ON_CALL(keyMap, mapKey(_, _, _, _, _, _, _)).WillByDefault(Invoke(stubMapKey)); + keyState.fakeKeyDown(1, 0, 1); + + // method under test + bool actual = keyState.isKeyDown(1); + + ASSERT_TRUE(actual); +} + +TEST(KeyStateTests, isKeyDown_noKeysDown_returnsFalse) +{ + MockKeyMap keyMap; + MockEventQueue eventQueue; + KeyStateImpl keyState(eventQueue, keyMap); + + // method under test + bool actual = keyState.isKeyDown(1); + + ASSERT_FALSE(actual); +} + +void +stubPollPressedKeys(IKeyState::KeyButtonSet& pressedKeys) +{ + pressedKeys.insert(1); +} + +void +assertMaskIsOne(ForeachKeyCallback cb, void* userData) +{ + ASSERT_EQ(1, ((KeyState::AddActiveModifierContext*)userData)->m_mask); +} + +const barrier::KeyMap::KeyItem* +stubMapKey( + barrier::KeyMap::Keystrokes& keys, KeyID id, SInt32 group, + barrier::KeyMap::ModifierToKeys& activeModifiers, + KeyModifierMask& currentState, + KeyModifierMask desiredMask, + bool isAutoRepeat) +{ + keys.push_back(s_stubKeystroke); + return &s_stubKeyItem; +} diff --git a/src/test/unittests/barrier/ServerArgsParsingTests.cpp b/src/test/unittests/barrier/ServerArgsParsingTests.cpp new file mode 100644 index 0000000..ed5ed85 --- /dev/null +++ b/src/test/unittests/barrier/ServerArgsParsingTests.cpp @@ -0,0 +1,66 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2014-2016 Symless 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 LICENSE 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 "barrier/ArgParser.h" +#include "barrier/ServerArgs.h" +#include "test/mock/barrier/MockArgParser.h" + +#include "test/global/gtest.h" + +using ::testing::_; +using ::testing::Invoke; +using ::testing::NiceMock; + +bool +server_stubParseGenericArgs(int, const char* const*, int&) +{ + return false; +} + +bool +server_stubCheckUnexpectedArgs() +{ + return false; +} + +TEST(ServerArgsParsingTests, parseServerArgs_addressArg_setBarrierAddress) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(server_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(server_stubCheckUnexpectedArgs)); + ServerArgs serverArgs; + const int argc = 3; + const char* kAddressCmd[argc] = { "stub", "--address", "mock_address" }; + + argParser.parseServerArgs(serverArgs, argc, kAddressCmd); + + EXPECT_EQ("mock_address", serverArgs.m_barrierAddress); +} + +TEST(ServerArgsParsingTests, parseServerArgs_configArg_setConfigFile) +{ + NiceMock<MockArgParser> argParser; + ON_CALL(argParser, parseGenericArgs(_, _, _)).WillByDefault(Invoke(server_stubParseGenericArgs)); + ON_CALL(argParser, checkUnexpectedArgs()).WillByDefault(Invoke(server_stubCheckUnexpectedArgs)); + ServerArgs serverArgs; + const int argc = 3; + const char* kConfigCmd[argc] = { "stub", "--config", "mock_configFile" }; + + argParser.parseServerArgs(serverArgs, argc, kConfigCmd); + + EXPECT_EQ("mock_configFile", serverArgs.m_configFile); +} diff --git a/src/test/unittests/base/StringTests.cpp b/src/test/unittests/base/StringTests.cpp new file mode 100644 index 0000000..39ad6e8 --- /dev/null +++ b/src/test/unittests/base/StringTests.cpp @@ -0,0 +1,177 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2014-2016 Symless 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 LICENSE 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 "base/String.h" + +#include "test/global/gtest.h" + +using namespace barrier; + +TEST(StringTests, format_formatWithArguments_formatedString) +{ + const char* format = "%%%{1}=%{2}"; + const char* arg1 = "answer"; + const char* arg2 = "42"; + + String result = string::format(format, arg1, arg2); + + EXPECT_EQ("%answer=42", result); +} + +TEST(StringTests, findReplaceAll_inputString_replacedString) +{ + String subject = "foobar"; + String find = "bar"; + String replace = "baz"; + + string::findReplaceAll(subject, find, replace); + + EXPECT_EQ("foobaz", subject); +} + +TEST(StringTests, sprintf_formatWithArgument_formatedString) +{ + const char* format = "%s=%d"; + const char* arg1 = "answer"; + int arg2 = 42; + + String result = string::sprintf(format, arg1, arg2); + + EXPECT_EQ("answer=42", result); +} + +TEST(StringTests, toHex_plaintext_hexString) +{ + String subject = "foobar"; + int width = 2; + + string::toHex(subject, width); + + EXPECT_EQ("666f6f626172", subject); +} + +TEST(StringTests, uppercase_lowercaseInput_uppercaseOutput) +{ + String subject = "12foo3BaR"; + + string::uppercase(subject); + + EXPECT_EQ("12FOO3BAR", subject); +} + +TEST(StringTests, removeChar_inputString_removeAllSpecifiedCharactors) +{ + String subject = "foobar"; + const char c = 'o'; + + string::removeChar(subject, c); + + EXPECT_EQ("fbar", subject); +} + +TEST(StringTests, intToString_inputInt_outputString) +{ + size_t value = 123; + + String number = string::sizeTypeToString(value); + + EXPECT_EQ("123", number); +} + +TEST(StringTests, stringToUint_inputString_outputInt) +{ + String number = "123"; + + size_t value = string::stringToSizeType(number); + + EXPECT_EQ(123, value); +} + +TEST(StringTests, splitString_twoSeparator_returnThreeParts) +{ + String string = "stub1:stub2:stub3"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(3, results.size()); + EXPECT_EQ("stub1", results[0]); + EXPECT_EQ("stub2", results[1]); + EXPECT_EQ("stub3", results[2]); +} + +TEST(StringTests, splitString_oneSeparator_returnTwoParts) +{ + String string = "stub1:stub2"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(2, results.size()); + EXPECT_EQ("stub1", results[0]); + EXPECT_EQ("stub2", results[1]); +} + +TEST(StringTests, splitString_noSeparator_returnOriginalString) +{ + String string = "stub1"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(1, results.size()); + EXPECT_EQ("stub1", results[0]); +} + +TEST(StringTests, splitString_emptyString_returnEmptyVector) +{ + String string; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(0, results.size()); +} + +TEST(StringTests, splitString_tailSeparator_returnTwoParts) +{ + String string = "stub1:stub2:"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(2, results.size()); + EXPECT_EQ("stub1", results[0]); + EXPECT_EQ("stub2", results[1]); +} + +TEST(StringTests, splitString_headSeparator_returnTwoParts) +{ + String string = ":stub1:stub2"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(2, results.size()); + EXPECT_EQ("stub1", results[0]); + EXPECT_EQ("stub2", results[1]); +} + +TEST(StringTests, splitString_headAndTailSeparators_returnTwoParts) +{ + String string = ":stub1:stub2:"; + + std::vector<String> results = string::splitString(string, ':'); + + EXPECT_EQ(2, results.size()); + EXPECT_EQ("stub1", results[0]); + EXPECT_EQ("stub2", results[1]); +} diff --git a/src/test/unittests/ipc/IpcLogOutputterTests.cpp b/src/test/unittests/ipc/IpcLogOutputterTests.cpp new file mode 100644 index 0000000..bbfed9c --- /dev/null +++ b/src/test/unittests/ipc/IpcLogOutputterTests.cpp @@ -0,0 +1,165 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2015-2016 Symless 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 LICENSE 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 "test/mock/ipc/MockIpcServer.h" + +#include "mt/Thread.h" +#include "ipc/IpcLogOutputter.h" +#include "base/String.h" +#include "common/common.h" + +#include "test/global/gmock.h" +#include "test/global/gtest.h" + +// HACK: ipc logging only used on windows anyway +#if WINAPI_MSWINDOWS + +using ::testing::_; +using ::testing::Return; +using ::testing::Matcher; +using ::testing::MatcherCast; +using ::testing::Property; +using ::testing::StrEq; +using ::testing::AtLeast; + +using namespace barrier; + +inline const Matcher<const IpcMessage&> IpcLogLineMessageEq(const String& s) { + const Matcher<const IpcLogLineMessage&> m( + Property(&IpcLogLineMessage::logLine, StrEq(s))); + return MatcherCast<const IpcMessage&>(m); +} + +TEST(IpcLogOutputterTests, write_threadingEnabled_bufferIsSent) +{ + MockIpcServer mockServer; + mockServer.delegateToFake(); + + ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); + + EXPECT_CALL(mockServer, hasClients(_)).Times(AtLeast(3)); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\n"), _)).Times(1); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 2\n"), _)).Times(1); + + IpcLogOutputter outputter(mockServer, kIpcClientUnknown, true); + outputter.write(kNOTE, "mock 1"); + mockServer.waitForSend(); + outputter.write(kNOTE, "mock 2"); + mockServer.waitForSend(); +} + +TEST(IpcLogOutputterTests, write_overBufferMaxSize_firstLineTruncated) +{ + MockIpcServer mockServer; + + ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); + EXPECT_CALL(mockServer, hasClients(_)).Times(1); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 2\nmock 3\n"), _)).Times(1); + + IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); + outputter.bufferMaxSize(2); + + // log more lines than the buffer can contain + outputter.write(kNOTE, "mock 1"); + outputter.write(kNOTE, "mock 2"); + outputter.write(kNOTE, "mock 3"); + outputter.sendBuffer(); +} + +TEST(IpcLogOutputterTests, write_underBufferMaxSize_allLinesAreSent) +{ + MockIpcServer mockServer; + + ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); + + EXPECT_CALL(mockServer, hasClients(_)).Times(1); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); + + IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); + outputter.bufferMaxSize(2); + + // log more lines than the buffer can contain + outputter.write(kNOTE, "mock 1"); + outputter.write(kNOTE, "mock 2"); + outputter.sendBuffer(); +} + +// HACK: temporarily disable this intermittently failing unit test. +// when the build machine is under heavy load, a race condition +// usually happens. +#if 0 +TEST(IpcLogOutputterTests, write_overBufferRateLimit_lastLineTruncated) +{ + MockIpcServer mockServer; + + ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); + + EXPECT_CALL(mockServer, hasClients(_)).Times(2); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 4\nmock 5\n"), _)).Times(1); + + IpcLogOutputter outputter(mockServer, false); + outputter.bufferRateLimit(2, 1); // 1s + + // log 1 more line than the buffer can accept in time limit. + outputter.write(kNOTE, "mock 1"); + outputter.write(kNOTE, "mock 2"); + outputter.write(kNOTE, "mock 3"); + + outputter.sendBuffer(); + + // after waiting the time limit send another to make sure + // we can log after the time limit passes. + // HACK: sleep causes the unit test to fail intermittently, + // so lets try 100ms (there must be a better way to solve this) + ARCH->sleep(2); // 2s + outputter.write(kNOTE, "mock 4"); + outputter.write(kNOTE, "mock 5"); + outputter.write(kNOTE, "mock 6"); + + outputter.sendBuffer(); +} +#endif + +TEST(IpcLogOutputterTests, write_underBufferRateLimit_allLinesAreSent) +{ + MockIpcServer mockServer; + + ON_CALL(mockServer, hasClients(_)).WillByDefault(Return(true)); + + EXPECT_CALL(mockServer, hasClients(_)).Times(2); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 1\nmock 2\n"), _)).Times(1); + EXPECT_CALL(mockServer, send(IpcLogLineMessageEq("mock 3\nmock 4\n"), _)).Times(1); + + IpcLogOutputter outputter(mockServer, kIpcClientUnknown, false); + outputter.bufferRateLimit(4, 1); // 1s (should be plenty of time) + + // log 1 more line than the buffer can accept in time limit. + outputter.write(kNOTE, "mock 1"); + outputter.write(kNOTE, "mock 2"); + outputter.sendBuffer(); + + // after waiting the time limit send another to make sure + // we can log after the time limit passes. + outputter.write(kNOTE, "mock 3"); + outputter.write(kNOTE, "mock 4"); + outputter.sendBuffer(); +} + +#endif // WINAPI_MSWINDOWS diff --git a/src/test/unittests/platform/OSXKeyStateTests.cpp b/src/test/unittests/platform/OSXKeyStateTests.cpp new file mode 100644 index 0000000..dd9e80f --- /dev/null +++ b/src/test/unittests/platform/OSXKeyStateTests.cpp @@ -0,0 +1,57 @@ +/* + * barrier -- mouse and keyboard sharing utility + * Copyright (C) 2012-2016 Symless Ltd. + * Copyright (C) 2011 Nick Bolton + * + * 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 LICENSE 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 "test/mock/barrier/MockKeyMap.h" +#include "test/mock/barrier/MockEventQueue.h" +#include "platform/OSXKeyState.h" + +#include "test/global/gtest.h" +#include "test/global/gmock.h" + +TEST(OSXKeyStateTests, mapModifiersFromOSX_OSXMask_returnBarrierMask) +{ + barrier::KeyMap keyMap; + MockEventQueue eventQueue; + OSXKeyState keyState(&eventQueue, keyMap); + + KeyModifierMask outMask = 0; + + UInt32 shiftMask = 0 | kCGEventFlagMaskShift; + outMask = keyState.mapModifiersFromOSX(shiftMask); + EXPECT_EQ(KeyModifierShift, outMask); + + UInt32 ctrlMask = 0 | kCGEventFlagMaskControl; + outMask = keyState.mapModifiersFromOSX(ctrlMask); + EXPECT_EQ(KeyModifierControl, outMask); + + UInt32 altMask = 0 | kCGEventFlagMaskAlternate; + outMask = keyState.mapModifiersFromOSX(altMask); + EXPECT_EQ(KeyModifierAlt, outMask); + + UInt32 cmdMask = 0 | kCGEventFlagMaskCommand; + outMask = keyState.mapModifiersFromOSX(cmdMask); + EXPECT_EQ(KeyModifierSuper, outMask); + + UInt32 capsMask = 0 | kCGEventFlagMaskAlphaShift; + outMask = keyState.mapModifiersFromOSX(capsMask); + EXPECT_EQ(KeyModifierCapsLock, outMask); + + UInt32 numMask = 0 | kCGEventFlagMaskNumericPad; + outMask = keyState.mapModifiersFromOSX(numMask); + EXPECT_EQ(KeyModifierNumLock, outMask); +} |
