1 | /* $Id: tstGuestControlSvc.cpp 90794 2021-08-23 13:16:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Testcase for the guest control service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
23 | #include <iprt/initterm.h>
|
---|
24 | #include <iprt/stream.h>
|
---|
25 | #include <iprt/test.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Global Variables *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | static RTTEST g_hTest = NIL_RTTEST;
|
---|
32 |
|
---|
33 | using namespace guestControl;
|
---|
34 |
|
---|
35 | extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable);
|
---|
36 |
|
---|
37 | /** Simple call handle structure for the guest call completion callback */
|
---|
38 | struct VBOXHGCMCALLHANDLE_TYPEDEF
|
---|
39 | {
|
---|
40 | /** Where to store the result code. */
|
---|
41 | int32_t rc;
|
---|
42 | };
|
---|
43 |
|
---|
44 | /** Call completion callback for guest calls. */
|
---|
45 | static DECLCALLBACK(int) callComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
|
---|
46 | {
|
---|
47 | callHandle->rc = rc;
|
---|
48 | return VINF_SUCCESS;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Initialise the HGCM service table as much as we need to start the
|
---|
53 | * service.
|
---|
54 | *
|
---|
55 | * @return IPRT status code.
|
---|
56 | * @param pTable the table to initialise
|
---|
57 | */
|
---|
58 | int initTable(VBOXHGCMSVCFNTABLE *pTable, VBOXHGCMSVCHELPERS *pHelpers)
|
---|
59 | {
|
---|
60 | pTable->cbSize = sizeof (VBOXHGCMSVCFNTABLE);
|
---|
61 | pTable->u32Version = VBOX_HGCM_SVC_VERSION;
|
---|
62 | pHelpers->pfnCallComplete = callComplete;
|
---|
63 | pTable->pHelpers = pHelpers;
|
---|
64 |
|
---|
65 | return VINF_SUCCESS;
|
---|
66 | }
|
---|
67 |
|
---|
68 | typedef struct CMDHOST
|
---|
69 | {
|
---|
70 | /** The HGCM command to execute. */
|
---|
71 | int cmd;
|
---|
72 | /** Number of parameters. */
|
---|
73 | int num_parms;
|
---|
74 | /** The actual parameters. */
|
---|
75 | const PVBOXHGCMSVCPARM parms;
|
---|
76 | /** Flag indicating whether we need a connected client for this command. */
|
---|
77 | bool fNeedsClient;
|
---|
78 | /** The desired return value from the host. */
|
---|
79 | int rc;
|
---|
80 | } CMDHOST, *PCMDHOST;
|
---|
81 |
|
---|
82 | typedef struct CMDCLIENT
|
---|
83 | {
|
---|
84 | /** The client's ID. */
|
---|
85 | int client_id;
|
---|
86 | /** The HGCM command to execute. */
|
---|
87 | int cmd;
|
---|
88 | /** Number of parameters. */
|
---|
89 | int num_parms;
|
---|
90 | /** The actual parameters. */
|
---|
91 | const PVBOXHGCMSVCPARM parms;
|
---|
92 | /** The desired return value from the host. */
|
---|
93 | int rc;
|
---|
94 | } CMDCLIENT, *PCMDCLIENT;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Tests the HOST_EXEC_CMD function.
|
---|
98 | * @returns iprt status value to indicate whether the test went as expected.
|
---|
99 | * @note prints its own diagnostic information to stdout.
|
---|
100 | */
|
---|
101 | static int testHostCmd(const VBOXHGCMSVCFNTABLE *pTable, const PCMDHOST pCmd, uint32_t uNumTests)
|
---|
102 | {
|
---|
103 | int rc = VINF_SUCCESS;
|
---|
104 | if (!RT_VALID_PTR(pTable->pfnHostCall))
|
---|
105 | {
|
---|
106 | RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid pfnHostCall() pointer\n");
|
---|
107 | rc = VERR_INVALID_POINTER;
|
---|
108 | }
|
---|
109 | if (RT_SUCCESS(rc))
|
---|
110 | {
|
---|
111 | for (unsigned i = 0; (i < uNumTests) && RT_SUCCESS(rc); i++)
|
---|
112 | {
|
---|
113 | RTTestPrintf(g_hTest, RTTESTLVL_INFO, "Testing #%u (cmd: %d, num_parms: %d, parms: 0x%p\n",
|
---|
114 | i, pCmd[i].cmd, pCmd[i].num_parms, pCmd[i].parms);
|
---|
115 |
|
---|
116 | if (pCmd[i].fNeedsClient)
|
---|
117 | {
|
---|
118 | int client_rc = pTable->pfnConnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */, 0, false);
|
---|
119 | if (RT_FAILURE(client_rc))
|
---|
120 | rc = client_rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (RT_SUCCESS(rc))
|
---|
124 | {
|
---|
125 | int host_rc = pTable->pfnHostCall(pTable->pvService,
|
---|
126 | pCmd[i].cmd,
|
---|
127 | pCmd[i].num_parms,
|
---|
128 | pCmd[i].parms);
|
---|
129 | if (host_rc != pCmd[i].rc)
|
---|
130 | {
|
---|
131 | RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Host call test #%u returned with rc=%Rrc instead of rc=%Rrc\n",
|
---|
132 | i, host_rc, pCmd[i].rc);
|
---|
133 | rc = host_rc;
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | rc = VERR_INVALID_PARAMETER;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (pCmd[i].fNeedsClient)
|
---|
139 | {
|
---|
140 | int client_rc = pTable->pfnDisconnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */);
|
---|
141 | if (RT_SUCCESS(rc))
|
---|
142 | rc = client_rc;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static int testHost(const VBOXHGCMSVCFNTABLE *pTable)
|
---|
151 | {
|
---|
152 | RTTestSub(g_hTest, "Testing host commands ...");
|
---|
153 |
|
---|
154 | VBOXHGCMSVCPARM aParms[1];
|
---|
155 | HGCMSvcSetU32(&aParms[0], 1000 /* Context ID */);
|
---|
156 |
|
---|
157 | CMDHOST aCmdHostAll[] =
|
---|
158 | {
|
---|
159 | #if 0
|
---|
160 | /** No client connected. */
|
---|
161 | { 1024 /* Not existing command */, 0, 0, false, VERR_NOT_FOUND },
|
---|
162 | { -1 /* Invalid command */, 0, 0, false, VERR_NOT_FOUND },
|
---|
163 | { HOST_CANCEL_PENDING_WAITS, 1024, 0, false, VERR_NOT_FOUND },
|
---|
164 | { HOST_CANCEL_PENDING_WAITS, 0, &aParms[0], false, VERR_NOT_FOUND },
|
---|
165 |
|
---|
166 | /** No client connected, valid command. */
|
---|
167 | { HOST_CANCEL_PENDING_WAITS, 0, 0, false, VERR_NOT_FOUND },
|
---|
168 |
|
---|
169 | /** Client connected, no parameters given. */
|
---|
170 | { HOST_EXEC_SET_INPUT, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
|
---|
171 | { 1024 /* Not existing command */, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
|
---|
172 | { -1 /* Invalid command */, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
|
---|
173 |
|
---|
174 | /** Client connected, valid parameters given. */
|
---|
175 | { HOST_CANCEL_PENDING_WAITS, 0, 0, true, VINF_SUCCESS },
|
---|
176 | { HOST_CANCEL_PENDING_WAITS, 1024, &aParms[0], true, VINF_SUCCESS },
|
---|
177 | { HOST_CANCEL_PENDING_WAITS, 0, &aParms[0], true, VINF_SUCCESS},
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | /** Client connected, invalid parameters given. */
|
---|
181 | { HOST_MSG_EXEC_CMD, 1024, 0, true, VERR_INVALID_POINTER },
|
---|
182 | { HOST_MSG_EXEC_CMD, 1, 0, true, VERR_INVALID_POINTER },
|
---|
183 | { HOST_MSG_EXEC_CMD, -1, 0, true, VERR_INVALID_POINTER },
|
---|
184 |
|
---|
185 | /** Client connected, parameters given. */
|
---|
186 | { HOST_MSG_CANCEL_PENDING_WAITS, 1, &aParms[0], true, VINF_SUCCESS },
|
---|
187 | { HOST_MSG_EXEC_CMD, 1, &aParms[0], true, VINF_SUCCESS },
|
---|
188 | { HOST_MSG_EXEC_SET_INPUT, 1, &aParms[0], true, VINF_SUCCESS },
|
---|
189 | { HOST_MSG_EXEC_GET_OUTPUT, 1, &aParms[0], true, VINF_SUCCESS },
|
---|
190 |
|
---|
191 | /** Client connected, unknown command + valid parameters given. */
|
---|
192 | { -1, 1, &aParms[0], true, VINF_SUCCESS }
|
---|
193 | };
|
---|
194 |
|
---|
195 | int rc = testHostCmd(pTable, &aCmdHostAll[0], RT_ELEMENTS(aCmdHostAll));
|
---|
196 | RTTestSubDone(g_hTest);
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 |
|
---|
200 | static int testClient(const VBOXHGCMSVCFNTABLE *pTable)
|
---|
201 | {
|
---|
202 | RTTestSub(g_hTest, "Testing client commands ...");
|
---|
203 |
|
---|
204 | int rc = pTable->pfnConnect(pTable->pvService, 1 /* Client ID */, NULL /* pvClient */, 0, false);
|
---|
205 | if (RT_SUCCESS(rc))
|
---|
206 | {
|
---|
207 | VBOXHGCMCALLHANDLE_TYPEDEF callHandle = { VINF_SUCCESS };
|
---|
208 |
|
---|
209 | /* No commands from host yet. */
|
---|
210 | VBOXHGCMSVCPARM aParmsGuest[8];
|
---|
211 | HGCMSvcSetU32(&aParmsGuest[0], 0 /* Msg type */);
|
---|
212 | HGCMSvcSetU32(&aParmsGuest[1], 0 /* Parameters */);
|
---|
213 | pTable->pfnCall(pTable->pvService, &callHandle, 1 /* Client ID */, NULL /* pvClient */,
|
---|
214 | GUEST_MSG_WAIT, 2, &aParmsGuest[0], 0);
|
---|
215 | RTTEST_CHECK_RC_RET(g_hTest, callHandle.rc, VINF_SUCCESS, callHandle.rc);
|
---|
216 |
|
---|
217 | /* Host: Add a dummy command. */
|
---|
218 | VBOXHGCMSVCPARM aParmsHost[8];
|
---|
219 | HGCMSvcSetU32(&aParmsHost[0], 1000 /* Context ID */);
|
---|
220 | HGCMSvcSetStr(&aParmsHost[1], "foo.bar");
|
---|
221 | HGCMSvcSetStr(&aParmsHost[2], "baz");
|
---|
222 |
|
---|
223 | rc = pTable->pfnHostCall(pTable->pvService, HOST_MSG_EXEC_CMD, 3, &aParmsHost[0]);
|
---|
224 | RTTEST_CHECK_RC_RET(g_hTest, rc, VINF_SUCCESS, rc);
|
---|
225 |
|
---|
226 | /* Client: Disconnect again. */
|
---|
227 | int rc2 = pTable->pfnDisconnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | rc = rc2;
|
---|
230 | }
|
---|
231 |
|
---|
232 | RTTestSubDone(g_hTest);
|
---|
233 | return rc;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Set environment variable "IPRT_TEST_MAX_LEVEL=all" to get more debug output!
|
---|
238 | */
|
---|
239 | int main()
|
---|
240 | {
|
---|
241 | RTEXITCODE rcExit = RTTestInitAndCreate("tstGuestControlSvc", &g_hTest);
|
---|
242 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
243 | return rcExit;
|
---|
244 | RTTestBanner(g_hTest);
|
---|
245 |
|
---|
246 | /* Some host info. */
|
---|
247 | RTTestIPrintf(RTTESTLVL_ALWAYS, "sizeof(void*)=%d\n", sizeof(void*));
|
---|
248 |
|
---|
249 | /* Do the tests. */
|
---|
250 | VBOXHGCMSVCFNTABLE svcTable;
|
---|
251 | VBOXHGCMSVCHELPERS svcHelpers;
|
---|
252 | RTTEST_CHECK_RC_RET(g_hTest, initTable(&svcTable, &svcHelpers), VINF_SUCCESS, 1);
|
---|
253 |
|
---|
254 | do
|
---|
255 | {
|
---|
256 | RTTESTI_CHECK_RC_BREAK(VBoxHGCMSvcLoad(&svcTable), VINF_SUCCESS);
|
---|
257 |
|
---|
258 | RTTESTI_CHECK_RC_BREAK(testHost(&svcTable), VINF_SUCCESS);
|
---|
259 |
|
---|
260 | RTTESTI_CHECK_RC_BREAK(svcTable.pfnUnload(svcTable.pvService), VINF_SUCCESS);
|
---|
261 |
|
---|
262 | RTTESTI_CHECK_RC_BREAK(VBoxHGCMSvcLoad(&svcTable), VINF_SUCCESS);
|
---|
263 |
|
---|
264 | RTTESTI_CHECK_RC_BREAK(testClient(&svcTable), VINF_SUCCESS);
|
---|
265 |
|
---|
266 | RTTESTI_CHECK_RC_BREAK(svcTable.pfnUnload(svcTable.pvService), VINF_SUCCESS);
|
---|
267 |
|
---|
268 | } while (0);
|
---|
269 |
|
---|
270 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
271 | }
|
---|
272 |
|
---|