Files
FreeRDP/winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c

73 lines
1.5 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
#include <winpr/error.h>
#define TEST_NAME "WINPR_TEST_VARIABLE"
#define TEST_VALUE "WINPR_TEST_VALUE"
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
{
2019-04-05 13:17:24 +02:00
int rc = -1;
DWORD nSize = 0;
2026-02-26 14:32:50 +01:00
LPSTR lpBuffer = nullptr;
DWORD error = 0;
2021-07-29 10:18:52 +02:00
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);
2026-02-26 14:32:50 +01:00
nSize = GetEnvironmentVariableA(TEST_NAME, nullptr, 0);
2019-04-05 13:17:24 +02:00
/* check if value returned is len + 1 ) */
if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)) + 1)
{
printf("GetEnvironmentVariableA not found error\n");
return -1;
}
2019-11-06 15:24:51 +01:00
lpBuffer = (LPSTR)malloc(nSize);
2019-04-05 13:17:24 +02:00
if (!lpBuffer)
return -1;
nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);
if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)))
{
printf("GetEnvironmentVariableA wrong size returned\n");
2019-04-05 13:17:24 +02:00
goto fail;
}
if (strcmp(lpBuffer, TEST_VALUE) != 0)
{
printf("GetEnvironmentVariableA returned value doesn't match\n");
2019-04-05 13:17:24 +02:00
goto fail;
}
2019-04-05 13:17:24 +02:00
nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize);
error = GetLastError();
2019-04-05 13:17:24 +02:00
if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
{
printf("GetEnvironmentVariableA not found error\n");
2019-04-05 13:17:24 +02:00
goto fail;
}
/* clear variable */
2026-02-26 14:32:50 +01:00
SetEnvironmentVariableA(TEST_NAME, nullptr);
nSize = GetEnvironmentVariableA(TEST_VALUE, nullptr, 0);
2019-04-05 13:17:24 +02:00
if (0 != nSize)
{
printf("SetEnvironmentVariableA failed to clear variable\n");
2019-04-05 13:17:24 +02:00
goto fail;
}
2019-04-05 13:17:24 +02:00
rc = 0;
fail:
free(lpBuffer);
return rc;
}