1#include <winpr/sysinfo.h>
3#include <winpr/crypto.h>
6#include <freerdp/freerdp.h>
7#include <freerdp/gdi/gdi.h>
8#include <freerdp/client/cmdline.h>
10static HANDLE s_sync =
nullptr;
12static int runInstance(
int argc,
char* argv[], freerdp** inst, DWORD timeout)
15 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = WINPR_C_ARRAY_INIT;
16 rdpContext* context =
nullptr;
18 clientEntryPoints.Size =
sizeof(RDP_CLIENT_ENTRY_POINTS);
19 clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
20 clientEntryPoints.ContextSize =
sizeof(rdpContext);
21 context = freerdp_client_context_new(&clientEntryPoints);
27 *inst = context->instance;
29 context->instance->ChooseSmartcard =
nullptr;
30 context->instance->PresentGatewayMessage =
nullptr;
31 context->instance->LogonErrorInfo =
nullptr;
32 context->instance->AuthenticateEx =
nullptr;
33 context->instance->VerifyCertificateEx =
nullptr;
34 context->instance->VerifyChangedCertificateEx =
nullptr;
39 if (freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE) < 0)
45 if (!freerdp_client_load_addins(context->channels, context->settings))
50 if (!SetEvent(s_sync))
56 if (!freerdp_connect(context->instance))
61 if (!freerdp_disconnect(context->instance))
66 freerdp_client_context_free(context);
72static int testTimeout(
int port)
74 const DWORD timeout = 200;
78 char arg1[] =
"/v:192.0.2.1:XXXXX";
79 char* argv[] = {
"test",
"/v:192.0.2.1:XXXXX" };
81 (void)_snprintf(arg1, 18,
"/v:192.0.2.1:%d", port);
83 start = GetTickCount();
84 rc = runInstance(ARRAYSIZE(argv), argv,
nullptr, timeout);
92 if (diff > 4 * timeout)
98 printf(
"%s: Success!\n", __func__);
108static DWORD WINAPI testThread(LPVOID arg)
110 char arg1[] =
"/v:192.0.2.1:XXXXX";
111 char* argv[] = {
"test",
"/v:192.0.2.1:XXXXX" };
113 struct testThreadArgs* args = arg;
114 (void)_snprintf(arg1, 18,
"/v:192.0.2.1:%d", args->port);
116 rc = runInstance(ARRAYSIZE(argv), argv, args->arg, 5000);
125static int testAbort(
int port)
131 HANDLE thread =
nullptr;
132 struct testThreadArgs args;
133 freerdp* instance =
nullptr;
134 s_sync = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
140 args.arg = &instance;
141 start = GetTickCount();
142 thread = CreateThread(
nullptr, 0, testThread, &args, 0,
nullptr);
146 (void)CloseHandle(s_sync);
151 (void)WaitForSingleObject(s_sync, INFINITE);
155 freerdp_abort_connect_context(instance->context);
157 if (!freerdp_shall_disconnect_context(instance->context))
159 (void)CloseHandle(s_sync);
160 (void)CloseHandle(thread);
166 status = WaitForSingleObject(thread, 20000);
167 end = GetTickCount();
168 (void)CloseHandle(s_sync);
169 (void)CloseHandle(thread);
175 printf(
"%s required %" PRIu32
"ms for the test\n", __func__, diff);
179 if (WAIT_OBJECT_0 != status)
182 printf(
"%s: Success!\n", __func__);
186static char* concatenate(
size_t count, ...)
189 va_list ap = WINPR_C_ARRAY_INIT;
191 rc = _strdup(va_arg(ap,
char*));
192 for (
size_t x = 1; x < count; x++)
194 const char* cur = va_arg(ap,
const char*);
195 char* tmp = GetCombinedPath(rc, cur);
203static BOOL prepare_certificates(
const char* path)
214 exe = concatenate(5, TESTING_OUTPUT_DIRECTORY,
"winpr",
"tools",
"makecert-cli",
215 "winpr-makecert" CMAKE_EXECUTABLE_SUFFIX);
219 char commandLine[] =
"-format crt -path . -n server";
220 rc = CreateProcessA(exe, commandLine,
nullptr,
nullptr, TRUE, 0,
nullptr, path, &si, &process);
224 status = WaitForSingleObject(process.hProcess, 30000);
225 if (status != WAIT_OBJECT_0)
229 (void)CloseHandle(process.hProcess);
230 (void)CloseHandle(process.hThread);
234static int testSuccess(
int port)
240 char arg1[] =
"/v:127.0.0.1:XXXXX";
241 char* clientArgs[] = {
"test",
"/v:127.0.0.1:XXXXX",
"/cert:ignore",
"/rfx",
nullptr };
242 char* commandLine =
nullptr;
243 size_t commandLineLen = 0;
245 char* path =
nullptr;
246 char* wpath =
nullptr;
247 char* exe = GetCombinedPath(TESTING_OUTPUT_DIRECTORY,
"server");
248 (void)_snprintf(arg1, 18,
"/v:127.0.0.1:%d", port);
249 clientArgs[1] = arg1;
254 path = GetCombinedPath(exe,
"Sample");
255 wpath = GetCombinedPath(exe,
"Sample");
262 exe = GetCombinedPath(path,
"sfreerdp-server" CMAKE_EXECUTABLE_SUFFIX);
267 printf(
"Sample Server: %s\n", exe);
268 printf(
"Workspace: %s\n", wpath);
270 if (!winpr_PathFileExists(exe))
273 if (!prepare_certificates(wpath))
277 commandLineLen = strlen(exe) + strlen(
"--port=XXXXX") + 1;
278 commandLine = malloc(commandLineLen);
283 (void)_snprintf(commandLine, commandLineLen,
"%s --port=%d", exe, port);
286 if (!CreateProcessA(
nullptr, commandLine,
nullptr,
nullptr, FALSE, 0,
nullptr, wpath, &si,
291 r = runInstance(argc, clientArgs,
nullptr, 10000);
293 if (!TerminateProcess(process.hProcess, 0))
296 (void)WaitForSingleObject(process.hProcess, INFINITE);
297 (void)CloseHandle(process.hProcess);
298 (void)CloseHandle(process.hThread);
299 printf(
"%s: returned %d!\n", __func__, r);
303 printf(
"%s: Success!\n", __func__);
313int TestConnect(
int argc,
char* argv[])
319 if (winpr_RAND(&random,
sizeof(random)) < 0)
321 randomPort = 3389 + (random % 200);
325 if (testTimeout(randomPort))
330 if (testAbort(randomPort))
335 if (testSuccess(randomPort))
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.