| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/tchar.h> |
| 5 |
#include <winpr/environment.h> |
| 6 |
#include <winpr/error.h> |
| 7 |
|
| 8 |
#define TEST_NAME "WINPR_TEST_VARIABLE" |
| 9 |
#define TEST_VALUE "WINPR_TEST_VALUE" |
| 10 |
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[]) |
| 11 |
{ |
| 12 |
int rc = -1; |
| 13 |
DWORD nSize = 0; |
| 14 |
LPSTR lpBuffer = nullptr; |
| 15 |
DWORD error = 0; |
| 16 |
|
| 17 |
WINPR_UNUSED(argc); |
| 18 |
WINPR_UNUSED(argv); |
| 19 |
|
| 20 |
SetEnvironmentVariableA(TEST_NAME, TEST_VALUE); |
| 21 |
nSize = GetEnvironmentVariableA(TEST_NAME, nullptr, 0); |
| 22 |
|
| 23 |
/* check if value returned is len + 1 ) */ |
| 24 |
if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)) + 1) |
| 25 |
{ |
| 26 |
printf("GetEnvironmentVariableA not found error\n"); |
| 27 |
return -1; |
| 28 |
} |
| 29 |
|
| 30 |
lpBuffer = (LPSTR)malloc(nSize); |
| 31 |
|
| 32 |
if (!lpBuffer) |
| 33 |
return -1; |
| 34 |
|
| 35 |
nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize); |
| 36 |
|
| 37 |
if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE))) |
| 38 |
{ |
| 39 |
printf("GetEnvironmentVariableA wrong size returned\n"); |
| 40 |
goto fail; |
| 41 |
} |
| 42 |
|
| 43 |
if (strcmp(lpBuffer, TEST_VALUE) != 0) |
| 44 |
{ |
| 45 |
printf("GetEnvironmentVariableA returned value doesn't match\n"); |
| 46 |
goto fail; |
| 47 |
} |
| 48 |
|
| 49 |
nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize); |
| 50 |
error = GetLastError(); |
| 51 |
|
| 52 |
if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error) |
| 53 |
{ |
| 54 |
printf("GetEnvironmentVariableA not found error\n"); |
| 55 |
goto fail; |
| 56 |
} |
| 57 |
|
| 58 |
/* clear variable */ |
| 59 |
SetEnvironmentVariableA(TEST_NAME, nullptr); |
| 60 |
nSize = GetEnvironmentVariableA(TEST_VALUE, nullptr, 0); |
| 61 |
|
| 62 |
if (0 != nSize) |
| 63 |
{ |
| 64 |
printf("SetEnvironmentVariableA failed to clear variable\n"); |
| 65 |
goto fail; |
| 66 |
} |
| 67 |
|
| 68 |
rc = 0; |
| 69 |
fail: |
| 70 |
free(lpBuffer); |
| 71 |
return rc; |
| 72 |
} |