Settings getter tests (#7869)

* Added tests for freerdp_[static|dynamic]_collection_* functions

* Added settings unit test for clone functions

* Fix argument for winpr_RAND and winpr_RAND_pseudo

Using void* eliminates all assumptions of which type the buffer is.
This eliminates unnecessary warnings.
This commit is contained in:
akallabeth 2022-05-02 11:39:38 +02:00 committed by GitHub
parent 3d9c972d5c
commit 6ce3499e13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 304 additions and 11 deletions

View File

@ -573,9 +573,13 @@ BOOL freerdp_static_channel_collection_del(rdpSettings* settings, const char* na
{
if (strcmp(name, cur->argv[0]) == 0)
{
const size_t rem = settings->StaticChannelArraySize - count + 1;
memmove_s(&settings->StaticChannelArray[x], (count - x) * sizeof(ADDIN_ARGV*),
&settings->StaticChannelArray[x + 1],
(count - x - 1) * sizeof(ADDIN_ARGV*));
memset(&settings->StaticChannelArray[count - 1], 0, sizeof(ADDIN_ARGV*) * rem);
freerdp_addin_argv_free(cur);
return freerdp_settings_set_uint32(settings, FreeRDP_StaticChannelCount, count - 1);
}
}
@ -675,14 +679,15 @@ BOOL freerdp_dynamic_channel_collection_del(rdpSettings* settings, const char* n
ADDIN_ARGV* cur = settings->DynamicChannelArray[x];
if (cur && (cur->argc > 0))
{
if (strcmp(name, cur->argv[0]))
if (strcmp(name, cur->argv[0]) == 0)
{
const size_t rem = settings->DynamicChannelArraySize - count;
const size_t rem = settings->DynamicChannelArraySize - count + 1;
memmove_s(&settings->DynamicChannelArray[x], (count - x) * sizeof(ADDIN_ARGV*),
&settings->DynamicChannelArray[x + 1],
(count - x - 1) * sizeof(ADDIN_ARGV*));
memset(&settings->DynamicChannelArray[count], 0, sizeof(ADDIN_ARGV*) * rem);
memset(&settings->DynamicChannelArray[count - 1], 0, sizeof(ADDIN_ARGV*) * rem);
freerdp_addin_argv_free(cur);
return freerdp_settings_set_uint32(settings, FreeRDP_DynamicChannelCount,
count - 1);
}

View File

@ -503,6 +503,13 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
const rdpSettings* other)
{
size_t x;
BOOL rc = FALSE;
if (!settings && other)
rc = TRUE;
if (settings && !other)
rc = TRUE;
for (x = 0; x < ARRAYSIZE(settings_map); x++)
{
const struct settings_str_entry* cur = &settings_map[x];
@ -513,8 +520,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
BOOL sval = freerdp_settings_get_bool(settings, cur->id);
BOOL cval = freerdp_settings_get_bool(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [BOOL]: %s -> %s", cur->str, sval ? "TRUE" : "FALSE",
cval ? "TRUE" : "FALSE");
rc = TRUE;
}
}
break;
case 1: /* UINT16 */
@ -522,8 +532,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
UINT16 sval = freerdp_settings_get_uint16(settings, cur->id);
UINT16 cval = freerdp_settings_get_uint16(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [UINT16]: %" PRIu16 " -> %" PRIu16, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 2: /* INT16 */
@ -531,8 +544,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
INT16 sval = freerdp_settings_get_int16(settings, cur->id);
INT16 cval = freerdp_settings_get_int16(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [INT16]: %" PRId16 " -> %" PRId16, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 3: /* UINT32 */
@ -540,8 +556,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
UINT32 sval = freerdp_settings_get_uint32(settings, cur->id);
UINT32 cval = freerdp_settings_get_uint32(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [UINT32]: %" PRIu32 " -> %" PRIu32, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 4: /* INT32 */
@ -549,8 +568,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
INT32 sval = freerdp_settings_get_int32(settings, cur->id);
INT32 cval = freerdp_settings_get_int32(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [INT32]: %" PRId32 " -> %" PRId32, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 5: /* UINT64 */
@ -558,8 +580,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
UINT64 sval = freerdp_settings_get_uint64(settings, cur->id);
UINT64 cval = freerdp_settings_get_uint64(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [UINT64]: %" PRIu64 " -> %" PRIu64, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 6: /* INT64 */
@ -567,8 +592,11 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
INT64 sval = freerdp_settings_get_int64(settings, cur->id);
INT64 cval = freerdp_settings_get_int64(other, cur->id);
if (sval != cval)
{
WLog_Print(log, level, "%s [INT64]: %" PRId64 " -> %" PRId64, cur->str, sval,
cval);
rc = TRUE;
}
}
break;
case 7: /* strings */
@ -578,7 +606,10 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
if (sval != cval)
{
if (!sval || !cval || (strcmp(sval, cval) != 0))
{
WLog_Print(log, level, "%s [STRING]: '%s' -> '%s'", cur->str, sval, cval);
rc = TRUE;
}
}
}
break;
@ -587,12 +618,18 @@ BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* sett
const void* sval = freerdp_settings_get_pointer(settings, cur->id);
const void* cval = freerdp_settings_get_pointer(other, cur->id);
if (sval != cval)
WLog_Print(log, level, "%s [POINTER]: '%p' -> '%p'", cur->str, sval, cval);
{
if ((sval && !cval) || (!sval && cval))
{
WLog_Print(log, level, "%s [POINTER]: '%p' -> '%p'", cur->str, sval, cval);
rc = TRUE;
}
}
}
break;
}
}
return TRUE;
return rc;
}
void freerdp_settings_dump(wLog* log, DWORD level, const rdpSettings* settings)
{

View File

@ -1,6 +1,250 @@
#include <freerdp/settings.h>
#include "settings_property_lists.h"
static BOOL compare(const ADDIN_ARGV* got, const ADDIN_ARGV* expect)
{
int x;
if (!got && expect)
return FALSE;
if (got && !expect)
return FALSE;
if (got->argc != expect->argc)
return FALSE;
for (x = 0; x < expect->argc; x++)
{
if (strcmp(got->argv[x], expect->argv[x]) != 0)
return FALSE;
}
return TRUE;
}
static BOOL test_dyn_channels(void)
{
BOOL rc = FALSE;
BOOL test;
UINT32 u32;
rdpSettings* settings = freerdp_settings_new(0);
const char* argv1[] = { "foobar" };
ADDIN_ARGV* args1 = NULL;
const ADDIN_ARGV* cmp1;
const char* argv2[] = { "gaga", "abba", "foo" };
ADDIN_ARGV* args2 = NULL;
const ADDIN_ARGV* cmp2;
const ADDIN_ARGV* got;
if (!settings)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelCount);
if (u32 != 0)
goto fail;
/* Test the function return an error for unknown channels */
test = freerdp_dynamic_channel_collection_del(settings, "foobar");
if (test)
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "foobar");
if (got)
goto fail;
/* Add the channel */
cmp1 = args1 = freerdp_addin_argv_new(ARRAYSIZE(argv1), argv1);
test = freerdp_dynamic_channel_collection_add(settings, args1);
if (!test)
goto fail;
args1 = NULL; /* settings have taken ownership */
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelCount);
if (u32 != 1)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelArraySize);
if (u32 < 1)
goto fail;
cmp2 = args2 = freerdp_addin_argv_new(ARRAYSIZE(argv2), argv2);
test = freerdp_dynamic_channel_collection_add(settings, args2);
if (!test)
goto fail;
args2 = NULL; /* settings have taken ownership */
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelCount);
if (u32 != 2)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelArraySize);
if (u32 < 2)
goto fail;
/* Test the function return success for known channels */
got = freerdp_dynamic_channel_collection_find(settings, "foobar");
if (!compare(got, cmp1))
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "gaga");
if (!compare(got, cmp2))
goto fail;
test = freerdp_dynamic_channel_collection_del(settings, "foobar");
if (!test)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelCount);
if (u32 != 1)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelArraySize);
if (u32 < 1)
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "foobar");
if (compare(got, cmp1))
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "gaga");
if (!compare(got, cmp2))
goto fail;
test = freerdp_dynamic_channel_collection_del(settings, "gaga");
if (!test)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_DynamicChannelCount);
if (u32 != 0)
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "foobar");
if (compare(got, cmp1))
goto fail;
got = freerdp_dynamic_channel_collection_find(settings, "gaga");
if (compare(got, cmp2))
goto fail;
rc = TRUE;
fail:
freerdp_settings_free(settings);
freerdp_addin_argv_free(args1);
freerdp_addin_argv_free(args2);
return rc;
}
static BOOL test_static_channels(void)
{
BOOL rc = FALSE;
BOOL test;
UINT32 u32;
rdpSettings* settings = freerdp_settings_new(0);
const char* argv1[] = { "foobar" };
ADDIN_ARGV* args1 = NULL;
const ADDIN_ARGV* cmp1;
const char* argv2[] = { "gaga", "abba", "foo" };
ADDIN_ARGV* args2 = NULL;
const ADDIN_ARGV* cmp2;
const ADDIN_ARGV* got;
if (!settings)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelCount);
if (u32 != 0)
goto fail;
/* Test the function return an error for unknown channels */
test = freerdp_static_channel_collection_del(settings, "foobar");
if (test)
goto fail;
got = freerdp_static_channel_collection_find(settings, "foobar");
if (got)
goto fail;
/* Add the channel */
cmp1 = args1 = freerdp_addin_argv_new(ARRAYSIZE(argv1), argv1);
test = freerdp_static_channel_collection_add(settings, args1);
if (!test)
goto fail;
args1 = NULL; /* settings have taken ownership */
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelCount);
if (u32 != 1)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelArraySize);
if (u32 < 1)
goto fail;
cmp2 = args2 = freerdp_addin_argv_new(ARRAYSIZE(argv2), argv2);
test = freerdp_static_channel_collection_add(settings, args2);
if (!test)
goto fail;
args2 = NULL; /* settings have taken ownership */
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelCount);
if (u32 != 2)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelArraySize);
if (u32 < 2)
goto fail;
/* Test the function return success for known channels */
got = freerdp_static_channel_collection_find(settings, "foobar");
if (!compare(got, cmp1))
goto fail;
got = freerdp_static_channel_collection_find(settings, "gaga");
if (!compare(got, cmp2))
goto fail;
test = freerdp_static_channel_collection_del(settings, "foobar");
if (!test)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelCount);
if (u32 != 1)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelArraySize);
if (u32 < 1)
goto fail;
got = freerdp_static_channel_collection_find(settings, "foobar");
if (compare(got, cmp1))
goto fail;
got = freerdp_static_channel_collection_find(settings, "gaga");
if (!compare(got, cmp2))
goto fail;
test = freerdp_static_channel_collection_del(settings, "gaga");
if (!test)
goto fail;
u32 = freerdp_settings_get_uint32(settings, FreeRDP_StaticChannelCount);
if (u32 != 0)
goto fail;
got = freerdp_static_channel_collection_find(settings, "foobar");
if (compare(got, cmp1))
goto fail;
got = freerdp_static_channel_collection_find(settings, "gaga");
if (compare(got, cmp2))
goto fail;
rc = TRUE;
fail:
freerdp_settings_free(settings);
freerdp_addin_argv_free(args1);
freerdp_addin_argv_free(args2);
return rc;
}
static BOOL test_copy(void)
{
BOOL rc = FALSE;
wLog* log = WLog_Get(__FUNCTION__);
rdpSettings* settings = freerdp_settings_new(0);
rdpSettings* copy = freerdp_settings_clone(settings);
rdpSettings* modified = freerdp_settings_clone(settings);
if (!settings || !copy || !modified)
goto fail;
if (!freerdp_settings_set_string(modified, FreeRDP_ServerHostname, "somerandomname"))
goto fail;
if (freerdp_settings_print_diff(log, WLOG_WARN, settings, copy))
goto fail;
if (!freerdp_settings_print_diff(log, WLOG_WARN, settings, modified))
goto fail;
rc = TRUE;
fail:
freerdp_settings_free(settings);
freerdp_settings_free(copy);
freerdp_settings_free(modified);
return rc;
}
int TestSettings(int argc, char* argv[])
{
int rc = -1;
@ -10,6 +254,13 @@ int TestSettings(int argc, char* argv[])
rdpSettings* cloned2 = NULL;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!test_dyn_channels())
return -1;
if (!test_static_channels())
return -1;
if (!test_copy())
return -1;
settings = freerdp_settings_new(0);
if (!settings)

View File

@ -734,8 +734,8 @@ extern "C"
{
#endif
WINPR_API int winpr_RAND(BYTE* output, size_t len);
WINPR_API int winpr_RAND_pseudo(BYTE* output, size_t len);
WINPR_API int winpr_RAND(void* output, size_t len);
WINPR_API int winpr_RAND_pseudo(void* output, size_t len);
#ifdef __cplusplus
}

View File

@ -34,7 +34,7 @@
#include <mbedtls/hmac_drbg.h>
#endif
int winpr_RAND(BYTE* output, size_t len)
int winpr_RAND(void* output, size_t len)
{
#if defined(WITH_OPENSSL)
if (len > INT_MAX)
@ -53,7 +53,7 @@ int winpr_RAND(BYTE* output, size_t len)
return 0;
}
int winpr_RAND_pseudo(BYTE* output, size_t len)
int winpr_RAND_pseudo(void* output, size_t len)
{
return winpr_RAND(output, len);
}

View File

@ -693,13 +693,13 @@ static UUID UUID_NIL = {
RPC_STATUS UuidCreate(UUID* Uuid)
{
winpr_RAND_pseudo((BYTE*)Uuid, 16);
winpr_RAND_pseudo(Uuid, 16);
return RPC_S_OK;
}
RPC_STATUS UuidCreateSequential(UUID* Uuid)
{
winpr_RAND_pseudo((BYTE*)Uuid, 16);
winpr_RAND_pseudo(Uuid, 16);
return RPC_S_OK;
}