From a31e984aadfee31ca7c96b778aefcd321b0101e7 Mon Sep 17 00:00:00 2001 From: Serhii Hadzhilov Date: Tue, 13 Oct 2020 12:56:26 +0300 Subject: [PATCH] SYNERGY-323 "No configuration available" error. Main fix and additional tests. --- src/lib/synergy/ProtocolUtil.cpp | 67 ++++--- src/lib/synergy/ProtocolUtil.h | 6 +- .../unittests/synergy/ProtocolUtilTests.cpp | 166 ++++++++++++++---- 3 files changed, 175 insertions(+), 64 deletions(-) diff --git a/src/lib/synergy/ProtocolUtil.cpp b/src/lib/synergy/ProtocolUtil.cpp index 9771c2379f..c52b763fe0 100644 --- a/src/lib/synergy/ProtocolUtil.cpp +++ b/src/lib/synergy/ProtocolUtil.cpp @@ -111,15 +111,18 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args) UInt32 len = eatLength(&fmt); switch (*fmt) { case 'i': { - readInt(stream, len, args); + void* destination = va_arg(args, void*); + readInt(stream, len, destination); break; } case 'I': { - readVectorInt(stream, len, args); + void* destination = va_arg(args, void*); + readVectorInt(stream, len, destination); break; } case 's': { - readBytes(stream, len, args); + String* destination = va_arg(args, String*); + readBytes(stream, len, destination); break; } @@ -412,7 +415,7 @@ ProtocolUtil::read(synergy::IStream* stream, void* vbuffer, UInt32 count) } } -void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, va_list args) { +void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, void* destination) { // check for valid length if (len == 4 || len == 2 || len == 1) { @@ -422,32 +425,39 @@ void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, va_list args) //Read the buffer till the len or buffers_size, which ever is smaller read(stream, buffer, len > buffer_size ? buffer_size : len); - // convert it - void* v = va_arg(args, void*); switch (len) { case 1: // 1 byte integer - *static_cast(v) = buffer[0]; - LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast(v), *static_cast(v))); + *static_cast(destination) = buffer[0]; + LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", + len, + *static_cast(destination), + *static_cast(destination))); break; case 2: // 2 byte integer - *static_cast(v) = + *static_cast(destination) = static_cast( (static_cast(buffer[0]) << 8) | static_cast(buffer[1])); - LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast(v), *static_cast(v))); + LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", + len, + *static_cast(destination), + *static_cast(destination))); break; case 4: // 4 byte integer - *static_cast(v) = + *static_cast(destination) = (static_cast(buffer[0]) << 24) | (static_cast(buffer[1]) << 16) | (static_cast(buffer[2]) << 8) | static_cast(buffer[3]); - LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast(v), *static_cast(v))); + LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", + len, + *static_cast(destination), + *static_cast(destination))); break; } } @@ -458,7 +468,7 @@ void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, va_list args) } } -void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, va_list args) { +void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, void* destination) { // check for valid length assert(len == 1 || len == 2 || len == 4); @@ -471,15 +481,16 @@ void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, va_list static_cast(buffer[3]); // convert it - void* v = va_arg(args, void*); switch (len) { case 1: // 1 byte integer for (UInt32 i = 0; i < n; ++i) { read(stream, buffer, 1); - static_cast*>(v)->push_back( - buffer[0]); - LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast*>(v)->back(), static_cast*>(v)->back())); + static_cast*>(destination)->push_back(buffer[0]); + LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", + len, i, + static_cast*>(destination)->back(), + static_cast*>(destination)->back())); } break; @@ -487,11 +498,14 @@ void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, va_list // 2 byte integer for (UInt32 i = 0; i < n; ++i) { read(stream, buffer, 2); - static_cast*>(v)->push_back( + static_cast*>(destination)->push_back( static_cast( (static_cast(buffer[0]) << 8) | static_cast(buffer[1]))); - LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast*>(v)->back(), static_cast*>(v)->back())); + LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", + len, i, + static_cast*>(destination)->back(), + static_cast*>(destination)->back())); } break; @@ -499,18 +513,21 @@ void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, va_list // 4 byte integer for (UInt32 i = 0; i < n; ++i) { read(stream, buffer, 4); - static_cast*>(v)->push_back( + static_cast*>(destination)->push_back( (static_cast(buffer[0]) << 24) | (static_cast(buffer[1]) << 16) | (static_cast(buffer[2]) << 8) | static_cast(buffer[3])); - LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast*>(v)->back(), static_cast*>(v)->back())); + LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", + len, i, + static_cast*>(destination)->back(), + static_cast*>(destination)->back())); } break; } } -void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, va_list args) { +void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, String* destination) { assert(len == 0); // read the string length @@ -552,8 +569,10 @@ void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, va_list args LOG((CLOG_DEBUG2 "readf: read %d byte string", len)); // save the data - String* dst = va_arg(args, String*); - dst->assign((const char*)sBuffer, len); + + if (destination){ + destination->assign((const char*)sBuffer, len); + } // release the buffer if (!useFixed) { diff --git a/src/lib/synergy/ProtocolUtil.h b/src/lib/synergy/ProtocolUtil.h index 04fad66e23..877a36e2b7 100644 --- a/src/lib/synergy/ProtocolUtil.h +++ b/src/lib/synergy/ProtocolUtil.h @@ -86,17 +86,17 @@ private: /** * @brief Handles 1,2, or 4 byte Integers */ - static void readInt(synergy::IStream*, UInt32, va_list); + static void readInt(synergy::IStream*, UInt32, void*); /** * @brief Handles a Vector of integers */ - static void readVectorInt(synergy::IStream*, UInt32, va_list); + static void readVectorInt(synergy::IStream*, UInt32, void*); /** * @brief Handles an array of bytes */ - static void readBytes(synergy::IStream*, UInt32, va_list); + static void readBytes(synergy::IStream*, UInt32, String*); }; //! Mismatched read exception diff --git a/src/test/unittests/synergy/ProtocolUtilTests.cpp b/src/test/unittests/synergy/ProtocolUtilTests.cpp index 9fd56b353f..d8b37c9235 100644 --- a/src/test/unittests/synergy/ProtocolUtilTests.cpp +++ b/src/test/unittests/synergy/ProtocolUtilTests.cpp @@ -101,34 +101,8 @@ TEST(ProtocolUtilTests, readf_asserts) TEST(ProtocolUtilTests, readf_string) { std::string Data; - const std::string Expected = "expected string"; - const UInt8 Length = Expected.length(); - UInt8 Size[4] = {0,0,0,Length}; - - MockStream stream; - EXPECT_CALL(stream, read(_, _)) - .WillOnce( - DoAll( - SetValueToVoidPointerArg0(&Size, sizeof(Size)), - Return(sizeof(Size)) - ) - ) - .WillOnce( - DoAll( - SetValueToVoidPointerArg0(Expected.c_str(), Expected.length()), - Return(Expected.length()) - ) - ); - - EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s", &Data)); - EXPECT_EQ(Expected, Data); -} - -TEST(ProtocolUtilTests, readf_string_200) -{ - std::string Data; - const std::string Expected(200, 'x'); - const UInt8 Length = Expected.length(); + const UInt8 Length = 200; + const std::string Expected(Length, 'x'); UInt8 Size[4] = {0,0,0,Length}; MockStream stream; @@ -264,8 +238,8 @@ INSTANTIATE_TEST_CASE_P( TEST(ProtocolUtilTests, readf_int1byte_and_string) { std::string ActualString; - const std::string ExpectedString(200, 'x'); - const UInt8 StringLength = ExpectedString.length(); + const UInt8 StringLength = 200; + const std::string ExpectedString(StringLength, 'x'); UInt8 Size[4] = {0,0,0,StringLength}; UInt8 ActualInt = 0; @@ -301,13 +275,13 @@ TEST(ProtocolUtilTests, readf_int1byte_and_string) TEST(ProtocolUtilTests, readf_int2byte_and_string) { std::string ActualString; - const std::string ExpectedString(200, 'x'); - const UInt8 StringLength = ExpectedString.length(); + const UInt8 StringLength = 200; + const std::string ExpectedString(StringLength, 'x'); UInt8 Size[4] = {0,0,0,StringLength}; - UInt8 ActualInt = 0; - const int ExpectedInt = 10; - UInt8 StreamIntData[2] = {0, ExpectedInt}; + UInt16 ActualInt = 0; + const UInt16 ExpectedInt = 10; + UInt8 StreamIntData[2] = {0, 10}; MockStream stream; EXPECT_CALL(stream, read(_, _)) @@ -337,8 +311,8 @@ TEST(ProtocolUtilTests, readf_int2byte_and_string) TEST(ProtocolUtilTests, readf_int4byte_and_string) { - UInt8 ActualInt = 0; - const int ExpectedInt = 10; + UInt32 ActualInt = 0; + const UInt8 ExpectedInt = 10; UInt8 StreamIntData[4] = {0,0,0,ExpectedInt}; std::string ActualString; @@ -371,13 +345,131 @@ TEST(ProtocolUtilTests, readf_int4byte_and_string) EXPECT_EQ(ExpectedInt, ActualInt); } +TEST(ProtocolUtilTests, readf_string_and_int4bytes) +{ + UInt32 ActualInt = 0; + const UInt8 ExpectedInt = 10; + UInt8 StreamIntData[4] = {0,0,0,ExpectedInt}; + std::string ActualString; + const std::string ExpectedString(32768, 'x'); + UInt8 Size[4] = {0,0,128,0}; + MockStream stream; + EXPECT_CALL(stream, read(_, _)) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(&Size, sizeof(Size)), + Return(sizeof(Size)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()), + Return(ExpectedString.length()) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(&StreamIntData, sizeof(StreamIntData)), + Return(sizeof(StreamIntData)) + ) + ); + EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4i", &ActualString, &ActualInt)); + EXPECT_EQ(ExpectedString, ActualString); + EXPECT_EQ(ExpectedInt, ActualInt); +} +TEST(ProtocolUtilTests, readf_string_and_vector_int4bytes) +{ + std::vector Actual4Bytes = {}; + const std::vector Expected4Bytes = {10,10}; + UInt8 StreamVectorSize[4] = {0,0,0,2}; + UInt8 StreamData4Bytes[4] = {0, 0, 0, 10}; + std::string ActualString; + const std::string ExpectedString(32768, 'x'); + UInt8 Size[4] = {0,0,128,0}; + MockStream stream; + EXPECT_CALL(stream, read(_, _)) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(&Size, sizeof(Size)), + Return(sizeof(Size)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()), + Return(ExpectedString.length()) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(StreamVectorSize, sizeof(StreamVectorSize)), + Return(sizeof(StreamVectorSize)) + ) + ) + .WillRepeatedly( + DoAll( + SetValueToVoidPointerArg0(StreamData4Bytes, sizeof(StreamData4Bytes)), + Return(sizeof(StreamData4Bytes)) + ) + ); + EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual4Bytes)); + EXPECT_EQ(ExpectedString, ActualString); + EXPECT_EQ(Expected4Bytes, Actual4Bytes); +} +TEST(ProtocolUtilTests, readf_vector_int4bytes_and_string) +{ + std::vector Actual4Bytes = {}; + const std::vector Expected4Bytes = {10,10}; + UInt8 StreamVectorSize[4] = {0,0,0,2}; + UInt8 StreamData4Bytes[4] = {0, 0, 0, 10}; + std::string ActualString; + const std::string ExpectedString(32768, 'x'); + UInt8 Size[4] = {0,0,128,0}; + + MockStream stream; + EXPECT_CALL(stream, read(_, _)) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(StreamVectorSize, sizeof(StreamVectorSize)), + Return(sizeof(StreamVectorSize)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(StreamData4Bytes, sizeof(StreamData4Bytes)), + Return(sizeof(StreamData4Bytes)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(StreamData4Bytes, sizeof(StreamData4Bytes)), + Return(sizeof(StreamData4Bytes)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(&Size, sizeof(Size)), + Return(sizeof(Size)) + ) + ) + .WillOnce( + DoAll( + SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()), + Return(ExpectedString.length()) + ) + ); + + EXPECT_TRUE(ProtocolUtil::readf(&stream, "%4I%s", &Actual4Bytes, &ActualString)); + EXPECT_EQ(ExpectedString, ActualString); + EXPECT_EQ(Expected4Bytes, Actual4Bytes); +}