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