crypt32: Add additional path for Solaris 11 Express.
[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 const WCHAR inffileW[] = {'t','e','s','t','.','i','n','f',0};
39 static char CURR_DIR[MAX_PATH];
40
41 /* Notes on InstallHinfSectionA/W:
42  * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
43  *   and simply return without displaying any error message or setting last error. We test whether
44  *   InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
45  * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
46  *   occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
47  *   is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
48  *   "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
49  *   or not installation occurred.
50  * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
51  *   CBT hook.
52  */
53
54 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
55 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
56 static BOOL (WINAPI *pSetupGetInfFileListA)(PCSTR, DWORD, PSTR, DWORD, PDWORD);
57 static BOOL (WINAPI *pSetupGetInfFileListW)(PCWSTR, DWORD, PWSTR, DWORD, PDWORD);
58
59 /*
60  * Helpers
61  */
62
63 static void create_inf_file(LPCSTR filename, const char *data)
64 {
65     DWORD res;
66     HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
67                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
68     assert(handle != INVALID_HANDLE_VALUE);
69     assert(WriteFile(handle, data, strlen(data), &res, NULL));
70     CloseHandle(handle);
71 }
72
73 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
74 static HHOOK hhook;
75 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
76 {
77     return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
78 }
79
80 /*
81  * Tests
82  */
83
84 static const char *cmdline_inf = "[Version]\n"
85     "Signature=\"$Chicago$\"\n"
86     "[DefaultInstall]\n"
87     "AddReg=Add.Settings\n"
88     "[Add.Settings]\n"
89     "HKCU,Software\\Wine\\setupapitest,,\n";
90
91 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
92 {
93     CHAR cmdline[MAX_PATH * 2];
94
95     sprintf(cmdline, "%s %d %s", section, mode, path);
96     if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
97     else
98     {
99         WCHAR cmdlinew[MAX_PATH * 2];
100         MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
101         pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
102     }
103 }
104
105 static void ok_registry(BOOL expectsuccess)
106 {
107     LONG ret;
108
109     /* Functional tests for success of install and clean up */
110     ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
111     ok((expectsuccess && ret == ERROR_SUCCESS) ||
112        (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
113        "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
114        expectsuccess ? "exist" : "not exist",
115        ret);
116 }
117
118 /* Test command line processing */
119 static void test_cmdline(void)
120 {
121     static const char infwithspaces[] = "test file.inf";
122     char path[MAX_PATH];
123     BOOL ret;
124
125     create_inf_file(inffile, cmdline_inf);
126     sprintf(path, "%s\\%s", CURR_DIR, inffile);
127     run_cmdline("DefaultInstall", 128, path);
128     ok_registry(TRUE);
129     ret = DeleteFile(inffile);
130     ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
131
132     /* Test handling of spaces in path, unquoted and quoted */
133     create_inf_file(infwithspaces, cmdline_inf);
134
135     sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
136     run_cmdline("DefaultInstall", 128, path);
137     ok_registry(TRUE);
138
139     sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
140     run_cmdline("DefaultInstall", 128, path);
141     ok_registry(FALSE);
142
143     ret = DeleteFile(infwithspaces);
144     ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
145 }
146
147 static const char *cmdline_inf_reg = "[Version]\n"
148     "Signature=\"$Chicago$\"\n"
149     "[DefaultInstall]\n"
150     "DelReg=Del.Settings\n"
151     "[Del.Settings]\n"
152     "HKCU,Software\\Wine\\setupapitest\n";
153
154 static void test_registry(void)
155 {
156     HKEY key;
157     LONG res;
158     char path[MAX_PATH];
159     BOOL ret;
160
161     /* First create a registry structure we would like to be deleted */
162     ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
163         "Expected RegCreateKeyA to succeed\n");
164
165     /* Doublecheck if the registry key is present */
166     ok(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
167         "Expected registry key to exist\n");
168
169     create_inf_file(inffile, cmdline_inf_reg);
170     sprintf(path, "%s\\%s", CURR_DIR, inffile);
171     run_cmdline("DefaultInstall", 128, path);
172
173     /* Check if the registry key is recursively deleted */
174     res = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
175     todo_wine
176     ok(res == ERROR_FILE_NOT_FOUND, "Didn't expect the registry key to exist\n");
177     /* Just in case */
178     if (res == ERROR_SUCCESS)
179     {
180         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest");
181         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
182     }
183     ret = DeleteFile(inffile);
184     ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
185 }
186
187 static void test_install_svc_from(void)
188 {
189     char inf[2048];
190     char path[MAX_PATH];
191     HINF infhandle;
192     BOOL ret;
193     SC_HANDLE scm_handle, svc_handle;
194
195     /* Bail out if we are on win98 */
196     SetLastError(0xdeadbeef);
197     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
198
199     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
200     {
201         win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
202         return;
203     }
204     CloseServiceHandle(scm_handle);
205
206     /* Basic inf file to satisfy SetupOpenInfFileA */
207     strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
208     create_inf_file(inffile, inf);
209     sprintf(path, "%s\\%s", CURR_DIR, inffile);
210     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
211
212     /* Nothing but the Version section */
213     SetLastError(0xdeadbeef);
214     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
215     ok(!ret, "Expected failure\n");
216     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
217         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
218     SetupCloseInfFile(infhandle);
219     DeleteFile(inffile);
220
221     /* Add the section */
222     strcat(inf, "[Winetest.Services]\n");
223     create_inf_file(inffile, inf);
224     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
225     SetLastError(0xdeadbeef);
226     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
227     ok(!ret, "Expected failure\n");
228     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
229         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
230     SetupCloseInfFile(infhandle);
231     DeleteFile(inffile);
232
233     /* Add a reference */
234     strcat(inf, "AddService=Winetest,,Winetest.Service\n");
235     create_inf_file(inffile, inf);
236     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
237     SetLastError(0xdeadbeef);
238     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
239     ok(!ret, "Expected failure\n");
240     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
241         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
242     SetupCloseInfFile(infhandle);
243     DeleteFile(inffile);
244
245     /* Add the section */
246     strcat(inf, "[Winetest.Service]\n");
247     create_inf_file(inffile, inf);
248     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
249     SetLastError(0xdeadbeef);
250     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
251     ok(!ret, "Expected failure\n");
252     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
253         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
254     SetupCloseInfFile(infhandle);
255     DeleteFile(inffile);
256
257     /* Just the ServiceBinary */
258     strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
259     create_inf_file(inffile, inf);
260     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
261     SetLastError(0xdeadbeef);
262     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
263     ok(!ret, "Expected failure\n");
264     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
265         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
266     SetupCloseInfFile(infhandle);
267     DeleteFile(inffile);
268
269     /* Add the ServiceType */
270     strcat(inf, "ServiceType=1\n");
271     create_inf_file(inffile, inf);
272     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
273     SetLastError(0xdeadbeef);
274     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
275     ok(!ret, "Expected failure\n");
276     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
277         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
278     SetupCloseInfFile(infhandle);
279     DeleteFile(inffile);
280
281     /* Add the StartType */
282     strcat(inf, "StartType=4\n");
283     create_inf_file(inffile, inf);
284     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
285     SetLastError(0xdeadbeef);
286     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
287     ok(!ret, "Expected failure\n");
288     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
289         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
290     SetupCloseInfFile(infhandle);
291     DeleteFile(inffile);
292
293     /* This should be it, the minimal entries to install a service */
294     strcat(inf, "ErrorControl=1");
295     create_inf_file(inffile, inf);
296     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
297     SetLastError(0xdeadbeef);
298     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
299     if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
300     {
301         skip("Not enough rights to install the service\n");
302         SetupCloseInfFile(infhandle);
303         DeleteFile(inffile);
304         return;
305     }
306     ok(ret, "Expected success\n");
307     ok(GetLastError() == ERROR_SUCCESS,
308         "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
309     SetupCloseInfFile(infhandle);
310     DeleteFile(inffile);
311
312     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
313
314     /* Open the service to see if it's really there */
315     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
316     ok(svc_handle != NULL, "Service was not created\n");
317
318     SetLastError(0xdeadbeef);
319     ret = DeleteService(svc_handle);
320     ok(ret, "Service could not be deleted : %d\n", GetLastError());
321
322     CloseServiceHandle(svc_handle);
323     CloseServiceHandle(scm_handle);
324
325     /* TODO: Test the Flags */
326 }
327
328 static void test_driver_install(void)
329 {
330     HANDLE handle;
331     SC_HANDLE scm_handle, svc_handle;
332     BOOL ret;
333     char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
334     DWORD attrs;
335     /* Minimal stuff needed */
336     static const char *inf =
337         "[Version]\n"
338         "Signature=\"$Chicago$\"\n"
339         "[DestinationDirs]\n"
340         "Winetest.DriverFiles=12\n"
341         "[DefaultInstall]\n"
342         "CopyFiles=Winetest.DriverFiles\n"
343         "[DefaultInstall.Services]\n"
344         "AddService=Winetest,,Winetest.Service\n"
345         "[Winetest.Service]\n"
346         "ServiceBinary=%12%\\winetest.sys\n"
347         "ServiceType=1\n"
348         "StartType=4\n"
349         "ErrorControl=1\n"
350         "[Winetest.DriverFiles]\n"
351         "winetest.sys";
352
353     /* Bail out if we are on win98 */
354     SetLastError(0xdeadbeef);
355     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
356
357     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
358     {
359         win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
360         return;
361     }
362     else if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
363     {
364         skip("Not enough rights to install the service\n");
365         return;
366     }
367     CloseServiceHandle(scm_handle);
368
369     /* Place where we expect the driver to be installed */
370     GetWindowsDirectoryA(windir, MAX_PATH);
371     lstrcpyA(driver, windir);
372     lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
373
374     /* Create a dummy driver file */
375     handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
376                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
377     CloseHandle(handle);
378
379     create_inf_file(inffile, inf);
380     sprintf(path, "%s\\%s", CURR_DIR, inffile);
381     run_cmdline("DefaultInstall", 128, path);
382
383     /* Driver should have been installed */
384     attrs = GetFileAttributes(driver);
385     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
386
387     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
388
389     /* Open the service to see if it's really there */
390     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
391     ok(svc_handle != NULL, "Service was not created\n");
392
393     SetLastError(0xdeadbeef);
394     ret = DeleteService(svc_handle);
395     ok(ret, "Service could not be deleted : %d\n", GetLastError());
396
397     CloseServiceHandle(svc_handle);
398     CloseServiceHandle(scm_handle);
399
400     /* File cleanup */
401     DeleteFile(inffile);
402     DeleteFile("winetest.sys");
403     DeleteFile(driver);
404 }
405
406 static void test_profile_items(void)
407 {
408     char path[MAX_PATH], commonprogs[MAX_PATH];
409     HMODULE hShell32;
410     BOOL (WINAPI *pSHGetFolderPathA)(HWND hwnd, int nFolder, HANDLE hToken, DWORD dwFlags, LPSTR pszPath);
411
412     static const char *inf =
413         "[Version]\n"
414         "Signature=\"$Chicago$\"\n"
415         "[DefaultInstall]\n"
416         "ProfileItems=TestItem,TestItem2,TestGroup\n"
417         "[TestItem]\n"
418         "Name=TestItem\n"
419         "CmdLine=11,,notepad.exe\n"
420         "[TestItem2]\n"
421         "Name=TestItem2\n"
422         "CmdLine=11,,notepad.exe\n"
423         "SubDir=TestDir\n"
424         "[TestGroup]\n"
425         "Name=TestGroup,4\n"
426         ;
427
428     hShell32 = LoadLibraryA("shell32");
429     pSHGetFolderPathA = (void*)GetProcAddress(hShell32, "SHGetFolderPathA");
430     if (!pSHGetFolderPathA)
431     {
432         win_skip("SHGetFolderPathA is not available\n");
433         goto cleanup;
434     }
435
436     if (S_OK != pSHGetFolderPathA(NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, commonprogs))
437     {
438         skip("No common program files directory exists\n");
439         goto cleanup;
440     }
441
442     create_inf_file(inffile, inf);
443     sprintf(path, "%s\\%s", CURR_DIR, inffile);
444     run_cmdline("DefaultInstall", 128, path);
445
446     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
447     if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path))
448     {
449         win_skip("ProfileItems not implemented on this system\n");
450     }
451     else
452     {
453         snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
454         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "directory not created\n");
455         snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
456         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "link not created\n");
457         snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
458         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "group not created\n");
459     }
460
461     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
462     DeleteFile(path);
463     snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
464     DeleteFile(path);
465     snprintf(path, MAX_PATH, "%s\\TestItem2.lnk", commonprogs);
466     DeleteFile(path);
467     snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
468     RemoveDirectory(path);
469     snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
470     RemoveDirectory(path);
471
472 cleanup:
473     if (hShell32) FreeLibrary(hShell32);
474     DeleteFile(inffile);
475 }
476
477 static void test_inffilelistA(void)
478 {
479     static const char inffile2[] = "test2.inf";
480     static const char *inf =
481         "[Version]\n"
482         "Signature=\"$Chicago$\"";
483
484     char buffer[MAX_PATH] = { 0 };
485     char dir[MAX_PATH], *p;
486     DWORD expected, outsize;
487     BOOL ret;
488
489     if(!pSetupGetInfFileListA)
490     {
491         win_skip("SetupGetInfFileListA not present\n");
492         return;
493     }
494
495     /* create a private directory, the temp directory may contain some
496      * inf files left over from old installations
497      */
498     if (!GetTempFileNameA(CURR_DIR, "inftest", 1, dir))
499     {
500         win_skip("GetTempFileNameA failed with error %d\n", GetLastError());
501         return;
502     }
503     if (!CreateDirectoryA(dir, NULL ))
504     {
505         win_skip("CreateDirectoryA(%s) failed with error %d\n", dir, GetLastError());
506         return;
507     }
508     if (!SetCurrentDirectoryA(dir))
509     {
510         win_skip("SetCurrentDirectoryA failed with error %d\n", GetLastError());
511         RemoveDirectoryA(dir);
512         return;
513     }
514
515     create_inf_file(inffile, inf);
516     create_inf_file(inffile2, inf);
517
518     /* mixed style
519      */
520     expected = 3 + strlen(inffile) + strlen(inffile2);
521     ret = pSetupGetInfFileListA(dir, INF_STYLE_OLDNT | INF_STYLE_WIN4, buffer,
522                                 MAX_PATH, &outsize);
523     ok(ret, "expected SetupGetInfFileListA to succeed!\n");
524     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
525          expected, outsize);
526     for(p = buffer; lstrlenA(p) && (outsize > (p - buffer)); p+=lstrlenA(p) + 1)
527         ok(!lstrcmpA(p,inffile2) || !lstrcmpA(p,inffile),
528             "unexpected filename %s\n",p);
529
530     DeleteFile(inffile);
531     DeleteFile(inffile2);
532     SetCurrentDirectoryA(CURR_DIR);
533     RemoveDirectoryA(dir);
534 }
535
536 static void test_inffilelist(void)
537 {
538     static const char inffile2[] = "test2.inf";
539     static const WCHAR inffile2W[] = {'t','e','s','t','2','.','i','n','f',0};
540     static const char invalid_inf[] = "invalid.inf";
541     static const WCHAR invalid_infW[] = {'i','n','v','a','l','i','d','.','i','n','f',0};
542     static const char *inf =
543         "[Version]\n"
544         "Signature=\"$Chicago$\"";
545     static const char *inf2 =
546         "[Version]\n"
547         "Signature=\"$CHICAGO$\"";
548     static const char *infNT =
549         "[Version]\n"
550         "Signature=\"$WINDOWS NT$\"";
551
552     WCHAR *p, *ptr;
553     char dirA[MAX_PATH];
554     WCHAR dir[MAX_PATH] = { 0 };
555     WCHAR buffer[MAX_PATH] = { 0 };
556     DWORD expected, outsize;
557     BOOL ret;
558
559     if(!pSetupGetInfFileListW)
560     {
561         win_skip("SetupGetInfFileListW not present\n");
562         return;
563     }
564
565     /* NULL means %windir%\\inf
566      * get the value as reference
567      */
568     expected = 0;
569     SetLastError(0xdeadbeef);
570     ret = pSetupGetInfFileListW(NULL, INF_STYLE_WIN4, NULL, 0, &expected);
571     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
572     {
573         win_skip("SetupGetInfFileListW not implemented\n");
574         return;
575     }
576     ok(ret, "expected SetupGetInfFileListW to succeed! Error: %d\n", GetLastError());
577     ok(expected > 0, "expected required buffersize to be at least 1\n");
578
579     /* check if an empty string doesn't behaves like NULL */
580     outsize = 0;
581     SetLastError(0xdeadbeef);
582     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
583     ok(!ret, "expected SetupGetInfFileListW to fail!\n");
584
585     /* create a private directory, the temp directory may contain some
586      * inf files left over from old installations
587      */
588     if (!GetTempFileNameA(CURR_DIR, "inftest", 1, dirA))
589     {
590         win_skip("GetTempFileNameA failed with error %d\n", GetLastError());
591         return;
592     }
593     if (!CreateDirectoryA(dirA, NULL ))
594     {
595         win_skip("CreateDirectoryA(%s) failed with error %d\n", dirA, GetLastError());
596         return;
597     }
598     if (!SetCurrentDirectoryA(dirA))
599     {
600         win_skip("SetCurrentDirectoryA failed with error %d\n", GetLastError());
601         RemoveDirectoryA(dirA);
602         return;
603     }
604
605     MultiByteToWideChar(CP_ACP, 0, dirA, -1, dir, MAX_PATH);
606     /* check a not existing directory
607      */
608     ptr = dir + lstrlenW(dir);
609     MultiByteToWideChar(CP_ACP, 0, "\\not_existent", -1, ptr, MAX_PATH - lstrlenW(dir));
610     outsize = 0xffffffff;
611     SetLastError(0xdeadbeef);
612     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
613     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
614     ok(outsize == 1, "expected required buffersize to be 1, got %d\n", outsize);
615     ok(ERROR_PATH_NOT_FOUND == GetLastError(),
616        "expected error ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
617     
618     create_inf_file(inffile, inf);
619     create_inf_file(inffile2, inf);
620     create_inf_file(invalid_inf, "This content does not match the inf file format");
621
622     /* pass a filename instead of a directory
623      */
624     *ptr = '\\';
625     MultiByteToWideChar(CP_ACP, 0, invalid_inf, -1, ptr+1, MAX_PATH - lstrlenW(dir));
626     outsize = 0xffffffff;
627     SetLastError(0xdeadbeef);
628     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
629     ok(!ret, "expected SetupGetInfFileListW to fail!\n");
630     ok(ERROR_DIRECTORY == GetLastError(),
631        "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
632
633     /* make the filename look like directory
634      */
635     dir[1 + lstrlenW(dir)] = 0;
636     dir[lstrlenW(dir)] = '\\';
637     SetLastError(0xdeadbeef);
638     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
639     ok(!ret, "expected SetupGetInfFileListW to fail!\n");
640     ok(ERROR_DIRECTORY == GetLastError(),
641        "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
642
643     /* now check the buffer content of a vaild call
644      */
645     *ptr = 0;
646     expected = 3 + strlen(inffile) + strlen(inffile2);
647     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, buffer, MAX_PATH, &outsize);
648     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
649     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
650          expected, outsize);
651     for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
652         ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW),
653             "unexpected filename %s\n",wine_dbgstr_w(p));
654
655     /* upper case value
656      */
657     create_inf_file(inffile2, inf2);
658     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, buffer, MAX_PATH, &outsize);
659     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
660     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
661          expected, outsize);
662     for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
663         ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW),
664             "unexpected filename %s\n",wine_dbgstr_w(p));
665
666     /* signature Windows NT is also inf style win4
667      */
668     create_inf_file(inffile2, infNT);
669     expected = 3 + strlen(inffile) + strlen(inffile2);
670     ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, buffer, MAX_PATH, &outsize);
671     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
672     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
673          expected, outsize);
674     for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
675         ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW),
676             "unexpected filename %s\n",wine_dbgstr_w(p));
677
678     /* old style
679      */
680     expected = 2 + strlen(invalid_inf);
681     ret = pSetupGetInfFileListW(dir, INF_STYLE_OLDNT, buffer, MAX_PATH, &outsize);
682     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
683     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
684          expected, outsize);
685     for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
686         ok(!lstrcmpW(p,invalid_infW), "unexpected filename %s\n",wine_dbgstr_w(p));
687
688     /* mixed style
689      */
690     expected = 4 + strlen(inffile) + strlen(inffile2) + strlen(invalid_inf);
691     ret = pSetupGetInfFileListW(dir, INF_STYLE_OLDNT | INF_STYLE_WIN4, buffer,
692                                 MAX_PATH, &outsize);
693     ok(ret, "expected SetupGetInfFileListW to succeed!\n");
694     ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
695          expected, outsize);
696     for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
697         ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW) || !lstrcmpW(p,invalid_infW),
698             "unexpected filename %s\n",wine_dbgstr_w(p));
699
700     DeleteFile(inffile);
701     DeleteFile(inffile2);
702     DeleteFile(invalid_inf);
703     SetCurrentDirectoryA(CURR_DIR);
704     RemoveDirectoryA(dirA);
705 }
706
707 START_TEST(install)
708 {
709     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
710     char temp_path[MAX_PATH], prev_path[MAX_PATH];
711     DWORD len;
712
713     GetCurrentDirectory(MAX_PATH, prev_path);
714     GetTempPath(MAX_PATH, temp_path);
715     SetCurrentDirectory(temp_path);
716
717     strcpy(CURR_DIR, temp_path);
718     len = strlen(CURR_DIR);
719     if(len && (CURR_DIR[len - 1] == '\\'))
720         CURR_DIR[len - 1] = 0;
721
722     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
723     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
724     pSetupGetInfFileListA = (void *)GetProcAddress(hsetupapi, "SetupGetInfFileListA");
725     pSetupGetInfFileListW = (void *)GetProcAddress(hsetupapi, "SetupGetInfFileListW");
726
727     if (pInstallHinfSectionA)
728     {
729         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
730         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
731         char cmdline[MAX_PATH*2];
732         BOOL ret;
733         create_inf_file(inffile, minimal_inf);
734         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
735         SetLastError(0xdeadbeef);
736         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
737         if (GetLastError() == 0xdeadbeef)
738         {
739             skip("InstallHinfSectionA is broken (stub)\n");
740             pInstallHinfSectionA = NULL;
741         }
742         ret = DeleteFile(inffile);
743         ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
744     }
745     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
746         win_skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
747     else
748     {
749         /* Set CBT hook to disallow MessageBox creation in current thread */
750         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
751         assert(hhook != 0);
752
753         test_cmdline();
754         test_registry();
755         test_install_svc_from();
756         test_driver_install();
757
758         UnhookWindowsHookEx(hhook);
759
760         /* We have to run this test after the CBT hook is disabled because
761             ProfileItems needs to create a window on Windows XP. */
762         test_profile_items();
763     }
764
765     test_inffilelist();
766     test_inffilelistA();
767
768     SetCurrentDirectory(prev_path);
769 }