1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
/* $Id: tstClipboardMockHGCM.cpp $ */
/** @file
* Shared Clipboard host service test case.
*/
/*
* Copyright (C) 2011-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "../VBoxSharedClipboardSvc-internal.h"
#include <VBox/HostServices/VBoxClipboardSvc.h>
#include <VBox/VBoxGuestLib.h>
#ifdef RT_OS_LINUX
# include <VBox/GuestHost/SharedClipboard-x11.h>
#endif
#ifdef RT_OS_WINDOWS
# include <VBox/GuestHost/SharedClipboard-win.h>
#endif
#include <VBox/GuestHost/HGCMMock.h>
#include <VBox/GuestHost/HGCMMockUtils.h>
#include <iprt/assert.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/rand.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/test.h>
#include <iprt/utf16.h>
/*********************************************************************************************************************************
* Static globals *
*********************************************************************************************************************************/
static RTTEST g_hTest;
/*********************************************************************************************************************************
* Shared Clipboard testing *
*********************************************************************************************************************************/
struct CLIPBOARDTESTDESC;
/** Pointer to a test description. */
typedef CLIPBOARDTESTDESC *PTESTDESC;
struct CLIPBOARDTESTCTX;
/** Pointer to a test context. */
typedef CLIPBOARDTESTCTX *PCLIPBOARDTESTCTX;
/** Pointer a test descriptor. */
typedef CLIPBOARDTESTDESC *PTESTDESC;
typedef DECLCALLBACKTYPE(int, FNTESTSETUP,(PCLIPBOARDTESTCTX pTstCtx, void **ppvCtx));
/** Pointer to a test setup callback. */
typedef FNTESTSETUP *PFNTESTSETUP;
typedef DECLCALLBACKTYPE(int, FNTESTEXEC,(PCLIPBOARDTESTCTX pTstCtx, void *pvCtx));
/** Pointer to a test exec callback. */
typedef FNTESTEXEC *PFNTESTEXEC;
typedef DECLCALLBACKTYPE(int, FNTESTDESTROY,(PCLIPBOARDTESTCTX pTstCtx, void *pvCtx));
/** Pointer to a test destroy callback. */
typedef FNTESTDESTROY *PFNTESTDESTROY;
/**
* Structure for keeping a clipboard test task.
*/
typedef struct CLIPBOARDTESTTASK
{
SHCLFORMATS enmFmtHst;
SHCLFORMATS enmFmtGst;
/** For testing chunked reads / writes. */
size_t cbChunk;
/** Data buffer to read / write for this task.
* Can be NULL if not needed. */
void *pvData;
/** Size (in bytes) of \a pvData. */
size_t cbData;
/** Number of bytes read / written from / to \a pvData. */
size_t cbProcessed;
} CLIPBOARDTESTTASK;
typedef CLIPBOARDTESTTASK *PCLIPBOARDTESTTASK;
/**
* Structure for keeping a clipboard test context.
*/
typedef struct CLIPBOARDTESTCTX
{
/** The HGCM Mock utils context. */
TSTHGCMUTILSCTX HGCM;
/** Clipboard-specific task data. */
CLIPBOARDTESTTASK Task;
struct
{
/** The VbglR3 Shared Clipboard context to work on. */
VBGLR3SHCLCMDCTX CmdCtx;
} Guest;
} CLIPBOARDTESTCTX;
/** The one and only clipboard test context. One at a time. */
CLIPBOARDTESTCTX g_TstCtx;
/**
* Structure for keeping a clipboard test description.
*/
typedef struct CLIPBOARDTESTDESC
{
/** The setup callback. */
PFNTESTSETUP pfnSetup;
/** The exec callback. */
PFNTESTEXEC pfnExec;
/** The destruction callback. */
PFNTESTDESTROY pfnDestroy;
} CLIPBOARDTESTDESC;
typedef struct SHCLCONTEXT
{
} SHCLCONTEXT;
static int tstSetModeRc(PTSTHGCMMOCKSVC pSvc, uint32_t uMode, int rcExpected)
{
VBOXHGCMSVCPARM aParms[2];
HGCMSvcSetU32(&aParms[0], uMode);
int rc2 = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_MODE, 1, aParms);
RTTESTI_CHECK_MSG_RET(rcExpected == rc2, ("Expected %Rrc, got %Rrc\n", rcExpected, rc2), rc2);
if (RT_SUCCESS(rcExpected))
{
uint32_t const uModeRet = ShClSvcGetMode();
RTTESTI_CHECK_MSG_RET(uMode == uModeRet, ("Expected mode %RU32, got %RU32\n", uMode, uModeRet), VERR_WRONG_TYPE);
}
return rc2;
}
static int tstClipboardSetMode(PTSTHGCMMOCKSVC pSvc, uint32_t uMode)
{
return tstSetModeRc(pSvc, uMode, VINF_SUCCESS);
}
static bool tstClipboardGetMode(PTSTHGCMMOCKSVC pSvc, uint32_t uModeExpected)
{
RT_NOREF(pSvc);
RTTESTI_CHECK_RET(ShClSvcGetMode() == uModeExpected, false);
return true;
}
static void tstOperationModes(void)
{
struct VBOXHGCMSVCPARM parms[2];
uint32_t u32Mode;
int rc;
RTTestISub("Testing VBOX_SHCL_HOST_FN_SET_MODE");
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
/* Reset global variable which doesn't reset itself. */
HGCMSvcSetU32(&parms[0], VBOX_SHCL_MODE_OFF);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_MODE, 1, parms);
RTTESTI_CHECK_RC_OK(rc);
u32Mode = ShClSvcGetMode();
RTTESTI_CHECK_MSG(u32Mode == VBOX_SHCL_MODE_OFF, ("u32Mode=%u\n", (unsigned) u32Mode));
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_MODE, 0, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_MODE, 2, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
HGCMSvcSetU64(&parms[0], 99);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_MODE, 1, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
tstClipboardSetMode(pSvc, VBOX_SHCL_MODE_HOST_TO_GUEST);
tstSetModeRc(pSvc, 99, VERR_NOT_SUPPORTED);
tstClipboardGetMode(pSvc, VBOX_SHCL_MODE_OFF);
}
#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
static void testSetTransferMode(void)
{
RTTestISub("Testing VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE");
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
/* Invalid parameter. */
VBOXHGCMSVCPARM parms[2];
HGCMSvcSetU64(&parms[0], 99);
int rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE, 1, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
/* Invalid mode. */
HGCMSvcSetU32(&parms[0], 99);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE, 1, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_FLAGS);
/* Enable transfers. */
HGCMSvcSetU32(&parms[0], VBOX_SHCL_TRANSFER_MODE_ENABLED);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE, 1, parms);
RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
/* Disable transfers again. */
HGCMSvcSetU32(&parms[0], VBOX_SHCL_TRANSFER_MODE_DISABLED);
rc = TstHgcmMockSvcHostCall(pSvc, NULL, VBOX_SHCL_HOST_FN_SET_TRANSFER_MODE, 1, parms);
RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
}
#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
static void testGuestSimple(void)
{
RTTestISub("Testing client (guest) API - Simple");
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
/* Preparations. */
VBGLR3SHCLCMDCTX Ctx;
RT_ZERO(Ctx);
/*
* Multiple connects / disconnects.
*/
RTTESTI_CHECK_RC_OK(VbglR3ClipboardConnectEx(&Ctx, VBOX_SHCL_GF_0_CONTEXT_ID));
RTTESTI_CHECK_RC_OK(VbglR3ClipboardDisconnectEx(&Ctx));
/* Report bogus guest features while connecting. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardConnectEx(&Ctx, 0xdeadbeef));
RTTESTI_CHECK_RC_OK(VbglR3ClipboardDisconnectEx(&Ctx));
RTTESTI_CHECK_RC_OK(VbglR3ClipboardConnectEx(&Ctx, VBOX_SHCL_GF_0_CONTEXT_ID));
/*
* Feature tests.
*/
RTTESTI_CHECK_RC_OK(VbglR3ClipboardReportFeatures(Ctx.idClient, 0x0, NULL /* pfHostFeatures */));
/* Report bogus features to the host. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardReportFeatures(Ctx.idClient, 0xdeadb33f, NULL /* pfHostFeatures */));
/*
* Access denied tests.
*/
/* Try reading data from host. */
uint8_t abData[32]; uint32_t cbIgnored;
RTTESTI_CHECK_RC(VbglR3ClipboardReadData(Ctx.idClient, VBOX_SHCL_FMT_UNICODETEXT,
abData, sizeof(abData), &cbIgnored), VERR_ACCESS_DENIED);
/* Try writing data without reporting formats before (legacy). */
RTTESTI_CHECK_RC(VbglR3ClipboardWriteData(Ctx.idClient, 0xdeadb33f, abData, sizeof(abData)), VERR_ACCESS_DENIED);
/* Try writing data without reporting formats before. */
RTTESTI_CHECK_RC(VbglR3ClipboardWriteDataEx(&Ctx, 0xdeadb33f, abData, sizeof(abData)), VERR_ACCESS_DENIED);
/* Report bogus formats to the host. */
RTTESTI_CHECK_RC(VbglR3ClipboardReportFormats(Ctx.idClient, 0xdeadb33f), VERR_ACCESS_DENIED);
/* Report supported formats to host. */
RTTESTI_CHECK_RC(VbglR3ClipboardReportFormats(Ctx.idClient,
VBOX_SHCL_FMT_UNICODETEXT | VBOX_SHCL_FMT_BITMAP | VBOX_SHCL_FMT_HTML),
VERR_ACCESS_DENIED);
/*
* Access allowed tests.
*/
tstClipboardSetMode(pSvc, VBOX_SHCL_MODE_BIDIRECTIONAL);
/* Try writing data without reporting formats before. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardWriteDataEx(&Ctx, 0xdeadb33f, abData, sizeof(abData)));
/* Try reading data from host. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardReadData(Ctx.idClient, VBOX_SHCL_FMT_UNICODETEXT,
abData, sizeof(abData), &cbIgnored));
/* Report bogus formats to the host. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardReportFormats(Ctx.idClient, 0xdeadb33f));
/* Report supported formats to host. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardReportFormats(Ctx.idClient,
VBOX_SHCL_FMT_UNICODETEXT | VBOX_SHCL_FMT_BITMAP | VBOX_SHCL_FMT_HTML));
/* Tear down. */
RTTESTI_CHECK_RC_OK(VbglR3ClipboardDisconnectEx(&Ctx));
}
static RTUTF16 tstGetRandUtf8(void)
{
return RTRandU32Ex(0x20, 0x7A);
}
static char *tstGenerateUtf8StringA(uint32_t uCch)
{
char * pszRand = (char *)RTMemAlloc(uCch + 1);
for (uint32_t i = 0; i < uCch; i++)
pszRand[i] = tstGetRandUtf8();
pszRand[uCch] = 0;
return pszRand;
}
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
static RTUTF16 tstGetRandUtf16(void)
{
RTUTF16 wc;
do
{
wc = (RTUTF16)RTRandU32Ex(1, 0xfffd);
} while (wc >= 0xd800 && wc <= 0xdfff);
return wc;
}
static PRTUTF16 tstGenerateUtf16StringA(uint32_t uCch)
{
PRTUTF16 pwszRand = (PRTUTF16)RTMemAlloc((uCch + 1) * sizeof(RTUTF16));
for (uint32_t i = 0; i < uCch; i++)
pwszRand[i] = tstGetRandUtf16();
pwszRand[uCch] = 0;
return pwszRand;
}
#endif /* RT_OS_WINDOWS) || RT_OS_OS2 */
static void testSetHeadless(void)
{
RTTestISub("Testing HOST_FN_SET_HEADLESS");
PTSTHGCMMOCKSVC pSvc = TstHgcmMockSvcInst();
VBOXHGCMSVCPARM parms[2];
HGCMSvcSetU32(&parms[0], false);
int rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 1, parms);
RTTESTI_CHECK_RC_OK(rc);
bool fHeadless = ShClSvcGetHeadless();
RTTESTI_CHECK_MSG(fHeadless == false, ("fHeadless=%RTbool\n", fHeadless));
rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 0, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 2, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
HGCMSvcSetU64(&parms[0], 99);
rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 1, parms);
RTTESTI_CHECK_RC(rc, VERR_INVALID_PARAMETER);
HGCMSvcSetU32(&parms[0], true);
rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 1, parms);
RTTESTI_CHECK_RC_OK(rc);
fHeadless = ShClSvcGetHeadless();
RTTESTI_CHECK_MSG(fHeadless == true, ("fHeadless=%RTbool\n", fHeadless));
HGCMSvcSetU32(&parms[0], 99);
rc = pSvc->fnTable.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_HEADLESS, 1, parms);
RTTESTI_CHECK_RC_OK(rc);
fHeadless = ShClSvcGetHeadless();
RTTESTI_CHECK_MSG(fHeadless == true, ("fHeadless=%RTbool\n", fHeadless));
}
static void testHostCall(void)
{
tstOperationModes();
#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
testSetTransferMode();
#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
testSetHeadless();
}
/*********************************************************************************************************************************
* Test: Guest reading from host *
********************************************************************************************************************************/
#if defined (RT_OS_LINUX) || defined (RT_OS_SOLARIS)
/* Called from SHCLX11 thread. */
static DECLCALLBACK(int) tstTestReadFromHost_ReportFormatsCallback(PSHCLCONTEXT pCtx, uint32_t fFormats, void *pvUser)
{
RT_NOREF(pCtx, fFormats, pvUser);
RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "tstTestReadFromHost_SvcReportFormatsCallback: fFormats=%#x\n", fFormats);
return VINF_SUCCESS;
}
/* Called by the backend, e.g. for X11 in the SHCLX11 thread. */
static DECLCALLBACK(int) tstTestReadFromHost_OnClipboardReadCallback(PSHCLCONTEXT pCtx,
SHCLFORMAT uFmt, void **ppv, size_t *pcb, void *pvUser)
{
RT_NOREF(pCtx, uFmt, pvUser);
PCLIPBOARDTESTTASK pTask = (PCLIPBOARDTESTTASK)TstHGCMUtilsTaskGetCurrent(&g_TstCtx.HGCM)->pvUser;
void *pvData = NULL;
size_t cbData = pTask->cbData - pTask->cbProcessed;
if (cbData)
{
pvData = RTMemDup((uint8_t *)pTask->pvData + pTask->cbProcessed, cbData);
AssertPtr(pvData);
}
RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Host reporting back %RU32 bytes of data\n", cbData);
*ppv = pvData;
*pcb = cbData;
return VINF_SUCCESS;
}
#endif /* (RT_OS_LINUX) || defined (RT_OS_SOLARIS) */
typedef struct TSTUSERMOCK
{
#if defined(RT_OS_LINUX)
SHCLX11CTX X11Ctx;
#endif
PSHCLCONTEXT pCtx;
} TSTUSERMOCK;
typedef TSTUSERMOCK *PTSTUSERMOCK;
static void tstTestReadFromHost_MockInit(PTSTUSERMOCK pUsrMock, const char *pszName)
{
#if defined(RT_OS_LINUX)
SHCLCALLBACKS Callbacks;
RT_ZERO(Callbacks);
Callbacks.pfnReportFormats = tstTestReadFromHost_ReportFormatsCallback;
Callbacks.pfnOnClipboardRead = tstTestReadFromHost_OnClipboardReadCallback;
pUsrMock->pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
AssertPtrReturnVoid(pUsrMock->pCtx);
ShClX11Init(&pUsrMock->X11Ctx, &Callbacks, pUsrMock->pCtx, false);
ShClX11ThreadStartEx(&pUsrMock->X11Ctx, pszName, false /* fGrab */);
/* Give the clipboard time to synchronise. */
RTThreadSleep(500);
#else
RT_NOREF(pUsrMock, pszName);
#endif /* RT_OS_LINUX */
}
static void tstTestReadFromHost_MockDestroy(PTSTUSERMOCK pUsrMock)
{
#if defined(RT_OS_LINUX)
ShClX11ThreadStop(&pUsrMock->X11Ctx);
ShClX11Destroy(&pUsrMock->X11Ctx);
RTMemFree(pUsrMock->pCtx);
#else
RT_NOREF(pUsrMock);
#endif
}
static int tstTestReadFromHost_DoIt(PCLIPBOARDTESTCTX pCtx, PCLIPBOARDTESTTASK pTask)
{
size_t cbDst = RT_MAX(_64K, pTask->cbData);
uint8_t *pabDst = (uint8_t *)RTMemAllocZ(cbDst);
AssertPtrReturn(pabDst, VERR_NO_MEMORY);
AssertPtr(pTask->pvData); /* Racing condition with host thread? */
Assert(pTask->cbChunk); /* Buggy test? */
Assert(pTask->cbChunk <= pTask->cbData); /* Ditto. */
size_t cbToRead = pTask->cbData;
switch (pTask->enmFmtGst)
{
case VBOX_SHCL_FMT_UNICODETEXT:
#ifndef RT_OS_WINDOWS /** @todo Not sure about OS/2. */
cbToRead *= sizeof(RTUTF16);
#endif
break;
default:
break;
}
PVBGLR3SHCLCMDCTX pCmdCtx = &pCtx->Guest.CmdCtx;
/* Do random chunked reads. */
uint32_t const cChunkedReads = RTRandU32Ex(1, 16);
RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "%RU32 chunked reads\n", cChunkedReads);
for (uint32_t i = 0; i < cChunkedReads; i++)
{
/* Note! VbglR3ClipboardReadData() currently does not support chunked reads!
* It in turn returns VINF_BUFFER_OVERFLOW when the supplied buffer was too small. */
uint32_t cbChunk = RTRandU32Ex(1, (uint32_t)(pTask->cbData / cChunkedReads));
uint32_t cbRead = 0;
RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest trying to read %RU32 bytes\n", cbChunk);
int vrc2 = VbglR3ClipboardReadData(pCmdCtx->idClient, pTask->enmFmtGst, pabDst, cbChunk, &cbRead);
if ( vrc2 == VINF_SUCCESS
&& cbRead == 0) /* No data there yet? */
{
RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "No data (yet) from host\n");
RTThreadSleep(10);
continue;
}
RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Trying reading host clipboard data with a %RU32 buffer -> %Rrc (%RU32)\n", cbChunk, vrc2, cbRead);
RTTEST_CHECK_MSG(g_hTest, vrc2 == VINF_BUFFER_OVERFLOW, (g_hTest, "Got %Rrc, expected VINF_BUFFER_OVERFLOW\n", vrc2));
}
/* Last read: Read the data with a buffer big enough. This must succeed. */
RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Reading full data (%zu)\n", pTask->cbData);
uint32_t cbRead = 0;
int vrc2 = VbglR3ClipboardReadData(pCmdCtx->idClient, pTask->enmFmtGst, pabDst, (uint32_t)cbDst, &cbRead);
RTTEST_CHECK_MSG(g_hTest, vrc2 == VINF_SUCCESS, (g_hTest, "Got %Rrc, expected VINF_SUCCESS\n", vrc2));
RTTEST_CHECK_MSG(g_hTest, cbRead == cbToRead, (g_hTest, "Read %RU32 bytes, expected %zu\n", cbRead, cbToRead));
if (pTask->enmFmtGst == VBOX_SHCL_FMT_UNICODETEXT)
RTTEST_CHECK_MSG(g_hTest, RTUtf16ValidateEncoding((PRTUTF16)pabDst) == VINF_SUCCESS, (g_hTest, "Read data is not valid UTF-16\n"));
if (cbRead == cbToRead)
{
#ifndef RT_OS_WINDOWS /** @todo Not sure about OS/2. */
PRTUTF16 pwszSrc = NULL;
RTTEST_CHECK(g_hTest, RT_SUCCESS(RTStrToUtf16((const char *)pTask->pvData, &pwszSrc)));
RTTEST_CHECK_MSG(g_hTest, memcmp(pwszSrc, pabDst, cbRead) == 0, (g_hTest, "Read data does not match host data\n"));
RTUtf16Free(pwszSrc);
#else
RTTEST_CHECK_MSG(g_hTest, memcmp(pTask->pvData, pabDst, cbRead) == 0, (g_hTest, "Read data does not match host data\n"));
#endif
}
RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Read data from host:\n%.*Rhxd\n", cbRead, pabDst);
RTMemFree(pabDst);
return VINF_SUCCESS;
}
static DECLCALLBACK(int) tstTestReadFromHost_ThreadGuest(PTSTHGCMUTILSCTX pCtx, void *pvCtx)
{
RTThreadSleep(1000); /* Fudge; wait until the host has prepared the data for the clipboard. */
PCLIPBOARDTESTCTX pTstCtx = (PCLIPBOARDTESTCTX)pvCtx;
AssertPtr(pTstCtx);
RT_ZERO(pTstCtx->Guest.CmdCtx);
RTTEST_CHECK_RC_OK(g_hTest, VbglR3ClipboardConnectEx(&pTstCtx->Guest.CmdCtx, VBOX_SHCL_GF_0_CONTEXT_ID));
RTThreadSleep(1000); /* Fudge; wait until the host has prepared the data for the clipboard. */
PCLIPBOARDTESTTASK pTstTask = (PCLIPBOARDTESTTASK)pCtx->Task.pvUser;
AssertPtr(pTstTask);
tstTestReadFromHost_DoIt(pTstCtx, pTstTask);
/* Signal that the task ended. */
TstHGCMUtilsTaskSignal(&pCtx->Task, VINF_SUCCESS);
RTTEST_CHECK_RC_OK(g_hTest, VbglR3ClipboardDisconnectEx(&pTstCtx->Guest.CmdCtx));
return VINF_SUCCESS;
}
static DECLCALLBACK(int) tstTestReadFromHost_ClientConnectedCallback(PTSTHGCMUTILSCTX pCtx, PTSTHGCMMOCKCLIENT pClient,
void *pvUser)
{
RT_NOREF(pCtx, pClient);
PCLIPBOARDTESTCTX pTstCtx = (PCLIPBOARDTESTCTX)pvUser;
AssertPtr(pTstCtx); RT_NOREF(pTstCtx);
RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Client %RU32 connected\n", pClient->idClient);
return VINF_SUCCESS;
}
static DECLCALLBACK(int) tstTestReadFromHostSetup(PCLIPBOARDTESTCTX pTstCtx, void **ppvCtx)
{
RT_NOREF(ppvCtx);
/* Set the right clipboard mode, so that the guest can read from the host. */
tstClipboardSetMode(TstHgcmMockSvcInst(), VBOX_SHCL_MODE_BIDIRECTIONAL);
/* Start the host thread first, so that the guest thread can connect to it later. */
TSTHGCMUTILSHOSTCALLBACKS HostCallbacks;
RT_ZERO(HostCallbacks);
HostCallbacks.pfnOnClientConnected = tstTestReadFromHost_ClientConnectedCallback;
TstHGCMUtilsHostThreadStart(&pTstCtx->HGCM, &HostCallbacks, pTstCtx /* pvUser */);
PCLIPBOARDTESTTASK pTask = &pTstCtx->Task;
AssertPtr(pTask);
pTask->enmFmtGst = VBOX_SHCL_FMT_UNICODETEXT;
pTask->enmFmtHst = pTask->enmFmtGst;
pTask->cbChunk = RTRandU32Ex(1, 512);
pTask->cbData = RT_ALIGN_32(pTask->cbChunk * RTRandU32Ex(1, 16), 2);
Assert(pTask->cbData % sizeof(RTUTF16) == 0);
#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
pTask->pvData = tstGenerateUtf8StringA(pTask->cbData);
pTask->cbData++; /* Add terminating zero. */
#else
pTask->pvData = tstGenerateUtf16StringA((uint32_t)(pTask->cbData /* We use bytes == chars here */));
pTask->cbData *= sizeof(RTUTF16);
pTask->cbData += sizeof(RTUTF16); /* Add terminating zero. */
#endif
pTask->cbProcessed = 0;
int rc = VINF_SUCCESS;
#if defined (RT_OS_LINUX) || defined (RT_OS_SOLARIS)
/* Initialize the Shared Clipboard backend callbacks. */
PSHCLBACKEND pBackend = ShClSvcGetBackend();
SHCLCALLBACKS ShClCallbacks;
RT_ZERO(ShClCallbacks);
ShClCallbacks.pfnReportFormats = tstTestReadFromHost_ReportFormatsCallback;
ShClCallbacks.pfnOnClipboardRead = tstTestReadFromHost_OnClipboardReadCallback;
ShClBackendSetCallbacks(pBackend, &ShClCallbacks);
#elif defined (RT_OS_WINDOWS)
rc = SharedClipboardWinOpen(GetDesktopWindow());
if (RT_SUCCESS(rc))
{
rc = SharedClipboardWinDataWrite(CF_UNICODETEXT, pTask->pvData, (uint32_t)pTask->cbData);
SharedClipboardWinClose();
}
#endif /* defined (RT_OS_LINUX) || defined (RT_OS_SOLARIS) */
RTTestPrintf(g_hTest, RTTESTLVL_DEBUG, "Host data (%RU32):\n%.*Rhxd\n", pTask->cbData, pTask->cbData, pTask->pvData);
return rc;
}
static DECLCALLBACK(int) tstTestReadFromHostExec(PCLIPBOARDTESTCTX pTstCtx, void *pvCtx)
{
RT_NOREF(pvCtx);
TstHGCMUtilsGuestThreadStart(&pTstCtx->HGCM, tstTestReadFromHost_ThreadGuest, pTstCtx);
PTSTHGCMUTILSTASK pTask = (PTSTHGCMUTILSTASK)TstHGCMUtilsTaskGetCurrent(&pTstCtx->HGCM);
bool fUseMock = false;
TSTUSERMOCK UsrMock;
if (fUseMock)
tstTestReadFromHost_MockInit(&UsrMock, "tstX11Hst");
/* Wait until the task has been finished. */
TstHGCMUtilsTaskWait(pTask, RT_MS_30SEC);
if (fUseMock)
tstTestReadFromHost_MockDestroy(&UsrMock);
return VINF_SUCCESS;
}
static DECLCALLBACK(int) tstTestReadFromHostDestroy(PCLIPBOARDTESTCTX pTstCtx, void *pvCtx)
{
RT_NOREF(pvCtx);
int vrc = TstHGCMUtilsGuestThreadStop(&pTstCtx->HGCM);
AssertRC(vrc);
vrc = TstHGCMUtilsHostThreadStop(&pTstCtx->HGCM);
AssertRC(vrc);
return vrc;
}
/*********************************************************************************************************************************
* Main *
********************************************************************************************************************************/
/** Test definition table. */
CLIPBOARDTESTDESC g_aTests[] =
{
/* Tests guest reading clipboard data from the host. */
{ tstTestReadFromHostSetup, tstTestReadFromHostExec, tstTestReadFromHostDestroy }
};
/** Number of tests defined. */
unsigned g_cTests = RT_ELEMENTS(g_aTests);
static int tstOne(PTESTDESC pTstDesc)
{
PCLIPBOARDTESTCTX pTstCtx = &g_TstCtx;
void *pvCtx;
int rc = pTstDesc->pfnSetup(pTstCtx, &pvCtx);
if (RT_SUCCESS(rc))
{
rc = pTstDesc->pfnExec(pTstCtx, pvCtx);
int rc2 = pTstDesc->pfnDestroy(pTstCtx, pvCtx);
if (RT_SUCCESS(rc))
rc = rc2;
}
return rc;
}
int main(int argc, char *argv[])
{
/*
* Init the runtime, test and say hello.
*/
const char *pcszExecName;
NOREF(argc);
pcszExecName = strrchr(argv[0], '/');
pcszExecName = pcszExecName ? pcszExecName + 1 : argv[0];
RTEXITCODE rcExit = RTTestInitAndCreate(pcszExecName, &g_hTest);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
RTTestBanner(g_hTest);
#ifndef DEBUG_andy
/* Don't let assertions in the host service panic (core dump) the test cases. */
RTAssertSetMayPanic(false);
#endif
PTSTHGCMMOCKSVC const pSvc = TstHgcmMockSvcInst();
TstHgcmMockSvcCreate(pSvc, sizeof(SHCLCLIENT));
TstHgcmMockSvcStart(pSvc);
/*
* Run the tests.
*/
if (0)
{
testGuestSimple();
testHostCall();
}
RT_ZERO(g_TstCtx);
PTSTHGCMUTILSCTX pCtx = &g_TstCtx.HGCM;
TstHGCMUtilsCtxInit(pCtx, pSvc);
PTSTHGCMUTILSTASK pTask = (PTSTHGCMUTILSTASK)TstHGCMUtilsTaskGetCurrent(pCtx);
TstHGCMUtilsTaskInit(pTask);
pTask->pvUser = &g_TstCtx.Task;
for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
tstOne(&g_aTests[i]);
TstHGCMUtilsTaskDestroy(pTask);
TstHgcmMockSvcStop(pSvc);
TstHgcmMockSvcDestroy(pSvc);
/*
* Summary
*/
return RTTestSummaryAndDestroy(g_hTest);
}
|