setupapi: Implement SetupDiCreateDeviceInfoW.
[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
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 *pSetupDiDestroyDeviceInfoList)(HDEVINFO);
39 static BOOL     (WINAPI *pSetupDiEnumDeviceInfo)(HDEVINFO, DWORD, PSP_DEVINFO_DATA);
40 static HKEY     (WINAPI *pSetupDiOpenClassRegKeyExA)(GUID*,REGSAM,DWORD,PCSTR,PVOID);
41 static BOOL     (WINAPI *pSetupDiCreateDeviceInfoA)(HDEVINFO, PCSTR, GUID *, PCSTR, HWND, DWORD, PSP_DEVINFO_DATA);
42 static BOOL     (WINAPI *pSetupDiGetDeviceInstanceIdA)(HDEVINFO, PSP_DEVINFO_DATA, PSTR, DWORD, PDWORD);
43
44 static void init_function_pointers(void)
45 {
46     hSetupAPI = GetModuleHandleA("setupapi.dll");
47
48     pSetupDiCreateDeviceInfoA = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoA");
49     pSetupDiCreateDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoList");
50     pSetupDiCreateDeviceInfoListExW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoListExW");
51     pSetupDiDestroyDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiDestroyDeviceInfoList");
52     pSetupDiEnumDeviceInfo = (void *)GetProcAddress(hSetupAPI, "SetupDiEnumDeviceInfo");
53     pSetupDiGetDeviceInstanceIdA = (void *)GetProcAddress(hSetupAPI, "SetupDiGetDeviceInstanceIdA");
54     pSetupDiOpenClassRegKeyExA = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenClassRegKeyExA");
55 }
56
57 static void test_SetupDiCreateDeviceInfoListEx(void) 
58 {
59     HDEVINFO devlist;
60     BOOL ret;
61     DWORD error;
62     static CHAR notnull[] = "NotNull";
63     static const WCHAR machine[] = { 'd','u','m','m','y',0 };
64
65     SetLastError(0xdeadbeef);
66     /* create empty DeviceInfoList, but set Reserved to a value, which is not NULL */
67     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, notnull);
68
69     error = GetLastError();
70     if (error == ERROR_CALL_NOT_IMPLEMENTED)
71     {
72         skip("SetupDiCreateDeviceInfoListExW is not implemented\n");
73         return;
74     }
75     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
76     ok(error == ERROR_INVALID_PARAMETER, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_PARAMETER);
77
78     SetLastError(0xdeadbeef);
79     /* create empty DeviceInfoList, but set MachineName to something */
80     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, machine, NULL);
81
82     error = GetLastError();
83     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
84     ok(error == ERROR_INVALID_MACHINENAME, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_MACHINENAME);
85
86     /* create empty DeviceInfoList */
87     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, NULL);
88     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
89
90     /* destroy DeviceInfoList */
91     ret = pSetupDiDestroyDeviceInfoList(devlist);
92     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
93 }
94
95 static void test_SetupDiOpenClassRegKeyExA(void)
96 {
97     /* This is a unique guid for testing purposes */
98     GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
99         0x00,0x11,0x95,0x5c,0x2b,0xdb}};
100     static const CHAR guidString[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
101     HKEY hkey;
102
103     /* Check return value for nonexistent key */
104     hkey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
105         DIOCR_INSTALLER, NULL, NULL);
106     ok(hkey == INVALID_HANDLE_VALUE,
107         "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey);
108
109     /* Test it for a key that exists */
110     hkey = SetupDiOpenClassRegKey(NULL, KEY_ALL_ACCESS);
111     if (hkey != INVALID_HANDLE_VALUE)
112     {
113         HKEY classKey;
114         if (RegCreateKeyA(hkey, guidString, &classKey) == ERROR_SUCCESS)
115         {
116             RegCloseKey(classKey);
117             SetLastError(0xdeadbeef);
118             classKey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
119                 DIOCR_INSTALLER, NULL, NULL);
120             ok(classKey != INVALID_HANDLE_VALUE,
121                 "opening class registry key failed with error %d\n",
122                 GetLastError());
123             if (classKey != INVALID_HANDLE_VALUE)
124                 RegCloseKey(classKey);
125             RegDeleteKeyA(hkey, guidString);
126         }
127         else
128             trace("failed to create registry key for test\n");
129     }
130     else
131         trace("failed to open classes key\n");
132 }
133
134 static void testCreateDeviceInfo(void)
135 {
136     BOOL ret;
137     HDEVINFO set;
138     GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
139         0x00,0x11,0x95,0x5c,0x2b,0xdb}};
140
141     if (!pSetupDiCreateDeviceInfoList || !pSetupDiEnumDeviceInfo ||
142      !pSetupDiDestroyDeviceInfoList || !pSetupDiCreateDeviceInfoA)
143     {
144         skip("No SetupDiCreateDeviceInfoA\n");
145         return;
146     }
147     SetLastError(0xdeadbeef);
148     ret = pSetupDiCreateDeviceInfoA(NULL, NULL, NULL, NULL, NULL, 0, NULL);
149     ok(!ret && GetLastError() == ERROR_INVALID_DEVINST_NAME,
150      "Expected ERROR_INVALID_DEVINST_NAME, got %08x\n", GetLastError());
151     SetLastError(0xdeadbeef);
152     ret = pSetupDiCreateDeviceInfoA(NULL, "Root\\LEGACY_BOGUS\\0000", NULL,
153      NULL, NULL, 0, NULL);
154     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
155      "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
156     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
157     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
158      GetLastError());
159     if (set)
160     {
161         SP_DEVINFO_DATA devInfo = { 0 };
162         DWORD i;
163
164         SetLastError(0xdeadbeef);
165         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", NULL,
166          NULL, NULL, 0, NULL);
167         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
168             "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
169         /* Finally, with all three required parameters, this succeeds: */
170         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
171          NULL, NULL, 0, NULL);
172         ok(ret, "pSetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
173         /* This fails because the device ID already exists.. */
174         SetLastError(0xdeadbeef);
175         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
176          NULL, NULL, 0, &devInfo);
177         ok(!ret && GetLastError() == ERROR_DEVINST_ALREADY_EXISTS,
178          "Expected ERROR_DEVINST_ALREADY_EXISTS, got %08x\n", GetLastError());
179         /* whereas this "fails" because cbSize is wrong.. */
180         SetLastError(0xdeadbeef);
181         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
182          DICD_GENERATE_ID, &devInfo);
183         ok(!ret && GetLastError() == ERROR_INVALID_USER_BUFFER,
184          "Expected ERROR_INVALID_USER_BUFFER, got %08x\n", GetLastError());
185         devInfo.cbSize = sizeof(devInfo);
186         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid, NULL, NULL,
187          DICD_GENERATE_ID, &devInfo);
188         /* and this finally succeeds. */
189         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
190         /* There were three devices added, however - the second failure just
191          * resulted in the SP_DEVINFO_DATA not getting copied.
192          */
193         SetLastError(0xdeadbeef);
194         i = 0;
195         while (pSetupDiEnumDeviceInfo(set, i, &devInfo))
196             i++;
197         ok(i == 3, "Expected 3 devices, got %d\n", i);
198         ok(GetLastError() == ERROR_NO_MORE_ITEMS,
199          "SetupDiEnumDeviceInfo failed: %08x\n", GetLastError());
200         pSetupDiDestroyDeviceInfoList(set);
201     }
202 }
203
204 static void testGetDeviceInstanceId(void)
205 {
206     BOOL ret;
207     HDEVINFO set;
208     GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
209         0x00,0x11,0x95,0x5c,0x2b,0xdb}};
210     SP_DEVINFO_DATA devInfo = { 0 };
211
212     if (!pSetupDiCreateDeviceInfoList || !pSetupDiDestroyDeviceInfoList ||
213      !pSetupDiCreateDeviceInfoA || !pSetupDiGetDeviceInstanceIdA)
214     {
215         skip("No SetupDiGetDeviceInstanceIdA\n");
216         return;
217     }
218     SetLastError(0xdeadbeef);
219     ret = pSetupDiGetDeviceInstanceIdA(NULL, NULL, NULL, 0, NULL);
220     todo_wine
221     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
222      "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
223     SetLastError(0xdeadbeef);
224     ret = pSetupDiGetDeviceInstanceIdA(NULL, &devInfo, NULL, 0, NULL);
225     todo_wine
226     ok(!ret && GetLastError() == ERROR_INVALID_HANDLE,
227      "Expected ERROR_INVALID_HANDLEHANDLE, got %08x\n", GetLastError());
228     set = pSetupDiCreateDeviceInfoList(&guid, NULL);
229     ok(set != NULL, "SetupDiCreateDeviceInfoList failed: %08x\n",
230      GetLastError());
231     if (set)
232     {
233         char instanceID[MAX_PATH];
234         DWORD size;
235
236         SetLastError(0xdeadbeef);
237         ret = pSetupDiGetDeviceInstanceIdA(set, NULL, NULL, 0, NULL);
238         todo_wine
239         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
240          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
241         SetLastError(0xdeadbeef);
242         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, NULL);
243         todo_wine
244         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
245          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
246         SetLastError(0xdeadbeef);
247         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
248         todo_wine
249         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
250          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
251         devInfo.cbSize = sizeof(devInfo);
252         SetLastError(0xdeadbeef);
253         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
254         todo_wine
255         ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
256          "Expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
257         ret = pSetupDiCreateDeviceInfoA(set, "Root\\LEGACY_BOGUS\\0000", &guid,
258          NULL, NULL, 0, &devInfo);
259         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
260         SetLastError(0xdeadbeef);
261         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, NULL, 0, &size);
262         todo_wine
263         ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER,
264          "Expected ERROR_INSUFFICIENT_BUFFER, got %08x\n", GetLastError());
265         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
266          sizeof(instanceID), NULL);
267         todo_wine
268         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
269         todo_wine
270         ok(!lstrcmpA(instanceID, "ROOT\\LEGACY_BOGUS\\0000"),
271          "Unexpected instance ID %s\n", instanceID);
272         ret = pSetupDiCreateDeviceInfoA(set, "LEGACY_BOGUS", &guid,
273          NULL, NULL, DICD_GENERATE_ID, &devInfo);
274         ok(ret, "SetupDiCreateDeviceInfoA failed: %08x\n", GetLastError());
275         ret = pSetupDiGetDeviceInstanceIdA(set, &devInfo, instanceID,
276          sizeof(instanceID), NULL);
277         todo_wine
278         ok(ret, "SetupDiGetDeviceInstanceIdA failed: %08x\n", GetLastError());
279         todo_wine
280         ok(!lstrcmpA(instanceID, "ROOT\\LEGACY_BOGUS\\0001"),
281          "Unexpected instance ID %s\n", instanceID);
282         pSetupDiDestroyDeviceInfoList(set);
283     }
284 }
285
286 START_TEST(devinst)
287 {
288     init_function_pointers();
289
290     if (pSetupDiCreateDeviceInfoListExW && pSetupDiDestroyDeviceInfoList)
291         test_SetupDiCreateDeviceInfoListEx();
292     else
293         skip("SetupDiCreateDeviceInfoListExW and/or SetupDiDestroyDeviceInfoList not available\n");
294
295     if (pSetupDiOpenClassRegKeyExA)
296         test_SetupDiOpenClassRegKeyExA();
297     else
298         skip("SetupDiOpenClassRegKeyExA is not available\n");
299     testCreateDeviceInfo();
300     testGetDeviceInstanceId();
301 }