FreeRDP/libfreerdp/core/test/TestConnect.c

261 lines
5.3 KiB
C
Raw Normal View History

#include <winpr/sysinfo.h>
#include <winpr/path.h>
2017-02-17 12:59:22 +03:00
#include <winpr/crypto.h>
#include <freerdp/freerdp.h>
#include <freerdp/client/cmdline.h>
static HANDLE s_sync = NULL;
static int runInstance(int argc, char* argv[], freerdp** inst)
{
int rc = -1;
RDP_CLIENT_ENTRY_POINTS clientEntryPoints;
ZeroMemory(&clientEntryPoints, sizeof(RDP_CLIENT_ENTRY_POINTS));
clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
clientEntryPoints.ContextSize = sizeof(rdpContext);
rdpContext* context = freerdp_client_context_new(&clientEntryPoints);
if (!context)
goto finish;
if (inst)
*inst = context->instance;
if (freerdp_client_settings_parse_command_line(context->settings, argc, argv, FALSE) < 0)
goto finish;
if (!freerdp_client_load_addins(context->channels, context->settings))
goto finish;
if (s_sync)
{
if (!SetEvent(s_sync))
goto finish;
}
rc = 1;
2016-10-13 23:02:25 +03:00
if (!freerdp_connect(context->instance))
goto finish;
rc = 2;
2016-10-13 23:02:25 +03:00
if (!freerdp_disconnect(context->instance))
goto finish;
rc = 0;
finish:
freerdp_client_context_free(context);
return rc;
}
static int testTimeout(int port)
{
DWORD start, end, diff;
char arg1[] = "/v:192.0.2.1:XXXXX";
2019-11-06 17:24:51 +03:00
char* argv[] = { "test", "/v:192.0.2.1:XXXXX", NULL };
int rc;
2017-02-17 12:56:16 +03:00
_snprintf(arg1, 18, "/v:192.0.2.1:%d", port);
argv[1] = arg1;
start = GetTickCount();
rc = runInstance(2, argv, NULL);
end = GetTickCount();
if (rc != 1)
return -1;
diff = end - start;
2016-10-13 23:02:25 +03:00
if (diff > 16000)
return -1;
if (diff < 14000)
return -1;
printf("%s: Success!\n", __FUNCTION__);
return 0;
}
2016-10-13 23:02:25 +03:00
struct testThreadArgs
{
int port;
freerdp** arg;
};
static DWORD WINAPI testThread(LPVOID arg)
{
char arg1[] = "/v:192.0.2.1:XXXXX";
2019-11-06 17:24:51 +03:00
char* argv[] = { "test", "/v:192.0.2.1:XXXXX", NULL };
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);
argv[1] = arg1;
rc = runInstance(2, argv, args->arg);
if (rc != 1)
ExitThread(-1);
ExitThread(0);
return 0;
}
static int testAbort(int port)
{
DWORD status;
DWORD start, end, diff;
HANDLE thread;
struct testThreadArgs args;
freerdp* instance = NULL;
s_sync = CreateEvent(NULL, TRUE, FALSE, NULL);
2016-10-13 23:02:25 +03:00
if (!s_sync)
return -1;
args.port = port;
args.arg = &instance;
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
if (!thread)
{
CloseHandle(s_sync);
s_sync = NULL;
return -1;
}
WaitForSingleObject(s_sync, INFINITE);
Sleep(1000); /* Wait until freerdp_connect has been called */
freerdp_abort_connect(instance);
status = WaitForSingleObject(instance->context->abortEvent, 0);
2016-10-13 23:02:25 +03:00
if (status != WAIT_OBJECT_0)
{
CloseHandle(s_sync);
CloseHandle(thread);
s_sync = NULL;
return -1;
}
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
if (diff > 5000)
{
2019-11-06 17:24:51 +03:00
printf("%s required %" PRIu32 "ms for the test\n", __FUNCTION__, diff);
return -1;
}
if (WAIT_OBJECT_0 != status)
return -1;
printf("%s: Success!\n", __FUNCTION__);
return 0;
}
static int testSuccess(int port)
{
2019-04-05 14:01:59 +03:00
int r;
int rc = -2;
STARTUPINFOA si;
PROCESS_INFORMATION process;
char arg1[] = "/v:127.0.0.1:XXXXX";
2019-11-06 17:24:51 +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;
size_t commandLineLen;
int argc = 4;
2019-04-05 14:01:59 +03:00
char* path = NULL;
char* wpath = NULL;
char* exe = GetCombinedPath(TESTING_OUTPUT_DIRECTORY, "server");
char* wexe = GetCombinedPath(TESTING_SRC_DIRECTORY, "server");
2017-02-17 12:56:16 +03:00
_snprintf(arg1, 18, "/v:127.0.0.1:%d", port);
clientArgs[1] = arg1;
if (!exe || !wexe)
2019-04-05 14:01:59 +03:00
goto fail;
path = GetCombinedPath(exe, "Sample");
wpath = GetCombinedPath(wexe, "Sample");
if (!path || !wpath)
2019-04-05 14:01:59 +03:00
goto fail;
exe = GetCombinedPath(path, "sfreerdp-server");
if (!exe)
2019-04-05 14:01:59 +03:00
goto fail;
printf("Sample Server: %s\n", exe);
printf("Workspace: %s\n", wpath);
2016-10-13 23:02:25 +03:00
if (!PathFileExistsA(exe))
2019-04-05 14:01:59 +03:00
goto fail;
// Start sample server locally.
commandLineLen = strlen(exe) + strlen("--local-only --port=XXXXX") + 1;
commandLine = malloc(commandLineLen);
2016-10-13 23:02:25 +03:00
if (!commandLine)
2019-04-05 14:01:59 +03:00
goto fail;
_snprintf(commandLine, commandLineLen, "%s --local-only --port=%d", exe, port);
memset(&si, 0, sizeof(si));
2016-10-13 23:02:25 +03:00
si.cb = sizeof(si);
2019-11-06 17:24:51 +03:00
if (!CreateProcessA(exe, commandLine, NULL, NULL, FALSE, 0, NULL, wpath, &si, &process))
2019-04-05 14:01:59 +03:00
goto fail;
Sleep(1 * 1000); /* let the server start */
2019-04-05 14:01:59 +03:00
r = runInstance(argc, clientArgs, NULL);
if (!TerminateProcess(process.hProcess, 0))
2019-04-05 14:01:59 +03:00
goto fail;
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__);
2019-04-05 14:01:59 +03:00
fail:
free(exe);
free(path);
free(wpath);
free(commandLine);
return rc;
}
int TestConnect(int argc, char* argv[])
{
int randomPort;
2017-02-17 12:59:22 +03:00
int random;
WINPR_UNUSED(argc);
WINPR_UNUSED(argv);
2017-02-17 12:59:22 +03:00
winpr_RAND((BYTE*)&random, sizeof(random));
randomPort = 3389 + (random % 200);
/* Test connect to not existing server,
* check if timeout is honored. */
if (testTimeout(randomPort))
return -1;
/* Test connect to not existing server,
* check if connection abort is working. */
if (testAbort(randomPort))
return -1;
/* Test connect to existing server,
* check if connection is working. */
if (testSuccess(randomPort))
return -1;
return 0;
}