summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/environment/test/TestEnvironmentGetSetEB.c
blob: 603b4c2f09dc7eee9037017ab26209556a04ebef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>

int TestEnvironmentGetSetEB(int argc, char* argv[])
{
	int rc = 0;
#ifndef _WIN32
	char test[1024];
	TCHAR* p = NULL;
	DWORD length = 0;
	LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0";
	LPTCH lpszEnvironmentBlockNew = NULL;

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	rc = -1;
	/* Get length of an variable */
	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", NULL, 0);

	if (0 == length)
		return -1;

	/* Get the variable itself */
	p = (LPSTR)malloc(length);

	if (!p)
		goto fail;

	if (GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", p, length) != length - 1)
		goto fail;

	printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n", p);

	if (strcmp(p, "WINPR_TEST_VALUE") != 0)
		goto fail;

	/* Get length of an non-existing variable */
	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "BLA", NULL, 0);

	if (0 != length)
	{
		printf("Unset variable returned\n");
		goto fail;
	}

	/* Get length of an similar called variables */
	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", NULL, 0);

	if (0 != length)
	{
		printf("Similar named variable returned (XDISPLAY, length %d)\n", length);
		goto fail;
	}

	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAYX", NULL, 0);

	if (0 != length)
	{
		printf("Similar named variable returned (DISPLAYX, length %d)\n", length);
		goto fail;
	}

	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLA", NULL, 0);

	if (0 != length)
	{
		printf("Similar named variable returned (DISPLA, length %d)\n", length);
		goto fail;
	}

	length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "ISPLAY", NULL, 0);

	if (0 != length)
	{
		printf("Similar named variable returned (ISPLAY, length %d)\n", length);
		goto fail;
	}

	/* Set variable in empty environment block */
	if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
	{
		if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
		{
			if (strcmp(test, "5") != 0)
				goto fail;
		}
		else
			goto fail;
	}

	/* Clear variable */
	if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", NULL))
	{
		if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
			goto fail;
		else
		{
			// not found .. this is expected
		}
	}

	free(lpszEnvironmentBlockNew);
	lpszEnvironmentBlockNew = (LPTCH)calloc(1024, sizeof(TCHAR));

	if (!lpszEnvironmentBlockNew)
		goto fail;

	memcpy(lpszEnvironmentBlockNew, lpszEnvironmentBlock, length);

	/* Set variable in empty environment block */
	if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
	{
		if (0 != GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "testr", test, 1023))
		{
			printf("GetEnvironmentVariableEBA returned unset variable\n");
			goto fail;
		}

		if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
		{
			if (strcmp(test, "5") != 0)
				goto fail;
		}
		else
			goto fail;
	}

	rc = 0;
fail:
	free(p);
	free(lpszEnvironmentBlockNew);
#endif
	return rc;
}