SYNERGY-323 "No configuration available" error. Sonar issues fix.

This commit is contained in:
Serhii Hadzhilov 2020-10-13 22:38:50 +03:00
parent 75efede1e3
commit 6c37cfad3b
3 changed files with 123 additions and 136 deletions

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <array>
#include "synergy/ProtocolUtil.h"
#include "io/IStream.h"
#include "base/Log.h"
@ -60,7 +61,7 @@ ProtocolUtil::readf(synergy::IStream* stream, const char* fmt, ...)
catch (XIO&) {
result = false;
}
catch (std::bad_alloc & exception) {
catch (std::bad_alloc&) {
result = false;
}
va_end(args);
@ -113,14 +114,52 @@ ProtocolUtil::vreadf(synergy::IStream* stream, const char* fmt, va_list args)
switch (*fmt) {
case 'i': {
void* destination = va_arg(args, void*);
readInt(stream, len, destination);
switch (len) {
case 1:
// 1 byte integer
*static_cast<UInt8*>(destination) = read1ByteInt(stream);
break;
case 2:
// 2 byte integer
*static_cast<UInt16*>(destination) = read2BytesInt(stream);
break;
case 4:
// 4 byte integer
*static_cast<UInt32*>(destination) = read4BytesInt(stream);
break;
default:
//the length is wrong
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging
break;
}
break;
}
case 'I': {
void* destination = va_arg(args, void*);
readVectorInt(stream, len, destination);
switch (len) {
case 1:
// 1 byte integer
readVector1ByteInt(stream, *static_cast<std::vector<UInt8>*>(destination));
break;
case 2:
// 2 byte integer
readVector2BytesInt(stream, *static_cast<std::vector<UInt16>*>(destination));
break;
case 4:
// 4 byte integer
readVector4BytesInt(stream, *static_cast<std::vector<UInt32>*>(destination));
break;
default:
//the length is wrong
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging
break;
}
break;
}
case 's': {
String* destination = va_arg(args, String*);
readBytes(stream, len, destination);
@ -416,115 +455,67 @@ ProtocolUtil::read(synergy::IStream* stream, void* vbuffer, UInt32 count)
}
}
void ProtocolUtil::readInt(synergy::IStream * stream, UInt32 len, void* destination) {
// check for valid length
if (len == 4 || len == 2 || len == 1) {
UInt8 ProtocolUtil::read1ByteInt(synergy::IStream * stream)
{
const UInt32 BufferSize = 1;
std::array<UInt8, 1> buffer = {};
read(stream, buffer.data(), BufferSize);
static const int buffer_size = 4;
// read the data
UInt8 buffer[buffer_size];
//Read the buffer till the len or buffers_size, which ever is smaller
read(stream, buffer, len > buffer_size ? buffer_size : len);
UInt8 Result = buffer[0];
LOG((CLOG_DEBUG2 "readf: read 1 byte integer: %d (0x%x)", Result, Result));
switch (len) {
case 1:
// 1 byte integer
*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;
return Result;
}
case 2:
// 2 byte integer
*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*>(destination),
*static_cast<UInt16*>(destination)));
break;
UInt16 ProtocolUtil::read2BytesInt(synergy::IStream * stream)
{
const UInt32 BufferSize = 2;
std::array<UInt8, BufferSize> buffer = {};
read(stream, buffer.data(), BufferSize);
case 4:
// 4 byte integer
*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*>(destination),
*static_cast<UInt32*>(destination)));
break;
}
}
else {
//the length is wrong
LOG((CLOG_ERR "read: length to be read is wrong: '%d' should be 1,2, or 4", len));
assert(false); //assert for debugging
UInt16 Result = (static_cast<UInt16>(buffer[0]) << 8) | static_cast<UInt16>(buffer[1]);
LOG((CLOG_DEBUG2 "readf: read 2 byte integer: %d (0x%x)", Result, Result));
return Result;
}
UInt32 ProtocolUtil::read4BytesInt(synergy::IStream * stream)
{
const int BufferSize = 4;
std::array<UInt8, BufferSize> buffer = {};
read(stream, buffer.data(), BufferSize);
UInt32 Result = (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 4 byte integer: %d (0x%x)", Result, Result));
return Result;
}
void ProtocolUtil::readVector1ByteInt(synergy::IStream* stream, std::vector<UInt8>& destination)
{
UInt32 size = read4BytesInt(stream);
for (UInt32 i = 0; i < size; ++i) {
destination.push_back(read1ByteInt(stream));
}
}
void ProtocolUtil::readVectorInt(synergy::IStream * stream, UInt32 len, void* destination) {
// check for valid length
assert(len == 1 || len == 2 || len == 4);
void ProtocolUtil::readVector2BytesInt(synergy::IStream* stream, std::vector<UInt16>& destination)
{
UInt32 size = read4BytesInt(stream);
for (UInt32 i = 0; i < size; ++i) {
destination.push_back(read2BytesInt(stream));
}
}
// read the vector length
UInt8 buffer[4];
read(stream, buffer, 4);
UInt32 n = (static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]);
// convert it
switch (len) {
case 1:
// 1 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 1);
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;
case 2:
// 2 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 2);
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>*>(destination)->back(),
static_cast<std::vector<UInt16>*>(destination)->back()));
}
break;
case 4:
// 4 byte integer
for (UInt32 i = 0; i < n; ++i) {
read(stream, buffer, 4);
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>*>(destination)->back(),
static_cast<std::vector<UInt32>*>(destination)->back()));
}
break;
void ProtocolUtil::readVector4BytesInt(synergy::IStream* stream, std::vector<UInt32>& destination)
{
UInt32 size = read4BytesInt(stream);
for (UInt32 i = 0; i < size; ++i) {
destination.push_back(read4BytesInt(stream));
}
}
@ -533,12 +524,7 @@ void ProtocolUtil::readBytes(synergy::IStream * stream, UInt32 len, String* dest
// read the string length
UInt8 buffer[128];
read(stream, buffer, 4);
len = (static_cast<UInt32>(buffer[0]) << 24) |
(static_cast<UInt32>(buffer[1]) << 16) |
(static_cast<UInt32>(buffer[2]) << 8) |
static_cast<UInt32>(buffer[3]);
len = read4BytesInt(stream);
// use a fixed size buffer if its big enough
const bool useFixed = (len <= sizeof(buffer));

View file

@ -86,12 +86,16 @@ private:
/**
* @brief Handles 1,2, or 4 byte Integers
*/
static void readInt(synergy::IStream*, UInt32, void*);
static UInt8 read1ByteInt(synergy::IStream * stream);
static UInt16 read2BytesInt(synergy::IStream * stream);
static UInt32 read4BytesInt(synergy::IStream * stream);
/**
* @brief Handles a Vector of integers
*/
static void readVectorInt(synergy::IStream*, UInt32, void*);
static void readVector1ByteInt(synergy::IStream*, std::vector<UInt8>&);
static void readVector2BytesInt(synergy::IStream*, std::vector<UInt16>&);
static void readVector4BytesInt(synergy::IStream*, std::vector<UInt32>&);
/**
* @brief Handles an array of bytes

View file

@ -124,15 +124,12 @@ TEST_F(ProtocolUtilTests, readf_string)
class ReadfIntTestFixture : public ::testing::TestWithParam< std::tuple<const char*, int> >
{
private:
public:
MockStream stream;
UInt8 StreamData1Byte = 10;
std::array<UInt8, 2> StreamData2Bytes = {0, 10};
std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10};
public:
MockStream stream;
UInt8* getStreamData(int size)
{
UInt8* StreamData = nullptr;
@ -240,8 +237,8 @@ class ReadfIntAndStringTest : public ReadfIntTestFixture
{
public:
UInt8 ActualInt8 = 0;
UInt8 ActualInt16 = 0;
UInt8 ActualInt32 = 32;
UInt16 ActualInt16 = 0;
UInt32 ActualInt32 = 32;
std::string ActualString;
};
@ -307,20 +304,20 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
const UInt8 ExpectedInt = 10;
std::array<UInt8, 4> StreamIntData = {0,0,0,ExpectedInt};
const std::string ExpectedString(32768, 'x');
std::array<UInt8, 4> StringSize = {0,0,128,0};
const std::string ExpectedStr(32768, 'x');
std::array<UInt8, 4> Size = {0,0,128,0};
EXPECT_CALL(stream, read(_, _))
.WillOnce(
DoAll(
SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()),
Return(StringSize.size())
SetValueToVoidPointerArg0(Size.data(), Size.size()),
Return(Size.size())
)
)
.WillOnce(
DoAll(
SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()),
Return(ExpectedString.length())
SetValueToVoidPointerArg0(ExpectedStr.c_str(), ExpectedStr.length()),
Return(ExpectedStr.length())
)
)
.WillOnce(
@ -331,31 +328,31 @@ TEST_F(ProtocolUtilTests, readf_string_and_int4bytes)
);
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4i", &ActualString, &ActualInt32));
EXPECT_EQ(ExpectedString, ActualString);
EXPECT_EQ(ExpectedStr, ActualString);
EXPECT_EQ(ExpectedInt, ActualInt32);
}
TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
{
std::vector<UInt32> Actual4Bytes = {};
std::vector<UInt32> Actual = {};
const std::vector<UInt32> Expected4Bytes = {10,10};
std::array<UInt8, 4> StreamVectorSize = {0,0,0,2};
std::array<UInt8, 4> StreamData4Bytes = {0, 0, 0, 10};
const std::string ExpectedString(32768, 'x');
std::array<UInt8, 4> StringSize = {0,0,128,0};
const std::string ExpString(32768, 'x');
std::array<UInt8, 4> SizeString = {0,0,128,0};
EXPECT_CALL(stream, read(_, _))
.WillOnce(
DoAll(
SetValueToVoidPointerArg0(StringSize.data(), StringSize.size()),
Return(StringSize.size())
SetValueToVoidPointerArg0(SizeString.data(), SizeString.size()),
Return(SizeString.size())
)
)
.WillOnce(
DoAll(
SetValueToVoidPointerArg0(ExpectedString.c_str(), ExpectedString.length()),
Return(ExpectedString.length())
SetValueToVoidPointerArg0(ExpString.c_str(), ExpString.length()),
Return(ExpString.length())
)
)
.WillOnce(
@ -371,9 +368,9 @@ TEST_F(ProtocolUtilTests, readf_string_and_vector_int4bytes)
)
);
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual4Bytes));
EXPECT_EQ(ExpectedString, ActualString);
EXPECT_EQ(Expected4Bytes, Actual4Bytes);
EXPECT_TRUE(ProtocolUtil::readf(&stream, "%s%4I", &ActualString, &Actual));
EXPECT_EQ(ExpString, ActualString);
EXPECT_EQ(Expected4Bytes, Actual);
}
TEST_F(ProtocolUtilTests, readf_vector_int4bytes_and_string)