| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/tchar.h> |
| 5 |
#include <winpr/environment.h> |
| 6 |
|
| 7 |
int TestEnvironmentGetSetEB(int argc, char* argv[]) |
| 8 |
{ |
| 9 |
int rc = 0; |
| 10 |
#ifndef _WIN32 |
| 11 |
char test[1024]; |
| 12 |
TCHAR* p = nullptr; |
| 13 |
DWORD length = 0; |
| 14 |
LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0"; |
| 15 |
LPTCH lpszEnvironmentBlockNew = nullptr; |
| 16 |
|
| 17 |
WINPR_UNUSED(argc); |
| 18 |
WINPR_UNUSED(argv); |
| 19 |
|
| 20 |
rc = -1; |
| 21 |
/* Get length of an variable */ |
| 22 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", nullptr, 0); |
| 23 |
|
| 24 |
if (0 == length) |
| 25 |
return -1; |
| 26 |
|
| 27 |
/* Get the variable itself */ |
| 28 |
p = (LPSTR)malloc(length); |
| 29 |
|
| 30 |
if (!p) |
| 31 |
goto fail; |
| 32 |
|
| 33 |
if (GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", p, length) != length - 1) |
| 34 |
goto fail; |
| 35 |
|
| 36 |
printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n", p); |
| 37 |
|
| 38 |
if (strcmp(p, "WINPR_TEST_VALUE") != 0) |
| 39 |
goto fail; |
| 40 |
|
| 41 |
/* Get length of an non-existing variable */ |
| 42 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "BLA", nullptr, 0); |
| 43 |
|
| 44 |
if (0 != length) |
| 45 |
{ |
| 46 |
printf("Unset variable returned\n"); |
| 47 |
goto fail; |
| 48 |
} |
| 49 |
|
| 50 |
/* Get length of an similar called variables */ |
| 51 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", nullptr, 0); |
| 52 |
|
| 53 |
if (0 != length) |
| 54 |
{ |
| 55 |
printf("Similar named variable returned (XDISPLAY, length %d)\n", length); |
| 56 |
goto fail; |
| 57 |
} |
| 58 |
|
| 59 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAYX", nullptr, 0); |
| 60 |
|
| 61 |
if (0 != length) |
| 62 |
{ |
| 63 |
printf("Similar named variable returned (DISPLAYX, length %d)\n", length); |
| 64 |
goto fail; |
| 65 |
} |
| 66 |
|
| 67 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLA", nullptr, 0); |
| 68 |
|
| 69 |
if (0 != length) |
| 70 |
{ |
| 71 |
printf("Similar named variable returned (DISPLA, length %d)\n", length); |
| 72 |
goto fail; |
| 73 |
} |
| 74 |
|
| 75 |
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "ISPLAY", nullptr, 0); |
| 76 |
|
| 77 |
if (0 != length) |
| 78 |
{ |
| 79 |
printf("Similar named variable returned (ISPLAY, length %d)\n", length); |
| 80 |
goto fail; |
| 81 |
} |
| 82 |
|
| 83 |
/* Set variable in empty environment block */ |
| 84 |
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5")) |
| 85 |
{ |
| 86 |
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023)) |
| 87 |
{ |
| 88 |
if (strcmp(test, "5") != 0) |
| 89 |
goto fail; |
| 90 |
} |
| 91 |
else |
| 92 |
goto fail; |
| 93 |
} |
| 94 |
|
| 95 |
/* Clear variable */ |
| 96 |
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", nullptr)) |
| 97 |
{ |
| 98 |
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023)) |
| 99 |
goto fail; |
| 100 |
else |
| 101 |
{ |
| 102 |
// not found .. this is expected |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
free(lpszEnvironmentBlockNew); |
| 107 |
lpszEnvironmentBlockNew = (LPTCH)calloc(1024, sizeof(TCHAR)); |
| 108 |
|
| 109 |
if (!lpszEnvironmentBlockNew) |
| 110 |
goto fail; |
| 111 |
|
| 112 |
memcpy(lpszEnvironmentBlockNew, lpszEnvironmentBlock, length); |
| 113 |
|
| 114 |
/* Set variable in empty environment block */ |
| 115 |
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5")) |
| 116 |
{ |
| 117 |
if (0 != GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "testr", test, 1023)) |
| 118 |
{ |
| 119 |
printf("GetEnvironmentVariableEBA returned unset variable\n"); |
| 120 |
goto fail; |
| 121 |
} |
| 122 |
|
| 123 |
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023)) |
| 124 |
{ |
| 125 |
if (strcmp(test, "5") != 0) |
| 126 |
goto fail; |
| 127 |
} |
| 128 |
else |
| 129 |
goto fail; |
| 130 |
} |
| 131 |
|
| 132 |
rc = 0; |
| 133 |
fail: |
| 134 |
free(p); |
| 135 |
free(lpszEnvironmentBlockNew); |
| 136 |
#endif |
| 137 |
return rc; |
| 138 |
} |