1 | /* $Id: tstClipboardServiceImpl.cpp 93919 2022-02-24 13:59:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard host service implementation (backend) test case.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "../VBoxSharedClipboardSvc-internal.h"
|
---|
19 |
|
---|
20 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
21 | #ifdef RT_OS_WINDOWS
|
---|
22 | # include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/test.h>
|
---|
28 |
|
---|
29 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *ptable);
|
---|
30 |
|
---|
31 | static SHCLCLIENT g_Client;
|
---|
32 | static VBOXHGCMSVCHELPERS g_Helpers = { NULL };
|
---|
33 |
|
---|
34 | /** Simple call handle structure for the guest call completion callback */
|
---|
35 | struct VBOXHGCMCALLHANDLE_TYPEDEF
|
---|
36 | {
|
---|
37 | /** Where to store the result code */
|
---|
38 | int32_t rc;
|
---|
39 | };
|
---|
40 |
|
---|
41 | /** Call completion callback for guest calls. */
|
---|
42 | static DECLCALLBACK(int) callComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
|
---|
43 | {
|
---|
44 | callHandle->rc = rc;
|
---|
45 | return VINF_SUCCESS;
|
---|
46 | }
|
---|
47 |
|
---|
48 | static int setupTable(VBOXHGCMSVCFNTABLE *pTable)
|
---|
49 | {
|
---|
50 | pTable->cbSize = sizeof(*pTable);
|
---|
51 | pTable->u32Version = VBOX_HGCM_SVC_VERSION;
|
---|
52 | g_Helpers.pfnCallComplete = callComplete;
|
---|
53 | pTable->pHelpers = &g_Helpers;
|
---|
54 | return VBoxHGCMSvcLoad(pTable);
|
---|
55 | }
|
---|
56 |
|
---|
57 | int ShClBackendInit(PSHCLBACKEND, VBOXHGCMSVCFNTABLE *) { return VINF_SUCCESS; }
|
---|
58 | void ShClBackendDestroy(PSHCLBACKEND) { }
|
---|
59 | int ShClBackendDisconnect(PSHCLBACKEND, PSHCLCLIENT) { return VINF_SUCCESS; }
|
---|
60 | int ShClBackendConnect(PSHCLBACKEND, PSHCLCLIENT, bool) { return VINF_SUCCESS; }
|
---|
61 | int ShClBackendReportFormats(PSHCLBACKEND, PSHCLCLIENT, SHCLFORMATS) { AssertFailed(); return VINF_SUCCESS; }
|
---|
62 | int ShClBackendReadData(PSHCLBACKEND, PSHCLCLIENT, PSHCLCLIENTCMDCTX, SHCLFORMAT, void *, uint32_t, unsigned int *) { AssertFailed(); return VERR_WRONG_ORDER; }
|
---|
63 | int ShClBackendWriteData(PSHCLBACKEND, PSHCLCLIENT, PSHCLCLIENTCMDCTX, SHCLFORMAT, void *, uint32_t) { AssertFailed(); return VINF_SUCCESS; }
|
---|
64 | int ShClBackendSync(PSHCLBACKEND, PSHCLCLIENT) { return VINF_SUCCESS; }
|
---|
65 |
|
---|
66 | static void testAnnounceAndReadData(void)
|
---|
67 | {
|
---|
68 | struct VBOXHGCMSVCPARM parms[2];
|
---|
69 | VBOXHGCMSVCFNTABLE table;
|
---|
70 | int rc;
|
---|
71 |
|
---|
72 | RTTestISub("Setting up client ...");
|
---|
73 | RTTestIDisableAssertions();
|
---|
74 |
|
---|
75 | rc = setupTable(&table);
|
---|
76 | RTTESTI_CHECK_MSG_RETV(RT_SUCCESS(rc), ("rc=%Rrc\n", rc));
|
---|
77 | /* Unless we are bidirectional the host message requests will be dropped. */
|
---|
78 | HGCMSvcSetU32(&parms[0], VBOX_SHCL_MODE_BIDIRECTIONAL);
|
---|
79 | rc = table.pfnHostCall(NULL, VBOX_SHCL_HOST_FN_SET_MODE, 1, parms);
|
---|
80 | RTTESTI_CHECK_RC_OK(rc);
|
---|
81 | rc = shClSvcClientInit(&g_Client, 1 /* clientId */);
|
---|
82 | RTTESTI_CHECK_RC_OK(rc);
|
---|
83 |
|
---|
84 | RTTestIRestoreAssertions();
|
---|
85 | }
|
---|
86 |
|
---|
87 | #ifdef RT_OS_WINDOWS
|
---|
88 | # include "VBoxOrgCfHtml1.h" /* From chrome 97.0.4692.71 */
|
---|
89 | # include "VBoxOrgMimeHtml1.h"
|
---|
90 |
|
---|
91 | static void testHtmlCf(void)
|
---|
92 | {
|
---|
93 | RTTestISub("CF_HTML");
|
---|
94 |
|
---|
95 | char *pszOutput = NULL;
|
---|
96 | uint32_t cbOutput = UINT32_MAX/2;
|
---|
97 | RTTestIDisableAssertions();
|
---|
98 | RTTESTI_CHECK_RC(SharedClipboardWinConvertCFHTMLToMIME("", 0, &pszOutput, &cbOutput), VERR_INVALID_PARAMETER);
|
---|
99 | RTTestIRestoreAssertions();
|
---|
100 |
|
---|
101 | pszOutput = NULL;
|
---|
102 | cbOutput = UINT32_MAX/2;
|
---|
103 | RTTESTI_CHECK_RC(SharedClipboardWinConvertCFHTMLToMIME((char *)&g_abVBoxOrgCfHtml1[0], g_cbVBoxOrgCfHtml1,
|
---|
104 | &pszOutput, &cbOutput), VINF_SUCCESS);
|
---|
105 | RTTESTI_CHECK(cbOutput == g_cbVBoxOrgMimeHtml1);
|
---|
106 | RTTESTI_CHECK(memcmp(pszOutput, g_abVBoxOrgMimeHtml1, cbOutput) == 0);
|
---|
107 | RTMemFree(pszOutput);
|
---|
108 |
|
---|
109 |
|
---|
110 | static RTSTRTUPLE const s_aRoundTrips[] =
|
---|
111 | {
|
---|
112 | { RT_STR_TUPLE("") },
|
---|
113 | { RT_STR_TUPLE("1") },
|
---|
114 | { RT_STR_TUPLE("12") },
|
---|
115 | { RT_STR_TUPLE("123") },
|
---|
116 | { RT_STR_TUPLE("1234") },
|
---|
117 | { RT_STR_TUPLE("12345") },
|
---|
118 | { RT_STR_TUPLE("123456") },
|
---|
119 | { RT_STR_TUPLE("1234567") },
|
---|
120 | { RT_STR_TUPLE("12345678") },
|
---|
121 | { RT_STR_TUPLE("123456789") },
|
---|
122 | { RT_STR_TUPLE("1234567890") },
|
---|
123 | { RT_STR_TUPLE("<h2>asdfkjhasdflhj</h2>") },
|
---|
124 | { RT_STR_TUPLE("<h2>asdfkjhasdflhj</h2>\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") },
|
---|
125 | { (const char *)g_abVBoxOrgMimeHtml1, sizeof(g_abVBoxOrgMimeHtml1) },
|
---|
126 | };
|
---|
127 |
|
---|
128 | for (size_t i = 0; i < RT_ELEMENTS(s_aRoundTrips); i++)
|
---|
129 | {
|
---|
130 | int rc;
|
---|
131 | char *pszCfHtml = NULL;
|
---|
132 | uint32_t cbCfHtml = UINT32_MAX/2;
|
---|
133 | rc = SharedClipboardWinConvertMIMEToCFHTML(s_aRoundTrips[i].psz, s_aRoundTrips[i].cch + 1, &pszCfHtml, &cbCfHtml);
|
---|
134 | if (rc == VINF_SUCCESS)
|
---|
135 | {
|
---|
136 | if (strlen(pszCfHtml) + 1 != cbCfHtml)
|
---|
137 | RTTestIFailed("#%u: SharedClipboardWinConvertMIMEToCFHTML(%s, %#zx,,) returned incorrect length: %#x, actual %#zx",
|
---|
138 | i, s_aRoundTrips[i].psz, s_aRoundTrips[i].cch, cbCfHtml, strlen(pszCfHtml) + 1);
|
---|
139 |
|
---|
140 | char *pszHtml = NULL;
|
---|
141 | uint32_t cbHtml = UINT32_MAX/4;
|
---|
142 | rc = SharedClipboardWinConvertCFHTMLToMIME(pszCfHtml, (uint32_t)strlen(pszCfHtml), &pszHtml, &cbHtml);
|
---|
143 | if (rc == VINF_SUCCESS)
|
---|
144 | {
|
---|
145 | if (strlen(pszHtml) + 1 != cbHtml)
|
---|
146 | RTTestIFailed("#%u: SharedClipboardWinConvertCFHTMLToMIME(%s, %#zx,,) returned incorrect length: %#x, actual %#zx",
|
---|
147 | i, pszHtml, strlen(pszHtml), cbHtml, strlen(pszHtml) + 1);
|
---|
148 | if (strcmp(pszHtml, s_aRoundTrips[i].psz) != 0)
|
---|
149 | RTTestIFailed("#%u: roundtrip for '%s' LB %#zx failed, ended up with '%s'",
|
---|
150 | i, s_aRoundTrips[i].psz, s_aRoundTrips[i].cch, pszHtml);
|
---|
151 | RTMemFree(pszHtml);
|
---|
152 | }
|
---|
153 | else
|
---|
154 | RTTestIFailed("#%u: SharedClipboardWinConvertCFHTMLToMIME(%s, %#zx,,) returned %Rrc, expected VINF_SUCCESS",
|
---|
155 | i, pszCfHtml, strlen(pszCfHtml), rc);
|
---|
156 | RTMemFree(pszCfHtml);
|
---|
157 | }
|
---|
158 | else
|
---|
159 | RTTestIFailed("#%u: SharedClipboardWinConvertMIMEToCFHTML(%s, %#zx,,) returned %Rrc, expected VINF_SUCCESS",
|
---|
160 | i, s_aRoundTrips[i].psz, s_aRoundTrips[i].cch, rc);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | #endif /* RT_OS_WINDOWS */
|
---|
165 |
|
---|
166 |
|
---|
167 | int main(int argc, char *argv[])
|
---|
168 | {
|
---|
169 | /*
|
---|
170 | * Init the runtime, test and say hello.
|
---|
171 | */
|
---|
172 | const char *pcszExecName;
|
---|
173 | NOREF(argc);
|
---|
174 | pcszExecName = strrchr(argv[0], '/');
|
---|
175 | pcszExecName = pcszExecName ? pcszExecName + 1 : argv[0];
|
---|
176 | RTTEST hTest;
|
---|
177 | RTEXITCODE rcExit = RTTestInitAndCreate(pcszExecName, &hTest);
|
---|
178 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
179 | return rcExit;
|
---|
180 | RTTestBanner(hTest);
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Run the tests.
|
---|
184 | */
|
---|
185 | testAnnounceAndReadData();
|
---|
186 | #ifdef RT_OS_WINDOWS
|
---|
187 | testHtmlCf();
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Summary
|
---|
192 | */
|
---|
193 | return RTTestSummaryAndDestroy(hTest);
|
---|
194 | }
|
---|
195 |
|
---|