FreeRDP
Loading...
Searching...
No Matches
TestInputWriteFail.c
1#include <stdio.h>
2#include <errno.h>
3
4#include <winpr/crt.h>
5#include <winpr/synch.h>
6
7#include <freerdp/freerdp.h>
8#include <freerdp/client.h>
9#include <freerdp/input.h>
10#include <freerdp/transport_io.h>
11
12#include "../connection.h"
13#include "../input.h"
14#include "../rdp.h"
15#include "../transport.h"
16
17/* A connection that drops while the session is active: the first input write fails, and that call
18 * says so. From then on the session is no longer active, so the next input is skipped as during
19 * the reconnect, before the thread that reads has noticed. */
20static HANDLE s_layerEvent = nullptr;
21
22WINPR_ATTR_NODISCARD
23static int layer_read(WINPR_ATTR_UNUSED void* userContext, WINPR_ATTR_UNUSED void* data,
24 WINPR_ATTR_UNUSED int bytes)
25{
26 errno = EAGAIN;
27 return -1;
28}
29
30WINPR_ATTR_NODISCARD
31static int layer_write(WINPR_ATTR_UNUSED void* userContext, WINPR_ATTR_UNUSED const void* data,
32 WINPR_ATTR_UNUSED int bytes)
33{
34 errno = EPIPE;
35 return -1;
36}
37
38WINPR_ATTR_NODISCARD
39static BOOL layer_wait(WINPR_ATTR_UNUSED void* userContext, WINPR_ATTR_UNUSED BOOL waitWrite,
40 WINPR_ATTR_UNUSED DWORD timeout)
41{
42 return FALSE;
43}
44
45WINPR_ATTR_NODISCARD
46static HANDLE layer_get_event(WINPR_ATTR_UNUSED void* userContext)
47{
48 return s_layerEvent;
49}
50
51int TestInputWriteFail(int argc, char* argv[])
52{
53 int rc = -1;
54 rdpTransportLayer* layer = nullptr;
55 WINPR_UNUSED(argc);
56 WINPR_UNUSED(argv);
57
58 RDP_CLIENT_ENTRY_POINTS clientEntryPoints = WINPR_C_ARRAY_INIT;
59 clientEntryPoints.Size = sizeof(RDP_CLIENT_ENTRY_POINTS);
60 clientEntryPoints.Version = RDP_CLIENT_INTERFACE_VERSION;
61 clientEntryPoints.ContextSize = sizeof(rdpClientContext);
62
63 rdpContext* context = freerdp_client_context_new(&clientEntryPoints);
64 if (!context)
65 return -1;
66
67 s_layerEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
68 if (!s_layerEvent)
69 goto fail;
70
71 {
72 rdpTransport* transport = freerdp_get_transport(context);
73 if (!transport)
74 goto fail;
75 layer = transport_layer_new(transport, 0);
76 if (!layer)
77 goto fail;
78 layer->Read = layer_read;
79 layer->Write = layer_write;
80 layer->Wait = layer_wait;
81 layer->GetEvent = layer_get_event;
82 if (!transport_attach_layer(transport, layer))
83 goto fail;
84 layer = nullptr; /* the transport owns it now */
85 }
86
87 if (!input_register_client_callbacks(context->input) ||
88 !rdp_client_transition_to_state(context->rdp, CONNECTION_STATE_ACTIVE))
89 goto fail;
90
91 /* the connection drops: the first write fails */
92 const BOOL first = freerdp_input_send_mouse_event(context->input, PTR_FLAGS_MOVE, 10, 10);
93 const UINT32 error = freerdp_get_last_error(context);
94 const BOOL active = freerdp_is_active_state(context);
95 /* the transport is closed, the reader has not noticed yet: the next input */
96 const BOOL second = freerdp_input_send_mouse_event(context->input, PTR_FLAGS_MOVE, 20, 20);
97
98 (void)fprintf(stderr,
99 "input while the connection drops: first %s, then active %s, second %s, last "
100 "error 0x%08" PRIx32 "\n",
101 first ? "TRUE" : "FALSE", active ? "TRUE" : "FALSE", second ? "TRUE" : "FALSE",
102 error);
103
104 if (error != FREERDP_ERROR_CONNECT_TRANSPORT_FAILED)
105 {
106 (void)fprintf(stderr, "the dropped connection is not reported\n");
107 goto fail;
108 }
109 if (first)
110 {
111 (void)fprintf(stderr, "the input whose write failed reports success\n");
112 goto fail;
113 }
114 if (active || !second)
115 {
116 (void)fprintf(stderr, "the session is still active after its transport closed\n");
117 goto fail;
118 }
119
120 rc = 0;
121fail:
122 transport_layer_free(layer);
123 freerdp_client_context_free(context);
124 if (s_layerEvent)
125 (void)CloseHandle(s_layerEvent);
126 s_layerEvent = nullptr;
127 return rc;
128}