3ca7f26acf40181a009ed2ac79ea0072251635b8
[reactos.git] / modules / 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 snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
458 if (!CreateDirectoryA(path, NULL) && GetLastError() == ERROR_ACCESS_DENIED)
459 {
460 skip("need admin rights\n");
461 return;
462 }
463 RemoveDirectoryA(path);
464
465 create_inf_file(inffile, inf);
466 sprintf(path, "%s\\%s", CURR_DIR, inffile);
467 run_cmdline("DefaultInstall", 128, path);
468
469 snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
470 if (INVALID_FILE_ATTRIBUTES == GetFileAttributesA(path))
471 {
472 win_skip("ProfileItems not implemented on this system\n");
473 }
474 else
475 {
476 snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
477 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributesA(path), "directory not created\n");
478 snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
479 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributesA(path), "link not created\n");
480 snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
481 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributesA(path), "group not created\n");
482 }
483
484 snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
485 DeleteFileA(path);
486 snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
487 DeleteFileA(path);
488 snprintf(path, MAX_PATH, "%s\\TestItem2.lnk", commonprogs);
489 DeleteFileA(path);
490 snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
491 RemoveDirectoryA(path);
492 snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
493 RemoveDirectoryA(path);
494
495 cleanup:
496 if (hShell32) FreeLibrary(hShell32);
497 DeleteFileA(inffile);
498 }
499
500 static void test_inffilelistA(void)
501 {
502 static const char inffile2[] = "test2.inf";
503 static const char *inf =
504 "[Version]\n"
505 "Signature=\"$Chicago$\"";
506
507 char buffer[MAX_PATH] = { 0 };
508 char dir[MAX_PATH], *p;
509 DWORD expected, outsize;
510 BOOL ret;
511
512 if(!pSetupGetInfFileListA)
513 {
514 win_skip("SetupGetInfFileListA not present\n");
515 return;
516 }
517
518 /* create a private directory, the temp directory may contain some
519 * inf files left over from old installations
520 */
521 if (!GetTempFileNameA(CURR_DIR, "inftest", 1, dir))
522 {
523 win_skip("GetTempFileNameA failed with error %d\n", GetLastError());
524 return;
525 }
526 if (!CreateDirectoryA(dir, NULL ))
527 {
528 win_skip("CreateDirectoryA(%s) failed with error %d\n", dir, GetLastError());
529 return;
530 }
531 if (!SetCurrentDirectoryA(dir))
532 {
533 win_skip("SetCurrentDirectoryA failed with error %d\n", GetLastError());
534 RemoveDirectoryA(dir);
535 return;
536 }
537
538 create_inf_file(inffile, inf);
539 create_inf_file(inffile2, inf);
540
541 /* mixed style
542 */
543 expected = 3 + strlen(inffile) + strlen(inffile2);
544 ret = pSetupGetInfFileListA(dir, INF_STYLE_OLDNT | INF_STYLE_WIN4, buffer,
545 MAX_PATH, &outsize);
546 ok(ret, "expected SetupGetInfFileListA to succeed!\n");
547 ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
548 expected, outsize);
549 for(p = buffer; lstrlenA(p) && (outsize > (p - buffer)); p+=lstrlenA(p) + 1)
550 ok(!lstrcmpA(p,inffile2) || !lstrcmpA(p,inffile),
551 "unexpected filename %s\n",p);
552
553 DeleteFileA(inffile);
554 DeleteFileA(inffile2);
555 SetCurrentDirectoryA(CURR_DIR);
556 RemoveDirectoryA(dir);
557 }
558
559 static void test_inffilelist(void)
560 {
561 static const char inffile2[] = "test2.inf";
562 static const WCHAR inffile2W[] = {'t','e','s','t','2','.','i','n','f',0};
563 static const char invalid_inf[] = "invalid.inf";
564 static const WCHAR invalid_infW[] = {'i','n','v','a','l','i','d','.','i','n','f',0};
565 static const char *inf =
566 "[Version]\n"
567 "Signature=\"$Chicago$\"";
568 static const char *inf2 =
569 "[Version]\n"
570 "Signature=\"$CHICAGO$\"";
571 static const char *infNT =
572 "[Version]\n"
573 "Signature=\"$WINDOWS NT$\"";
574
575 WCHAR *p, *ptr;
576 char dirA[MAX_PATH];
577 WCHAR dir[MAX_PATH] = { 0 };
578 WCHAR buffer[MAX_PATH] = { 0 };
579 DWORD expected, outsize;
580 BOOL ret;
581
582 if(!pSetupGetInfFileListW)
583 {
584 win_skip("SetupGetInfFileListW not present\n");
585 return;
586 }
587
588 /* NULL means %windir%\\inf
589 * get the value as reference
590 */
591 expected = 0;
592 SetLastError(0xdeadbeef);
593 ret = pSetupGetInfFileListW(NULL, INF_STYLE_WIN4, NULL, 0, &expected);
594 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
595 {
596 win_skip("SetupGetInfFileListW not implemented\n");
597 return;
598 }
599 ok(ret, "expected SetupGetInfFileListW to succeed! Error: %d\n", GetLastError());
600 ok(expected > 0, "expected required buffersize to be at least 1\n");
601
602 /* check if an empty string doesn't behaves like NULL */
603 outsize = 0;
604 SetLastError(0xdeadbeef);
605 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
606 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
607
608 /* create a private directory, the temp directory may contain some
609 * inf files left over from old installations
610 */
611 if (!GetTempFileNameA(CURR_DIR, "inftest", 1, dirA))
612 {
613 win_skip("GetTempFileNameA failed with error %d\n", GetLastError());
614 return;
615 }
616 if (!CreateDirectoryA(dirA, NULL ))
617 {
618 win_skip("CreateDirectoryA(%s) failed with error %d\n", dirA, GetLastError());
619 return;
620 }
621 if (!SetCurrentDirectoryA(dirA))
622 {
623 win_skip("SetCurrentDirectoryA failed with error %d\n", GetLastError());
624 RemoveDirectoryA(dirA);
625 return;
626 }
627
628 MultiByteToWideChar(CP_ACP, 0, dirA, -1, dir, MAX_PATH);
629 /* check a not existing directory
630 */
631 ptr = dir + lstrlenW(dir);
632 MultiByteToWideChar(CP_ACP, 0, "\\not_existent", -1, ptr, MAX_PATH - lstrlenW(dir));
633 outsize = 0xffffffff;
634 SetLastError(0xdeadbeef);
635 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
636 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
637 ok(outsize == 1, "expected required buffersize to be 1, got %d\n", outsize);
638 ok(ERROR_PATH_NOT_FOUND == GetLastError(),
639 "expected error ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
640
641 create_inf_file(inffile, inf);
642 create_inf_file(inffile2, inf);
643 create_inf_file(invalid_inf, "This content does not match the inf file format");
644
645 /* pass a filename instead of a directory
646 */
647 *ptr = '\\';
648 MultiByteToWideChar(CP_ACP, 0, invalid_inf, -1, ptr+1, MAX_PATH - lstrlenW(dir));
649 outsize = 0xffffffff;
650 SetLastError(0xdeadbeef);
651 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
652 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
653 ok(ERROR_DIRECTORY == GetLastError(),
654 "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
655
656 /* make the filename look like directory
657 */
658 dir[1 + lstrlenW(dir)] = 0;
659 dir[lstrlenW(dir)] = '\\';
660 SetLastError(0xdeadbeef);
661 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
662 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
663 ok(ERROR_DIRECTORY == GetLastError(),
664 "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
665
666 /* now check the buffer contents of a valid call
667 */
668 *ptr = 0;
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 /* upper case value
679 */
680 create_inf_file(inffile2, inf2);
681 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, 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,inffile2W) || !lstrcmpW(p,inffileW),
687 "unexpected filename %s\n",wine_dbgstr_w(p));
688
689 /* signature Windows NT is also inf style win4
690 */
691 create_inf_file(inffile2, infNT);
692 expected = 3 + strlen(inffile) + strlen(inffile2);
693 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, buffer, MAX_PATH, &outsize);
694 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
695 ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
696 expected, outsize);
697 for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
698 ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW),
699 "unexpected filename %s\n",wine_dbgstr_w(p));
700
701 /* old style
702 */
703 expected = 2 + strlen(invalid_inf);
704 ret = pSetupGetInfFileListW(dir, INF_STYLE_OLDNT, buffer, MAX_PATH, &outsize);
705 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
706 ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
707 expected, outsize);
708 for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
709 ok(!lstrcmpW(p,invalid_infW), "unexpected filename %s\n",wine_dbgstr_w(p));
710
711 /* mixed style
712 */
713 expected = 4 + strlen(inffile) + strlen(inffile2) + strlen(invalid_inf);
714 ret = pSetupGetInfFileListW(dir, INF_STYLE_OLDNT | INF_STYLE_WIN4, buffer,
715 MAX_PATH, &outsize);
716 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
717 ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
718 expected, outsize);
719 for(p = buffer; lstrlenW(p) && (outsize > (p - buffer)); p+=lstrlenW(p) + 1)
720 ok(!lstrcmpW(p,inffile2W) || !lstrcmpW(p,inffileW) || !lstrcmpW(p,invalid_infW),
721 "unexpected filename %s\n",wine_dbgstr_w(p));
722
723 DeleteFileA(inffile);
724 DeleteFileA(inffile2);
725 DeleteFileA(invalid_inf);
726 SetCurrentDirectoryA(CURR_DIR);
727 RemoveDirectoryA(dirA);
728 }
729
730 static const char dirid_inf[] = "[Version]\n"
731 "Signature=\"$Chicago$\"\n"
732 "[DefaultInstall]\n"
733 "AddReg=Add.Settings\n"
734 "[Add.Settings]\n"
735 "HKCU,Software\\Wine\\setupapitest,dirid,,%%%i%%\n";
736
737 static void check_dirid(int dirid, LPCSTR expected)
738 {
739 char buffer[sizeof(dirid_inf)+11];
740 char path[MAX_PATH], actual[MAX_PATH];
741 LONG ret;
742 DWORD size, type;
743 HKEY key;
744
745 sprintf(buffer, dirid_inf, dirid);
746
747 create_inf_file(inffile, buffer);
748
749 sprintf(path, "%s\\%s", CURR_DIR, inffile);
750 run_cmdline("DefaultInstall", 128, path);
751
752 size = sizeof(actual);
753 actual[0] = '\0';
754 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
755 if (ret == ERROR_SUCCESS)
756 {
757 ret = RegQueryValueExA(key, "dirid", NULL, &type, (BYTE*)&actual, &size);
758 RegCloseKey(key);
759 if (type != REG_SZ)
760 ret = ERROR_FILE_NOT_FOUND;
761 }
762
763 ok(ret == ERROR_SUCCESS, "Failed getting value for dirid %i, err=%d\n", dirid, ret);
764 ok(!strcmp(actual, expected), "Expected path for dirid %i was \"%s\", got \"%s\"\n", dirid, expected, actual);
765
766 ok_registry(TRUE);
767 ret = DeleteFileA(inffile);
768 ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
769 }
770
771 /* Test dirid values */
772 static void test_dirid(void)
773 {
774 char expected[MAX_PATH];
775
776 check_dirid(DIRID_NULL, "");
777
778 GetWindowsDirectoryA(expected, MAX_PATH);
779 check_dirid(DIRID_WINDOWS, expected);
780
781 GetSystemDirectoryA(expected, MAX_PATH);
782 check_dirid(DIRID_SYSTEM, expected);
783
784 strcat(expected, "\\unknown");
785 check_dirid(40, expected);
786 }
787
788 START_TEST(install)
789 {
790 HMODULE hsetupapi = GetModuleHandleA("setupapi.dll");
791 char temp_path[MAX_PATH], prev_path[MAX_PATH];
792 DWORD len;
793
794 GetCurrentDirectoryA(MAX_PATH, prev_path);
795 GetTempPathA(MAX_PATH, temp_path);
796 SetCurrentDirectoryA(temp_path);
797
798 strcpy(CURR_DIR, temp_path);
799 len = strlen(CURR_DIR);
800 if(len && (CURR_DIR[len - 1] == '\\'))
801 CURR_DIR[len - 1] = 0;
802
803 pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
804 pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
805 pSetupGetInfFileListA = (void *)GetProcAddress(hsetupapi, "SetupGetInfFileListA");
806 pSetupGetInfFileListW = (void *)GetProcAddress(hsetupapi, "SetupGetInfFileListW");
807
808 if (pInstallHinfSectionA)
809 {
810 /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
811 static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
812 char cmdline[MAX_PATH*2];
813 BOOL ret;
814 create_inf_file(inffile, minimal_inf);
815 sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
816 SetLastError(0xdeadbeef);
817 pInstallHinfSectionA(NULL, NULL, cmdline, 0);
818 if (GetLastError() == 0xdeadbeef)
819 {
820 skip("InstallHinfSectionA is broken (stub)\n");
821 pInstallHinfSectionA = NULL;
822 }
823 ret = DeleteFileA(inffile);
824 ok(ret, "Expected source inf to exist, last error was %d\n", GetLastError());
825 }
826 if (!pInstallHinfSectionW && !pInstallHinfSectionA)
827 win_skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
828 else
829 {
830 /* Set CBT hook to disallow MessageBox creation in current thread */
831 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
832 assert(hhook != 0);
833
834 test_cmdline();
835 test_registry();
836 test_install_svc_from();
837 test_driver_install();
838 test_dirid();
839
840 UnhookWindowsHookEx(hhook);
841
842 /* We have to run this test after the CBT hook is disabled because
843 ProfileItems needs to create a window on Windows XP. */
844 test_profile_items();
845 }
846
847 test_inffilelist();
848 test_inffilelistA();
849
850 SetCurrentDirectoryA(prev_path);
851 }