Milestone 5: deliver embedded RDP sessions and lifecycle hardening

This commit is contained in:
Keith Smith
2026-03-03 18:59:26 -07:00
parent 230a401386
commit 36006bd4aa
2941 changed files with 724359 additions and 77 deletions

View File

@@ -0,0 +1,25 @@
set(MODULE_NAME "TestEnvironment")
set(MODULE_PREFIX "TEST_ENVIRONMENT")
disable_warnings_for_directory(${CMAKE_CURRENT_BINARY_DIR})
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS TestEnvironmentGetEnvironmentStrings.c TestEnvironmentSetEnvironmentVariable.c
TestEnvironmentMergeEnvironmentStrings.c TestEnvironmentGetSetEB.c
)
create_test_sourcelist(${MODULE_PREFIX}_SRCS ${${MODULE_PREFIX}_DRIVER} ${${MODULE_PREFIX}_TESTS})
add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
target_link_libraries(${MODULE_NAME} winpr)
set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
foreach(test ${${MODULE_PREFIX}_TESTS})
get_filename_component(TestName ${test} NAME_WE)
add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
endforeach()
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")

View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
int TestEnvironmentGetEnvironmentStrings(int argc, char* argv[])
{
int r = -1;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
LPTCH lpszEnvironmentBlock = GetEnvironmentStrings();
if (!lpszEnvironmentBlock)
goto fail;
TCHAR* p = lpszEnvironmentBlock;
while (p[0] && p[1])
{
const size_t max = _tcslen(p);
const int rc = _sntprintf(nullptr, 0, _T("%s\n"), p);
if (rc < 1)
{
_tprintf(_T("test failed: return %d\n"), rc);
goto fail;
}
if (max != (size_t)(rc - 1))
{
_tprintf(_T("test failed: length %") _T(PRIuz) _T(" != %d [%s]\n"), max, rc - 1, p);
goto fail;
}
p += (max + 1);
}
r = 0;
fail:
FreeEnvironmentStrings(lpszEnvironmentBlock);
return r;
}

View File

@@ -0,0 +1,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 = nullptr;
DWORD length = 0;
LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0";
LPTCH lpszEnvironmentBlockNew = nullptr;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
rc = -1;
/* Get length of an variable */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", nullptr, 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", nullptr, 0);
if (0 != length)
{
printf("Unset variable returned\n");
goto fail;
}
/* Get length of an similar called variables */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", nullptr, 0);
if (0 != length)
{
printf("Similar named variable returned (XDISPLAY, length %d)\n", length);
goto fail;
}
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAYX", nullptr, 0);
if (0 != length)
{
printf("Similar named variable returned (DISPLAYX, length %d)\n", length);
goto fail;
}
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLA", nullptr, 0);
if (0 != length)
{
printf("Similar named variable returned (DISPLA, length %d)\n", length);
goto fail;
}
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "ISPLAY", nullptr, 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", nullptr))
{
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;
}

View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
int TestEnvironmentMergeEnvironmentStrings(int argc, char* argv[])
{
#ifndef _WIN32
TCHAR* p = nullptr;
size_t length = 0;
LPTCH lpszEnvironmentBlock = nullptr;
LPTCH lpsz2Merge = "SHELL=123\0test=1\0test1=2\0DISPLAY=:77\0\0";
LPTCH lpszMergedEnvironmentBlock = nullptr;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
lpszEnvironmentBlock = GetEnvironmentStrings();
lpszMergedEnvironmentBlock = MergeEnvironmentStrings(lpszEnvironmentBlock, lpsz2Merge);
p = (TCHAR*)lpszMergedEnvironmentBlock;
while (p[0] && p[1])
{
printf("%s\n", p);
length = strlen(p);
p += (length + 1);
}
FreeEnvironmentStrings(lpszMergedEnvironmentBlock);
FreeEnvironmentStrings(lpszEnvironmentBlock);
#endif
return 0;
}

View File

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