1 /*
2  * Copyright 2010 Maarten Lankhorst for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #define NONAMELESSUNION
20 #include "wine/test.h"
21 
22 #define COBJMACROS
23 
24 #ifdef STANDALONE
25 #include "initguid.h"
26 #endif
27 
28 #include "unknwn.h"
29 #include "uuids.h"
30 #include "mmdeviceapi.h"
31 #include "devpkey.h"
32 
33 static BOOL (WINAPI *pIsWow64Process)(HANDLE, BOOL *);
34 
35 static const WCHAR software_renderW[] =
36     { 'S','o','f','t','w','a','r','e','\\',
37       'M','i','c','r','o','s','o','f','t','\\',
38       'W','i','n','d','o','w','s','\\',
39       'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
40       'M','M','D','e','v','i','c','e','s','\\',
41       'A','u','d','i','o','\\',
42       'R','e','n','d','e','r',0 };
43 static const WCHAR propertiesW[] = {'P','r','o','p','e','r','t','i','e','s',0};
44 
45 
46 static void test_propertystore(IPropertyStore *store)
47 {
48     HRESULT hr;
49     PROPVARIANT pv;
50     char temp[128];
51     temp[sizeof(temp)-1] = 0;
52 
53     pv.vt = VT_EMPTY;
54     hr = IPropertyStore_GetValue(store, &PKEY_AudioEndpoint_GUID, &pv);
55     ok(hr == S_OK, "Failed with %08x\n", hr);
56     ok(pv.vt == VT_LPWSTR, "Value should be %i, is %i\n", VT_LPWSTR, pv.vt);
57     if (hr == S_OK && pv.vt == VT_LPWSTR)
58     {
59         WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1, temp, sizeof(temp)-1, NULL, NULL);
60         trace("guid: %s\n", temp);
61         CoTaskMemFree(pv.u.pwszVal);
62     }
63 
64     pv.vt = VT_EMPTY;
65     hr = IPropertyStore_GetValue(store, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
66     ok(hr == S_OK, "Failed with %08x\n", hr);
67     ok(pv.vt == VT_LPWSTR && pv.u.pwszVal, "FriendlyName value had wrong type: 0x%x or was NULL\n", pv.vt);
68 
69     pv.vt = VT_EMPTY;
70     hr = IPropertyStore_GetValue(store, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_Enabled, &pv);
71     ok(hr == S_OK, "Failed with %08x\n", hr);
72     ok(pv.vt == VT_EMPTY, "Key should not be found\n");
73 
74     pv.vt = VT_EMPTY;
75     hr = IPropertyStore_GetValue(store, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_ClassGuid, &pv);
76     ok(hr == S_OK, "Failed with %08x\n", hr);
77     ok(pv.vt == VT_EMPTY, "Key should not be found\n");
78 }
79 
80 static void test_deviceinterface(IPropertyStore *store)
81 {
82     HRESULT hr;
83     PROPVARIANT pv;
84 
85     static const PROPERTYKEY deviceinterface_key = {
86         {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
87     };
88 
89     pv.vt = VT_EMPTY;
90     hr = IPropertyStore_GetValue(store, &deviceinterface_key, &pv);
91     ok(hr == S_OK, "GetValue failed: %08x\n", hr);
92     ok(pv.vt == VT_LPWSTR, "Got wrong variant type: 0x%x\n", pv.vt);
93     trace("device interface: %s\n", wine_dbgstr_w(pv.u.pwszVal));
94     CoTaskMemFree(pv.u.pwszVal);
95 }
96 
97 static void test_getat(IPropertyStore *store)
98 {
99     HRESULT hr;
100     DWORD propcount;
101     DWORD prop;
102     PROPERTYKEY pkey;
103     BOOL found_name = FALSE;
104     BOOL found_desc = FALSE;
105     char temp[128];
106     temp[sizeof(temp)-1] = 0;
107 
108     hr = IPropertyStore_GetCount(store, &propcount);
109 
110     ok(hr == S_OK, "Failed with %08x\n", hr);
111     ok(propcount > 0, "Propcount %d should be greather than zero\n", propcount);
112 
113     for (prop = 0; prop < propcount; prop++) {
114 	hr = IPropertyStore_GetAt(store, prop, &pkey);
115 	ok(hr == S_OK, "Failed with %08x\n", hr);
116 	if (IsEqualPropertyKey(pkey, DEVPKEY_Device_FriendlyName))
117 	    found_name = TRUE;
118 	if (IsEqualPropertyKey(pkey, DEVPKEY_Device_DeviceDesc))
119 	    found_desc = TRUE;
120     }
121     ok(found_name ||
122             broken(!found_name) /* vista */, "DEVPKEY_Device_FriendlyName not found\n");
123     ok(found_desc, "DEVPKEY_Device_DeviceDesc not found\n");
124 }
125 
126 static void test_setvalue_on_wow64(IPropertyStore *store)
127 {
128     PROPVARIANT pv;
129     HRESULT hr;
130     LONG ret;
131     WCHAR *guidW;
132     HKEY root, props, devkey;
133     DWORD type, regval, size;
134 
135     static const PROPERTYKEY PKEY_Bogus = {
136         {0x1da5d803, 0xd492, 0x4edd, {0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x00}}, 0x7f
137     };
138     static const WCHAR bogusW[] = {'{','1','D','A','5','D','8','0','3','-','D','4','9','2','-','4','E','D','D','-','8','C','2','3','-','E','0','C','0','F','F','E','E','7','F','0','0','}',',','1','2','7',0};
139 
140     PropVariantInit(&pv);
141 
142     pv.vt = VT_EMPTY;
143     hr = IPropertyStore_GetValue(store, &PKEY_AudioEndpoint_GUID, &pv);
144     ok(hr == S_OK, "Failed to get Endpoint GUID: %08x\n", hr);
145 
146     guidW = pv.u.pwszVal;
147 
148     pv.vt = VT_UI4;
149     pv.u.ulVal = 0xAB;
150 
151     hr = IPropertyStore_SetValue(store, &PKEY_Bogus, &pv);
152     ok(hr == S_OK || hr == E_ACCESSDENIED, "SetValue failed: %08x\n", hr);
153     if (hr != S_OK)
154     {
155         win_skip("Missing permission to write to registry\n");
156         return;
157     }
158 
159     pv.u.ulVal = 0x00;
160 
161     hr = IPropertyStore_GetValue(store, &PKEY_Bogus, &pv);
162     ok(hr == S_OK, "GetValue failed: %08x\n", hr);
163     ok(pv.u.ulVal == 0xAB, "Got wrong value: 0x%x\n", pv.u.ulVal);
164 
165     /* should find the key in 64-bit view */
166     ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE, software_renderW, 0, KEY_READ|KEY_WOW64_64KEY, &root);
167     ok(ret == ERROR_SUCCESS, "Couldn't open mmdevices Render key: %u\n", ret);
168 
169     ret = RegOpenKeyExW(root, guidW, 0, KEY_READ|KEY_WOW64_64KEY, &devkey);
170     ok(ret == ERROR_SUCCESS, "Couldn't open mmdevice guid key: %u\n", ret);
171 
172     ret = RegOpenKeyExW(devkey, propertiesW, 0, KEY_READ|KEY_WOW64_64KEY, &props);
173     ok(ret == ERROR_SUCCESS, "Couldn't open mmdevice property key: %u\n", ret);
174 
175     /* Note: the registry key exists even without calling IPropStore::Commit */
176     size = sizeof(regval);
177     ret = RegQueryValueExW(props, bogusW, NULL, &type, (LPBYTE)&regval, &size);
178     ok(ret == ERROR_SUCCESS, "Couldn't get bogus propertykey value: %u\n", ret);
179     ok(type == REG_DWORD, "Got wrong value type: %u\n", type);
180     ok(regval == 0xAB, "Got wrong value: 0x%x\n", regval);
181 
182     RegCloseKey(props);
183     RegCloseKey(devkey);
184     RegCloseKey(root);
185 
186     CoTaskMemFree(guidW);
187 
188     /* should NOT find the key in 32-bit view */
189     ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE, software_renderW, 0, KEY_READ, &root);
190     ok(ret == ERROR_FILE_NOT_FOUND, "Wrong error when opening mmdevices Render key: %u\n", ret);
191 }
192 
193 START_TEST(propstore)
194 {
195     HRESULT hr;
196     IMMDeviceEnumerator *mme = NULL;
197     IMMDevice *dev = NULL;
198     IPropertyStore *store;
199     BOOL is_wow64 = FALSE;
200     HMODULE hk32 = GetModuleHandleA("kernel32.dll");
201 
202     pIsWow64Process = (void *)GetProcAddress(hk32, "IsWow64Process");
203 
204     if (pIsWow64Process)
205         pIsWow64Process(GetCurrentProcess(), &is_wow64);
206 
207     CoInitializeEx(NULL, COINIT_MULTITHREADED);
208     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
209     if (FAILED(hr))
210     {
211         skip("mmdevapi not available: 0x%08x\n", hr);
212         goto cleanup;
213     }
214 
215     hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(mme, eRender, eMultimedia, &dev);
216     ok(hr == S_OK || hr == E_NOTFOUND, "GetDefaultAudioEndpoint failed: 0x%08x\n", hr);
217     if (hr != S_OK)
218     {
219         if (hr == E_NOTFOUND)
220             skip("No sound card available\n");
221         else
222             skip("GetDefaultAudioEndpoint returns 0x%08x\n", hr);
223         goto cleanup;
224     }
225     store = NULL;
226     hr = IMMDevice_OpenPropertyStore(dev, 3, &store);
227     ok(hr == E_INVALIDARG, "Wrong hr returned: %08x\n", hr);
228     if (hr != S_OK)
229         /* It seems on windows returning with E_INVALIDARG doesn't
230          * set store to NULL, so just don't set store to non-null
231          * before calling this function
232          */
233         ok(!store, "Store set to non-NULL on failure: %p/%08x\n", store, hr);
234     else if (store)
235         IPropertyStore_Release(store);
236     hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, NULL);
237     ok(hr == E_POINTER, "Wrong hr returned: %08x\n", hr);
238 
239     store = NULL;
240     hr = IMMDevice_OpenPropertyStore(dev, STGM_READWRITE, &store);
241     if(hr == E_ACCESSDENIED)
242         hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &store);
243     ok(hr == S_OK, "Opening valid store returned %08x\n", hr);
244     if (store)
245     {
246         test_propertystore(store);
247         test_deviceinterface(store);
248         test_getat(store);
249         if (is_wow64)
250             test_setvalue_on_wow64(store);
251         IPropertyStore_Release(store);
252     }
253     IMMDevice_Release(dev);
254 cleanup:
255     if (mme)
256         IMMDeviceEnumerator_Release(mme);
257     CoUninitialize();
258 }
259