winspool.drv: Replace WINSPOOL_SHDeleteKeyW with RegDeleteTreeW.
[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 *pSetupDiCreateDeviceInfoListExW)(GUID*,HWND,PCWSTR,PVOID);
37 static BOOL     (WINAPI *pSetupDiDestroyDeviceInfoList)(HDEVINFO);
38 static HKEY     (WINAPI *pSetupDiOpenClassRegKeyExA)(GUID*,REGSAM,DWORD,PCSTR,PVOID);
39
40 static void init_function_pointers(void)
41 {
42     hSetupAPI = LoadLibraryA("setupapi.dll");
43
44     if (hSetupAPI)
45     {
46         pSetupDiCreateDeviceInfoListExW = (void *)GetProcAddress(hSetupAPI, "SetupDiCreateDeviceInfoListExW");
47         pSetupDiDestroyDeviceInfoList = (void *)GetProcAddress(hSetupAPI, "SetupDiDestroyDeviceInfoList");
48         pSetupDiOpenClassRegKeyExA = (void *)GetProcAddress(hSetupAPI, "SetupDiOpenClassRegKeyExA");
49     }
50 }
51
52 static void test_SetupDiCreateDeviceInfoListEx(void) 
53 {
54     HDEVINFO devlist;
55     BOOL ret;
56     DWORD error;
57     static CHAR notnull[] = "NotNull";
58     static const WCHAR machine[] = { 'd','u','m','m','y',0 };
59
60     SetLastError(0xdeadbeef);
61     /* create empty DeviceInfoList, but set Reserved to a value, which is not NULL */
62     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, notnull);
63
64     error = GetLastError();
65     if (error == ERROR_CALL_NOT_IMPLEMENTED)
66     {
67         skip("SetupDiCreateDeviceInfoListExW is not implemented\n");
68         return;
69     }
70     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
71     ok(error == ERROR_INVALID_PARAMETER, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_PARAMETER);
72
73     SetLastError(0xdeadbeef);
74     /* create empty DeviceInfoList, but set MachineName to something */
75     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, machine, NULL);
76
77     error = GetLastError();
78     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
79     ok(error == ERROR_INVALID_MACHINENAME, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_MACHINENAME);
80
81     /* create empty DeviceInfoList */
82     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, NULL);
83     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
84
85     /* destroy DeviceInfoList */
86     ret = pSetupDiDestroyDeviceInfoList(devlist);
87     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
88 }
89
90 static void test_SetupDiOpenClassRegKeyExA(void)
91 {
92     /* This is a unique guid for testing purposes */
93     GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
94         0x00,0x11,0x95,0x5c,0x2b,0xdb}};
95     static const CHAR guidString[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
96     HKEY hkey;
97
98     /* Check return value for nonexistent key */
99     hkey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
100         DIOCR_INSTALLER, NULL, NULL);
101     ok(hkey == INVALID_HANDLE_VALUE,
102         "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey);
103
104     /* Test it for a key that exists */
105     hkey = SetupDiOpenClassRegKey(NULL, KEY_ALL_ACCESS);
106     if (hkey != INVALID_HANDLE_VALUE)
107     {
108         HKEY classKey;
109         if (RegCreateKeyA(hkey, guidString, &classKey) == ERROR_SUCCESS)
110         {
111             RegCloseKey(classKey);
112             SetLastError(0xdeadbeef);
113             classKey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
114                 DIOCR_INSTALLER, NULL, NULL);
115             ok(classKey != INVALID_HANDLE_VALUE,
116                 "opening class registry key failed with error %d\n",
117                 GetLastError());
118             if (classKey != INVALID_HANDLE_VALUE)
119                 RegCloseKey(classKey);
120             RegDeleteKeyA(hkey, guidString);
121         }
122         else
123             trace("failed to create registry key for test\n");
124     }
125     else
126         trace("failed to open classes key\n");
127 }
128
129 START_TEST(devinst)
130 {
131     init_function_pointers();
132     if (!hSetupAPI)
133         return;
134
135     if (pSetupDiCreateDeviceInfoListExW && pSetupDiDestroyDeviceInfoList)
136         test_SetupDiCreateDeviceInfoListEx();
137     else
138         trace("Needed calls for SetupDiCreateDeviceInfoListEx not all available, skipping test.\n");
139
140     if (pSetupDiOpenClassRegKeyExA)
141         test_SetupDiOpenClassRegKeyExA();
142     else
143         trace("Needed call for SetupDiOpenClassRegKeyExA not available, skipping test.\n");
144 }