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,23 @@
set(MODULE_NAME "TestThread")
set(MODULE_PREFIX "TEST_THREAD")
disable_warnings_for_directory(${CMAKE_CURRENT_BINARY_DIR})
set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
set(${MODULE_PREFIX}_TESTS TestThreadCommandLineToArgv.c TestThreadCreateProcess.c TestThreadExitThread.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,116 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/thread.h>
static const char* test_args_line_1 = "app.exe abc d e";
static const char* test_args_list_1[] = { "app.exe", "abc", "d", "e" };
static const char* test_args_line_2 = "app.exe abc \t def";
static const char* test_args_list_2[] = { "app.exe", "abc", "def" };
static const char* test_args_line_3 = "app.exe \"abc\" d e";
static const char* test_args_list_3[] = { "app.exe", "abc", "d", "e" };
static const char* test_args_line_4 = "app.exe a\\\\b d\"e f\"g h";
static const char* test_args_list_4[] = { "app.exe", "a\\\\b", "de fg", "h" };
static const char* test_args_line_5 = "app.exe a\\\\\\\"b c d";
static const char* test_args_list_5[] = { "app.exe", "a\\\"b", "c", "d" };
static const char* test_args_line_6 = "app.exe a\\\\\\\\\"b c\" d e";
static const char* test_args_list_6[] = { "app.exe", "a\\\\b c", "d", "e" };
static const char* test_args_line_7 = "app.exe a\\\\\\\\\"b c\" d e f\\\\\\\\\"g h\" i j";
static const char* test_args_list_7[] = { "app.exe", "a\\\\b c", "d", "e", "f\\\\g h", "i", "j" };
static const char* test_args_line_8 = "app.exe arg1 \"arg2\"";
static const char* test_args_list_8[] = { "app.exe", "arg1", "arg2" };
static BOOL test_command_line_parsing_case(const char* line, const char** list, size_t expect)
{
BOOL rc = FALSE;
int numArgs = 0;
printf("Parsing: %s\n", line);
LPSTR* pArgs = CommandLineToArgvA(line, &numArgs);
if (numArgs < 0)
{
(void)fprintf(stderr, "expected %" PRIuz " arguments, got %d return\n", expect, numArgs);
goto fail;
}
if (numArgs != expect)
{
(void)fprintf(stderr, "expected %" PRIuz " arguments, got %d from '%s'\n", expect, numArgs,
line);
goto fail;
}
if ((numArgs > 0) && !pArgs)
{
(void)fprintf(stderr, "expected %d arguments, got nullptr return\n", numArgs);
goto fail;
}
printf("pNumArgs: %d\n", numArgs);
for (int i = 0; i < numArgs; i++)
{
printf("argv[%d] = %s\n", i, pArgs[i]);
if (strcmp(pArgs[i], list[i]) != 0)
{
(void)fprintf(stderr, "failed at argument %d: got '%s' but expected '%s'\n", i,
pArgs[i], list[i]);
goto fail;
}
}
rc = TRUE;
fail:
free((void*)pArgs);
return rc;
}
int TestThreadCommandLineToArgv(int argc, char* argv[])
{
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
if (!test_command_line_parsing_case(test_args_line_1, test_args_list_1,
ARRAYSIZE(test_args_list_1)))
return -1;
if (!test_command_line_parsing_case(test_args_line_2, test_args_list_2,
ARRAYSIZE(test_args_list_2)))
return -1;
if (!test_command_line_parsing_case(test_args_line_3, test_args_list_3,
ARRAYSIZE(test_args_list_3)))
return -1;
if (!test_command_line_parsing_case(test_args_line_4, test_args_list_4,
ARRAYSIZE(test_args_list_4)))
return -1;
if (!test_command_line_parsing_case(test_args_line_5, test_args_list_5,
ARRAYSIZE(test_args_list_5)))
return -1;
if (!test_command_line_parsing_case(test_args_line_6, test_args_list_6,
ARRAYSIZE(test_args_list_6)))
return -1;
if (!test_command_line_parsing_case(test_args_line_7, test_args_list_7,
ARRAYSIZE(test_args_list_7)))
return -1;
if (!test_command_line_parsing_case(test_args_line_8, test_args_list_8,
ARRAYSIZE(test_args_list_8)))
return -1;
return 0;
}

View File

@@ -0,0 +1,156 @@
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/tchar.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
#include <winpr/environment.h>
#include <winpr/pipe.h>
#define TESTENV_A "HELLO=WORLD"
#define TESTENV_T _T(TESTENV_A)
int TestThreadCreateProcess(int argc, char* argv[])
{
BOOL status = 0;
DWORD exitCode = 0;
LPCTSTR lpApplicationName = nullptr;
#ifdef _WIN32
TCHAR lpCommandLine[200] = _T("cmd /C set");
#else
TCHAR lpCommandLine[200] = _T("printenv");
#endif
// LPTSTR lpCommandLine;
LPSECURITY_ATTRIBUTES lpProcessAttributes = nullptr;
LPSECURITY_ATTRIBUTES lpThreadAttributes = nullptr;
BOOL bInheritHandles = 0;
DWORD dwCreationFlags = 0;
LPVOID lpEnvironment = nullptr;
LPCTSTR lpCurrentDirectory = nullptr;
STARTUPINFO StartupInfo = WINPR_C_ARRAY_INIT;
PROCESS_INFORMATION ProcessInformation = WINPR_C_ARRAY_INIT;
LPTCH lpszEnvironmentBlock = nullptr;
HANDLE pipe_read = nullptr;
HANDLE pipe_write = nullptr;
char buf[1024] = WINPR_C_ARRAY_INIT;
DWORD read_bytes = 0;
int ret = 0;
SECURITY_ATTRIBUTES saAttr;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
lpszEnvironmentBlock = GetEnvironmentStrings();
lpApplicationName = nullptr;
lpProcessAttributes = nullptr;
lpThreadAttributes = nullptr;
bInheritHandles = FALSE;
dwCreationFlags = 0;
#ifdef _UNICODE
dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
#endif
lpEnvironment = lpszEnvironmentBlock;
lpCurrentDirectory = nullptr;
StartupInfo.cb = sizeof(STARTUPINFO);
status = CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
lpCurrentDirectory, &StartupInfo, &ProcessInformation);
if (!status)
{
printf("CreateProcess failed. error=%" PRIu32 "\n", GetLastError());
return 1;
}
if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0)
{
printf("Failed to wait for first process. error=%" PRIu32 "\n", GetLastError());
return 1;
}
exitCode = 0;
status = GetExitCodeProcess(ProcessInformation.hProcess, &exitCode);
printf("GetExitCodeProcess status: %" PRId32 "\n", status);
printf("Process exited with code: 0x%08" PRIX32 "\n", exitCode);
(void)CloseHandle(ProcessInformation.hProcess);
(void)CloseHandle(ProcessInformation.hThread);
FreeEnvironmentStrings(lpszEnvironmentBlock);
/* Test stdin,stdout,stderr redirection */
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = nullptr;
if (!CreatePipe(&pipe_read, &pipe_write, &saAttr, 0))
{
printf("Pipe creation failed. error=%" PRIu32 "\n", GetLastError());
return 1;
}
bInheritHandles = TRUE;
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.hStdOutput = pipe_write;
StartupInfo.hStdError = pipe_write;
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION));
if (!(lpEnvironment = calloc(1, sizeof(TESTENV_T) + sizeof(TCHAR))))
{
printf("Failed to allocate environment buffer. error=%" PRIu32 "\n", GetLastError());
return 1;
}
memcpy(lpEnvironment, (void*)TESTENV_T, sizeof(TESTENV_T));
status = CreateProcess(lpApplicationName, lpCommandLine, lpProcessAttributes,
lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
lpCurrentDirectory, &StartupInfo, &ProcessInformation);
free(lpEnvironment);
if (!status)
{
(void)CloseHandle(pipe_read);
(void)CloseHandle(pipe_write);
printf("CreateProcess failed. error=%" PRIu32 "\n", GetLastError());
return 1;
}
if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0)
{
printf("Failed to wait for second process. error=%" PRIu32 "\n", GetLastError());
return 1;
}
ZeroMemory(buf, sizeof(buf));
ReadFile(pipe_read, buf, sizeof(buf) - 1, &read_bytes, nullptr);
if (!strstr((const char*)buf, TESTENV_A))
{
printf("No or unexpected data read from pipe\n");
ret = 1;
}
(void)CloseHandle(pipe_read);
(void)CloseHandle(pipe_write);
exitCode = 0;
status = GetExitCodeProcess(ProcessInformation.hProcess, &exitCode);
printf("GetExitCodeProcess status: %" PRId32 "\n", status);
printf("Process exited with code: 0x%08" PRIX32 "\n", exitCode);
(void)CloseHandle(ProcessInformation.hProcess);
(void)CloseHandle(ProcessInformation.hThread);
return ret;
}

View File

@@ -0,0 +1,53 @@
// Copyright © 2015 Hewlett-Packard Development Company, L.P.
#include <winpr/file.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
static DWORD WINAPI thread_func(LPVOID arg)
{
WINPR_UNUSED(arg);
/* exists of the thread the quickest as possible */
ExitThread(0);
return 0;
}
int TestThreadExitThread(int argc, char* argv[])
{
HANDLE thread = nullptr;
DWORD waitResult = 0;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
/* FIXME: create some noise to better guaranty the test validity and
* decrease the number of loops */
for (int i = 0; i < 100; i++)
{
thread = CreateThread(nullptr, 0, thread_func, nullptr, 0, nullptr);
if (thread == INVALID_HANDLE_VALUE)
{
(void)fprintf(stderr, "Got an invalid thread!\n");
return -1;
}
waitResult = WaitForSingleObject(thread, 300);
if (waitResult != WAIT_OBJECT_0)
{
/* When the thread exits before the internal thread_list
* was updated, ExitThread() is not able to retrieve the
* related WINPR_THREAD object and is not able to signal
* the end of the thread. Therefore WaitForSingleObject
* never get the signal.
*/
(void)fprintf(
stderr, "300ms should have been enough for the thread to be in a signaled state\n");
return -1;
}
(void)CloseHandle(thread);
}
return 0;
}