1 | /* $Id: tstWinAdditionsInstallHelper.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * tstWinAdditionsInstallHelper - Testcases for the Windows Guest Additions Installer Helper DLL.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <iprt/win/windows.h>
|
---|
33 | #include "../exdll.h"
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 | #include <iprt/ldr.h>
|
---|
38 | #include <iprt/path.h>
|
---|
39 | #include <iprt/process.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/test.h>
|
---|
42 | #include <iprt/utf16.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Defined Constants And Macros *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /** Symbol names to test. */
|
---|
49 | #define TST_FILEGETARCHITECTURE_NAME "FileGetArchitecture"
|
---|
50 | #define TST_FILEGETVENDOR_NAME "FileGetVendor"
|
---|
51 | #define TST_VBOXTRAYSHOWBALLONMSG_NAME "VBoxTrayShowBallonMsg"
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Structures and Typedefs *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 | /** A generic NSIS plugin function. */
|
---|
58 | typedef void __cdecl NSIS_PLUGIN_FUNC(HWND hwndParent, int string_size, WCHAR *variables, stack_t **stacktop, extra_parameters *extra);
|
---|
59 | /** Pointer to a generic NSIS plugin function. */
|
---|
60 | typedef NSIS_PLUGIN_FUNC *PNSIS_PLUGIN_FUNC;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Destroys a stack.
|
---|
66 | *
|
---|
67 | * @param pStackTop Stack to destroy.
|
---|
68 | */
|
---|
69 | static void tstStackDestroy(stack_t *pStackTop)
|
---|
70 | {
|
---|
71 | while (pStackTop)
|
---|
72 | {
|
---|
73 | stack_t *pStackNext = pStackTop->next;
|
---|
74 | GlobalFree(pStackTop);
|
---|
75 | pStackTop = pStackNext;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Pushes a string to a stack
|
---|
81 | *
|
---|
82 | * @returns VBox status code.
|
---|
83 | * @param ppStackTop Stack to push string to.
|
---|
84 | * @param pwszString String to push to the stack.
|
---|
85 | */
|
---|
86 | static int tstStackPushString(stack_t **ppStackTop, const WCHAR *pwszString)
|
---|
87 | {
|
---|
88 | size_t cwcString = RTUtf16Len(pwszString);
|
---|
89 | stack_t *pStack = (stack_t *)GlobalAlloc(GPTR, RT_UOFFSETOF_DYN(stack_t, text[cwcString + 1]));
|
---|
90 | if (pStack)
|
---|
91 | {
|
---|
92 | AssertCompile(sizeof(pStack->text[0]) == sizeof(*pwszString));
|
---|
93 | memcpy(pStack->text, pwszString, (cwcString + 1) * sizeof(pStack->text[0]));
|
---|
94 | pStack->next = ppStackTop ? *ppStackTop : NULL;
|
---|
95 |
|
---|
96 | *ppStackTop = pStack;
|
---|
97 | return VINF_SUCCESS;
|
---|
98 | }
|
---|
99 | return VERR_NO_MEMORY;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Pops a string off a stack.
|
---|
104 | *
|
---|
105 | * @returns IPRT status code.
|
---|
106 | * @param ppStackTop Stack to pop off string from.
|
---|
107 | * @param pwszDst Where to return the string.
|
---|
108 | * @param cwcDst The size of the destination buffer.
|
---|
109 | */
|
---|
110 | static int tstStackPopString(stack_t **ppStackTop, WCHAR *pwszDst, size_t cwcDst)
|
---|
111 | {
|
---|
112 | stack_t *pStack = *ppStackTop;
|
---|
113 | if (pStack)
|
---|
114 | {
|
---|
115 | int rc = RTUtf16Copy(pwszDst, cwcDst, pStack->text);
|
---|
116 |
|
---|
117 | *ppStackTop = pStack->next;
|
---|
118 | GlobalFree((HGLOBAL)pStack);
|
---|
119 |
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 | return VERR_NOT_FOUND;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int main(int argc, char **argv)
|
---|
126 | {
|
---|
127 | RTTEST hTest;
|
---|
128 | RTEXITCODE rcExit = RTTestInitExAndCreate(argc, &argv, 0, "tstWinAdditionsInstallHelper", &hTest);
|
---|
129 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
130 | return rcExit;
|
---|
131 | RTTestBanner(hTest);
|
---|
132 |
|
---|
133 | char szGuestInstallHelperDll[RTPATH_MAX];
|
---|
134 | RTPathExecDir(szGuestInstallHelperDll, sizeof(szGuestInstallHelperDll));
|
---|
135 |
|
---|
136 | /** @todo This ASSUMES that this testcase always is located in the separate "bin/additions" sub directory
|
---|
137 | * and that we currently always repack the Guest Additions stuff in a separate directory.
|
---|
138 | * Might need some more tweaking ... */
|
---|
139 | int rc = RTPathAppend(szGuestInstallHelperDll, sizeof(szGuestInstallHelperDll),
|
---|
140 | "..\\..\\repackadd\\resources\\VBoxGuestInstallHelper\\VBoxGuestInstallHelper.dll");
|
---|
141 | if (RT_SUCCESS(rc))
|
---|
142 | {
|
---|
143 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Using DLL: %s\n", szGuestInstallHelperDll);
|
---|
144 |
|
---|
145 | RTLDRMOD hLdrMod;
|
---|
146 | rc = RTLdrLoad(szGuestInstallHelperDll, &hLdrMod);
|
---|
147 | if (RT_SUCCESS(rc))
|
---|
148 | {
|
---|
149 | WCHAR wszVars[NSIS_MAX_STRLEN * sizeof(WCHAR)] = { 0 };
|
---|
150 | WCHAR wszResult[NSIS_MAX_STRLEN];
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Tests FileGetArchitecture
|
---|
154 | */
|
---|
155 | PNSIS_PLUGIN_FUNC pfnFileGetArchitecture = NULL;
|
---|
156 | rc = RTLdrGetSymbol(hLdrMod, TST_FILEGETARCHITECTURE_NAME, (void**)&pfnFileGetArchitecture);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | stack_t *pStack = NULL;
|
---|
160 | tstStackPushString(&pStack, L"c:\\windows\\system32\\kernel32.dll");
|
---|
161 |
|
---|
162 | pfnFileGetArchitecture(NULL /* hWnd */, NSIS_MAX_STRLEN, wszVars, &pStack, NULL /* extra */);
|
---|
163 |
|
---|
164 | rc = tstStackPopString(&pStack, wszResult, sizeof(wszResult));
|
---|
165 | if ( RT_SUCCESS(rc)
|
---|
166 | && ( RTUtf16CmpAscii(wszResult, "x86") == 0
|
---|
167 | || RTUtf16CmpAscii(wszResult, "amd64") == 0))
|
---|
168 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Arch: %ls\n", wszResult);
|
---|
169 | else
|
---|
170 | RTTestIFailed("Getting file arch on kernel32 failed: %Rrc - '%ls', expected 'x86' or 'amd64'", rc, wszResult);
|
---|
171 |
|
---|
172 | if (pStack)
|
---|
173 | RTTestIFailed("Too many items on the stack!");
|
---|
174 | tstStackDestroy(pStack);
|
---|
175 | }
|
---|
176 | else
|
---|
177 | RTTestIFailed("Loading pfnFileGetArchitecture failed: %Rrc", rc);
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Tests FileGetVendor
|
---|
181 | */
|
---|
182 | PNSIS_PLUGIN_FUNC pfnFileGetVendor;
|
---|
183 | rc = RTLdrGetSymbol(hLdrMod, TST_FILEGETVENDOR_NAME, (void **)&pfnFileGetVendor);
|
---|
184 | if (RT_SUCCESS(rc))
|
---|
185 | {
|
---|
186 | stack_t *pStack = NULL;
|
---|
187 | tstStackPushString(&pStack, L"c:\\windows\\system32\\kernel32.dll");
|
---|
188 |
|
---|
189 | pfnFileGetVendor(NULL /* hWnd */, NSIS_MAX_STRLEN, wszVars, &pStack, NULL /* extra */);
|
---|
190 |
|
---|
191 | rc = tstStackPopString(&pStack, wszResult, RT_ELEMENTS(wszResult));
|
---|
192 | if ( RT_SUCCESS(rc)
|
---|
193 | && RTUtf16CmpAscii(wszResult, "Microsoft Corporation") == 0)
|
---|
194 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Vendor: %ls\n", wszResult);
|
---|
195 | else
|
---|
196 | RTTestIFailed("Getting file vendor failed: %Rrc - '%ls', expected 'Microsoft Corporation'\n", rc, wszResult);
|
---|
197 |
|
---|
198 | if (pStack)
|
---|
199 | RTTestIFailed("Too many items on the stack!");
|
---|
200 | tstStackDestroy(pStack);
|
---|
201 | }
|
---|
202 | else
|
---|
203 | RTTestIFailed("Loading pfnFileGetVendor failed: %Rrc", rc);
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Tests VBoxTrayShowBallonMsg
|
---|
207 | */
|
---|
208 | PNSIS_PLUGIN_FUNC pfnVBoxTrayShowBallonMsg;
|
---|
209 | rc = RTLdrGetSymbol(hLdrMod, TST_VBOXTRAYSHOWBALLONMSG_NAME, (void **)&pfnVBoxTrayShowBallonMsg);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | stack_t *pStack = NULL;
|
---|
213 | /* Push stuff in reverse order to the stack. */
|
---|
214 | tstStackPushString(&pStack, L"5000" /* Time to show in ms */);
|
---|
215 | tstStackPushString(&pStack, L"1" /* Type - info */);
|
---|
216 | tstStackPushString(&pStack, L"This is a message from tstWinAdditionsInstallHelper!");
|
---|
217 | tstStackPushString(&pStack, L"This is a title from tstWinAdditionsInstallHelper!");
|
---|
218 |
|
---|
219 | pfnVBoxTrayShowBallonMsg(NULL /* hWnd */, NSIS_MAX_STRLEN, wszVars, &pStack, NULL /* extra */);
|
---|
220 |
|
---|
221 | rc = tstStackPopString(&pStack, wszResult, RT_ELEMENTS(wszResult));
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Reply: '%ls'\n", wszResult);
|
---|
224 | else
|
---|
225 | RTTestIFailed("Sending message to VBoxTray failed: stack pop error - %Rrc", rc);
|
---|
226 |
|
---|
227 | if (pStack)
|
---|
228 | RTTestIFailed("Too many items on the stack!");
|
---|
229 | tstStackDestroy(pStack);
|
---|
230 | }
|
---|
231 | else
|
---|
232 | RTTestIFailed("Loading pfnVBoxTrayShowBallonMsg failed: %Rrc", rc);
|
---|
233 |
|
---|
234 | RTLdrClose(hLdrMod);
|
---|
235 | }
|
---|
236 | else
|
---|
237 | RTTestIFailed("Loading DLL failed: %Rrc", rc);
|
---|
238 | }
|
---|
239 | else
|
---|
240 | RTTestIFailed("Getting absolute path of DLL failed: %Rrc", rc);
|
---|
241 |
|
---|
242 | return RTTestSummaryAndDestroy(hTest);
|
---|
243 | }
|
---|
244 |
|
---|