1 #include <winpr/sysinfo.h>
2 #include <winpr/path.h>
3 #include <winpr/crypto.h>
4 #include <winpr/pipe.h>
6 #include <freerdp/freerdp.h>
7 #include <freerdp/gdi/gdi.h>
8 #include <freerdp/client/cmdline.h>
10 static HANDLE s_sync = NULL;
12 static int runInstance(
int argc,
char* argv[], freerdp** inst, DWORD timeout)
15 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = { 0 };
16 rdpContext* context = NULL;
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 = NULL;
30 context->instance->PresentGatewayMessage = NULL;
31 context->instance->LogonErrorInfo = NULL;
32 context->instance->AuthenticateEx = NULL;
33 context->instance->VerifyCertificateEx = NULL;
34 context->instance->VerifyChangedCertificateEx = NULL;
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);
72 static 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, NULL, timeout);
92 if (diff > 4 * timeout)
98 printf(
"%s: Success!\n", __func__);
102 struct testThreadArgs
108 static 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);
125 static int testAbort(
int port)
131 HANDLE thread = NULL;
132 struct testThreadArgs args;
133 freerdp* instance = NULL;
134 s_sync = CreateEvent(NULL, TRUE, FALSE, NULL);
140 args.arg = &instance;
141 start = GetTickCount();
142 thread = CreateThread(NULL, 0, testThread, &args, 0, NULL);
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__);
186 static char* concatenate(
size_t count, ...)
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);
203 static BOOL prepare_certificates(
const char* path)
209 PROCESS_INFORMATION process = { 0 };
210 char commandLine[8192] = { 0 };
215 exe = concatenate(5, TESTING_OUTPUT_DIRECTORY,
"winpr",
"tools",
"makecert-cli",
216 "winpr-makecert" CMAKE_EXECUTABLE_SUFFIX);
219 (void)_snprintf(commandLine,
sizeof(commandLine),
"%s -format crt -path . -n server", exe);
221 rc = CreateProcessA(exe, commandLine, NULL, NULL, TRUE, 0, NULL, path, &si, &process);
225 status = WaitForSingleObject(process.hProcess, 30000);
226 if (status != WAIT_OBJECT_0)
230 (void)CloseHandle(process.hProcess);
231 (void)CloseHandle(process.hThread);
235 static int testSuccess(
int port)
240 PROCESS_INFORMATION process = { 0 };
241 char arg1[] =
"/v:127.0.0.1:XXXXX";
242 char* clientArgs[] = {
"test",
"/v:127.0.0.1:XXXXX",
"/cert:ignore",
"/rfx", NULL };
243 char* commandLine = NULL;
244 size_t commandLineLen = 0;
248 char* exe = GetCombinedPath(TESTING_OUTPUT_DIRECTORY,
"server");
249 (void)_snprintf(arg1, 18,
"/v:127.0.0.1:%d", port);
250 clientArgs[1] = arg1;
255 path = GetCombinedPath(exe,
"Sample");
256 wpath = GetCombinedPath(exe,
"Sample");
263 exe = GetCombinedPath(path,
"sfreerdp-server" CMAKE_EXECUTABLE_SUFFIX);
268 printf(
"Sample Server: %s\n", exe);
269 printf(
"Workspace: %s\n", wpath);
271 if (!winpr_PathFileExists(exe))
274 if (!prepare_certificates(wpath))
278 commandLineLen = strlen(exe) + strlen(
"--port=XXXXX") + 1;
279 commandLine = malloc(commandLineLen);
284 (void)_snprintf(commandLine, commandLineLen,
"%s --port=%d", exe, port);
287 if (!CreateProcessA(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, wpath, &si, &process))
291 r = runInstance(argc, clientArgs, NULL, 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__);
313 int TestConnect(
int argc,
char* argv[])
319 winpr_RAND(&random,
sizeof(random));
320 randomPort = 3389 + (random % 200);
324 if (testTimeout(randomPort))
329 if (testAbort(randomPort))
334 if (testSuccess(randomPort))
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.