2015-07-03 12:56:57 +03:00
|
|
|
#include <winpr/sysinfo.h>
|
|
|
|
#include <winpr/path.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;
|
|
|
|
freerdp* instance = freerdp_new();
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!instance)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (inst)
|
|
|
|
*inst = instance;
|
|
|
|
|
|
|
|
if (!freerdp_context_new(instance))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (freerdp_client_settings_parse_command_line(instance->settings, argc, argv, FALSE) < 0)
|
|
|
|
goto finish;
|
|
|
|
|
2016-02-01 17:09:51 +03:00
|
|
|
if (!freerdp_client_load_addins(instance->context->channels, instance->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
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!freerdp_connect(instance))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
rc = 2;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!freerdp_disconnect(instance))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
finish:
|
|
|
|
freerdp_context_free(instance);
|
|
|
|
freerdp_free(instance);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
static int testTimeout(int port)
|
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";
|
2015-07-03 12:56:57 +03:00
|
|
|
char* argv[] =
|
|
|
|
{
|
|
|
|
"test",
|
2016-07-06 19:31:22 +03:00
|
|
|
"/v:192.0.2.1:XXXXX",
|
2015-07-03 12:56:57 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int rc;
|
2016-07-06 19:31:22 +03:00
|
|
|
snprintf(arg1, 18, "/v:192.0.2.1:%d", port);
|
|
|
|
argv[1] = arg1;
|
2015-07-03 12:56:57 +03:00
|
|
|
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
|
|
|
|
2015-07-03 12:56:57 +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
|
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
int port;
|
|
|
|
freerdp** arg;
|
|
|
|
};
|
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
static void* testThread(void* arg)
|
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
char arg1[] = "/v:192.0.2.1:XXXXX";
|
2015-07-03 12:56:57 +03:00
|
|
|
char* argv[] =
|
|
|
|
{
|
|
|
|
"test",
|
2016-07-06 19:31:22 +03:00
|
|
|
"/v:192.0.2.1:XXXXX",
|
2015-07-03 12:56:57 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int rc;
|
2016-10-13 23:02:25 +03:00
|
|
|
struct testThreadArgs* args = arg;
|
2016-07-06 19:31:22 +03:00
|
|
|
snprintf(arg1, 18, "/v:192.0.2.1:%d", args->port);
|
|
|
|
argv[1] = arg1;
|
|
|
|
rc = runInstance(2, argv, args->arg);
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (rc != 1)
|
|
|
|
ExitThread(-1);
|
|
|
|
|
|
|
|
ExitThread(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)testThread,
|
2016-10-13 23:02:25 +03:00
|
|
|
&args, 0, NULL);
|
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!thread)
|
|
|
|
{
|
|
|
|
CloseHandle(s_sync);
|
|
|
|
s_sync = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForSingleObject(s_sync, INFINITE);
|
|
|
|
freerdp_abort_connect(instance);
|
|
|
|
status = WaitForSingleObject(instance->context->abortEvent, 0);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +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
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (diff > 1000)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (WAIT_OBJECT_0 != status)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("%s: Success!\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
static int testSuccess(int port)
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION process;
|
2016-07-06 19:31:22 +03:00
|
|
|
char arg1[] = "/v:127.0.0.1:XXXXX";
|
|
|
|
char* clientArgs[] =
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
|
|
|
"test",
|
2016-07-06 19:31:22 +03:00
|
|
|
"/v:127.0.0.1:XXXXX",
|
2015-07-03 12:56:57 +03:00
|
|
|
"/cert-ignore",
|
2015-09-05 19:55:08 +03:00
|
|
|
"/rfx",
|
2015-07-03 12:56:57 +03:00
|
|
|
NULL
|
|
|
|
};
|
2016-10-13 23:02:25 +03:00
|
|
|
char* commandLine;
|
2016-07-06 19:31:22 +03:00
|
|
|
int commandLineLen;
|
2015-09-05 19:55:08 +03:00
|
|
|
int argc = 4;
|
2015-07-03 12:56:57 +03:00
|
|
|
char* path = TESTING_OUTPUT_DIRECTORY;
|
|
|
|
char* wpath = TESTING_SRC_DIRECTORY;
|
|
|
|
char* exe = GetCombinedPath(path, "server");
|
|
|
|
char* wexe = GetCombinedPath(wpath, "server");
|
2016-07-06 19:31:22 +03:00
|
|
|
snprintf(arg1, 18, "/v:127.0.0.1:%d", port);
|
|
|
|
clientArgs[1] = arg1;
|
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!exe || !wexe)
|
|
|
|
{
|
|
|
|
free(exe);
|
|
|
|
free(wexe);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
path = GetCombinedPath(exe, "Sample");
|
|
|
|
wpath = GetCombinedPath(wexe, "Sample");
|
|
|
|
free(exe);
|
|
|
|
free(wexe);
|
|
|
|
|
|
|
|
if (!path || !wpath)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
exe = GetCombinedPath(path, "sfreerdp-server");
|
|
|
|
|
|
|
|
if (!exe)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Sample Server: %s\n", exe);
|
|
|
|
printf("Workspace: %s\n", wpath);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (!PathFileExistsA(exe))
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
free(exe);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2015-09-05 19:55:08 +03:00
|
|
|
// Start sample server locally.
|
2016-07-06 19:31:22 +03:00
|
|
|
commandLineLen = strlen(exe) + strlen(" --port=XXXXX") + 1;
|
|
|
|
commandLine = malloc(commandLineLen);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2016-07-06 19:31:22 +03:00
|
|
|
if (!commandLine)
|
|
|
|
{
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
free(exe);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
snprintf(commandLine, commandLineLen, "%s --port=%d", exe, port);
|
2016-07-06 19:31:22 +03:00
|
|
|
memset(&si, 0, sizeof(si));
|
2016-10-13 23:02:25 +03:00
|
|
|
si.cb = sizeof(si);
|
2016-07-06 19:31:22 +03:00
|
|
|
|
|
|
|
if (!CreateProcessA(exe, commandLine, NULL, NULL, FALSE, 0, NULL,
|
2016-10-13 23:02:25 +03:00
|
|
|
wpath, &si, &process))
|
2015-07-03 12:56:57 +03:00
|
|
|
{
|
|
|
|
free(exe);
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(exe);
|
|
|
|
free(path);
|
|
|
|
free(wpath);
|
2016-07-06 19:31:22 +03:00
|
|
|
free(commandLine);
|
|
|
|
Sleep(1 * 1000); /* let the server start */
|
|
|
|
rc = runInstance(argc, clientArgs, NULL);
|
2015-07-03 12:56:57 +03:00
|
|
|
|
|
|
|
if (!TerminateProcess(process.hProcess, 0))
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
WaitForSingleObject(process.hProcess, INFINITE);
|
|
|
|
CloseHandle(process.hProcess);
|
|
|
|
CloseHandle(process.hThread);
|
|
|
|
printf("%s: returned %d!\n", __FUNCTION__, rc);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-07-03 12:56:57 +03:00
|
|
|
if (rc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
printf("%s: Success!\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestConnect(int argc, char* argv[])
|
|
|
|
{
|
2016-07-06 19:31:22 +03:00
|
|
|
int randomPort;
|
2016-10-08 23:37:31 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|