d3d9: Implement IDirect3DVertexBuffer9 private data handling on top of wined3d_resource.
[wine] / dlls / setupapi / tests / devinst.c
1 /*
2  * Devinst tests
3  *
4  * Copyright 2006 Christian Gmeiner
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 <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "guiddef.h"
30 #include "setupapi.h"
31
32 #include "wine/test.h"
33
34 static BOOL is_wow64;
35
36 /* function pointers */
37 static HDEVINFO (WINAPI *pSetupDiCreateDeviceInfoList)(GUID*,HWND);
38 static HDEVINFO (WINAPI *pSetupDiCreateDeviceInfoListExW)(GUID*,HWND,PCWSTR,PVOID);
39 static BOOL     (WINAPI *pSetupDiCreateDeviceInterfaceA)(HDEVINFO, PSP_DEVINFO_DATA, const GUID *, PCSTR, DWORD, PSP_DEVICE_INTERFACE_DATA);
40 static BOOL     (WINAPI *pSetupDiCallClassInstaller)(DI_FUNCTION, HDEVINFO, PSP_DEVINFO_DATA);
41 static BOOL     (WINAPI *pSetupDiDestroyDeviceInfoList)(HDEVINFO);
42 static BOOL     (WINAPI *pSetupDiEnumDeviceInfo)(HDEVINFO, DWORD, PSP_DEVINFO_DATA);
43 static BOOL     (WINAPI *pSetupDiEnumDeviceInterfaces)(HDEVINFO, PSP_DEVINFO_DATA, const GUID *, DWORD, PSP_DEVICE_INTERFACE_DATA);
44 static BOOL     (WINAPI *pSetupDiGetINFClassA)(PCSTR, LPGUID, PSTR, DWORD, PDWORD);
45 static BOOL     (WINAPI *pSetupDiInstallClassA)(HWND, PCSTR, DWORD, HSPFILEQ);
46 static HKEY     (WINAPI *pSetupDiOpenClassRegKeyExA)(GUID*,REGSAM,DWORD,PCSTR,PVOID);
47 static HKEY     (WINAPI *pSetupDiOpenDevRegKey)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM);
48 static HKEY     (WINAPI *pSetupDiCreateDevRegKeyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, HINF, PCWSTR);
49 static BOOL     (WINAPI *pSetupDiCreateDeviceInfoA)(HDEVINFO, PCSTR, GUID *, PCSTR, HWND, DWORD, PSP_DEVINFO_DATA);
50 static BOOL     (WINAPI *pSetupDiCreateDeviceInfoW)(HDEVINFO, PCWSTR, GUID *, PCWSTR, HWND, DWORD, PSP_DEVINFO_DATA);
51 static BOOL     (WINAPI *pSetupDiGetDeviceInstanceIdA)(HDEVINFO, PSP_DEVINFO_DATA, PSTR, DWORD, PDWORD);
52 static BOOL     (WINAPI *pSetupDiGetDeviceInterfaceDetailA)(HDEVINFO, PSP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA);
53 static BOOL     (WINAPI *pSetupDiGetDeviceInterfaceDetailW)(HDEVINFO, PSP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA_W, DWORD, PDWORD, PSP_DEVINFO_DATA);
54 static BOOL     (WINAPI *pSetupDiRegisterDeviceInfo)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PSP_DETSIG_CMPPROC, PVOID, PSP_DEVINFO_DATA);
55 static HDEVINFO (WINAPI *pSetupDiGetClassDevsA)(CONST GUID *, LPCSTR, HWND, DWORD);
56 static HDEVINFO (WINAPI *pSetupDiGetClassDevsW)(CONST GUID *, LPCWSTR, HWND, DWORD);
57 static BOOL     (WINAPI *pSetupDiSetDeviceRegistryPropertyA)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, const BYTE *, DWORD);
58 static BOOL     (WINAPI *pSetupDiSetDeviceRegistryPropertyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, const BYTE *, DWORD);
59 static BOOL     (WINAPI *pSetupDiGetDeviceRegistryPropertyA)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
60 static BOOL     (WINAPI *pSetupDiGetDeviceRegistryPropertyW)(HDEVINFO, PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD);
61 static BOOL     (WINAPI *pIsWow64Process)(HANDLE, PBOOL);
62
63 /* This is a unique guid for testing purposes */
64 static GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,0x00,0x11,0x95,0x5c,0x2b,0xdb}};
65
66 static void init_function_pointers(void)
67 {
68     HMODULE hSetupAPI = GetModuleHandleA("setupapi.dll");
69     HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
70
71     pSetupDiCreateDeviceInfoA = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoA");
72     pSetupDiCreateDeviceInfoW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoW");
73     pSetupDiCreateDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoList");
74     pSetupDiCreateDeviceInfoListExW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoListExW");
75     pSetupDiCreateDeviceInterfaceA = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInterfaceA");
76     pSetupDiDestroyDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiDestroyDeviceInfoList");
77     pSetupDiCallClassInstaller = (void *)GetProcAddress(hSetupAPI, "SetupDiCallClassInstaller");
78     pSetupDiEnumDeviceInfo = (void *)GetProcAddress(hSetupAPI, "SetupDiEnumDeviceInfo");
79     pSetupDiEnumDeviceInterfaces = (void *)GetProcAddress(hSetupAPI, "SetupDiEnumDeviceInterfaces");
80     pSetupDiGetDeviceInstanceIdA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInstanceIdA");
81     pSetupDiGetDeviceInterfaceDetailA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInterfaceDetailA");
82     pSetupDiGetDeviceInterfaceDetailW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInterfaceDetailW");
83     pSetupDiInstallClassA = (void *)GetProcAddress(hSetupAPI, "SetupDiInstallClassA");
84     pSetupDiOpenClassRegKeyExA = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenClassRegKeyExA");
85     pSetupDiOpenDevRegKey = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenDevRegKey");
86     pSetupDiCreateDevRegKeyW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDevRegKeyW");
87     pSetupDiRegisterDeviceInfo = (void *)GetProcAddress(hSetupAPI, "SetupDiRegisterDeviceInfo");
88     pSetupDiGetClassDevsA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetClassDevsA");
89     pSetupDiGetClassDevsW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetClassDevsW");
90     pSetupDiGetINFClassA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetINFClassA");
91     pSetupDiSetDeviceRegistryPropertyA = (void *)GetProcAddress(hSetupAPI, "SetupDiSetDeviceRegistryPropertyA");
92     pSetupDiSetDeviceRegistryPropertyW = (void *)GetProcAddress(hSetupAPI, "SetupDiSetDeviceRegistryPropertyW");
93     pSetupDiGetDeviceRegistryPropertyA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceRegistryPropertyA");
94     pSetupDiGetDeviceRegistryPropertyW = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceRegistryPropertyW");
95     pIsWow64Process = (void *)GetProcAddress(hKernel32, "IsWow64Process");
96 }
97
98 static void change_reg_permissions(const WCHAR *regkey)
99 {
100     HKEY hkey;
101     SID_IDENTIFIER_AUTHORITY ident = { SECURITY_WORLD_SID_AUTHORITY };
102     SECURITY_DESCRIPTOR sd;
103     PSID EveryoneSid;
104     PACL pacl = NULL;
105
106     RegOpenKeyExW(HKEY_LOCAL_MACHINE, regkey, 0, WRITE_DAC, &hkey);
107
108     /* Initialize the 'Everyone' sid */
109     AllocateAndInitializeSid(&ident, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &EveryoneSid);
110
111     pacl = HeapAlloc(GetProcessHeap(), 0, 256);
112     InitializeAcl(pacl, 256, ACL_REVISION);
113
114     /* Add 'Full Control' for 'Everyone' */
115     AddAccessAllowedAce(pacl, ACL_REVISION, KEY_ALL_ACCESS, EveryoneSid);
116
117     InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
118
119     SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE);
120
121     /* Set the new security on the registry key */
122     RegSetKeySecurity(hkey, DACL_SECURITY_INFORMATION, &sd);
123
124     RegCloseKey(hkey);
125
126     HeapFree(GetProcessHeap(), 0, pacl);
127     if (EveryoneSid)
128         FreeSid(EveryoneSid);
129 }
130
131 static BOOL remove_device(void)
132 {
133     HDEVINFO set;
134     SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
135     BOOL ret, retval;
136
137     SetLastError(0xdeadbeef);
138     set = pSetupDiGetClassDevsA(&guid, NULL, 0, 0);
139     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
140      GetLastError());
141
142     SetLastError(0xdeadbeef);
143     ret = pSetupDiEnumDeviceInfo(set, 0, &devInfo);
144     ok(ret, "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
145
146     SetLastError(0xdeadbeef);
147     retval = pSetupDiCallClassInstaller(DIF_REMOVE, set, &devInfo);
148     if(is_wow64)
149         todo_wine ok(!retval && GetLastError() == ERROR_IN_WOW64,
150                      "SetupDiCallClassInstaller(DIF_REMOVE...) succeeded: %08x\n", GetLastError());
151     else
152         todo_wine ok(retval,
153                      "SetupDiCallClassInstaller(DIF_REMOVE...) failed: %08x\n", GetLastError());
154
155     SetLastError(0xdeadbeef);
156     ret = pSetupDiDestroyDeviceInfoList(set);
157     ok(ret, "SetupDiDestroyDeviceInfoList failed: %08x\n", GetLastError());
158
159     return retval;
160 }
161
162 /* RegDeleteTreeW from dlls/advapi32/registry.c */
163 static LSTATUS devinst_RegDeleteTreeW(HKEY hKey, LPCWSTR lpszSubKey)
164 {
165     LONG ret;
166     DWORD dwMaxSubkeyLen, dwMaxValueLen;
167     DWORD dwMaxLen, dwSize;
168     WCHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
169     HKEY hSubKey = hKey;
170
171     if(lpszSubKey)
172     {
173         ret = RegOpenKeyExW(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
174         if (ret) return ret;
175     }
176
177     /* Get highest length for keys, values */
178     ret = RegQueryInfoKeyW(hSubKey, NULL, NULL, NULL, NULL,
179             &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
180     if (ret) goto cleanup;
181
182     dwMaxSubkeyLen++;
183     dwMaxValueLen++;
184     dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
185     if (dwMaxLen > sizeof(szNameBuf)/sizeof(WCHAR))
186     {
187         /* Name too big: alloc a buffer for it */
188         if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(WCHAR))))
189         {
190             ret = ERROR_NOT_ENOUGH_MEMORY;
191             goto cleanup;
192         }
193     }
194
195
196     /* Recursively delete all the subkeys */
197     while (TRUE)
198     {
199         dwSize = dwMaxLen;
200         if (RegEnumKeyExW(hSubKey, 0, lpszName, &dwSize, NULL,
201                           NULL, NULL, NULL)) break;
202
203         ret = devinst_RegDeleteTreeW(hSubKey, lpszName);
204         if (ret) goto cleanup;
205     }
206
207     if (lpszSubKey)
208         ret = RegDeleteKeyW(hKey, lpszSubKey);
209     else
210         while (TRUE)
211         {
212             dwSize = dwMaxLen;
213             if (RegEnumValueW(hKey, 0, lpszName, &dwSize,
214                   NULL, NULL, NULL, NULL)) break;
215
216             ret = RegDeleteValueW(hKey, lpszName);
217             if (ret) goto cleanup;
218         }
219
220 cleanup:
221     /* Free buffer if allocated */
222     if (lpszName != szNameBuf)
223         HeapFree( GetProcessHeap(), 0, lpszName);
224     if(lpszSubKey)
225         RegCloseKey(hSubKey);
226     return ret;
227 }
228
229 static void clean_devclass_key(void)
230 {
231     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
232      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
233      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
234      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
235      '1','1','d','b','-','b','7','0','4','-',
236      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
237     HKEY key;
238     DWORD subkeys;
239
240     /* Check if we have subkeys as Windows 2000 doesn't delete
241      * the keys under the DeviceClasses key after a SetupDiDestroyDeviceInfoList.
242      */
243     RegOpenKeyW(HKEY_LOCAL_MACHINE, devclass, &key);
244     RegQueryInfoKey(key, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
245     if (subkeys > 0)
246     {
247         trace("We are most likely on Windows 2000\n");
248         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
249     }
250     else
251     {
252         ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, devclass),
253          "Couldn't delete deviceclass key\n");
254     }
255 }
256
257 static void test_SetupDiCreateDeviceInfoListEx(void) 
258 {
259     HDEVINFO devlist;
260     BOOL ret;
261     DWORD error;
262     static CHAR notnull[] = "NotNull";
263     static const WCHAR machine[] = { 'd','u','m','m','y',0 };
264     static const WCHAR empty[] = { 0 };
265
266     SetLastError(0xdeadbeef);
267     /* create empty DeviceInfoList, but set Reserved to a value, which is not NULL */
268     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, notnull);
269
270     error = GetLastError();
271     if (error == ERROR_CALL_NOT_IMPLEMENTED)
272     {
273         win_skip("SetupDiCreateDeviceInfoListExW is not implemented\n");
274         return;
275     }
276     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
277     ok(error == ERROR_INVALID_PARAMETER, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_PARAMETER);
278
279     SetLastError(0xdeadbeef);
280     /* create empty DeviceInfoList, but set MachineName to something */
281     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, machine, NULL);
282
283     error = GetLastError();
284     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
285     ok(error == ERROR_INVALID_MACHINENAME || error == ERROR_MACHINE_UNAVAILABLE, "GetLastError returned wrong value : %d, (expected %d or %d)\n", error, ERROR_INVALID_MACHINENAME, ERROR_MACHINE_UNAVAILABLE);
286
287     /* create empty DeviceInfoList */
288     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, NULL);
289     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
290
291     /* destroy DeviceInfoList */
292     ret = pSetupDiDestroyDeviceInfoList(devlist);
293     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
294
295     /* create empty DeviceInfoList with empty machine name */
296     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, empty, NULL);
297     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
298
299     /* destroy DeviceInfoList */
300     ret = pSetupDiDestroyDeviceInfoList(devlist);
301     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
302 }
303
304 static void test_SetupDiOpenClassRegKeyExA(void)
305 {
306     static const CHAR guidString[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
307     HKEY hkey;
308
309     /* Check return value for nonexistent key */
310     hkey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
311         DIOCR_INSTALLER, NULL, NULL);
312     ok(hkey == INVALID_HANDLE_VALUE,
313         "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey);
314
315     /* Test it for a key that exists */
316     hkey = SetupDiOpenClassRegKey(NULL, KEY_ALL_ACCESS);
317     if (hkey != INVALID_HANDLE_VALUE)
318     {
319         HKEY classKey;
320         if (RegCreateKeyA(hkey, guidString, &classKey) == ERROR_SUCCESS)
321         {
322             RegCloseKey(classKey);
323             SetLastError(0xdeadbeef);
324             classKey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
325                 DIOCR_INSTALLER, NULL, NULL);
326             ok(classKey != INVALID_HANDLE_VALUE,
327                 "opening class registry key failed with error %d\n",
328                 GetLastError());
329             if (classKey != INVALID_HANDLE_VALUE)
330                 RegCloseKey(classKey);
331             RegDeleteKeyA(hkey, guidString);
332         }
333         else
334             trace("failed to create registry key for test\n");
335
336         RegCloseKey(hkey);
337     }
338     else
339         trace("failed to open classes key\n");
340 }
341
342 static void create_inf_file(LPCSTR filename)
343 {
344     DWORD dwNumberOfBytesWritten;
345     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
346                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
347
348     static const char data[] =
349         "[Version]\n"
350         "Signature=\"$Chicago$\"\n"
351         "Class=Bogus\n"
352         "ClassGUID={6a55b5a4-3f65-11db-b704-0011955c2bdb}\n"
353         "[ClassInstall32]\n"
354         "AddReg=BogusClass.NT.AddReg\n"
355         "[BogusClass.NT.AddReg]\n"
356         "HKR,,,,\"Wine test devices\"\n";
357
358     WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
359     CloseHandle(hf);
360 }
361
362 static void get_temp_filename(LPSTR path)
363 {
364     static char curr[MAX_PATH] = { 0 };
365     char temp[MAX_PATH];
366     LPSTR ptr;
367
368     if (!*curr)
369         GetCurrentDirectoryA(MAX_PATH, curr);
370     GetTempFileNameA(curr, "set", 0, temp);
371     ptr = strrchr(temp, '\\');
372
373     lstrcpyA(path, ptr + 1);
374 }
375
376 static void testInstallClass(void)
377 {
378     static const WCHAR classKey[] = {'S','y','s','t','e','m','\\',
379      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
380      'C','o','n','t','r','o','l','\\','C','l','a','s','s','\\',
381      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
382      '1','1','d','b','-','b','7','0','4','-',
383      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
384     char tmpfile[MAX_PATH];
385     BOOL ret;
386
387     tmpfile[0] = '.';
388     tmpfile[1] = '\\';
389     get_temp_filename(tmpfile + 2);
390     create_inf_file(tmpfile + 2);
391
392     ret = pSetupDiInstallClassA(NULL, NULL, 0, NULL);
393     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
394      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
395     ret = pSetupDiInstallClassA(NULL, NULL, DI_NOVCP, NULL);
396     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
397      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
398     ret = pSetupDiInstallClassA(NULL, tmpfile + 2, DI_NOVCP, NULL);
399     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
400      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
401     ret = pSetupDiInstallClassA(NULL, tmpfile + 2, 0, NULL);
402     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
403      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
404     /* The next call will succeed. Information is put into the registry but the
405      * location(s) is/are depending on the Windows version.
406      */
407     ret = pSetupDiInstallClassA(NULL, tmpfile, 0, NULL);
408     ok(ret, "SetupDiInstallClassA failed: %08x\n", GetLastError());
409
410     ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, classKey),
411      "Couldn't delete classkey\n");
412
413     DeleteFile(tmpfile);
414 }
415
416 static void testCreateDeviceInfo(void)
417 {
418     BOOL ret;
419     HDEVINFO set;
420     HKEY key;
421     LONG res;
422     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
423      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
424      'E','n','u','m','\\','R','o','o','t','\\',
425      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
426
427     SetLastError(0xdeadbeef);
428     ret = pSetupDiCreateDeviceInfoA(NULL, NULL, NULL, NULL, NULL, 0, NULL);
429     ok(!ret, "Expected failure\n");
430     ok(GetLastError() == ERROR_INVALID_DEVINST_NAME ||
431       GetLastError() == ERROR_INVALID_PARAMETER /* NT4 */,
432      "Unexpected last error, got %08x\n", GetLastError());
433
434     SetLastError(0xdeadbeef);
435     ret = pSetupDiCreateDeviceInfoA(NULL, "Root\\LEGACY_BOGUS\\0000", NULL,
436      NULL, NULL, 0, NULL);
437     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
438      "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
439     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
440     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
441      GetLastError());
442     if (set)
443     {
444         SP_DEVINFO_DATA devInfo = { 0 };
445         DWORD i;
446         static GUID deadbeef =
447          {0xdeadbeef, 0xdead, 0xbeef, {0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
448         LONG res;
449         HKEY key;
450         static const WCHAR bogus0000[] = {'S','y','s','t','e','m','\\',
451          'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
452          'E','n','u','m','\\','R','o','o','t','\\',
453          'L','E','G','A','C','Y','_','B','O','G','U','S','\\','0','0','0','0',0};
454
455         /* So we know we have a clean start */
456         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key);
457         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
458         /* No GUID given */
459         SetLastError(0xdeadbeef);
460         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", NULL,
461          NULL, NULL, 0, NULL);
462         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
463             "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
464         /* Even though NT4 fails it still adds some stuff to the registry that
465          * can't be deleted via normal setupapi functions. As the registry is written
466          * by a different user (SYSTEM) we have to do some magic to get rid of the key
467          */
468         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key))
469         {
470             trace("NT4 created a bogus key on failure, will be removed now\n");
471             change_reg_permissions(bogus0000);
472             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus0000),
473              "Could not delete LEGACY_BOGUS\\0000 key\n");
474         }
475         /* We can't add device information to the set with a different GUID */
476         SetLastError(0xdeadbeef);
477         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000",
478          &deadbeef, NULL, NULL, 0, NULL);
479         ok(!ret && GetLastError() == ERROR_CLASS_MISMATCH,
480          "Expected ERROR_CLASS_MISMATCH, got %08x\n", GetLastError());
481         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus0000, &key))
482         {
483             trace("NT4 created a bogus key on failure, will be removed now\n");
484             change_reg_permissions(bogus0000);
485             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus0000),
486              "Could not delete LEGACY_BOGUS\\0000 key\n");
487         }
488         /* Finally, with all three required parameters, this succeeds: */
489         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
490          NULL, NULL, 0, NULL);
491         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
492         /* This fails because the device ID already exists.. */
493         SetLastError(0xdeadbeef);
494         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
495          NULL, NULL, 0, &devInfo);
496         ok(!ret && GetLastError() == ERROR_DEVINST_ALREADY_EXISTS,
497          "Expected ERROR_DEVINST_ALREADY_EXISTS, got %08x\n", GetLastError());
498         /* whereas this "fails" because cbSize is wrong.. */
499         SetLastError(0xdeadbeef);
500         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
501          DICD_GENERATE_ID, &devInfo);
502         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
503          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
504         /* and this finally succeeds. */
505         devInfo.cbSize = sizeof(devInfo);
506         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
507          DICD_GENERATE_ID, &devInfo);
508         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
509         /* There were three devices added, however - the second failure just
510          * resulted in the SP_DEVINFO_DATA not getting copied.
511          */
512         SetLastError(0xdeadbeef);
513         i = 0;
514         while (pSetupDiEnumDeviceInfo(set, i, &devInfo))
515             i++;
516         ok(i == 3, "Expected 3 devices, got %d\n", i);
517         ok(GetLastError() == ERROR_NO_MORE_ITEMS,
518          "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
519         pSetupDiDestroyDeviceInfoList(set);
520     }
521
522     /* The bogus registry key shouldn't be there after this test. The only
523      * reasons this key would still be present:
524      *
525      * - We are running on Wine which has to be fixed
526      * - We have leftovers from old tests
527      */
528     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
529     todo_wine
530     ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
531     if (res == ERROR_SUCCESS)
532     {
533         DWORD subkeys;
534
535         /* Check if we have subkeys */
536         RegQueryInfoKey(key, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
537         if (subkeys > 0)
538         {
539             int i;
540
541             /* Leftovers from old tests */
542             trace("Going to remove %d devices\n", subkeys);
543             for (i = 0; i < subkeys; i++)
544             {
545                 BOOL ret;
546
547                 ret = remove_device();
548                 ok(ret, "Expected a device to be removed\n");
549             }
550         }
551         else
552         {
553             /* Wine doesn't delete the bogus key itself currently */
554             trace("We are most likely on Wine\n");
555             RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus);
556         }
557     }
558 }
559
560 static void testGetDeviceInstanceId(void)
561 {
562     BOOL ret;
563     HDEVINFO set;
564     SP_DEVINFO_DATA devInfo = { 0 };
565
566     SetLastError(0xdeadbeef);
567     ret = pSetupDiGetDeviceInstanceIdA(NULL, NULL, NULL, 0, NULL);
568     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
569      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
570     SetLastError(0xdeadbeef);
571     ret = pSetupDiGetDeviceInstanceIdA(NULL, &devInfo, NULL, 0, NULL);
572     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
573      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
574     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
575     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
576      GetLastError());
577     if (set)
578     {
579         char instanceID[MAX_PATH];
580         DWORD size;
581
582         SetLastError(0xdeadbeef);
583         ret = pSetupDiGetDeviceInstanceIdA(set, NULL, NULL, 0, NULL);
584         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
585          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
586         SetLastError(0xdeadbeef);
587         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, NULL);
588         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
589          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
590         SetLastError(0xdeadbeef);
591         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
592         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
593          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
594         devInfo.cbSize = sizeof(devInfo);
595         SetLastError(0xdeadbeef);
596         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
597         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
598          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
599         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
600          NULL, NULL, 0, &devInfo);
601         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
602         SetLastError(0xdeadbeef);
603         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
604         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
605          "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
606         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
607          sizeof(instanceID), NULL);
608         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
609         ok(!lstrcmpA(instanceID, "ROOT\\LEGACY_BOGUS\\0000"),
610          "Unexpected instance ID %s\n", instanceID);
611         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid,
612          NULL, NULL, DICD_GENERATE_ID, &devInfo);
613         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
614         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
615          sizeof(instanceID), NULL);
616         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
617         /* NT4 returns 'Root' and W2K and above 'ROOT' */
618         ok(!lstrcmpiA(instanceID, "ROOT\\LEGACY_BOGUS\\0001"),
619          "Unexpected instance ID %s\n", instanceID);
620         pSetupDiDestroyDeviceInfoList(set);
621     }
622 }
623
624 static void testRegisterDeviceInfo(void)
625 {
626     BOOL ret;
627     HDEVINFO set;
628
629     SetLastError(0xdeadbeef);
630     ret = pSetupDiRegisterDeviceInfo(NULL, NULL, 0, NULL, NULL, NULL);
631     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
632      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
633     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
634     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
635     if (set)
636     {
637         SP_DEVINFO_DATA devInfo = { 0 };
638
639         SetLastError(0xdeadbeef);
640         ret = pSetupDiRegisterDeviceInfo(set, NULL, 0, NULL, NULL, NULL);
641         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
642          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
643         SetLastError(0xdeadbeef);
644         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
645         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
646          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
647         devInfo.cbSize = sizeof(devInfo);
648         SetLastError(0xdeadbeef);
649         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
650         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
651          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
652         ret = pSetupDiCreateDeviceInfoA(set, "USB\\BOGUS\\0000", &guid,
653          NULL, NULL, 0, &devInfo);
654         ok(ret || GetLastError() == ERROR_DEVINST_ALREADY_EXISTS,
655                 "SetupDiCreateDeviceInfoA failed: %d\n", GetLastError());
656         if (ret)
657         {
658             /* If it already existed, registering it again will fail */
659             ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL,
660              NULL);
661             ok(ret, "SetupDiCreateDeviceInfoA failed: %d\n", GetLastError());
662         }
663         /* FIXME: On Win2K+ systems, this is now persisted to registry in
664          * HKLM\System\CCS\Enum\USB\Bogus\0000.
665          * FIXME: the key also becomes undeletable.  How to get rid of it?
666          */
667         pSetupDiDestroyDeviceInfoList(set);
668     }
669 }
670
671 static void testCreateDeviceInterface(void)
672 {
673     BOOL ret;
674     HDEVINFO set;
675     HKEY key;
676     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
677      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
678      'E','n','u','m','\\','R','o','o','t','\\',
679      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
680     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
681      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
682      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
683      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
684      '1','1','d','b','-','b','7','0','4','-',
685      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
686
687     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiEnumDeviceInterfaces)
688     {
689         win_skip("SetupDiCreateDeviceInterfaceA and/or SetupDiEnumDeviceInterfaces are not available\n");
690         return;
691     }
692     SetLastError(0xdeadbeef);
693     ret = pSetupDiCreateDeviceInterfaceA(NULL, NULL, NULL, NULL, 0, NULL);
694     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
695      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
696     SetLastError(0xdeadbeef);
697     ret = pSetupDiCreateDeviceInterfaceA(NULL, NULL, &guid, NULL, 0, NULL);
698     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
699      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
700     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
701     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
702     if (set)
703     {
704         SP_DEVINFO_DATA devInfo = { 0 };
705         SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData),
706             { 0 } };
707         DWORD i;
708
709         SetLastError(0xdeadbeef);
710         ret = pSetupDiCreateDeviceInterfaceA(set, NULL, NULL, NULL, 0, NULL);
711         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
712          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
713         SetLastError(0xdeadbeef);
714         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, NULL, NULL, 0,
715                 NULL);
716         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
717          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
718         devInfo.cbSize = sizeof(devInfo);
719         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
720                 NULL, NULL, 0, &devInfo);
721         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
722         SetLastError(0xdeadbeef);
723         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, NULL, NULL, 0,
724                 NULL);
725         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
726          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
727         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
728                 NULL);
729         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
730         /* Creating the same interface a second time succeeds */
731         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
732                 NULL);
733         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
734         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, "Oogah", 0,
735                 NULL);
736         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
737         ret = pSetupDiEnumDeviceInterfaces(set, &devInfo, &guid, 0,
738                 &interfaceData);
739         ok(ret, "SetupDiEnumDeviceInterfaces failed: %d\n", GetLastError());
740         i = 0;
741         while (pSetupDiEnumDeviceInterfaces(set, &devInfo, &guid, i,
742                     &interfaceData))
743             i++;
744         ok(i == 2, "expected 2 interfaces, got %d\n", i);
745         ok(GetLastError() == ERROR_NO_MORE_ITEMS,
746          "SetupDiEnumDeviceInterfaces failed: %08x\n", GetLastError());
747         ret = pSetupDiDestroyDeviceInfoList(set);
748         ok(ret, "SetupDiDestroyDeviceInfoList failed: %08x\n", GetLastError());
749
750         /* Cleanup */
751         /* FIXME: On Wine we still have the bogus entry in Enum\Root and
752          * subkeys, as well as the deviceclass key with subkeys.
753          * Only clean the deviceclass key once Wine if fixed.
754          */
755         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key))
756         {
757             /* Wine doesn't delete the information currently */
758             trace("We are most likely on Wine\n");
759             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
760             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
761         }
762         else
763         {
764             clean_devclass_key();
765         }
766     }
767 }
768
769 static void testGetDeviceInterfaceDetail(void)
770 {
771     BOOL ret;
772     HDEVINFO set;
773     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
774      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
775      'E','n','u','m','\\','R','o','o','t','\\',
776      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
777     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
778      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
779      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
780      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
781      '1','1','d','b','-','b','7','0','4','-',
782      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
783
784     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiGetDeviceInterfaceDetailA)
785     {
786         win_skip("SetupDiCreateDeviceInterfaceA and/or SetupDiGetDeviceInterfaceDetailA are not available\n");
787         return;
788     }
789     SetLastError(0xdeadbeef);
790     ret = pSetupDiGetDeviceInterfaceDetailA(NULL, NULL, NULL, 0, NULL, NULL);
791     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
792      "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
793     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
794     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
795     if (set)
796     {
797         SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
798         SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData),
799             { 0 } };
800         DWORD size = 0;
801         HKEY key;
802
803         SetLastError(0xdeadbeef);
804         ret = pSetupDiGetDeviceInterfaceDetailA(set, NULL, NULL, 0, NULL,
805                 NULL);
806         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
807          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
808         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
809                 NULL, NULL, 0, &devInfo);
810         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
811         SetLastError(0xdeadbeef);
812         ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0,
813                 &interfaceData);
814         ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
815         SetLastError(0xdeadbeef);
816         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
817                 0, NULL, NULL);
818         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
819          "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
820         SetLastError(0xdeadbeef);
821         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
822                 100, NULL, NULL);
823         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
824          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
825         SetLastError(0xdeadbeef);
826         ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL,
827                 0, &size, NULL);
828         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
829          "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
830         if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
831         {
832             static const char path[] =
833              "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
834             static const char path_w2k[] =
835              "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}\\";
836             LPBYTE buf = HeapAlloc(GetProcessHeap(), 0, size);
837             SP_DEVICE_INTERFACE_DETAIL_DATA_A *detail =
838                 (SP_DEVICE_INTERFACE_DETAIL_DATA_A *)buf;
839             DWORD expectedsize = FIELD_OFFSET(SP_DEVICE_INTERFACE_DETAIL_DATA_W, DevicePath) + sizeof(WCHAR)*(1 + strlen(path));
840
841             detail->cbSize = 0;
842             SetLastError(0xdeadbeef);
843             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
844                     size, &size, NULL);
845             ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
846              "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
847             detail->cbSize = size;
848             SetLastError(0xdeadbeef);
849             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
850                     size, &size, NULL);
851             ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
852              "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
853             detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
854             SetLastError(0xdeadbeef);
855             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, detail,
856                     size, &size, NULL);
857             ok(ret, "SetupDiGetDeviceInterfaceDetailA failed: %d\n",
858                     GetLastError());
859             ok(!lstrcmpiA(path, detail->DevicePath) ||
860              !lstrcmpiA(path_w2k, detail->DevicePath), "Unexpected path %s\n",
861              detail->DevicePath);
862             /* Check SetupDiGetDeviceInterfaceDetailW */
863             ret = pSetupDiGetDeviceInterfaceDetailW(set, &interfaceData, NULL, 0, &size, NULL);
864             ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
865              "Expected ERROR_INSUFFICIENT_BUFFER, got error code: %d\n", GetLastError());
866             ok(expectedsize == size ||
867              (expectedsize + sizeof(WCHAR)) == size /* W2K adds a backslash */,
868              "SetupDiGetDeviceInterfaceDetailW returned wrong reqsize, got %d\n",
869              size);
870
871             HeapFree(GetProcessHeap(), 0, buf);
872         }
873         pSetupDiDestroyDeviceInfoList(set);
874
875         /* Cleanup */
876         /* FIXME: On Wine we still have the bogus entry in Enum\Root and
877          * subkeys, as well as the deviceclass key with subkeys.
878          * Only do the RegDeleteKey, once Wine is fixed.
879          */
880         if (!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key))
881         {
882             /* Wine doesn't delete the information currently */
883             trace("We are most likely on Wine\n");
884             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
885             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
886         }
887         else
888         {
889             clean_devclass_key();
890         }
891     }
892 }
893
894 static void testDevRegKey(void)
895 {
896     static const WCHAR classKey[] = {'S','y','s','t','e','m','\\',
897      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
898      'C','o','n','t','r','o','l','\\','C','l','a','s','s','\\',
899      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
900      '1','1','d','b','-','b','7','0','4','-',
901      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
902     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
903      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
904      'E','n','u','m','\\','R','o','o','t','\\',
905      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
906     BOOL ret;
907     HDEVINFO set;
908     HKEY key = NULL;
909     BOOL classKeyCreated;
910
911     SetLastError(0xdeadbeef);
912     key = pSetupDiCreateDevRegKeyW(NULL, NULL, 0, 0, 0, NULL, NULL);
913     ok(key == INVALID_HANDLE_VALUE,
914      "Expected INVALID_HANDLE_VALUE, got %p\n", key);
915     ok(GetLastError() == ERROR_INVALID_HANDLE,
916      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
917
918     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
919     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %d\n", GetLastError());
920     if (set)
921     {
922         SP_DEVINFO_DATA devInfo = { sizeof(devInfo), { 0 } };
923         LONG res;
924
925         /* The device info key shouldn't be there */
926         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
927         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
928         RegCloseKey(key);
929         /* Create the device information */
930         ret = pSetupDiCreateDeviceInfoA(set, "ROOT\\LEGACY_BOGUS\\0000", &guid,
931                 NULL, NULL, 0, &devInfo);
932         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
933         /* The device info key should have been created */
934         ok(!RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key),
935          "Expected registry key to exist\n");
936         RegCloseKey(key);
937         SetLastError(0xdeadbeef);
938         key = pSetupDiOpenDevRegKey(NULL, NULL, 0, 0, 0, 0);
939         ok(!key || key == INVALID_HANDLE_VALUE,
940          "Expected INVALID_HANDLE_VALUE or a NULL key (NT4)\n");
941         ok(GetLastError() == ERROR_INVALID_HANDLE,
942          "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
943         SetLastError(0xdeadbeef);
944         key = pSetupDiOpenDevRegKey(set, NULL, 0, 0, 0, 0);
945         ok(key == INVALID_HANDLE_VALUE &&
946          GetLastError() == ERROR_INVALID_PARAMETER,
947          "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
948         SetLastError(0xdeadbeef);
949         key = pSetupDiOpenDevRegKey(set, &devInfo, 0, 0, 0, 0);
950         ok(key == INVALID_HANDLE_VALUE &&
951          GetLastError() == ERROR_INVALID_FLAGS,
952          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
953         SetLastError(0xdeadbeef);
954         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0, 0, 0);
955         ok(key == INVALID_HANDLE_VALUE &&
956          GetLastError() == ERROR_INVALID_FLAGS,
957          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
958         SetLastError(0xdeadbeef);
959         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
960          DIREG_BOTH, 0);
961         ok(key == INVALID_HANDLE_VALUE &&
962          GetLastError() == ERROR_INVALID_FLAGS,
963          "Expected ERROR_INVALID_FLAGS, got %d\n", GetLastError());
964         SetLastError(0xdeadbeef);
965         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
966          DIREG_DRV, 0);
967         ok(key == INVALID_HANDLE_VALUE &&
968          GetLastError() == ERROR_DEVINFO_NOT_REGISTERED,
969          "Expected ERROR_DEVINFO_NOT_REGISTERED, got %08x\n", GetLastError());
970         SetLastError(0xdeadbeef);
971         ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
972         ok(ret, "SetupDiRegisterDeviceInfo failed: %08x\n", GetLastError());
973         SetLastError(0xdeadbeef);
974         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
975          DIREG_DRV, 0);
976         /* The software key isn't created by default */
977         todo_wine
978         ok(key == INVALID_HANDLE_VALUE &&
979          GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
980          "Expected ERROR_KEY_DOES_NOT_EXIST, got %08x\n", GetLastError());
981         SetLastError(0xdeadbeef);
982         key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
983          DIREG_DEV, 0);
984         todo_wine
985         ok(key == INVALID_HANDLE_VALUE &&
986          GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
987          "Expected ERROR_KEY_DOES_NOT_EXIST, got %08x\n", GetLastError());
988         SetLastError(0xdeadbeef);
989         /* The class key shouldn't be there */
990         res = RegOpenKeyW(HKEY_LOCAL_MACHINE, classKey, &key);
991         todo_wine
992         ok(res != ERROR_SUCCESS, "Expected key to not exist\n");
993         RegCloseKey(key);
994         /* Create the device reg key */
995         key = pSetupDiCreateDevRegKeyW(set, &devInfo, DICS_FLAG_GLOBAL, 0,
996          DIREG_DRV, NULL, NULL);
997         /* Vista and higher don't actually create the key */
998         ok(key != INVALID_HANDLE_VALUE || GetLastError() == ERROR_KEY_DOES_NOT_EXIST,
999          "SetupDiCreateDevRegKey failed: %08x\n", GetLastError());
1000         if (key != INVALID_HANDLE_VALUE)
1001         {
1002             classKeyCreated = TRUE;
1003             RegCloseKey(key);
1004             /* The class key should have been created */
1005             ok(!RegOpenKeyW(HKEY_LOCAL_MACHINE, classKey, &key),
1006              "Expected registry key to exist\n");
1007             RegCloseKey(key);
1008             SetLastError(0xdeadbeef);
1009             key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
1010              DIREG_DRV, 0);
1011             todo_wine
1012             ok(key == INVALID_HANDLE_VALUE &&
1013              (GetLastError() == ERROR_INVALID_DATA ||
1014              GetLastError() == ERROR_ACCESS_DENIED), /* win2k3 */
1015              "Expected ERROR_INVALID_DATA or ERROR_ACCESS_DENIED, got %08x\n", GetLastError());
1016             key = pSetupDiOpenDevRegKey(set, &devInfo, DICS_FLAG_GLOBAL, 0,
1017              DIREG_DRV, KEY_READ);
1018             ok(key != INVALID_HANDLE_VALUE, "SetupDiOpenDevRegKey failed: %08x\n",
1019              GetLastError());
1020             pSetupDiDestroyDeviceInfoList(set);
1021         }
1022         else
1023             classKeyCreated = FALSE;
1024
1025         /* Cleanup */
1026         ret = remove_device();
1027         if(!is_wow64)
1028             todo_wine ok(ret, "Expected the device to be removed: %08x\n", GetLastError());
1029
1030         /* FIXME: Only do the RegDeleteKey, once Wine is fixed */
1031         if (!ret)
1032         {
1033             /* Wine doesn't delete the information currently */
1034             trace("We are most likely on Wine\n");
1035             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
1036             devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, classKey);
1037         }
1038         else if (classKeyCreated)
1039         {
1040             /* There should only be a class key entry, so a simple
1041              * RegDeleteKey should work
1042              *
1043              * This could fail if it's the first time for this new test
1044              * after running the old tests.
1045              */
1046             ok(!RegDeleteKeyW(HKEY_LOCAL_MACHINE, classKey),
1047              "Couldn't delete classkey\n");
1048         }
1049     }
1050 }
1051
1052 static void testRegisterAndGetDetail(void)
1053 {
1054     HDEVINFO set;
1055     BOOL ret;
1056     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1057     SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof(interfaceData), { 0 } };
1058     DWORD dwSize = 0;
1059     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
1060      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1061      'E','n','u','m','\\','R','o','o','t','\\',
1062      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1063     static const WCHAR devclass[] = {'S','y','s','t','e','m','\\',
1064      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1065      'C','o','n','t','r','o','l','\\','D','e','v','i','c','e','C','l','a','s','s','e','s','\\',
1066      '{','6','a','5','5','b','5','a','4','-','3','f','6','5','-',
1067      '1','1','d','b','-','b','7','0','4','-',
1068      '0','0','1','1','9','5','5','c','2','b','d','b','}',0};
1069
1070     if (!pSetupDiCreateDeviceInterfaceA || !pSetupDiEnumDeviceInterfaces ||
1071         !pSetupDiGetDeviceInterfaceDetailA)
1072     {
1073         win_skip("Needed functions are not available\n");
1074         return;
1075     }
1076
1077     SetLastError(0xdeadbeef);
1078     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_ALLCLASSES);
1079     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1080      GetLastError());
1081
1082     SetLastError(0xdeadbeef);
1083     ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, 0,
1084      DICD_GENERATE_ID, &devInfo);
1085     ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
1086     SetLastError(0xdeadbeef);
1087     ret = pSetupDiCreateDeviceInterfaceA(set, &devInfo, &guid, NULL, 0, &interfaceData);
1088     ok(ret, "SetupDiCreateDeviceInterfaceA failed: %08x\n", GetLastError());
1089     SetLastError(0xdeadbeef);
1090     ret = pSetupDiRegisterDeviceInfo(set, &devInfo, 0, NULL, NULL, NULL);
1091     ok(ret, "SetupDiRegisterDeviceInfo failed: %08x\n", GetLastError());
1092
1093     pSetupDiDestroyDeviceInfoList(set);
1094
1095     SetLastError(0xdeadbeef);
1096     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1097     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1098      GetLastError());
1099
1100     SetLastError(0xdeadbeef);
1101     ret = pSetupDiEnumDeviceInterfaces(set, NULL, &guid, 0, &interfaceData);
1102     ok(ret, "SetupDiEnumDeviceInterfaces failed: %08x\n", GetLastError());
1103     SetLastError(0xdeadbeef);
1104     ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData, NULL, 0, &dwSize, NULL);
1105     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1106      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1107     if (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1108     {
1109         static const char path[] =
1110             "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
1111         static const char path_wow64[] =
1112             "\\\\?\\root#legacy_bogus#0001#{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
1113         static const char path_w2k[] =
1114             "\\\\?\\root#legacy_bogus#0000#{6a55b5a4-3f65-11db-b704-0011955c2bdb}\\";
1115         PSP_DEVICE_INTERFACE_DETAIL_DATA_A detail = NULL;
1116
1117         detail = HeapAlloc(GetProcessHeap(), 0, dwSize);
1118         if (detail)
1119         {
1120             detail->cbSize = sizeof(*detail);
1121             SetLastError(0xdeadbeef);
1122             ret = pSetupDiGetDeviceInterfaceDetailA(set, &interfaceData,
1123              detail, dwSize, &dwSize, NULL);
1124             ok(ret, "SetupDiGetDeviceInterfaceDetailA failed: %08x\n", GetLastError());
1125             /* FIXME: This one only worked because old data wasn't removed properly. As soon
1126              * as all the tests are cleaned up correctly this has to be (or should be) fixed
1127              */
1128             if(is_wow64)
1129                 ok(!lstrcmpiA(path_wow64, detail->DevicePath),
1130                    "Unexpected path %s\n", detail->DevicePath);
1131             else
1132                 todo_wine ok(!lstrcmpiA(path, detail->DevicePath) ||
1133                              !lstrcmpiA(path_w2k, detail->DevicePath),
1134                              "Unexpected path %s\n", detail->DevicePath);
1135             HeapFree(GetProcessHeap(), 0, detail);
1136         }
1137     }
1138
1139     pSetupDiDestroyDeviceInfoList(set);
1140
1141     /* Cleanup */
1142     ret = remove_device();
1143     if(!is_wow64)
1144         todo_wine ok(ret, "Expected the device to be removed: %08x\n", GetLastError());
1145
1146     /* FIXME: Only do the RegDeleteKey, once Wine is fixed */
1147     if (!ret)
1148     {
1149         /* Wine doesn't delete the information currently */
1150         trace("We are most likely on Wine\n");
1151         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, bogus);
1152         devinst_RegDeleteTreeW(HKEY_LOCAL_MACHINE, devclass);
1153     }
1154     else
1155     {
1156         clean_devclass_key();
1157     }
1158 }
1159
1160 static void testDeviceRegistryPropertyA(void)
1161 {
1162     HDEVINFO set;
1163     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1164     CHAR devName[] = "LEGACY_BOGUS";
1165     CHAR friendlyName[] = "Bogus";
1166     CHAR buf[6] = "";
1167     DWORD buflen = 6;
1168     DWORD size;
1169     DWORD regType;
1170     BOOL ret;
1171     LONG res;
1172     HKEY key;
1173     static const CHAR bogus[] =
1174      "System\\CurrentControlSet\\Enum\\Root\\LEGACY_BOGUS";
1175
1176     SetLastError(0xdeadbeef);
1177     set = pSetupDiGetClassDevsA(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1178     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsA failed: %08x\n",
1179      GetLastError());
1180     SetLastError(0xdeadbeef);
1181     ret = pSetupDiCreateDeviceInfoA(set, devName, &guid, NULL, NULL,
1182      DICD_GENERATE_ID, &devInfo);
1183     ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
1184     SetLastError(0xdeadbeef);
1185     ret = pSetupDiSetDeviceRegistryPropertyA(NULL, NULL, -1, NULL, 0);
1186     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1187      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1188     SetLastError(0xdeadbeef);
1189     ret = pSetupDiSetDeviceRegistryPropertyA(set, NULL, -1, NULL, 0);
1190     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1191      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1192     SetLastError(0xdeadbeef);
1193     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, -1, NULL, 0);
1194     todo_wine
1195     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1196      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1197     /* GetLastError() returns nonsense in win2k3 */
1198     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1199      NULL, 0);
1200     todo_wine
1201     ok(!ret, "Expected failure, got %d\n", ret);
1202     SetLastError(0xdeadbeef);
1203     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1204      (PBYTE)friendlyName, buflen);
1205     ok(ret, "SetupDiSetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1206     SetLastError(0xdeadbeef);
1207     ret = pSetupDiGetDeviceRegistryPropertyA(NULL, NULL, -1, NULL, NULL, 0, NULL);
1208     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1209      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1210     SetLastError(0xdeadbeef);
1211     ret = pSetupDiGetDeviceRegistryPropertyA(set, NULL, -1, NULL, NULL, 0, NULL);
1212     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1213      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1214     SetLastError(0xdeadbeef);
1215     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, -1, NULL, NULL, 0, NULL);
1216     todo_wine
1217     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1218      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1219     /* GetLastError() returns nonsense in win2k3 */
1220     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1221      NULL, NULL, buflen, NULL);
1222     ok(!ret, "Expected failure, got %d\n", ret);
1223     SetLastError(0xdeadbeef);
1224     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1225      NULL, NULL, 0, &size);
1226     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1227      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1228     ok(buflen == size, "Unexpected size: %d\n", size);
1229     SetLastError(0xdeadbeef);
1230     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1231      NULL, (PBYTE)buf, buflen, NULL);
1232     ok(ret, "SetupDiGetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1233     ok(!lstrcmpiA(friendlyName, buf), "Unexpected property\n");
1234     SetLastError(0xdeadbeef);
1235     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1236      &regType, (PBYTE)buf, buflen, NULL);
1237     ok(ret, "SetupDiGetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1238     ok(!lstrcmpiA(friendlyName, buf), "Unexpected value of property\n");
1239     ok(regType == REG_SZ, "Unexpected type of property: %d\n", regType);
1240     SetLastError(0xdeadbeef);
1241     ret = pSetupDiSetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1242      NULL, 0);
1243     ok(ret, "SetupDiSetDeviceRegistryPropertyA failed: %08x\n", GetLastError());
1244     SetLastError(0xdeadbeef);
1245     ret = pSetupDiGetDeviceRegistryPropertyA(set, &devInfo, SPDRP_FRIENDLYNAME,
1246      NULL, (PBYTE)buf, buflen, &size);
1247     todo_wine
1248     ok(!ret && GetLastError() == ERROR_INVALID_DATA,
1249      "Expected ERROR_INVALID_DATA, got %08x\n", GetLastError());
1250     pSetupDiDestroyDeviceInfoList(set);
1251
1252     res = RegOpenKeyA(HKEY_LOCAL_MACHINE, bogus, &key);
1253     if(!is_wow64)
1254         todo_wine ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
1255     /* FIXME: Remove when Wine is fixed */
1256     if (res == ERROR_SUCCESS)
1257     {
1258         /* Wine doesn't delete the information currently */
1259         trace("We are most likely on Wine\n");
1260         RegDeleteKeyA(HKEY_LOCAL_MACHINE, bogus);
1261     }
1262 }
1263
1264 static void testDeviceRegistryPropertyW(void)
1265 {
1266     HDEVINFO set;
1267     SP_DEVINFO_DATA devInfo = { sizeof(SP_DEVINFO_DATA), { 0 } };
1268     WCHAR devName[] = {'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1269     WCHAR friendlyName[] = {'B','o','g','u','s',0};
1270     WCHAR buf[6] = {0};
1271     DWORD buflen = 6 * sizeof(WCHAR);
1272     DWORD size;
1273     DWORD regType;
1274     BOOL ret;
1275     LONG res;
1276     HKEY key;
1277     static const WCHAR bogus[] = {'S','y','s','t','e','m','\\',
1278      'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
1279      'E','n','u','m','\\','R','o','o','t','\\',
1280      'L','E','G','A','C','Y','_','B','O','G','U','S',0};
1281
1282     SetLastError(0xdeadbeef);
1283     set = pSetupDiGetClassDevsW(&guid, NULL, 0, DIGCF_DEVICEINTERFACE);
1284     ok(set != INVALID_HANDLE_VALUE, "SetupDiGetClassDevsW failed: %08x\n",
1285      GetLastError());
1286     SetLastError(0xdeadbeef);
1287     ret = pSetupDiCreateDeviceInfoW(set, devName, &guid, NULL, NULL,
1288      DICD_GENERATE_ID, &devInfo);
1289     ok(ret, "SetupDiCreateDeviceInfoW failed: %08x\n", GetLastError());
1290     SetLastError(0xdeadbeef);
1291     ret = pSetupDiSetDeviceRegistryPropertyW(NULL, NULL, -1, NULL, 0);
1292     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1293      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1294     SetLastError(0xdeadbeef);
1295     ret = pSetupDiSetDeviceRegistryPropertyW(set, NULL, -1, NULL, 0);
1296     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1297      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1298     SetLastError(0xdeadbeef);
1299     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, -1, NULL, 0);
1300     todo_wine
1301     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1302      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1303     /* GetLastError() returns nonsense in win2k3 */
1304     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1305      NULL, 0);
1306     todo_wine
1307     ok(!ret, "Expected failure, got %d\n", ret);
1308     SetLastError(0xdeadbeef);
1309     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1310      (PBYTE)friendlyName, buflen);
1311     ok(ret, "SetupDiSetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1312     SetLastError(0xdeadbeef);
1313     ret = pSetupDiGetDeviceRegistryPropertyW(NULL, NULL, -1, NULL, NULL, 0, NULL);
1314     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
1315      "Expected ERROR_INVALID_HANDLE, got %08x\n", GetLastError());
1316     SetLastError(0xdeadbeef);
1317     ret = pSetupDiGetDeviceRegistryPropertyW(set, NULL, -1, NULL, NULL, 0, NULL);
1318     ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
1319      "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
1320     SetLastError(0xdeadbeef);
1321     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, -1, NULL, NULL, 0, NULL);
1322     todo_wine
1323     ok(!ret && GetLastError() == ERROR_INVALID_REG_PROPERTY,
1324      "Expected ERROR_INVALID_REG_PROPERTY, got %08x\n", GetLastError());
1325     /* GetLastError() returns nonsense in win2k3 */
1326     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1327      NULL, NULL, buflen, NULL);
1328     ok(!ret, "Expected failure, got %d\n", ret);
1329     SetLastError(0xdeadbeef);
1330     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1331      NULL, NULL, 0, &size);
1332     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
1333      "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
1334     ok(buflen == size, "Unexpected size: %d\n", size);
1335     SetLastError(0xdeadbeef);
1336     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1337      NULL, (PBYTE)buf, buflen, NULL);
1338     ok(ret, "SetupDiGetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1339     ok(!lstrcmpiW(friendlyName, buf), "Unexpected property\n");
1340     SetLastError(0xdeadbeef);
1341     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1342      &regType, (PBYTE)buf, buflen, NULL);
1343     ok(ret, "SetupDiGetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1344     ok(!lstrcmpiW(friendlyName, buf), "Unexpected value of property\n");
1345     ok(regType == REG_SZ, "Unexpected type of property: %d\n", regType);
1346     SetLastError(0xdeadbeef);
1347     ret = pSetupDiSetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1348      NULL, 0);
1349     ok(ret, "SetupDiSetDeviceRegistryPropertyW failed: %08x\n", GetLastError());
1350     SetLastError(0xdeadbeef);
1351     ret = pSetupDiGetDeviceRegistryPropertyW(set, &devInfo, SPDRP_FRIENDLYNAME,
1352      NULL, (PBYTE)buf, buflen, &size);
1353     todo_wine
1354     ok(!ret && GetLastError() == ERROR_INVALID_DATA,
1355      "Expected ERROR_INVALID_DATA, got %08x\n", GetLastError());
1356     pSetupDiDestroyDeviceInfoList(set);
1357
1358     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, bogus, &key);
1359     if(!is_wow64)
1360         todo_wine ok(res == ERROR_FILE_NOT_FOUND, "Expected key to not exist\n");
1361     /* FIXME: Remove when Wine is fixed */
1362     if (res == ERROR_SUCCESS)
1363     {
1364         /* Wine doesn't delete the information currently */
1365         trace("We are most likely on Wine\n");
1366         RegDeleteKeyW(HKEY_LOCAL_MACHINE, bogus);
1367     }
1368 }
1369
1370 static void testSetupDiGetINFClassA(void)
1371 {
1372     static const char inffile[] = "winetest.inf";
1373     static const char content[] = "[Version]\r\n\r\n";
1374
1375     char cn[MAX_PATH];
1376     char filename[MAX_PATH];
1377     DWORD count;
1378     BOOL retval;
1379     GUID guid;
1380     HANDLE h;
1381
1382     if(!pSetupDiGetINFClassA)
1383     {
1384         win_skip("SetupDiGetINFClassA not present\n");
1385         return;
1386     }
1387
1388     count = GetTempPathA(MAX_PATH, filename);
1389     if(!count)
1390     {
1391         win_skip("GetTempPathA failed\n");
1392         return;
1393     }
1394
1395     strcat(filename, inffile);
1396     DeleteFileA(filename);
1397
1398     /* not existing file */
1399     SetLastError(0xdeadbeef);
1400     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1401     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1402     if (ERROR_CALL_NOT_IMPLEMENTED == GetLastError())
1403     {
1404         skip("SetupDiGetINFClassA is not implemented\n");
1405         return;
1406     }
1407     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1408         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1409
1410     /* missing file wins against other invalid parameter */
1411     SetLastError(0xdeadbeef);
1412     retval = SetupDiGetINFClassA(filename, NULL, cn, MAX_PATH, &count);
1413     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1414     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1415         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1416
1417     SetLastError(0xdeadbeef);
1418     retval = SetupDiGetINFClassA(filename, &guid, NULL, MAX_PATH, &count);
1419     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1420     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1421         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1422
1423     SetLastError(0xdeadbeef);
1424     retval = SetupDiGetINFClassA(filename, &guid, cn, 0, &count);
1425     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1426     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1427         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1428
1429     SetLastError(0xdeadbeef);
1430     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, NULL);
1431     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1432     ok(ERROR_FILE_NOT_FOUND == GetLastError(),
1433         "expected error ERROR_FILE_NOT_FOUND, got %u\n", GetLastError());
1434
1435     /* test file content */
1436     h = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1437                     FILE_ATTRIBUTE_NORMAL, NULL);
1438     if(h == INVALID_HANDLE_VALUE)
1439     {
1440         win_skip("failed to create file %s (error %u)\n", filename, GetLastError());
1441         return;
1442     }
1443     CloseHandle( h);
1444
1445     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1446     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1447
1448     h = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1449                     FILE_ATTRIBUTE_NORMAL, NULL);
1450     if(h == INVALID_HANDLE_VALUE)
1451     {
1452         win_skip("failed to create file %s (error %u)\n", filename, GetLastError());
1453         return;
1454     }
1455     WriteFile( h, content, sizeof(content), &count, NULL);
1456     CloseHandle( h);
1457
1458     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1459     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1460
1461     WritePrivateProfileStringA("Version", "Signature", "\"$CHICAGO$\"", filename);
1462
1463     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1464     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1465
1466     WritePrivateProfileStringA("Version", "Class", "WINE", filename);
1467
1468     count = 0xdeadbeef;
1469     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1470     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1471     ok(count == 5, "expected count==5, got %u\n", count);
1472
1473     count = 0xdeadbeef;
1474     retval = SetupDiGetINFClassA(filename, &guid, cn, 5, &count);
1475     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1476     ok(count == 5, "expected count==5, got %u\n", count);
1477
1478     count = 0xdeadbeef;
1479     SetLastError(0xdeadbeef);
1480     retval = SetupDiGetINFClassA(filename, &guid, cn, 4, &count);
1481     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1482     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError(),
1483         "expected error ERROR_INSUFFICIENT_BUFFER, got %u\n", GetLastError());
1484     ok(count == 5, "expected count==5, got %u\n", count);
1485
1486     /* invalid parameter */
1487     SetLastError(0xdeadbeef);
1488     retval = SetupDiGetINFClassA(NULL, &guid, cn, MAX_PATH, &count);
1489     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1490     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1491         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1492
1493     SetLastError(0xdeadbeef);
1494     retval = SetupDiGetINFClassA(filename, NULL, cn, MAX_PATH, &count);
1495     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1496     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1497         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1498
1499     SetLastError(0xdeadbeef);
1500     retval = SetupDiGetINFClassA(filename, &guid, NULL, MAX_PATH, &count);
1501     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1502     ok(ERROR_INVALID_PARAMETER == GetLastError(),
1503         "expected error ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
1504
1505     SetLastError(0xdeadbeef);
1506     retval = SetupDiGetINFClassA(filename, &guid, cn, 0, &count);
1507     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1508     ok(ERROR_INSUFFICIENT_BUFFER == GetLastError() ||
1509        ERROR_INVALID_PARAMETER == GetLastError(),
1510         "expected error ERROR_INSUFFICIENT_BUFFER or ERROR_INVALID_PARAMETER, "
1511         "got %u\n", GetLastError());
1512
1513     DeleteFileA(filename);
1514
1515     WritePrivateProfileStringA("Version", "Signature", "\"$CHICAGO$\"", filename);
1516     WritePrivateProfileStringA("Version", "ClassGUID", "WINE", filename);
1517
1518     SetLastError(0xdeadbeef);
1519     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1520     ok(!retval, "expected SetupDiGetINFClassA to fail!\n");
1521     ok(RPC_S_INVALID_STRING_UUID == GetLastError() ||
1522        ERROR_INVALID_PARAMETER == GetLastError(),
1523         "expected error RPC_S_INVALID_STRING_UUID or ERROR_INVALID_PARAMETER, "
1524         "got %u\n", GetLastError());
1525
1526     /* network adapter guid */
1527     WritePrivateProfileStringA("Version", "ClassGUID",
1528                                "{4d36e972-e325-11ce-bfc1-08002be10318}", filename);
1529
1530     /* this test succeeds only if the guid is known to the system */
1531     count = 0xdeadbeef;
1532     retval = SetupDiGetINFClassA(filename, &guid, cn, MAX_PATH, &count);
1533     ok(retval, "expected SetupDiGetINFClassA to succeed! error %u\n", GetLastError());
1534     todo_wine
1535     ok(count == 4, "expected count==4, got %u(%s)\n", count, cn);
1536
1537     DeleteFileA(filename);
1538 }
1539
1540 START_TEST(devinst)
1541 {
1542     init_function_pointers();
1543
1544     if (pIsWow64Process)
1545         pIsWow64Process(GetCurrentProcess(), &is_wow64);
1546
1547     if (pSetupDiCreateDeviceInfoListExW)
1548         test_SetupDiCreateDeviceInfoListEx();
1549     else
1550         win_skip("SetupDiCreateDeviceInfoListExW is not available\n");
1551
1552     if (pSetupDiOpenClassRegKeyExA)
1553         test_SetupDiOpenClassRegKeyExA();
1554     else
1555         win_skip("SetupDiOpenClassRegKeyExA is not available\n");
1556
1557     testInstallClass();
1558     testCreateDeviceInfo();
1559     testGetDeviceInstanceId();
1560     testRegisterDeviceInfo();
1561     testCreateDeviceInterface();
1562     testGetDeviceInterfaceDetail();
1563     testDevRegKey();
1564     testRegisterAndGetDetail();
1565     testDeviceRegistryPropertyA();
1566     testDeviceRegistryPropertyW();
1567     testSetupDiGetINFClassA();
1568 }