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