FreeRDP
Loading...
Searching...
No Matches
TestDisconnect.c
1#include <stdio.h>
2
3#include <winpr/crt.h>
4
5#include <freerdp/freerdp.h>
6#include <freerdp/client.h>
7#include <freerdp/transport_io.h>
8
9#include "../connection.h"
10
11static rdpContext* s_context = nullptr;
12static pTransportFkt s_defaultDisconnect = nullptr;
13static size_t s_disconnects = 0;
14static size_t s_activeDisconnects = 0;
15
16/* The input functions run on other threads than the one that disconnects and only check that
17 * the session is active, so it must not be while the transport is torn down. */
18WINPR_ATTR_NODISCARD
19static BOOL test_transport_disconnect(rdpTransport* transport)
20{
21 s_disconnects++;
22 if (freerdp_is_active_state(s_context))
23 s_activeDisconnects++;
24
25 return s_defaultDisconnect(transport);
26}
27
28int TestDisconnect(int argc, char* argv[])
29{
30 int rc = -1;
31 WINPR_UNUSED(argc);
32 WINPR_UNUSED(argv);
33
34 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = WINPR_C_ARRAY_INIT;
35 clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
36 clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
37 clientEntryPoints.ContextSize = sizeof(rdpClientContext);
38
39 s_context = freerdp_client_context_new(&clientEntryPoints);
40 if (!s_context)
41 return -1;
42
43 const rdpTransportIo* defaultIo = freerdp_get_io_callbacks(s_context);
44 if (!defaultIo)
45 goto fail;
46 rdpTransportIo io = *defaultIo;
47 s_defaultDisconnect = io.TransportDisconnect;
48 io.TransportDisconnect = test_transport_disconnect;
49 if (!freerdp_set_io_callbacks(s_context, &io))
50 goto fail;
51
52 if (!rdp_client_transition_to_state(s_context->rdp, CONNECTION_STATE_ACTIVE))
53 goto fail;
54
55 if (!rdp_client_disconnect(s_context->rdp))
56 goto fail;
57
58 if (s_disconnects == 0)
59 {
60 (void)fprintf(stderr, "the transport was not disconnected\n");
61 goto fail;
62 }
63
64 if (s_activeDisconnects != 0)
65 {
66 (void)fprintf(stderr,
67 "%" PRIuz " of %" PRIuz " transport disconnects ran in the active state\n",
68 s_activeDisconnects, s_disconnects);
69 goto fail;
70 }
71
72 rc = 0;
73fail:
74 freerdp_client_context_free(s_context);
75 s_context = nullptr;
76 return rc;
77}