VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Installer/InstallHelper/testcase/tstWinAdditionsInstallHelper.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 3 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: tstWinAdditionsInstallHelper.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * tstWinAdditionsInstallHelper - Testcases for the Windows Guest Additions Installer Helper DLL.
4 */
5
6/*
7 * Copyright (C) 2019-2022 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
34#include <tchar.h>
35
36#pragma warning(push)
37#pragma warning(disable: 4995) /* warning C4995: 'lstrcpyA': name was marked as #pragma deprecated */
38#include "../exdll.h"
39#pragma warning(pop)
40
41#include <iprt/assert.h>
42#include <iprt/errcore.h>
43#include <iprt/ldr.h>
44#include <iprt/path.h>
45#include <iprt/process.h>
46#include <iprt/string.h>
47#include <iprt/test.h>
48#include <iprt/utf16.h>
49
50
51RT_C_DECLS_BEGIN
52
53/** A generic NSIS plugin function. */
54typedef void __cdecl NSIS_PLUGIN_FUNC(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra);
55/** Pointer to a generic NSIS plugin function. */
56typedef NSIS_PLUGIN_FUNC *PNSIS_PLUGIN_FUNC;
57
58RT_C_DECLS_END
59
60/** Symbol names to test. */
61#define TST_FILEGETARCHITECTURE_NAME "FileGetArchitecture"
62#define TST_FILEGETVENDOR_NAME "FileGetVendor"
63#define TST_VBOXTRAYSHOWBALLONMSG_NAME "VBoxTrayShowBallonMsg"
64
65
66/**
67 * Destroys a stack.
68 *
69 * @param pStackTop Stack to destroy.
70 */
71static void tstStackDestroy(stack_t *pStackTop)
72{
73 while (pStackTop)
74 {
75 stack_t *pStackNext = pStackTop->next;
76 GlobalFree(pStackTop);
77 pStackTop = pStackNext;
78 }
79}
80
81/**
82 * Pushes a string to a stack
83 *
84 * @returns VBox status code.
85 * @param ppStackTop Stack to push string to.
86 * @param pcszString String to push to the stack.
87 */
88static int tstStackPushString(stack_t **ppStackTop, const TCHAR *pcszString)
89{
90 int rc = VINF_SUCCESS;
91
92 /* Termination space is part of stack_t. */
93 stack_t *pStack = (stack_t *)GlobalAlloc(GPTR, sizeof(stack_t) + g_stringsize);
94 if (pStack)
95 {
96 lstrcpyn(pStack->text, pcszString, (int)g_stringsize);
97 pStack->next = ppStackTop ? *ppStackTop : NULL;
98
99 *ppStackTop = pStack;
100 }
101 else
102 rc = VERR_NO_MEMORY;
103
104 return rc;
105}
106
107/**
108 * Pops a string off a stack.
109 *
110 * @returns Allocated string popped off the stack, or NULL on error / empty stack.
111 * @param ppStackTop Stack to pop off string from.
112 */
113static int tstStackPopString(stack_t **ppStackTop, TCHAR *pszStr, size_t cchStr)
114{
115 stack_t *pStack = *ppStackTop;
116 lstrcpyn(pszStr, pStack->text, cchStr);
117 *ppStackTop = pStack->next;
118 GlobalFree((HGLOBAL)pStack);
119
120 return VINF_SUCCESS;
121}
122
123int main()
124{
125 RTTEST hTest;
126 RTEXITCODE rcExit = RTTestInitAndCreate("tstWinAdditionsInstallHelper", &hTest);
127 if (rcExit != RTEXITCODE_SUCCESS)
128 return rcExit;
129 RTTestBanner(hTest);
130
131 char szGuestInstallHelperDll[RTPATH_MAX];
132 RTProcGetExecutablePath(szGuestInstallHelperDll, sizeof(szGuestInstallHelperDll));
133
134 /* Set the default string size. */
135 g_stringsize = NSIS_MAX_STRLEN;
136
137 /** @todo This ASSUMES that this testcase always is located in the separate "bin/additions" sub directory
138 * and that we currently always repack the Guest Additions stuff in a separate directory.
139 * Might need some more tweaking ... */
140 int rc = RTPathAppend(szGuestInstallHelperDll, sizeof(szGuestInstallHelperDll),
141 "../../../repack/resources/VBoxGuestInstallHelper/VBoxGuestInstallHelper.dll");
142 if (RT_SUCCESS(rc))
143 {
144 RTTestIPrintf(RTTESTLVL_ALWAYS, "Using DLL: %s\n", szGuestInstallHelperDll);
145
146#ifdef UNICODE
147 const bool fUnicode = true;
148#else
149 const bool fUnicode = false;
150#endif
151 RTTestIPrintf(RTTESTLVL_ALWAYS, "Running %s build\n", fUnicode ? "UNICODE" : "ANSI");
152
153 RTLDRMOD hLdrMod;
154 rc = RTLdrLoad(szGuestInstallHelperDll, &hLdrMod);
155 if (RT_SUCCESS(rc))
156 {
157 TCHAR szVars[NSIS_MAX_STRLEN * sizeof(TCHAR)] = { 0 };
158
159 /*
160 * Tests FileGetArchitecture
161 */
162 PNSIS_PLUGIN_FUNC pfnFileGetArchitecture = NULL;
163 rc = RTLdrGetSymbol(hLdrMod, TST_FILEGETARCHITECTURE_NAME, (void**)&pfnFileGetArchitecture);
164 if (RT_SUCCESS(rc))
165 {
166 stack_t *pStack = NULL;
167 tstStackPushString(&pStack, _T("c:\\windows\\system32\\kernel32.dll"));
168
169 pfnFileGetArchitecture(NULL /* hWnd */, NSIS_MAX_STRLEN, szVars, &pStack, NULL /* extra */);
170
171 TCHAR szStack[NSIS_MAX_STRLEN];
172 rc = tstStackPopString(&pStack, szStack, sizeof(szStack));
173 if ( RT_SUCCESS(rc)
174 && ( !_tcscmp(szStack, _T("x86"))
175 || !_tcscmp(szStack, _T("amd64"))))
176 {
177 if (fUnicode)
178 RTTestIPrintf(RTTESTLVL_ALWAYS, "Arch: %ls\n", szStack);
179 else
180 RTTestIPrintf(RTTESTLVL_ALWAYS, "Arch: %s\n", szStack);
181 }
182 else
183 RTTestIFailed("Getting file arch failed (NSIS API changed?)\n");
184 tstStackDestroy(pStack);
185 }
186 else
187 RTTestIFailed("Loading pfnFileGetArchitecture failed: %Rrc", rc);
188
189 /*
190 * Tests FileGetVendor
191 */
192 PNSIS_PLUGIN_FUNC pfnFileGetVendor;
193 rc = RTLdrGetSymbol(hLdrMod, TST_FILEGETVENDOR_NAME, (void**)&pfnFileGetVendor);
194 if (RT_SUCCESS(rc))
195 {
196 stack_t *pStack = NULL;
197 tstStackPushString(&pStack, _T("c:\\windows\\system32\\kernel32.dll"));
198
199 pfnFileGetVendor(NULL /* hWnd */, NSIS_MAX_STRLEN, szVars, &pStack, NULL /* extra */);
200
201 TCHAR szStack[NSIS_MAX_STRLEN];
202 rc = tstStackPopString(&pStack, szStack, RT_ELEMENTS(szStack));
203 if ( RT_SUCCESS(rc)
204 && !_tcscmp(szStack, _T("Microsoft Corporation")))
205 {
206 if (fUnicode)
207 RTTestIPrintf(RTTESTLVL_ALWAYS, "Vendor: %ls\n", szStack);
208 else
209 RTTestIPrintf(RTTESTLVL_ALWAYS, "Vendor: %s\n", szStack);
210 }
211 else
212 RTTestIFailed("Getting file vendor failed (NSIS API changed?)\n");
213 tstStackDestroy(pStack);
214 }
215 else
216 RTTestIFailed("Loading pfnFileGetVendor failed: %Rrc", rc);
217
218 /*
219 * Tests VBoxTrayShowBallonMsg
220 */
221 PNSIS_PLUGIN_FUNC pfnVBoxTrayShowBallonMsg;
222 rc = RTLdrGetSymbol(hLdrMod, TST_VBOXTRAYSHOWBALLONMSG_NAME, (void **)&pfnVBoxTrayShowBallonMsg);
223 if (RT_SUCCESS(rc))
224 {
225 stack_t *pStack = NULL;
226 /* Push stuff in reverse order to the stack. */
227 tstStackPushString(&pStack, _T("5000") /* Time to show in ms */);
228 tstStackPushString(&pStack, _T("1") /* Type */);
229 tstStackPushString(&pStack, _T("This is a message from tstWinAdditionsInstallHelper!"));
230 tstStackPushString(&pStack, _T("This is a title from tstWinAdditionsInstallHelper!"));
231
232 pfnVBoxTrayShowBallonMsg(NULL /* hWnd */, NSIS_MAX_STRLEN, szVars, &pStack, NULL /* extra */);
233
234 TCHAR szStack[NSIS_MAX_STRLEN];
235 rc = tstStackPopString(&pStack, szStack, RT_ELEMENTS(szStack));
236 if (RT_SUCCESS(rc))
237 {
238 if (fUnicode)
239 RTTestIPrintf(RTTESTLVL_ALWAYS, "Reply: %ls\n", szStack);
240 else
241 RTTestIPrintf(RTTESTLVL_ALWAYS, "Reply: %s\n", szStack);
242 }
243 else
244 RTTestIFailed("Sending message to VBoxTray failed (NSIS API changed?)\n");
245 tstStackDestroy(pStack);
246 }
247 else
248 RTTestIFailed("Loading pfnVBoxTrayShowBallonMsg failed: %Rrc", rc);
249
250 RTLdrClose(hLdrMod);
251 }
252 else
253 RTTestIFailed("Loading DLL failed: %Rrc", rc);
254 }
255 else
256 RTTestIFailed("Getting absolute path of DLL failed: %Rrc", rc);
257
258 return RTTestSummaryAndDestroy(hTest);
259}
260
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette