SYNERGY-323 "No configuration available" error.

Main fix and additional tests.
This commit is contained in:
Serhii Hadzhilov 2020-10-13 12:56:26 +03:00
parent 4e51f3bb7a
commit a31e984aad
3 changed files with 175 additions and 64 deletions

View File

@ -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<UInt8*>(v) = buffer[0];
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt8*>(v), *static_cast<UInt8*>(v)));
*static_cast<UInt8*>(destination) = buffer[0];
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
len,
*static_cast<UInt8*>(destination),
*static_cast<UInt8*>(destination)));
break;
case 2:
// 2 byte integer
*static_cast<UInt16*>(v) =
*static_cast<UInt16*>(destination) =
static_cast<UInt16>(
(static_cast<UInt16>(buffer[0]) << 8) |
static_cast<UInt16>(buffer[1]));
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt16*>(v), *static_cast<UInt16*>(v)));
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
len,
*static_cast<UInt16*>(destination),
*static_cast<UInt16*>(destination)));
break;
case 4:
// 4 byte integer
*static_cast<UInt32*>(v) =
*static_cast<UInt32*>(destination) =
(static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]);
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)", len, *static_cast<UInt32*>(v), *static_cast<UInt32*>(v)));
LOG((CLOG_DEBUG2 "readf: read %d byte integer: %d (0x%x)",
len,
*static_cast<UInt32*>(destination),
*static_cast<UInt32*>(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<UInt32>(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<std::vector<UInt8>*>(v)->push_back(
buffer[0]);
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt8>*>(v)->back(), static_cast<std::vector<UInt8>*>(v)->back()));
static_cast<std::vector<UInt8>*>(destination)->push_back(buffer[0]);
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt8>*>(destination)->back(),
static_cast<std::vector<UInt8>*>(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<std::vector<UInt16>*>(v)->push_back(
static_cast<std::vector<UInt16>*>(destination)->push_back(
static_cast<UInt16>(
(static_cast<UInt16>(buffer[0]) << 8) |
static_cast<UInt16>(buffer[1])));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt16>*>(v)->back(), static_cast<std::vector<UInt16>*>(v)->back()));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt16>*>(destination)->back(),
static_cast<std::vector<UInt16>*>(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<std::vector<UInt32>*>(v)->push_back(
static_cast<std::vector<UInt32>*>(destination)->push_back(
(static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)", len, i, static_cast<std::vector<UInt32>*>(v)->back(), static_cast<std::vector<UInt32>*>(v)->back()));
LOG((CLOG_DEBUG2 "readf: read %d byte integer[%d]: %d (0x%x)",
len, i,
static_cast<std::vector<UInt32>*>(destination)->back(),
static_cast<std::vector<UInt32>*>(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) {

View File

@ -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

View File

@ -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<UInt32> Actual4Bytes = {};
const std::vector<UInt32> 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<UInt32> Actual4Bytes = {};
const std::vector<UInt32> 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);
}