FreeRDP
Loading...
Searching...
No Matches
TestServerTunnel.c
1
17
18#include <stdio.h>
19#include <string.h>
20
21#include <winpr/collections.h>
22#include <winpr/crt.h>
23#include <winpr/interlocked.h>
24#include <winpr/synch.h>
25#include <winpr/thread.h>
26
27/* Include the source file directly because Soft-Sync triggers are defined as static. */
28// NOLINTNEXTLINE(bugprone-suspicious-include)
29#include "../server_tunnel.c"
30
31#define TEST(cond) \
32 do \
33 { \
34 if (!(cond)) \
35 { \
36 (void)printf("%s:%d: %s failed\n", __func__, __LINE__, #cond); \
37 return FALSE; \
38 } \
39 } while (0)
40
41typedef struct
42{
44 struct s_wts_tunnel tunnel;
45 rdpPeerChannel drdynvc;
46 rdpPeerChannel channel;
47} tunnelFixture;
48
49static BOOL fixture_read_pdu(WINPR_ATTR_UNUSED rdpPeerChannel* channel,
50 WINPR_ATTR_UNUSED wStream* s, WINPR_ATTR_UNUSED UINT8 Cmd,
51 WINPR_ATTR_UNUSED UINT8 Sp, WINPR_ATTR_UNUSED UINT8 cbChId,
52 WINPR_ATTR_UNUSED BOOL fromTunnel)
53{
54 return TRUE;
55}
56
57/* A channel manager with drdynvc ready and one open dynamic channel on TCP. */
58static BOOL fixture_init(tunnelFixture* f)
59{
60 memset(f, 0, sizeof(*f));
61 f->vcm.tunnel = &f->tunnel;
62 f->vcm.drdynvc_channel = &f->drdynvc;
63 f->vcm.drdynvc_state = DRDYNVC_STATE_READY;
64 f->drdynvc.vcm = &f->vcm;
65 f->tunnel.readPdu = fixture_read_pdu;
66 f->tunnel.tunnelBacklog = Queue_New(FALSE, -1, -1);
67 f->vcm.queue = MessageQueue_New(nullptr);
68 f->vcm.dynamicVirtualChannels = HashTable_New(TRUE);
69 f->channel.vcm = &f->vcm;
70 f->channel.channelId = 5;
71 f->channel.dvc_open_state = DVC_OPEN_STATE_SUCCEEDED;
72 if (!f->tunnel.tunnelBacklog || !f->vcm.queue || !f->vcm.dynamicVirtualChannels ||
73 !InitializeCriticalSectionAndSpinCount(&f->channel.writeLock, 4000))
74 return FALSE;
75 return HashTable_Insert(f->vcm.dynamicVirtualChannels, &f->channel.channelId, &f->channel);
76}
77
78static void fixture_free(tunnelFixture* f)
79{
80 wMessage message = WINPR_C_ARRAY_INIT;
81 while (f->vcm.queue && (MessageQueue_Peek(f->vcm.queue, &message, TRUE) > 0))
82 free(message.wParam);
83 MessageQueue_Free(f->vcm.queue);
84 HashTable_Free(f->vcm.dynamicVirtualChannels);
85 Queue_Free(f->tunnel.tunnelBacklog);
86 DeleteCriticalSection(&f->channel.writeLock);
87}
88
89static BOOL queue_event_set(tunnelFixture* f)
90{
91 return WaitForSingleObject(MessageQueue_Event(f->vcm.queue), 0) == WAIT_OBJECT_0;
92}
93
94/* WTSVirtualChannelManagerCheckFileDescriptor's drain, without a client to send to. */
95static BOOL drain(tunnelFixture* f)
96{
97 wMessage message = WINPR_C_ARRAY_INIT;
98 while (MessageQueue_Peek(f->vcm.queue, &message, TRUE) > 0)
99 {
100 BOOL status = TRUE;
101 const BOOL handled = wts_tunnel_send_message(&f->vcm, message.id, message.wParam,
102 (UINT32)(UINT_PTR)message.lParam, &status);
103 free(message.wParam);
104 if (!handled || !status)
105 return FALSE;
106 }
107 return wts_tunnel_after_drain(&f->vcm, TRUE);
108}
109
110/* A Create Response wakes the drain, also when it arrives while the drain runs: the drain resets
111 * the queue's event when it takes the wake message, so it must wake itself again. Nothing is left
112 * set once Soft-Sync has run, so the server's loop does not spin. */
113static BOOL a_create_response_wakes_soft_sync(void)
114{
115 tunnelFixture f;
116 TEST(fixture_init(&f));
117
118 TEST(!queue_event_set(&f));
119 wts_tunnel_channel_opened(&f.vcm);
120 TEST(queue_event_set(&f));
121
122 /* the drain starts Soft-Sync, then a second channel answers before it drains */
123 TEST(wts_tunnel_start_soft_sync_if_due(&f.vcm));
124 wts_tunnel_channel_opened(&f.vcm);
125 TEST(drain(&f));
126 TEST(queue_event_set(&f));
127
128 TEST(wts_tunnel_start_soft_sync_if_due(&f.vcm));
129 TEST(drain(&f));
130 TEST(!queue_event_set(&f));
131
132 fixture_free(&f);
133 return TRUE;
134}
135
136static DWORD WINAPI soft_sync_thread(LPVOID arg)
137{
138 tunnelFixture* f = arg;
139 if (!wts_soft_sync(&f->vcm))
140 (void)printf("wts_soft_sync failed\n");
141 return 0;
142}
143
144/* The tunnel is lost while Soft-Sync waits for the table lock, which wts_tunnel_state_changed
145 * takes to decide that the session stays on TCP: Soft-Sync must not move the channels. */
146static BOOL soft_sync_leaves_a_lost_tunnel_alone(void)
147{
148 tunnelFixture f;
149 TEST(fixture_init(&f));
150 (void)InterlockedIncrement(&f.tunnel.tunnelReady);
151
152 HashTable_Lock(f.vcm.dynamicVirtualChannels);
153 HANDLE thread = CreateThread(nullptr, 0, soft_sync_thread, &f, 0, nullptr);
154 TEST(thread);
155 /* let it read tunnelReady and block on the lock */
156 Sleep(200);
157 (void)InterlockedDecrement(&f.tunnel.tunnelReady);
158 const BOOL moved = (f.tunnel.softSyncState != SOFT_SYNC_STATE_NONE);
159 HashTable_Unlock(f.vcm.dynamicVirtualChannels);
160 (void)WaitForSingleObject(thread, INFINITE);
161 (void)CloseHandle(thread);
162
163 TEST(!moved);
164 TEST(f.tunnel.softSyncState == SOFT_SYNC_STATE_NONE);
165 TEST(!f.channel.tunnel);
166 TEST(MessageQueue_Size(f.vcm.queue) == 0);
167
168 fixture_free(&f);
169 return TRUE;
170}
171
172/* Without a request outstanding a Soft-Sync Response is logged and ignored whatever it holds,
173 * as a server without Soft-Sync does. One that answers a request has to be well-formed. */
174static BOOL a_soft_sync_response_nobody_asked_for_is_ignored(void)
175{
176 tunnelFixture f;
177 TEST(fixture_init(&f));
178
179 const BYTE truncated[2] = { 0, 1 };
180 wStream sbuffer = WINPR_C_ARRAY_INIT;
181 wStream* s = Stream_StaticConstInit(&sbuffer, truncated, sizeof(truncated));
182 TEST(s);
183 TEST(wts_tunnel_read_soft_sync_response(&f.vcm, s));
184 TEST(f.tunnel.softSyncState == SOFT_SYNC_STATE_NONE);
185
186 f.tunnel.softSyncState = SOFT_SYNC_STATE_SENT;
187 s = Stream_StaticConstInit(&sbuffer, truncated, sizeof(truncated));
188 TEST(s);
189 TEST(!wts_tunnel_read_soft_sync_response(&f.vcm, s));
190
191 fixture_free(&f);
192 return TRUE;
193}
194
195/* A client may write on the tunnel as soon as it has sent its Soft-Sync Response on TCP, and
196 * the tunnel can be the faster of the two. What arrives before the Response waits; past
197 * SOFT_SYNC_HOLD_BACKLOG the tunnel is asked to take no more, and what was already on its way
198 * is still kept rather than ending the connection. */
199static BOOL tunnel_data_ahead_of_the_response_waits(void)
200{
201 tunnelFixture f;
202 TEST(fixture_init(&f));
203 f.tunnel.softSyncState = SOFT_SYNC_STATE_SENT;
204
205 const size_t count = SOFT_SYNC_HOLD_BACKLOG + 100;
206 for (size_t x = 0; x < count; x++)
207 {
208 wStream* s = Stream_New(nullptr, 16);
209 TEST(s);
210 Stream_Zero(s, 16);
211 Stream_SealLength(s);
212 Stream_ResetPosition(s);
213 TEST(wts_tunnel_data_received(&f.vcm, TUNNELTYPE_UDPFECR, s));
214 }
215 TEST((size_t)Queue_Count(f.tunnel.tunnelBacklog) == count);
216 TEST(f.tunnel.backlogBytes == 16 * count);
217
218 wStream* held = nullptr;
219 while ((held = Queue_Dequeue(f.tunnel.tunnelBacklog)))
220 Stream_Release(held);
221 fixture_free(&f);
222 return TRUE;
223}
224
225int TestServerTunnel(int argc, char* argv[])
226{
227 WINPR_UNUSED(argc);
228 WINPR_UNUSED(argv);
229
230 if (!a_create_response_wakes_soft_sync())
231 return -1;
232 if (!soft_sync_leaves_a_lost_tunnel_alone())
233 return -1;
234 if (!a_soft_sync_response_nobody_asked_for_is_ignored())
235 return -1;
236 if (!tunnel_data_ahead_of_the_response_waits())
237 return -1;
238 return 0;
239}