21#include <winpr/collections.h>
23#include <winpr/interlocked.h>
24#include <winpr/synch.h>
25#include <winpr/thread.h>
29#include "../server_tunnel.c"
36 (void)printf("%s:%d: %s failed\n", __func__, __LINE__, #cond); \
44 struct s_wts_tunnel tunnel;
45 rdpPeerChannel drdynvc;
46 rdpPeerChannel channel;
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)
58static BOOL fixture_init(tunnelFixture* f)
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))
75 return HashTable_Insert(f->vcm.dynamicVirtualChannels, &f->channel.channelId, &f->channel);
78static void fixture_free(tunnelFixture* f)
80 wMessage message = WINPR_C_ARRAY_INIT;
81 while (f->vcm.queue && (MessageQueue_Peek(f->vcm.queue, &message, TRUE) > 0))
83 MessageQueue_Free(f->vcm.queue);
84 HashTable_Free(f->vcm.dynamicVirtualChannels);
85 Queue_Free(f->tunnel.tunnelBacklog);
86 DeleteCriticalSection(&f->channel.writeLock);
89static BOOL queue_event_set(tunnelFixture* f)
91 return WaitForSingleObject(MessageQueue_Event(f->vcm.queue), 0) == WAIT_OBJECT_0;
95static BOOL drain(tunnelFixture* f)
97 wMessage message = WINPR_C_ARRAY_INIT;
98 while (MessageQueue_Peek(f->vcm.queue, &message, TRUE) > 0)
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)
107 return wts_tunnel_after_drain(&f->vcm, TRUE);
113static BOOL a_create_response_wakes_soft_sync(
void)
116 TEST(fixture_init(&f));
118 TEST(!queue_event_set(&f));
119 wts_tunnel_channel_opened(&f.vcm);
120 TEST(queue_event_set(&f));
123 TEST(wts_tunnel_start_soft_sync_if_due(&f.vcm));
124 wts_tunnel_channel_opened(&f.vcm);
126 TEST(queue_event_set(&f));
128 TEST(wts_tunnel_start_soft_sync_if_due(&f.vcm));
130 TEST(!queue_event_set(&f));
136static DWORD WINAPI soft_sync_thread(LPVOID arg)
138 tunnelFixture* f = arg;
139 if (!wts_soft_sync(&f->vcm))
140 (void)printf(
"wts_soft_sync failed\n");
146static BOOL soft_sync_leaves_a_lost_tunnel_alone(
void)
149 TEST(fixture_init(&f));
150 (void)InterlockedIncrement(&f.tunnel.tunnelReady);
152 HashTable_Lock(f.vcm.dynamicVirtualChannels);
153 HANDLE thread = CreateThread(
nullptr, 0, soft_sync_thread, &f, 0,
nullptr);
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);
164 TEST(f.tunnel.softSyncState == SOFT_SYNC_STATE_NONE);
165 TEST(!f.channel.tunnel);
166 TEST(MessageQueue_Size(f.vcm.queue) == 0);
174static BOOL a_soft_sync_response_nobody_asked_for_is_ignored(
void)
177 TEST(fixture_init(&f));
179 const BYTE truncated[2] = { 0, 1 };
180 wStream sbuffer = WINPR_C_ARRAY_INIT;
181 wStream* s = Stream_StaticConstInit(&sbuffer, truncated,
sizeof(truncated));
183 TEST(wts_tunnel_read_soft_sync_response(&f.vcm, s));
184 TEST(f.tunnel.softSyncState == SOFT_SYNC_STATE_NONE);
186 f.tunnel.softSyncState = SOFT_SYNC_STATE_SENT;
187 s = Stream_StaticConstInit(&sbuffer, truncated,
sizeof(truncated));
189 TEST(!wts_tunnel_read_soft_sync_response(&f.vcm, s));
199static BOOL tunnel_data_ahead_of_the_response_waits(
void)
202 TEST(fixture_init(&f));
203 f.tunnel.softSyncState = SOFT_SYNC_STATE_SENT;
205 const size_t count = SOFT_SYNC_HOLD_BACKLOG + 100;
206 for (
size_t x = 0; x < count; x++)
208 wStream* s = Stream_New(
nullptr, 16);
211 Stream_SealLength(s);
212 Stream_ResetPosition(s);
213 TEST(wts_tunnel_data_received(&f.vcm, TUNNELTYPE_UDPFECR, s));
215 TEST((
size_t)Queue_Count(f.tunnel.tunnelBacklog) == count);
216 TEST(f.tunnel.backlogBytes == 16 * count);
219 while ((held = Queue_Dequeue(f.tunnel.tunnelBacklog)))
220 Stream_Release(held);
225int TestServerTunnel(
int argc,
char* argv[])
230 if (!a_create_response_wakes_soft_sync())
232 if (!soft_sync_leaves_a_lost_tunnel_alone())
234 if (!a_soft_sync_response_nobody_asked_for_is_ignored())
236 if (!tunnel_data_ahead_of_the_response_waits())