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