Blame winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c

Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
#include <winpr/environment.h>
Packit 1fb8d4
#include <winpr/error.h>
Packit 1fb8d4
Packit 1fb8d4
#define TEST_NAME "WINPR_TEST_VARIABLE"
Packit 1fb8d4
#define TEST_VALUE "WINPR_TEST_VALUE"
Packit 1fb8d4
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
Packit 1fb8d4
{
Packit Service 5a9772
	int rc = -1;
Packit 1fb8d4
	DWORD nSize;
Packit Service 5a9772
	LPSTR lpBuffer = NULL;
Packit 1fb8d4
	DWORD error = 0;
Packit 1fb8d4
	SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);
Packit 1fb8d4
	nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
Packit Service 5a9772
Packit 1fb8d4
	/* check if value returned is len + 1 ) */
Packit Service 5a9772
	if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)) + 1)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("GetEnvironmentVariableA not found error\n");
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	lpBuffer = (LPSTR)malloc(nSize);
Packit Service 5a9772
Packit 1fb8d4
	if (!lpBuffer)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);
Packit 1fb8d4
Packit Service 5a9772
	if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)))
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("GetEnvironmentVariableA wrong size returned\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (strcmp(lpBuffer, TEST_VALUE) != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("GetEnvironmentVariableA returned value doesn't match\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize);
Packit 1fb8d4
	error = GetLastError();
Packit Service 5a9772
Packit 1fb8d4
	if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("GetEnvironmentVariableA not found error\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* clear variable */
Packit 1fb8d4
	SetEnvironmentVariableA(TEST_NAME, NULL);
Packit 1fb8d4
	nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0);
Packit Service 5a9772
Packit Service 5a9772
	if (0 != nSize)
Packit 1fb8d4
	{
Packit 1fb8d4
		printf("SetEnvironmentVariableA failed to clear variable\n");
Packit Service 5a9772
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	rc = 0;
Packit Service 5a9772
fail:
Packit Service 5a9772
	free(lpBuffer);
Packit Service 5a9772
	return rc;
Packit Service 5a9772
}