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
|
/* $Id: tstGuestControlSvc.cpp $ */
/** @file
* Testcase for the guest control service.
*/
/*
* 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
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <VBox/HostServices/GuestControlSvc.h>
#include <iprt/initterm.h>
#include <iprt/stream.h>
#include <iprt/test.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
static RTTEST g_hTest = NIL_RTTEST;
using namespace guestControl;
extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable);
/** Simple call handle structure for the guest call completion callback */
struct VBOXHGCMCALLHANDLE_TYPEDEF
{
/** Where to store the result code. */
int32_t rc;
};
/** Call completion callback for guest calls. */
static DECLCALLBACK(int) callComplete(VBOXHGCMCALLHANDLE callHandle, int32_t rc)
{
callHandle->rc = rc;
return VINF_SUCCESS;
}
/**
* Initialise the HGCM service table as much as we need to start the
* service.
*
* @return IPRT status code.
* @param pTable the table to initialise
*/
int initTable(VBOXHGCMSVCFNTABLE *pTable, VBOXHGCMSVCHELPERS *pHelpers)
{
pTable->cbSize = sizeof (VBOXHGCMSVCFNTABLE);
pTable->u32Version = VBOX_HGCM_SVC_VERSION;
pHelpers->pfnCallComplete = callComplete;
pTable->pHelpers = pHelpers;
return VINF_SUCCESS;
}
typedef struct CMDHOST
{
/** The HGCM command to execute. */
int cmd;
/** Number of parameters. */
int num_parms;
/** The actual parameters. */
const PVBOXHGCMSVCPARM parms;
/** Flag indicating whether we need a connected client for this command. */
bool fNeedsClient;
/** The desired return value from the host. */
int rc;
} CMDHOST, *PCMDHOST;
typedef struct CMDCLIENT
{
/** The client's ID. */
int client_id;
/** The HGCM command to execute. */
int cmd;
/** Number of parameters. */
int num_parms;
/** The actual parameters. */
const PVBOXHGCMSVCPARM parms;
/** The desired return value from the host. */
int rc;
} CMDCLIENT, *PCMDCLIENT;
/**
* Tests the HOST_EXEC_CMD function.
* @returns iprt status value to indicate whether the test went as expected.
* @note prints its own diagnostic information to stdout.
*/
static int testHostCmd(const VBOXHGCMSVCFNTABLE *pTable, const PCMDHOST pCmd, uint32_t uNumTests)
{
int rc = VINF_SUCCESS;
if (!RT_VALID_PTR(pTable->pfnHostCall))
{
RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid pfnHostCall() pointer\n");
rc = VERR_INVALID_POINTER;
}
if (RT_SUCCESS(rc))
{
for (unsigned i = 0; (i < uNumTests) && RT_SUCCESS(rc); i++)
{
RTTestPrintf(g_hTest, RTTESTLVL_INFO, "Testing #%u (cmd: %d, num_parms: %d, parms: 0x%p\n",
i, pCmd[i].cmd, pCmd[i].num_parms, pCmd[i].parms);
if (pCmd[i].fNeedsClient)
{
int client_rc = pTable->pfnConnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */, 0, false);
if (RT_FAILURE(client_rc))
rc = client_rc;
}
if (RT_SUCCESS(rc))
{
int host_rc = pTable->pfnHostCall(pTable->pvService,
pCmd[i].cmd,
pCmd[i].num_parms,
pCmd[i].parms);
if (host_rc != pCmd[i].rc)
{
RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Host call test #%u returned with rc=%Rrc instead of rc=%Rrc\n",
i, host_rc, pCmd[i].rc);
rc = host_rc;
if (RT_SUCCESS(rc))
rc = VERR_INVALID_PARAMETER;
}
if (pCmd[i].fNeedsClient)
{
int client_rc = pTable->pfnDisconnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */);
if (RT_SUCCESS(rc))
rc = client_rc;
}
}
}
}
return rc;
}
static int testHost(const VBOXHGCMSVCFNTABLE *pTable)
{
RTTestSub(g_hTest, "Testing host commands ...");
VBOXHGCMSVCPARM aParms[1];
HGCMSvcSetU32(&aParms[0], 1000 /* Context ID */);
CMDHOST aCmdHostAll[] =
{
#if 0
/** No client connected. */
{ 1024 /* Not existing command */, 0, 0, false, VERR_NOT_FOUND },
{ -1 /* Invalid command */, 0, 0, false, VERR_NOT_FOUND },
{ HOST_CANCEL_PENDING_WAITS, 1024, 0, false, VERR_NOT_FOUND },
{ HOST_CANCEL_PENDING_WAITS, 0, &aParms[0], false, VERR_NOT_FOUND },
/** No client connected, valid command. */
{ HOST_CANCEL_PENDING_WAITS, 0, 0, false, VERR_NOT_FOUND },
/** Client connected, no parameters given. */
{ HOST_EXEC_SET_INPUT, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
{ 1024 /* Not existing command */, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
{ -1 /* Invalid command */, 0 /* No parameters given */, 0, true, VERR_INVALID_PARAMETER },
/** Client connected, valid parameters given. */
{ HOST_CANCEL_PENDING_WAITS, 0, 0, true, VINF_SUCCESS },
{ HOST_CANCEL_PENDING_WAITS, 1024, &aParms[0], true, VINF_SUCCESS },
{ HOST_CANCEL_PENDING_WAITS, 0, &aParms[0], true, VINF_SUCCESS},
#endif
/** Client connected, invalid parameters given. */
{ HOST_MSG_EXEC_CMD, 1024, 0, true, VERR_INVALID_POINTER },
{ HOST_MSG_EXEC_CMD, 1, 0, true, VERR_INVALID_POINTER },
{ HOST_MSG_EXEC_CMD, -1, 0, true, VERR_INVALID_POINTER },
/** Client connected, parameters given. */
{ HOST_MSG_CANCEL_PENDING_WAITS, 1, &aParms[0], true, VINF_SUCCESS },
{ HOST_MSG_EXEC_CMD, 1, &aParms[0], true, VINF_SUCCESS },
{ HOST_MSG_EXEC_SET_INPUT, 1, &aParms[0], true, VINF_SUCCESS },
{ HOST_MSG_EXEC_GET_OUTPUT, 1, &aParms[0], true, VINF_SUCCESS },
/** Client connected, unknown command + valid parameters given. */
{ -1, 1, &aParms[0], true, VINF_SUCCESS }
};
int rc = testHostCmd(pTable, &aCmdHostAll[0], RT_ELEMENTS(aCmdHostAll));
RTTestSubDone(g_hTest);
return rc;
}
static int testClient(const VBOXHGCMSVCFNTABLE *pTable)
{
RTTestSub(g_hTest, "Testing client commands ...");
int rc = pTable->pfnConnect(pTable->pvService, 1 /* Client ID */, NULL /* pvClient */, 0, false);
if (RT_SUCCESS(rc))
{
VBOXHGCMCALLHANDLE_TYPEDEF callHandle = { VINF_SUCCESS };
/* No commands from host yet. */
VBOXHGCMSVCPARM aParmsGuest[8];
HGCMSvcSetU32(&aParmsGuest[0], 0 /* Msg type */);
HGCMSvcSetU32(&aParmsGuest[1], 0 /* Parameters */);
pTable->pfnCall(pTable->pvService, &callHandle, 1 /* Client ID */, NULL /* pvClient */,
GUEST_MSG_WAIT, 2, &aParmsGuest[0], 0);
RTTEST_CHECK_RC_RET(g_hTest, callHandle.rc, VINF_SUCCESS, callHandle.rc);
/* Host: Add a dummy command. */
VBOXHGCMSVCPARM aParmsHost[8];
HGCMSvcSetU32(&aParmsHost[0], 1000 /* Context ID */);
HGCMSvcSetStr(&aParmsHost[1], "foo.bar");
HGCMSvcSetStr(&aParmsHost[2], "baz");
rc = pTable->pfnHostCall(pTable->pvService, HOST_MSG_EXEC_CMD, 3, &aParmsHost[0]);
RTTEST_CHECK_RC_RET(g_hTest, rc, VINF_SUCCESS, rc);
/* Client: Disconnect again. */
int rc2 = pTable->pfnDisconnect(pTable->pvService, 1000 /* Client ID */, NULL /* pvClient */);
if (RT_SUCCESS(rc))
rc = rc2;
}
RTTestSubDone(g_hTest);
return rc;
}
/*
* Set environment variable "IPRT_TEST_MAX_LEVEL=all" to get more debug output!
*/
int main()
{
RTEXITCODE rcExit = RTTestInitAndCreate("tstGuestControlSvc", &g_hTest);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
RTTestBanner(g_hTest);
/* Some host info. */
RTTestIPrintf(RTTESTLVL_ALWAYS, "sizeof(void*)=%d\n", sizeof(void*));
/* Do the tests. */
VBOXHGCMSVCFNTABLE svcTable;
VBOXHGCMSVCHELPERS svcHelpers;
RTTEST_CHECK_RC_RET(g_hTest, initTable(&svcTable, &svcHelpers), VINF_SUCCESS, 1);
do
{
RTTESTI_CHECK_RC_BREAK(VBoxHGCMSvcLoad(&svcTable), VINF_SUCCESS);
RTTESTI_CHECK_RC_BREAK(testHost(&svcTable), VINF_SUCCESS);
RTTESTI_CHECK_RC_BREAK(svcTable.pfnUnload(svcTable.pvService), VINF_SUCCESS);
RTTESTI_CHECK_RC_BREAK(VBoxHGCMSvcLoad(&svcTable), VINF_SUCCESS);
RTTESTI_CHECK_RC_BREAK(testClient(&svcTable), VINF_SUCCESS);
RTTESTI_CHECK_RC_BREAK(svcTable.pfnUnload(svcTable.pvService), VINF_SUCCESS);
} while (0);
return RTTestSummaryAndDestroy(g_hTest);
}
|