wintrust: Simplify error handling in SoftpubLoadSignature.
[wine] / dlls / mmdevapi / tests / mmdevenum.c
1 /*
2  * Copyright 2009 Maarten Lankhorst
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 #include "wine/test.h"
20
21 #define CINTERFACE
22 #define COBJMACROS
23
24 #include "initguid.h"
25 #include "mmdeviceapi.h"
26 #include "dshow.h"
27 #include "dsound.h"
28
29 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
30
31 /* Some of the QueryInterface tests are really just to check if I got the IID's right :) */
32
33 /* IMMDeviceCollection appears to have no QueryInterface method and instead forwards to mme */
34 static void test_collection(IMMDeviceEnumerator *mme, IMMDeviceCollection *col)
35 {
36     IMMDeviceCollection *col2;
37     IMMDeviceEnumerator *mme2;
38     IUnknown *unk;
39     HRESULT hr;
40     ULONG ref;
41     UINT numdev;
42     IMMDevice *dev;
43
44     /* collection doesn't keep a ref on parent */
45     IUnknown_AddRef(mme);
46     ref = IUnknown_Release(mme);
47     ok(ref == 2, "Reference count on parent is %u\n", ref);
48
49     ref = IUnknown_AddRef(col);
50     IUnknown_Release(col);
51     ok(ref == 2, "Invalid reference count %u on collection\n", ref);
52
53     hr = IUnknown_QueryInterface(col, &IID_IUnknown, NULL);
54     ok(hr == E_POINTER, "Null ppv returns %08x\n", hr);
55
56     hr = IUnknown_QueryInterface(col, &IID_IUnknown, (void**)&unk);
57     ok(hr == S_OK, "Cannot query for IID_IUnknown: 0x%08x\n", hr);
58     if (hr == S_OK)
59     {
60         ok((LONG_PTR)col == (LONG_PTR)unk, "Pointers are not identical %p/%p/%p\n", col, unk, mme);
61         IUnknown_Release(unk);
62     }
63
64     hr = IUnknown_QueryInterface(col, &IID_IMMDeviceCollection, (void**)&col2);
65     ok(hr == S_OK, "Cannot query for IID_IMMDeviceCollection: 0x%08x\n", hr);
66     if (hr == S_OK)
67         IUnknown_Release(col2);
68
69     hr = IUnknown_QueryInterface(col, &IID_IMMDeviceEnumerator, (void**)&mme2);
70     ok(hr == E_NOINTERFACE, "Query for IID_IMMDeviceEnumerator returned: 0x%08x\n", hr);
71     if (hr == S_OK)
72         IUnknown_Release(mme2);
73
74     hr = IMMDeviceCollection_GetCount(col, NULL);
75     ok(hr == E_POINTER, "GetCount returned 0x%08x\n", hr);
76
77     hr = IMMDeviceCollection_GetCount(col, &numdev);
78     ok(hr == S_OK, "GetCount returned 0x%08x\n", hr);
79
80     dev = (void*)(LONG_PTR)0x12345678;
81     hr = IMMDeviceCollection_Item(col, numdev, &dev);
82     ok(hr == E_INVALIDARG, "Asking for too high device returned 0x%08x\n", hr);
83     ok(dev == NULL, "Returned non-null device\n");
84
85     if (numdev)
86     {
87         hr = IMMDeviceCollection_Item(col, 0, NULL);
88         ok(hr == E_POINTER, "Query with null pointer returned 0x%08x\n", hr);
89
90         hr = IMMDeviceCollection_Item(col, 0, &dev);
91         ok(hr == S_OK, "Valid Item returned 0x%08x\n", hr);
92         ok(dev != NULL, "Device is null!\n");
93         if (dev != NULL)
94         {
95             char temp[128];
96             WCHAR *id = NULL;
97             if (IMMDevice_GetId(dev, &id) == S_OK)
98             {
99                 temp[sizeof(temp)-1] = 0;
100                 WideCharToMultiByte(CP_ACP, 0, id, -1, temp, sizeof(temp)-1, NULL, NULL);
101                 trace("Device found: %s\n", temp);
102                 CoTaskMemFree(id);
103             }
104         }
105         if (dev)
106             IUnknown_Release(dev);
107     }
108     IUnknown_Release(col);
109 }
110
111 /* Only do parameter tests here, the actual MMDevice testing should be a separate test */
112 START_TEST(mmdevenum)
113 {
114     HRESULT hr;
115     IUnknown *unk = NULL;
116     IMMDeviceEnumerator *mme, *mme2;
117     ULONG ref;
118     IMMDeviceCollection *col;
119
120     CoInitializeEx(NULL, COINIT_MULTITHREADED);
121     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
122     if (FAILED(hr))
123     {
124         skip("mmdevapi not available: 0x%08x\n", hr);
125         return;
126     }
127
128     /* Odd behavior.. bug? */
129     ref = IUnknown_AddRef(mme);
130     ok(ref == 3, "Invalid reference count after incrementing: %u\n", ref);
131     IUnknown_Release(mme);
132
133     hr = IUnknown_QueryInterface(mme, &IID_IUnknown, (void**)&unk);
134     ok(hr == S_OK, "returned 0x%08x\n", hr);
135     if (hr != S_OK) return;
136
137     ok( (LONG_PTR)mme == (LONG_PTR)unk, "Pointers are unequal %p/%p\n", unk, mme);
138     IUnknown_Release(unk);
139
140     /* Proving that it is static.. */
141     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme2);
142     IUnknown_Release(mme2);
143     ok(mme == mme2, "Pointers are not equal!\n");
144
145     hr = IUnknown_QueryInterface(mme, &IID_IUnknown, NULL);
146     ok(hr == E_POINTER, "Null pointer on QueryInterface returned %08x\n", hr);
147
148     hr = IUnknown_QueryInterface(mme, &GUID_NULL, (void**)&unk);
149     ok(!unk, "Unk not reset to null after invalid QI\n");
150     ok(hr == E_NOINTERFACE, "Invalid hr %08x returned on IID_NULL\n", hr);
151
152     col = (void*)(LONG_PTR)0x12345678;
153     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, 0xffff, DEVICE_STATEMASK_ALL, &col);
154     ok(hr == E_INVALIDARG, "Setting invalid data flow returned 0x%08x\n", hr);
155     ok(col == NULL, "Collection pointer non-null on failure\n");
156
157     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL+1, &col);
158     ok(hr == E_INVALIDARG, "Setting invalid mask returned 0x%08x\n", hr);
159
160     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, NULL);
161     ok(hr == E_POINTER, "Invalid pointer returned: 0x%08x\n", hr);
162
163     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, &col);
164     ok(hr == S_OK, "Valid EnumAudioEndpoints returned 0x%08x\n", hr);
165     if (hr == S_OK)
166     {
167         ok(!!col, "Returned null pointer\n");
168         if (col)
169             test_collection(mme, col);
170     }
171
172     IUnknown_Release(mme);
173 }