push a3069cb3bfd71d4719e515a5e2d9729e18e3f983
[wine/hacks.git] / dlls / setupapi / tests / install.c
blob5cecd97467d8e1c4253ead91fa92e3243937b620
1 /*
2 * Unit test for setupapi.dll install functions
4 * Copyright 2007 Misha Koshelev
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "setupapi.h"
33 #include "wine/test.h"
35 static const char inffile[] = "test.inf";
36 static char CURR_DIR[MAX_PATH];
38 /* Notes on InstallHinfSectionA/W:
39 * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
40 * and simply return without displaying any error message or setting last error. We test whether
41 * InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
42 * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
43 * occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
44 * is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
45 * "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
46 * or not installation occured.
47 * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
48 * CBT hook.
51 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
52 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
55 * Helpers
58 static void create_inf_file(LPCSTR filename, const char *data)
60 DWORD res;
61 HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
62 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
63 assert(handle != INVALID_HANDLE_VALUE);
64 assert(WriteFile(handle, data, strlen(data), &res, NULL));
65 CloseHandle(handle);
68 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
69 static HHOOK hhook;
70 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
72 return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
76 * Tests
79 static const char *cmdline_inf = "[Version]\n"
80 "Signature=\"$Chicago$\"\n"
81 "[DefaultInstall]\n"
82 "AddReg=Add.Settings\n"
83 "[Add.Settings]\n"
84 "HKCU,Software\\Wine\\setupapitest,,\n";
86 static void ok_cmdline(LPCSTR section, int mode, LPCSTR path, BOOL expectsuccess)
88 CHAR cmdline[MAX_PATH * 2];
89 LONG ret;
91 sprintf(cmdline, "%s %d %s", section, mode, path);
92 if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
93 else
95 WCHAR cmdlinew[MAX_PATH * 2];
96 MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
97 pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
100 /* Functional tests for success of install and clean up */
101 ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
102 ok((expectsuccess && ret == ERROR_SUCCESS) ||
103 (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
104 "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
105 expectsuccess ? "exist" : "not exist",
106 ret);
109 /* Test command line processing */
110 static void test_cmdline(void)
112 static const char infwithspaces[] = "test file.inf";
113 char path[MAX_PATH];
115 create_inf_file(inffile, cmdline_inf);
116 sprintf(path, "%s\\%s", CURR_DIR, inffile);
117 ok_cmdline("DefaultInstall", 128, path, TRUE);
118 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
120 /* Test handling of spaces in path, unquoted and quoted */
121 create_inf_file(infwithspaces, cmdline_inf);
123 sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
124 ok_cmdline("DefaultInstall", 128, path, TRUE);
126 sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
127 todo_wine ok_cmdline("DefaultInstall", 128, path, FALSE);
129 ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
132 START_TEST(install)
134 HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
135 char temp_path[MAX_PATH], prev_path[MAX_PATH];
136 DWORD len;
138 GetCurrentDirectory(MAX_PATH, prev_path);
139 GetTempPath(MAX_PATH, temp_path);
140 SetCurrentDirectory(temp_path);
142 strcpy(CURR_DIR, temp_path);
143 len = strlen(CURR_DIR);
144 if(len && (CURR_DIR[len - 1] == '\\'))
145 CURR_DIR[len - 1] = 0;
147 pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
148 pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
149 if (pInstallHinfSectionA)
151 /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
152 static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
153 char cmdline[MAX_PATH*2];
154 create_inf_file(inffile, minimal_inf);
155 sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
156 SetLastError(0xdeadbeef);
157 pInstallHinfSectionA(NULL, NULL, cmdline, 0);
158 if (GetLastError() == 0xdeadbeef)
160 skip("InstallHinfSectionA is broken (stub)\n");
161 pInstallHinfSectionA = NULL;
163 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
165 if (!pInstallHinfSectionW && !pInstallHinfSectionA)
166 skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
167 else
169 /* Set CBT hook to disallow MessageBox creation in current thread */
170 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
171 assert(hhook != 0);
173 test_cmdline();
175 UnhookWindowsHookEx(hhook);
178 SetCurrentDirectory(prev_path);