setupapi/tests: Win64 printf format warning fixes.
[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     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
66     ok(error == ERROR_INVALID_PARAMETER, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_PARAMETER);
67
68     SetLastError(0xdeadbeef);
69     /* create empty DeviceInfoList, but set MachineName to something */
70     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, machine, NULL);
71
72     error = GetLastError();
73     ok(devlist == INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected %p)\n", devlist, error, INVALID_HANDLE_VALUE);
74     ok(error == ERROR_INVALID_MACHINENAME, "GetLastError returned wrong value : %d, (expected %d)\n", error, ERROR_INVALID_MACHINENAME);
75
76     /* create empty DeviceInfoList */
77     devlist = pSetupDiCreateDeviceInfoListExW(NULL, NULL, NULL, NULL);
78     ok(devlist && devlist != INVALID_HANDLE_VALUE, "SetupDiCreateDeviceInfoListExW failed : %p %d (expected != %p)\n", devlist, error, INVALID_HANDLE_VALUE);
79
80     /* destroy DeviceInfoList */
81     ret = pSetupDiDestroyDeviceInfoList(devlist);
82     ok(ret, "SetupDiDestroyDeviceInfoList failed : %d\n", error);
83 }
84
85 static void test_SetupDiOpenClassRegKeyExA(void)
86 {
87     /* This is a unique guid for testing purposes */
88     GUID guid = {0x6a55b5a4, 0x3f65, 0x11db, {0xb7,0x04,
89         0x00,0x11,0x95,0x5c,0x2b,0xdb}};
90     static const CHAR guidString[] = "{6a55b5a4-3f65-11db-b704-0011955c2bdb}";
91     HKEY hkey;
92
93     /* Check return value for nonexistent key */
94     hkey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
95         DIOCR_INSTALLER, NULL, NULL);
96     ok(hkey == INVALID_HANDLE_VALUE,
97         "returned %p (expected INVALID_HANDLE_VALUE)\n", hkey);
98
99     /* Test it for a key that exists */
100     hkey = SetupDiOpenClassRegKey(NULL, KEY_ALL_ACCESS);
101     if (hkey != INVALID_HANDLE_VALUE)
102     {
103         HKEY classKey;
104         if (RegCreateKeyA(hkey, guidString, &classKey) == ERROR_SUCCESS)
105         {
106             RegCloseKey(classKey);
107             SetLastError(0xdeadbeef);
108             classKey = pSetupDiOpenClassRegKeyExA(&guid, KEY_ALL_ACCESS,
109                 DIOCR_INSTALLER, NULL, NULL);
110             ok(classKey != INVALID_HANDLE_VALUE,
111                 "opening class registry key failed with error %d\n",
112                 GetLastError());
113             if (classKey != INVALID_HANDLE_VALUE)
114                 RegCloseKey(classKey);
115             RegDeleteKeyA(hkey, guidString);
116         }
117         else
118             trace("failed to create registry key for test\n");
119     }
120     else
121         trace("failed to open classes key\n");
122 }
123
124 START_TEST(devinst)
125 {
126     init_function_pointers();
127     if (!hSetupAPI)
128         return;
129
130     if (pSetupDiCreateDeviceInfoListExW && pSetupDiDestroyDeviceInfoList)
131         test_SetupDiCreateDeviceInfoListEx();
132     else
133         trace("Needed calls for SetupDiCreateDeviceInfoListEx not all available, skipping test.\n");
134
135     if (pSetupDiOpenClassRegKeyExA)
136         test_SetupDiOpenClassRegKeyExA();
137     else
138         trace("Needed call for SetupDiOpenClassRegKeyExA not available, skipping test.\n");
139 }