wined3d: Reinstall the G16R16F format surface load fixup.
[wine] / dlls / setupapi / tests / install.c
1 /*
2  * Unit test for setupapi.dll install functions
3  *
4  * Copyright 2007 Misha Koshelev
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "winsvc.h"
32 #include "setupapi.h"
33 #include "shlobj.h"
34
35 #include "wine/test.h"
36
37 static const char inffile[] = "test.inf";
38 static char CURR_DIR[MAX_PATH];
39
40 /* Notes on InstallHinfSectionA/W:
41  * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
42  *   and simply return without displaying any error message or setting last error. We test whether
43  *   InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
44  * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
45  *   occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
46  *   is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
47  *   "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
48  *   or not installation occurred.
49  * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
50  *   CBT hook.
51  */
52
53 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
54 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
55
56 /*
57  * Helpers
58  */
59
60 static void create_inf_file(LPCSTR filename, const char *data)
61 {
62     DWORD res;
63     HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
64                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
65     assert(handle != INVALID_HANDLE_VALUE);
66     assert(WriteFile(handle, data, strlen(data), &res, NULL));
67     CloseHandle(handle);
68 }
69
70 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
71 static HHOOK hhook;
72 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
73 {
74     return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
75 }
76
77 /*
78  * Tests
79  */
80
81 static const char *cmdline_inf = "[Version]\n"
82     "Signature=\"$Chicago$\"\n"
83     "[DefaultInstall]\n"
84     "AddReg=Add.Settings\n"
85     "[Add.Settings]\n"
86     "HKCU,Software\\Wine\\setupapitest,,\n";
87
88 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
89 {
90     CHAR cmdline[MAX_PATH * 2];
91
92     sprintf(cmdline, "%s %d %s", section, mode, path);
93     if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
94     else
95     {
96         WCHAR cmdlinew[MAX_PATH * 2];
97         MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
98         pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
99     }
100 }
101
102 static void ok_registry(BOOL expectsuccess)
103 {
104     LONG ret;
105
106     /* Functional tests for success of install and clean up */
107     ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
108     ok((expectsuccess && ret == ERROR_SUCCESS) ||
109        (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
110        "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
111        expectsuccess ? "exist" : "not exist",
112        ret);
113 }
114
115 /* Test command line processing */
116 static void test_cmdline(void)
117 {
118     static const char infwithspaces[] = "test file.inf";
119     char path[MAX_PATH];
120
121     create_inf_file(inffile, cmdline_inf);
122     sprintf(path, "%s\\%s", CURR_DIR, inffile);
123     run_cmdline("DefaultInstall", 128, path);
124     ok_registry(TRUE);
125     ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
126
127     /* Test handling of spaces in path, unquoted and quoted */
128     create_inf_file(infwithspaces, cmdline_inf);
129
130     sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
131     run_cmdline("DefaultInstall", 128, path);
132     ok_registry(TRUE);
133
134     sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
135     run_cmdline("DefaultInstall", 128, path);
136     ok_registry(FALSE);
137
138     ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
139 }
140
141 static const char *cmdline_inf_reg = "[Version]\n"
142     "Signature=\"$Chicago$\"\n"
143     "[DefaultInstall]\n"
144     "DelReg=Del.Settings\n"
145     "[Del.Settings]\n"
146     "HKCU,Software\\Wine\\setupapitest\n";
147
148 static void test_registry(void)
149 {
150     HKEY key;
151     LONG res;
152     char path[MAX_PATH];
153
154     /* First create a registry structure we would like to be deleted */
155     ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
156         "Expected RegCreateKeyA to succeed\n");
157
158     /* Doublecheck if the registry key is present */
159     ok(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
160         "Expected registry key to exist\n");
161
162     create_inf_file(inffile, cmdline_inf_reg);
163     sprintf(path, "%s\\%s", CURR_DIR, inffile);
164     run_cmdline("DefaultInstall", 128, path);
165
166     /* Check if the registry key is recursively deleted */
167     res = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
168     todo_wine
169     ok(res == ERROR_FILE_NOT_FOUND, "Didn't expect the registry key to exist\n");
170     /* Just in case */
171     if (res == ERROR_SUCCESS)
172     {
173         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest");
174         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
175     }
176     ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
177 }
178
179 static void test_install_svc_from(void)
180 {
181     char inf[2048];
182     char path[MAX_PATH];
183     HINF infhandle;
184     BOOL ret;
185     SC_HANDLE scm_handle, svc_handle;
186
187     /* Bail out if we are on win98 */
188     SetLastError(0xdeadbeef);
189     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
190
191     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
192     {
193         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
194         return;
195     }
196     CloseServiceHandle(scm_handle);
197
198     /* Basic inf file to satisfy SetupOpenInfFileA */
199     strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
200     create_inf_file(inffile, inf);
201     sprintf(path, "%s\\%s", CURR_DIR, inffile);
202     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
203
204     /* Nothing but the Version section */
205     SetLastError(0xdeadbeef);
206     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
207     ok(!ret, "Expected failure\n");
208     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
209         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
210     SetupCloseInfFile(infhandle);
211     DeleteFile(inffile);
212
213     /* Add the section */
214     strcat(inf, "[Winetest.Services]\n");
215     create_inf_file(inffile, inf);
216     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
217     SetLastError(0xdeadbeef);
218     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
219     ok(!ret, "Expected failure\n");
220     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
221         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
222     SetupCloseInfFile(infhandle);
223     DeleteFile(inffile);
224
225     /* Add a reference */
226     strcat(inf, "AddService=Winetest,,Winetest.Service\n");
227     create_inf_file(inffile, inf);
228     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
229     SetLastError(0xdeadbeef);
230     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
231     ok(!ret, "Expected failure\n");
232     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
233         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
234     SetupCloseInfFile(infhandle);
235     DeleteFile(inffile);
236
237     /* Add the section */
238     strcat(inf, "[Winetest.Service]\n");
239     create_inf_file(inffile, inf);
240     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
241     SetLastError(0xdeadbeef);
242     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
243     ok(!ret, "Expected failure\n");
244     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
245         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
246     SetupCloseInfFile(infhandle);
247     DeleteFile(inffile);
248
249     /* Just the ServiceBinary */
250     strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
251     create_inf_file(inffile, inf);
252     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
253     SetLastError(0xdeadbeef);
254     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
255     ok(!ret, "Expected failure\n");
256     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
257         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
258     SetupCloseInfFile(infhandle);
259     DeleteFile(inffile);
260
261     /* Add the ServiceType */
262     strcat(inf, "ServiceType=1\n");
263     create_inf_file(inffile, inf);
264     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
265     SetLastError(0xdeadbeef);
266     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
267     ok(!ret, "Expected failure\n");
268     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
269         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
270     SetupCloseInfFile(infhandle);
271     DeleteFile(inffile);
272
273     /* Add the StartType */
274     strcat(inf, "StartType=4\n");
275     create_inf_file(inffile, inf);
276     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
277     SetLastError(0xdeadbeef);
278     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
279     ok(!ret, "Expected failure\n");
280     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
281         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
282     SetupCloseInfFile(infhandle);
283     DeleteFile(inffile);
284
285     /* This should be it, the minimal entries to install a service */
286     strcat(inf, "ErrorControl=1");
287     create_inf_file(inffile, inf);
288     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
289     SetLastError(0xdeadbeef);
290     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
291     ok(ret, "Expected success\n");
292     ok(GetLastError() == ERROR_SUCCESS,
293         "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
294     SetupCloseInfFile(infhandle);
295     DeleteFile(inffile);
296
297     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
298
299     /* Open the service to see if it's really there */
300     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
301     ok(svc_handle != NULL, "Service was not created\n");
302
303     SetLastError(0xdeadbeef);
304     ret = DeleteService(svc_handle);
305     ok(ret, "Service could not be deleted : %d\n", GetLastError());
306
307     CloseServiceHandle(svc_handle);
308     CloseServiceHandle(scm_handle);
309
310     /* TODO: Test the Flags */
311 }
312
313 static void test_driver_install(void)
314 {
315     HANDLE handle;
316     SC_HANDLE scm_handle, svc_handle;
317     BOOL ret;
318     char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
319     DWORD attrs;
320     /* Minimal stuff needed */
321     static const char *inf =
322         "[Version]\n"
323         "Signature=\"$Chicago$\"\n"
324         "[DestinationDirs]\n"
325         "Winetest.DriverFiles=12\n"
326         "[DefaultInstall]\n"
327         "CopyFiles=Winetest.DriverFiles\n"
328         "[DefaultInstall.Services]\n"
329         "AddService=Winetest,,Winetest.Service\n"
330         "[Winetest.Service]\n"
331         "ServiceBinary=%12%\\winetest.sys\n"
332         "ServiceType=1\n"
333         "StartType=4\n"
334         "ErrorControl=1\n"
335         "[Winetest.DriverFiles]\n"
336         "winetest.sys";
337
338     /* Bail out if we are on win98 */
339     SetLastError(0xdeadbeef);
340     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
341
342     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
343     {
344         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
345         return;
346     }
347     CloseServiceHandle(scm_handle);
348
349     /* Place where we expect the driver to be installed */
350     GetWindowsDirectoryA(windir, MAX_PATH);
351     lstrcpyA(driver, windir);
352     lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
353
354     /* Create a dummy driver file */
355     handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
356                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
357     CloseHandle(handle);
358
359     create_inf_file(inffile, inf);
360     sprintf(path, "%s\\%s", CURR_DIR, inffile);
361     run_cmdline("DefaultInstall", 128, path);
362
363     /* Driver should have been installed */
364     attrs = GetFileAttributes(driver);
365     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
366
367     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
368
369     /* Open the service to see if it's really there */
370     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
371     ok(svc_handle != NULL, "Service was not created\n");
372
373     SetLastError(0xdeadbeef);
374     ret = DeleteService(svc_handle);
375     ok(ret, "Service could not be deleted : %d\n", GetLastError());
376
377     CloseServiceHandle(svc_handle);
378     CloseServiceHandle(scm_handle);
379
380     /* File cleanup */
381     DeleteFile(inffile);
382     DeleteFile("winetest.sys");
383     DeleteFile(driver);
384 }
385
386 static void test_profile_items(void)
387 {
388     char path[MAX_PATH], commonprogs[MAX_PATH];
389     HMODULE hShell32;
390     BOOL (WINAPI *pSHGetFolderPathA)(HWND hwnd, int nFolder, HANDLE hToken, DWORD dwFlags, LPSTR pszPath);
391
392     static const char *inf =
393         "[Version]\n"
394         "Signature=\"$Chicago$\"\n"
395         "[DefaultInstall]\n"
396         "ProfileItems=TestItem,TestItem2,TestGroup\n"
397         "[TestItem]\n"
398         "Name=TestItem\n"
399         "CmdLine=11,,notepad.exe\n"
400         "[TestItem2]\n"
401         "Name=TestItem2\n"
402         "CmdLine=11,,notepad.exe\n"
403         "SubDir=TestDir\n"
404         "[TestGroup]\n"
405         "Name=TestGroup,4\n"
406         ;
407
408     hShell32 = LoadLibraryA("shell32");
409     pSHGetFolderPathA = (void*)GetProcAddress(hShell32, "SHGetFolderPathA");
410     if (!pSHGetFolderPathA)
411     {
412         skip("SHGetFolderPathA is not available\n");
413         goto cleanup;
414     }
415
416     if (S_OK != pSHGetFolderPathA(NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, commonprogs))
417     {
418         skip("No common program files directory exists\n");
419         goto cleanup;
420     }
421
422     create_inf_file(inffile, inf);
423     sprintf(path, "%s\\%s", CURR_DIR, inffile);
424     run_cmdline("DefaultInstall", 128, path);
425
426     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
427     if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path))
428     {
429         win_skip("ProfileItems not implemented on this system\n");
430     }
431     else
432     {
433         snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
434         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "directory not created\n");
435         snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
436         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "link not created\n");
437         snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
438         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "group not created\n");
439     }
440
441     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
442     DeleteFile(path);
443     snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
444     DeleteFile(path);
445     snprintf(path, MAX_PATH, "%s\\TestItem2.lnk", commonprogs);
446     DeleteFile(path);
447     snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
448     RemoveDirectory(path);
449     snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
450     RemoveDirectory(path);
451
452 cleanup:
453     if (hShell32) FreeLibrary(hShell32);
454     DeleteFile(inffile);
455 }
456
457 START_TEST(install)
458 {
459     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
460     char temp_path[MAX_PATH], prev_path[MAX_PATH];
461     DWORD len;
462
463     GetCurrentDirectory(MAX_PATH, prev_path);
464     GetTempPath(MAX_PATH, temp_path);
465     SetCurrentDirectory(temp_path);
466
467     strcpy(CURR_DIR, temp_path);
468     len = strlen(CURR_DIR);
469     if(len && (CURR_DIR[len - 1] == '\\'))
470         CURR_DIR[len - 1] = 0;
471
472     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
473     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
474     if (pInstallHinfSectionA)
475     {
476         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
477         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
478         char cmdline[MAX_PATH*2];
479         create_inf_file(inffile, minimal_inf);
480         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
481         SetLastError(0xdeadbeef);
482         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
483         if (GetLastError() == 0xdeadbeef)
484         {
485             skip("InstallHinfSectionA is broken (stub)\n");
486             pInstallHinfSectionA = NULL;
487         }
488         ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
489     }
490     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
491         skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
492     else
493     {
494         /* Set CBT hook to disallow MessageBox creation in current thread */
495         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
496         assert(hhook != 0);
497
498         test_cmdline();
499         test_registry();
500         test_install_svc_from();
501         test_driver_install();
502
503         UnhookWindowsHookEx(hhook);
504
505         /* We have to run this test after the CBT hook is disabled because
506             ProfileItems needs to create a window on Windows XP. */
507         test_profile_items();
508     }
509
510     SetCurrentDirectory(prev_path);
511 }