2015-07-03 12:56:57 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
|
|
|
#include <winpr/path.h>
|
2017-02-17 12:59:22 +03:00
|
|
|
#include <winpr/crypto.h>
|
2021-07-29 16:04:15 +03:00
|
|
|
#include <winpr/pipe.h>
|
2022-10-21 12:29:59 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
#include <freerdp/freerdp.h>
|
2022-10-21 12:29:59 +03:00
|
|
|
#include <freerdp/gdi/gdi.h>
|
2015-07-03 12:56:57 +03:00
|
|
|
#include <freerdp/client/cmdline.h>
|
|
|
|
|
|
|
|
static HANDLE s_sync = NULL;
|
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
static int runInstance(int argc, char* argv[], freerdp** inst, DWORD timeout)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2022-02-14 16:59:22 +03:00
|
|
|
RDP_CLIENT_ENTRY_POINTS clientEntryPoints = { 0 };
|
|
|
|
rdpContext* context;
|
|
|
|
|
2017-09-25 14:30:05 +03:00
|
|
|
clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
|
|
|
|
clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
|
|
|
|
clientEntryPoints.ContextSize = sizeof(rdpContext);
|
2022-02-14 16:59:22 +03:00
|
|
|
context = freerdp_client_context_new(&clientEntryPoints);
|
2017-09-25 14:30:05 +03:00
|
|
|
|
|
|
|
if (!context)
|
2015-07-03 12:56:57 +03:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (inst)
|
2017-09-25 14:30:05 +03:00
|
|
|
*inst = context->instance;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
2022-10-21 12:29:59 +03:00
|
|
|
if (!freerdp_settings_set_bool(context->settings, FreeRDP_DeactivateClientDecoding, TRUE))
|
|
|
|
return FALSE;
|
|
|
|
|
2017-09-25 14:30:05 +03:00
|
|
|
if (freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE) < 0)
|
2015-07-03 12:56:57 +03:00
|
|
|
goto finish;
|
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
if (!freerdp_settings_set_uint32(context->settings, FreeRDP_TcpConnectTimeout, timeout))
|
|
|
|
goto finish;
|
|
|
|
|
2017-09-25 14:30:05 +03:00
|
|
|
if (!freerdp_client_load_addins(context->channels, context->settings))
|
2015-07-03 12:56:57 +03:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (s_sync)
|
|
|
|
{
|
|
|
|
if (!SetEvent(s_sync))
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = 1;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2017-09-25 14:30:05 +03:00
|
|
|
if (!freerdp_connect(context->instance))
|
2015-07-03 12:56:57 +03:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
rc = 2;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2017-09-25 14:30:05 +03:00
|
|
|
if (!freerdp_disconnect(context->instance))
|
2015-07-03 12:56:57 +03:00
|
|
|
goto finish;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
finish:
|
2017-09-25 14:30:05 +03:00
|
|
|
freerdp_client_context_free(context);
|
2022-11-16 17:00:23 +03:00
|
|
|
if (inst)
|
|
|
|
*inst = NULL;
|
2015-07-03 12:56:57 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
static int testTimeout(int port)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
2022-06-23 08:57:38 +03:00
|
|
|
const DWORD timeout = 200;
|
2015-07-03 12:56:57 +03:00
|
|
|
DWORD start, end, diff;
|
2016-07-06 19:31:22 +03:00
|
|
|
char arg1[] = "/v:192.0.2.1:XXXXX";
|
2021-06-02 16:46:27 +03:00
|
|
|
char* argv[] = { "test", "/v:192.0.2.1:XXXXX" };
|
2015-07-03 12:56:57 +03:00
|
|
|
int rc;
|
2017-02-17 12:56:16 +03:00
|
|
|
_snprintf(arg1, 18, "/v:192.0.2.1:%d", port);
|
2016-07-06 19:31:22 +03:00
|
|
|
argv[1] = arg1;
|
2015-07-03 12:56:57 +03:00
|
|
|
start = GetTickCount();
|
2021-06-02 16:46:27 +03:00
|
|
|
rc = runInstance(ARRAYSIZE(argv), argv, NULL, timeout);
|
2015-07-03 12:56:57 +03:00
|
|
|
end = GetTickCount();
|
|
|
|
|
|
|
|
if (rc != 1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
diff = end - start;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
if (diff > 4 * timeout)
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
if (diff < timeout)
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("%s: Success!\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
struct testThreadArgs
|
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
int port;
|
|
|
|
freerdp** arg;
|
|
|
|
};
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI testThread(LPVOID arg)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
char arg1[] = "/v:192.0.2.1:XXXXX";
|
2021-06-02 16:46:27 +03:00
|
|
|
char* argv[] = { "test", "/v:192.0.2.1:XXXXX" };
|
2015-07-03 12:56:57 +03:00
|
|
|
int rc;
|
2016-10-13 23:02:25 +03:00
|
|
|
struct testThreadArgs* args = arg;
|
2017-02-17 12:56:16 +03:00
|
|
|
_snprintf(arg1, 18, "/v:192.0.2.1:%d", args->port);
|
2016-07-06 19:31:22 +03:00
|
|
|
argv[1] = arg1;
|
2021-06-02 16:46:27 +03:00
|
|
|
rc = runInstance(ARRAYSIZE(argv), argv, args->arg, 5000);
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (rc != 1)
|
|
|
|
ExitThread(-1);
|
|
|
|
|
|
|
|
ExitThread(0);
|
2018-03-07 14:03:10 +03:00
|
|
|
return 0;
|
2015-07-03 12:56:57 +03:00
|
|
|
}
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
static int testAbort(int port)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
|
|
|
DWORD status;
|
|
|
|
DWORD start, end, diff;
|
|
|
|
HANDLE thread;
|
2016-07-06 19:31:22 +03:00
|
|
|
struct testThreadArgs args;
|
2015-07-03 12:56:57 +03:00
|
|
|
freerdp* instance = NULL;
|
|
|
|
s_sync = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!s_sync)
|
|
|
|
return -1;
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
args.port = port;
|
|
|
|
args.arg = &instance;
|
2015-07-03 12:56:57 +03:00
|
|
|
start = GetTickCount();
|
2019-11-06 17:24:51 +03:00
|
|
|
thread = CreateThread(NULL, 0, testThread, &args, 0, NULL);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
CloseHandle(s_sync);
|
|
|
|
s_sync = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForSingleObject(s_sync, INFINITE);
|
2021-06-02 16:46:27 +03:00
|
|
|
Sleep(100); /* Wait until freerdp_connect has been called */
|
2022-11-16 17:00:23 +03:00
|
|
|
if (instance)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
2022-11-16 17:00:23 +03:00
|
|
|
freerdp_abort_connect_context(instance->context);
|
|
|
|
|
|
|
|
if (!freerdp_shall_disconnect_context(instance->context))
|
|
|
|
{
|
|
|
|
CloseHandle(s_sync);
|
|
|
|
CloseHandle(thread);
|
|
|
|
s_sync = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-03 12:56:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
status = WaitForSingleObject(thread, 20000);
|
|
|
|
end = GetTickCount();
|
|
|
|
CloseHandle(s_sync);
|
|
|
|
CloseHandle(thread);
|
|
|
|
s_sync = NULL;
|
|
|
|
diff = end - start;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2017-09-26 11:52:13 +03:00
|
|
|
if (diff > 5000)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("%s required %" PRIu32 "ms for the test\n", __FUNCTION__, diff);
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
2017-09-26 11:52:13 +03:00
|
|
|
}
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (WAIT_OBJECT_0 != status)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("%s: Success!\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-29 16:04:15 +03:00
|
|
|
static char* concatenate(size_t count, ...)
|
|
|
|
{
|
|
|
|
size_t x;
|
|
|
|
char* rc;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, count);
|
|
|
|
rc = _strdup(va_arg(ap, char*));
|
|
|
|
for (x = 1; x < count; x++)
|
|
|
|
{
|
|
|
|
const char* cur = va_arg(ap, const char*);
|
|
|
|
char* tmp = GetCombinedPath(rc, cur);
|
|
|
|
free(rc);
|
|
|
|
rc = tmp;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL prepare_certificates(const char* path)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
char* exe = NULL;
|
|
|
|
DWORD status;
|
|
|
|
STARTUPINFOA si = { 0 };
|
|
|
|
SECURITY_ATTRIBUTES saAttr = { 0 };
|
|
|
|
PROCESS_INFORMATION process = { 0 };
|
|
|
|
char commandLine[8192] = { 0 };
|
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
exe = concatenate(5, TESTING_OUTPUT_DIRECTORY, "winpr", "tools", "makecert-cli",
|
|
|
|
"winpr-makecert");
|
|
|
|
if (!exe)
|
|
|
|
return FALSE;
|
|
|
|
_snprintf(commandLine, sizeof(commandLine), "%s -format crt -path . -n server", exe);
|
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
rc = CreateProcessA(exe, commandLine, NULL, NULL, TRUE, 0, NULL, path, &si, &process);
|
|
|
|
free(exe);
|
|
|
|
if (!rc)
|
|
|
|
goto fail;
|
|
|
|
status = WaitForSingleObject(process.hProcess, 30000);
|
|
|
|
if (status != WAIT_OBJECT_0)
|
|
|
|
goto fail;
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
CloseHandle(process.hProcess);
|
|
|
|
CloseHandle(process.hThread);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
static int testSuccess(int port)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
2019-04-05 14:01:59 +03:00
|
|
|
int r;
|
|
|
|
int rc = -2;
|
2022-10-13 17:25:36 +03:00
|
|
|
STARTUPINFOA si = { 0 };
|
|
|
|
PROCESS_INFORMATION process = { 0 };
|
2016-07-06 19:31:22 +03:00
|
|
|
char arg1[] = "/v:127.0.0.1:XXXXX";
|
2022-10-19 15:09:03 +03:00
|
|
|
char* clientArgs[] = { "test", "/v:127.0.0.1:XXXXX", "/cert:ignore", "/rfx", NULL };
|
2019-04-05 14:01:59 +03:00
|
|
|
char* commandLine = NULL;
|
2019-10-29 12:18:09 +03:00
|
|
|
size_t commandLineLen;
|
2015-09-05 19:55:08 +03:00
|
|
|
int argc = 4;
|
2019-04-05 14:01:59 +03:00
|
|
|
char* path = NULL;
|
|
|
|
char* wpath = NULL;
|
|
|
|
char* exe = GetCombinedPath(TESTING_OUTPUT_DIRECTORY, "server");
|
2017-02-17 12:56:16 +03:00
|
|
|
_snprintf(arg1, 18, "/v:127.0.0.1:%d", port);
|
2016-07-06 19:31:22 +03:00
|
|
|
clientArgs[1] = arg1;
|
|
|
|
|
2021-07-29 16:04:15 +03:00
|
|
|
if (!exe)
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
path = GetCombinedPath(exe, "Sample");
|
2021-07-29 16:04:15 +03:00
|
|
|
wpath = GetCombinedPath(exe, "Sample");
|
2020-05-18 09:10:42 +03:00
|
|
|
free(exe);
|
|
|
|
exe = NULL;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (!path || !wpath)
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
exe = GetCombinedPath(path, "sfreerdp-server");
|
|
|
|
|
|
|
|
if (!exe)
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
printf("Sample Server: %s\n", exe);
|
|
|
|
printf("Workspace: %s\n", wpath);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2021-05-31 12:42:03 +03:00
|
|
|
if (!winpr_PathFileExists(exe))
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
2021-07-29 16:04:15 +03:00
|
|
|
if (!prepare_certificates(wpath))
|
|
|
|
goto fail;
|
|
|
|
|
2015-09-05 19:55:08 +03:00
|
|
|
// Start sample server locally.
|
2021-09-03 09:41:58 +03:00
|
|
|
commandLineLen = strlen(exe) + strlen("--port=XXXXX") + 1;
|
2016-07-06 19:31:22 +03:00
|
|
|
commandLine = malloc(commandLineLen);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
if (!commandLine)
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2016-07-06 19:31:22 +03:00
|
|
|
|
2021-09-03 09:41:58 +03:00
|
|
|
_snprintf(commandLine, commandLineLen, "%s --port=%d", exe, port);
|
2016-10-13 23:02:25 +03:00
|
|
|
si.cb = sizeof(si);
|
2016-07-06 19:31:22 +03:00
|
|
|
|
2021-07-29 16:04:15 +03:00
|
|
|
if (!CreateProcessA(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, wpath, &si, &process))
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
2021-06-02 16:46:27 +03:00
|
|
|
Sleep(600); /* let the server start */
|
|
|
|
r = runInstance(argc, clientArgs, NULL, 5000);
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (!TerminateProcess(process.hProcess, 0))
|
2019-04-05 14:01:59 +03:00
|
|
|
goto fail;
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
WaitForSingleObject(process.hProcess, INFINITE);
|
|
|
|
CloseHandle(process.hProcess);
|
|
|
|
CloseHandle(process.hThread);
|
2019-04-05 14:01:59 +03:00
|
|
|
printf("%s: returned %d!\n", __FUNCTION__, r);
|
|
|
|
rc = r;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2019-04-05 14:01:59 +03:00
|
|
|
if (rc == 0)
|
|
|
|
printf("%s: Success!\n", __FUNCTION__);
|
2015-07-03 12:56:57 +03:00
|
|
|
|
2019-04-05 14:01:59 +03:00
|
|
|
fail:
|
|
|
|
free(exe);
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
free(commandLine);
|
|
|
|
return rc;
|
2015-07-03 12:56:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int TestConnect(int argc, char* argv[])
|
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
int randomPort;
|
2017-02-17 12:59:22 +03:00
|
|
|
int random;
|
2019-05-08 13:20:43 +03:00
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
2017-02-17 12:59:22 +03:00
|
|
|
winpr_RAND((BYTE*)&random, sizeof(random));
|
|
|
|
randomPort = 3389 + (random % 200);
|
2016-07-06 19:31:22 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
/* Test connect to not existing server,
|
|
|
|
* check if timeout is honored. */
|
2016-07-06 19:31:22 +03:00
|
|
|
if (testTimeout(randomPort))
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Test connect to not existing server,
|
|
|
|
* check if connection abort is working. */
|
2016-07-06 19:31:22 +03:00
|
|
|
if (testAbort(randomPort))
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Test connect to existing server,
|
|
|
|
* check if connection is working. */
|
2016-07-06 19:31:22 +03:00
|
|
|
if (testSuccess(randomPort))
|
2015-07-03 12:56:57 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|