2013-09-23 22:44:59 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/tchar.h>
|
|
|
|
#include <winpr/environment.h>
|
2014-04-17 13:48:50 +04:00
|
|
|
#include <winpr/error.h>
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
#define TEST_NAME "WINPR_TEST_VARIABLE"
|
|
|
|
#define TEST_VALUE "WINPR_TEST_VALUE"
|
2013-09-23 22:44:59 +04:00
|
|
|
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
|
|
|
|
{
|
2019-04-05 14:17:24 +03:00
|
|
|
int rc = -1;
|
2013-09-23 22:44:59 +04:00
|
|
|
DWORD nSize;
|
2019-04-05 14:17:24 +03:00
|
|
|
LPSTR lpBuffer = NULL;
|
2014-04-17 13:48:50 +04:00
|
|
|
DWORD error = 0;
|
|
|
|
SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);
|
|
|
|
nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
|
2019-04-05 14:17:24 +03:00
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
/* check if value returned is len + 1 ) */
|
|
|
|
if (nSize != strlen(TEST_VALUE) + 1)
|
|
|
|
{
|
|
|
|
printf("GetEnvironmentVariableA not found error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-23 22:44:59 +04:00
|
|
|
|
|
|
|
lpBuffer = (LPSTR) malloc(nSize);
|
2019-04-05 14:17:24 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!lpBuffer)
|
|
|
|
return -1;
|
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
if (nSize != strlen(TEST_VALUE))
|
|
|
|
{
|
|
|
|
printf("GetEnvironmentVariableA wrong size returned\n");
|
2019-04-05 14:17:24 +03:00
|
|
|
goto fail;
|
2014-04-17 13:48:50 +04:00
|
|
|
}
|
2013-09-23 22:44:59 +04:00
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
if (strcmp(lpBuffer, TEST_VALUE) != 0)
|
2013-09-23 22:44:59 +04:00
|
|
|
{
|
2014-04-17 13:48:50 +04:00
|
|
|
printf("GetEnvironmentVariableA returned value doesn't match\n");
|
2019-04-05 14:17:24 +03:00
|
|
|
goto fail;
|
2014-04-17 13:48:50 +04:00
|
|
|
}
|
|
|
|
|
2019-04-05 14:17:24 +03:00
|
|
|
nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize);
|
2014-04-17 13:48:50 +04:00
|
|
|
error = GetLastError();
|
2019-04-05 14:17:24 +03:00
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
|
|
|
|
{
|
|
|
|
printf("GetEnvironmentVariableA not found error\n");
|
2019-04-05 14:17:24 +03:00
|
|
|
goto fail;
|
2013-09-23 22:44:59 +04:00
|
|
|
}
|
|
|
|
|
2014-04-17 13:48:50 +04:00
|
|
|
/* clear variable */
|
|
|
|
SetEnvironmentVariableA(TEST_NAME, NULL);
|
|
|
|
nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0);
|
2019-04-05 14:17:24 +03:00
|
|
|
|
|
|
|
if (0 != nSize)
|
2014-04-17 13:48:50 +04:00
|
|
|
{
|
|
|
|
printf("SetEnvironmentVariableA failed to clear variable\n");
|
2019-04-05 14:17:24 +03:00
|
|
|
goto fail;
|
2014-04-17 13:48:50 +04:00
|
|
|
}
|
2019-04-05 14:17:24 +03:00
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
fail:
|
|
|
|
free(lpBuffer);
|
|
|
|
return rc;
|
2013-09-23 22:44:59 +04:00
|
|
|
}
|
|
|
|
|