0c426c569a329a5f85891386626a6febaf41aaa4
[reactos.git] / modules / rostests / winetests / wbemdisp / wbemdisp.c
1 /*
2 * Copyright 2015 Hans Leidekker 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 COBJMACROS
20
21 #include <stdio.h>
22 #include "windows.h"
23 #include "initguid.h"
24 #include "objidl.h"
25 #include "wbemdisp.h"
26 #include "wbemcli.h"
27 #include "wine/test.h"
28
29 DEFINE_GUID(CLSID_WINMGMTS,0x172bddf8,0xceea,0x11d1,0x8b,0x05,0x00,0x60,0x08,0x06,0xd9,0xb6);
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 static const LCID english = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
33
34 static void test_ParseDisplayName(void)
35 {
36 static const WCHAR biosW[] = {'W','i','n','3','2','_','B','i','o','s',0};
37 static const WCHAR manufacturerW[] = {'M','a','n','u','f','a','c','t','u','r','e','r',0};
38 static const WCHAR versionW[] = {'v','e','r','s','i','o','n',0};
39 static const WCHAR nosuchW[] = {'N','o','S','u','c','h',0};
40 static const WCHAR name1[] =
41 {'w','i','n','m','g','m','t','s',':',0};
42 static const WCHAR name2[] =
43 {'w','i','n','m','g','m','t','s',':','\\','\\','.','\\','r','o','o','t','\\','c','i','m','v','2',0};
44 static const WCHAR name3[] =
45 {'w','i','n','m','g','m','t','s',':','\\','\\','.','\\','r','o','o','t','\\','c','i','m','v','2',':',
46 'W','i','n','3','2','_','L','o','g','i','c','a','l','D','i','s','k','.',
47 'D','e','v','i','c','e','I','D','=','\'','C',':','\'',0};
48 static const WCHAR name4[] =
49 {'w','i','n','m','g','m','t','s',':','\\','\\','.','\\','r','o','o','t','\\','c','i','m','v','2',':',
50 'W','i','n','3','2','_','S','e','r','v','i','c','e',0};
51 static const struct
52 {
53 const WCHAR *name;
54 HRESULT hr;
55 REFIID iid;
56 ULONG eaten;
57 } tests[] =
58 {
59 { name1, S_OK, &IID_ISWbemServices, sizeof(name1)/sizeof(name1[0]) - 1 },
60 { name2, S_OK, &IID_ISWbemServices, sizeof(name2)/sizeof(name2[0]) - 1 },
61 { name3, S_OK, &IID_ISWbemObject, sizeof(name3)/sizeof(name3[0]) - 1 },
62 { name4, S_OK, &IID_ISWbemObject, sizeof(name4)/sizeof(name4[0]) - 1 }
63 };
64 IParseDisplayName *displayname;
65 IBindCtx *ctx;
66 IMoniker *moniker;
67 IUnknown *obj;
68 BSTR str;
69 ULONG i, eaten, count;
70 HRESULT hr;
71
72 hr = CoCreateInstance( &CLSID_WINMGMTS, NULL, CLSCTX_INPROC_SERVER, &IID_IParseDisplayName, (void **)&displayname );
73 if (hr != S_OK)
74 {
75 win_skip( "can't create instance of WINMGMTS\n" );
76 return;
77 }
78
79 hr = CreateBindCtx( 0, &ctx );
80 ok( hr == S_OK, "got %x\n", hr );
81
82 for (i =0; i < sizeof(tests)/sizeof(tests[0]); i++)
83 {
84 str = SysAllocString( tests[i].name );
85 eaten = 0xdeadbeef;
86 moniker = NULL;
87 hr = IParseDisplayName_ParseDisplayName( displayname, NULL, str, &eaten, &moniker );
88 SysFreeString( str );
89 ok( hr == tests[i].hr, "%u: got %x\n", i, hr );
90 ok( eaten == tests[i].eaten, "%u: got %u\n", i, eaten );
91 if (moniker)
92 {
93 obj = NULL;
94 hr = IMoniker_BindToObject( moniker, ctx, NULL, tests[i].iid, (void **)&obj );
95 ok( hr == S_OK, "%u: got %x\n", i, hr );
96 if (obj) IUnknown_Release( obj );
97 IMoniker_Release( moniker );
98 }
99 }
100
101 str = SysAllocString( name1 );
102 eaten = 0xdeadbeef;
103 moniker = NULL;
104 hr = IParseDisplayName_ParseDisplayName( displayname, NULL, str, &eaten, &moniker );
105 SysFreeString( str );
106 ok( hr == S_OK, "got %x\n", hr );
107 ok( eaten == lstrlenW(name1), "got %u\n", eaten );
108 if (moniker)
109 {
110 ISWbemServices *services = NULL;
111
112 hr = IMoniker_BindToObject( moniker, ctx, NULL, &IID_IUnknown, (void **)&services );
113 ok( hr == S_OK, "got %x\n", hr );
114 if (services)
115 {
116 ISWbemObjectSet *objectset = NULL;
117
118 str = SysAllocString( biosW );
119 hr = ISWbemServices_InstancesOf( services, str, 0, NULL, &objectset );
120 SysFreeString( str );
121 ok( hr == S_OK, "got %x\n", hr );
122 if (objectset)
123 {
124 hr = ISWbemObjectSet_get__NewEnum( objectset, &obj );
125 ok( hr == S_OK, "got %x\n", hr );
126 if (obj)
127 {
128 IEnumVARIANT *enumvar = NULL;
129
130 hr = IUnknown_QueryInterface( obj, &IID_IEnumVARIANT, (void **)&enumvar );
131 ok( hr == S_OK, "got %x\n", hr );
132
133 if (enumvar)
134 {
135 VARIANT var, res;
136 ULONG fetched;
137 IDispatch *dispatch = NULL;
138 DISPID dispid;
139 DISPPARAMS params;
140 UINT arg_err;
141
142 fetched = 0xdeadbeef;
143 hr = IEnumVARIANT_Next( enumvar, 0, &var, &fetched );
144 ok( hr == S_OK, "got %x\n", hr );
145 ok( !fetched, "got %u\n", fetched );
146
147 fetched = 0xdeadbeef;
148 V_VT( &var ) = VT_ERROR;
149 V_ERROR( &var ) = 0xdeadbeef;
150 hr = IEnumVARIANT_Next( enumvar, 1, &var, &fetched );
151 ok( hr == S_OK, "got %x\n", hr );
152 ok( fetched == 1, "got %u\n", fetched );
153 ok( V_VT( &var ) == VT_DISPATCH, "got %u\n", V_VT( &var ) );
154 ok( V_DISPATCH( &var ) != (IDispatch *)0xdeadbeef, "got %u\n", V_VT( &var ) );
155
156 dispatch = V_DISPATCH( &var );
157 count = 0;
158 hr = IDispatch_GetTypeInfoCount( dispatch, &count );
159 ok( hr == S_OK, "got %x\n", hr );
160 ok( count == 1, "got %u\n", count );
161
162 str = SysAllocString( manufacturerW );
163 dispid = 0xdeadbeef;
164 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &str, 1, english, &dispid );
165 SysFreeString( str );
166 ok( hr == S_OK, "got %x\n", hr );
167 ok( dispid == 0x1800001 || dispid == 0x10b /* win2k */, "got %x\n", dispid );
168
169 str = SysAllocString( versionW );
170 dispid = 0xdeadbeef;
171 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &str, 1, english, &dispid );
172 SysFreeString( str );
173 ok( hr == S_OK, "got %x\n", hr );
174 ok( dispid == 0x1800002 || dispid == 0x119 /* win2k */, "got %x\n", dispid );
175
176 str = SysAllocString( nosuchW );
177 dispid = 0xdeadbeef;
178 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &str, 1, english, &dispid );
179 SysFreeString( str );
180 ok( hr == DISP_E_UNKNOWNNAME, "got %x\n", hr );
181 ok( dispid == DISPID_UNKNOWN, "got %x\n", dispid );
182
183 str = SysAllocString( manufacturerW );
184 dispid = 0xdeadbeef;
185 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &str, 1, english, &dispid );
186 SysFreeString( str );
187 ok( hr == S_OK, "got %x\n", hr );
188 ok( dispid == 0x1800001 || dispid == 0x10b /* win2k */, "got %x\n", dispid );
189
190 if (dispid == 0x1800001) /* crashes on win2k */
191 {
192 V_VT( &res ) = VT_ERROR;
193 V_BSTR( &res ) = (BSTR)0xdeadbeef;
194 params.rgvarg = (VARIANTARG *)0xdeadbeef;
195 params.rgdispidNamedArgs = (DISPID *)0xdeadbeef;
196 params.cArgs = params.cNamedArgs = 0xdeadbeef;
197 arg_err = 0xdeadbeef;
198 hr = IDispatch_Invoke( dispatch, DISPID_UNKNOWN, &IID_NULL, english,
199 DISPATCH_METHOD|DISPATCH_PROPERTYGET,
200 &params, &res, NULL, &arg_err );
201 ok( hr == DISP_E_MEMBERNOTFOUND || hr == S_OK /* winxp */, "got %x\n", hr );
202 ok( params.rgvarg == (VARIANTARG *)0xdeadbeef, "got %p\n", params.rgvarg );
203 ok( params.rgdispidNamedArgs == (DISPID *)0xdeadbeef, "got %p\n", params.rgdispidNamedArgs );
204 ok( params.cArgs == 0xdeadbeef, "got %u\n", params.cArgs );
205 ok( params.cNamedArgs == 0xdeadbeef, "got %u\n", params.cNamedArgs );
206 ok( V_VT( &res ) == VT_ERROR, "got %u\n", V_VT( &res ) );
207 ok( V_ERROR( &res ) == 0xdeadbeef, "got %u\n", V_VT( &res ) );
208 ok( arg_err == 0xdeadbeef, "got %u\n", arg_err );
209 if (hr == S_OK) VariantClear( &res );
210 }
211
212 V_VT( &res ) = VT_ERROR;
213 V_BSTR( &res ) = (BSTR)0xdeadbeef;
214 memset( &params, 0, sizeof(params) );
215 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, english,
216 DISPATCH_METHOD|DISPATCH_PROPERTYGET,
217 &params, &res, NULL, NULL );
218 ok( hr == S_OK, "got %x\n", hr );
219 ok( params.rgvarg == NULL, "got %p\n", params.rgvarg );
220 ok( params.rgdispidNamedArgs == NULL, "got %p\n", params.rgdispidNamedArgs );
221 ok( !params.cArgs, "got %u\n", params.cArgs );
222 ok( !params.cNamedArgs, "got %u\n", params.cNamedArgs );
223 ok( V_VT( &res ) == VT_BSTR, "got %u\n", V_VT( &res ) );
224 ok( V_BSTR( &res ) != (BSTR)0xdeadbeef, "got %u\n", V_VT( &res ) );
225 VariantClear( &res );
226 VariantClear( &var );
227
228 fetched = 0xdeadbeef;
229 hr = IEnumVARIANT_Next( enumvar, 1, &var, &fetched );
230 ok( hr == S_FALSE, "got %x\n", hr );
231 ok( !fetched, "got %u\n", fetched );
232
233 IEnumVARIANT_Release( enumvar );
234 }
235 IUnknown_Release( obj );
236 }
237 ISWbemObjectSet_Release( objectset );
238 }
239 IUnknown_Release( services );
240 }
241 IMoniker_Release( moniker );
242 }
243
244 IBindCtx_Release( ctx );
245 IParseDisplayName_Release( displayname );
246 }
247
248 static const WCHAR localhost[] = {'l','o','c','a','l','h','o','s','t',0};
249 static const WCHAR root[] = {'r','o','o','t','\\','C','I','M','V','2',0};
250 static const WCHAR query[] = {'S','e','l','e','c','t',' ','P','r','o','c','e','s','s','o','r','I','d',' ','f','r','o','m',
251 ' ','W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0};
252 static const WCHAR lang[] = {'W','Q','L',0};
253 static const WCHAR props[] = {'P','r','o','p','e','r','t','i','e','s','_',0};
254 static const WCHAR procid[] = {'P','r','o','c','e','s','s','o','r','I','d',0};
255
256 static void test_locator(void)
257 {
258 HRESULT hr;
259 DISPID id;
260 BSTR host_bstr, root_bstr, query_bstr, lang_bstr, props_bstr, procid_bstr;
261 ISWbemLocator *locator;
262 ISWbemServices *services;
263 ISWbemObjectSet *object_set;
264 IEnumVARIANT *enum_var;
265 ISWbemObject *object;
266 ISWbemPropertySet *prop_set;
267 ISWbemProperty *prop;
268 VARIANT var;
269
270 hr = CoCreateInstance( &CLSID_SWbemLocator, NULL, CLSCTX_INPROC_SERVER, &IID_ISWbemLocator, (void **)&locator );
271 ok( hr == S_OK, "got %x\n", hr );
272
273 host_bstr = SysAllocString(localhost);
274 root_bstr = SysAllocString(root);
275 hr = ISWbemLocator_ConnectServer( locator, host_bstr, root_bstr, NULL, NULL, NULL, NULL, 0, NULL, &services);
276 ok( hr == S_OK, "got %x\n", hr );
277 SysFreeString( root_bstr );
278 SysFreeString( host_bstr );
279
280 query_bstr = SysAllocString(query);
281 lang_bstr = SysAllocString(lang);
282 hr = ISWbemServices_ExecQuery( services, query_bstr, lang_bstr, wbemFlagForwardOnly, NULL, &object_set);
283 ok( hr == S_OK, "got %x\n", hr );
284 SysFreeString( lang_bstr );
285 SysFreeString( query_bstr );
286
287 hr = ISWbemObjectSet_get__NewEnum( object_set, (IUnknown**)&enum_var );
288 ok( hr == S_OK, "got %x\n", hr );
289
290 VariantInit( &var );
291 hr = IEnumVARIANT_Next( enum_var, 1, &var, NULL );
292 ok( hr == S_OK, "got %x\n", hr );
293 ok( V_VT(&var) == VT_DISPATCH, "got %x\n", V_VT(&var));
294
295 props_bstr = SysAllocString( props );
296 hr = IDispatch_GetIDsOfNames( V_DISPATCH(&var), &IID_NULL, &props_bstr, 1, english, &id );
297 ok( hr == S_OK, "got %x\n", hr );
298 ok( id == 21, "got %d\n", id );
299
300 hr = IDispatch_QueryInterface( V_DISPATCH(&var), &IID_ISWbemObject, (void**)&object );
301 ok( hr == S_OK, "got %x\n", hr );
302 VariantClear( &var );
303
304 hr = ISWbemObject_get_Properties_( object, &prop_set );
305 ok( hr == S_OK, "got %x\n", hr );
306
307 hr = ISWbemPropertySet_Item( prop_set, props_bstr, 0, &prop );
308 ok( hr == WBEM_E_NOT_FOUND, "got %x\n", hr );
309 SysFreeString( props_bstr );
310
311 procid_bstr = SysAllocString( procid );
312 hr = ISWbemPropertySet_Item( prop_set, procid_bstr, 0, &prop );
313 ok( hr == S_OK, "got %x\n", hr );
314 SysFreeString( procid_bstr );
315
316 hr = ISWbemProperty_get_Value( prop, &var );
317 ok( hr == S_OK, "got %x\n", hr );
318 ok( V_VT(&var) == VT_BSTR, "got %x\n", V_VT(&var) );
319 VariantClear( &var );
320
321 ISWbemProperty_Release( prop );
322 ISWbemPropertySet_Release( prop_set );
323 ISWbemObject_Release( object );
324 IEnumVARIANT_Release( enum_var );
325 ISWbemObjectSet_Release( object_set );
326 ISWbemServices_Release( services );
327 ISWbemLocator_Release( locator );
328 }
329
330 START_TEST(wbemdisp)
331 {
332 CoInitialize( NULL );
333
334 test_ParseDisplayName();
335 test_locator();
336
337 CoUninitialize();
338 }