10467de2e37959fc06fd0d6860c13b934ed9aef9
[reactos.git] / rostests / apitests / com / com_apitest.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: COM interface test
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include "com_apitest.h"
9
10 #define NDEBUG
11 #include <debug.h>
12
13 #define myskip(c, ...) ((c) ? 0 : (skip(__VA_ARGS__), 1))
14 #define mytrace(...) do { \
15 int debug = winetest_debug; \
16 winetest_debug = 1; \
17 trace(__VA_ARGS__); \
18 winetest_debug = debug; \
19 } while (0)
20
21 typedef struct _KNOWN_INTERFACE
22 {
23 const IID *iid;
24 PCSTR name;
25 } KNOWN_INTERFACE;
26 typedef const KNOWN_INTERFACE *PCKNOWN_INTERFACE;
27
28 #undef ID_NAME
29 #define ID_NAME(c) { &c, #c }
30 static KNOWN_INTERFACE KnownInterfaces[] =
31 {
32 ID_NAME(IID_IACList),
33 ID_NAME(IID_IACList2),
34 ID_NAME(IID_IADesktopP2),
35 ID_NAME(IID_IAccessible),
36 ID_NAME(IID_IAccessControl),
37 ID_NAME(IID_IAccessor),
38 ID_NAME(IID_IActiveDesktop),
39 ID_NAME(IID_IActiveDesktopP),
40 ID_NAME(IID_IAddressBarParser),
41 ID_NAME(IID_IAddressBand),
42 ID_NAME(IID_IAddressEditBox),
43 ID_NAME(IID_IAutoComplete),
44 ID_NAME(IID_IAutoComplete2),
45 ID_NAME(IID_IBandNavigate),
46 ID_NAME(IID_IBandProxy),
47 ID_NAME(IID_IBandSite),
48 ID_NAME(IID_IBandSiteHelper),
49 ID_NAME(IID_IBanneredBar),
50 ID_NAME(IID_IBindCtx),
51 ID_NAME(IID_IBrowserFrameOptions),
52 ID_NAME(IID_IBrowserService),
53 ID_NAME(IID_IBrowserService2),
54 ID_NAME(IID_IBrowserService3),
55 ID_NAME(IID_IClassFactory),
56 ID_NAME(IID_IClassFactory2),
57 ID_NAME(IID_IClassFactory3),
58 ID_NAME(IID_IClientSecurity),
59 ID_NAME(IID_IComThreadingInfo),
60 ID_NAME(IID_IContext),
61 ID_NAME(IID_IContextMenu),
62 ID_NAME(IID_IContextMenu2),
63 ID_NAME(IID_IContextMenu3),
64 ID_NAME(IID_IContextMenuCB),
65 ID_NAME(IID_IContextMenuSite),
66 ID_NAME(IID_IContinue),
67 ID_NAME(IID_IDVGetEnum),
68 ID_NAME(IID_IDataObject),
69 ID_NAME(IID_IDefViewID),
70 ID_NAME(IID_IDefViewFrame),
71 ID_NAME(IID_IDefViewFrame3),
72 ID_NAME(IID_IDefViewFrameGroup),
73 ID_NAME(IID_IDefViewSafety),
74 ID_NAME(IID_IDefViewScript),
75 ID_NAME(IID_IDeskBand),
76 ID_NAME(IID_IDeskBandEx),
77 ID_NAME(IID_IDeskBar),
78 ID_NAME(IID_IDeskBarClient),
79 ID_NAME(IID_IDeskMovr),
80 ID_NAME(IID_IDispatch),
81 ID_NAME(IID_IDockingWindow),
82 ID_NAME(IID_IDockingWindowFrame),
83 ID_NAME(IID_IDockingWindowSite),
84 ID_NAME(IID_IDocViewSite),
85 ID_NAME(IID_IDragSourceHelper),
86 ID_NAME(IID_IDropSource),
87 ID_NAME(IID_IDropTarget),
88 ID_NAME(IID_IDropTargetHelper),
89 ID_NAME(IID_IEnumExtraSearch),
90 ID_NAME(IID_IEnumGUID),
91 ID_NAME(IID_IEnumIDList),
92 ID_NAME(IID_IEnumShellItems),
93 ID_NAME(IID_IEnumString),
94 ID_NAME(IID_IEnumUnknown),
95 ID_NAME(IID_IEnumVARIANT),
96 ID_NAME(IID_IErrorLog),
97 ID_NAME(IID_IExplorerToolbar),
98 ID_NAME(IID_IExtractIconA),
99 ID_NAME(IID_IExtractIconW),
100 ID_NAME(IID_IExtractImage),
101 ID_NAME(IID_IExtractImage2),
102 ID_NAME(IID_IFileDialog),
103 ID_NAME(IID_IFileDialog2),
104 ID_NAME(IID_IFileSearchBand),
105 ID_NAME(IID_IFolderBandPriv),
106 ID_NAME(IID_IFolderFilter),
107 ID_NAME(IID_IFolderFilterSite),
108 ID_NAME(IID_IFolderView),
109 ID_NAME(IID_IFolderView2),
110 ID_NAME(IID_IFolderViewOC),
111 ID_NAME(IID_IFolderViewSettings),
112 ID_NAME(IID_IGlobalFolderSettings),
113 ID_NAME(IID_IInitializeObject),
114 ID_NAME(IID_IInputObject),
115 ID_NAME(IID_IInputObjectSite),
116 ID_NAME(IID_IInternalUnknown),
117 ID_NAME(IID_IMarshal),
118 ID_NAME(IID_IMenuBand),
119 ID_NAME(IID_IMenuPopup),
120 ID_NAME(IID_IMoniker),
121 ID_NAME(IID_IMultiMonitorDockingSite),
122 ID_NAME(IID_IMultiQI),
123 ID_NAME(IID_INamespaceProxy),
124 ID_NAME(IID_INameSpaceTreeControl),
125 ID_NAME(IID_INSCTree),
126 ID_NAME(IID_INSCTree2),
127 ID_NAME(IID_IObjMgr),
128 ID_NAME(IID_IObjectSafety),
129 ID_NAME(IID_IObjectWithSite),
130 ID_NAME(IID_IOleCommandTarget),
131 ID_NAME(IID_IOleInPlaceActiveObject),
132 ID_NAME(IID_IOleInPlaceFrame),
133 ID_NAME(IID_IOleInPlaceObject),
134 ID_NAME(IID_IOleInPlaceObjectWindowless),
135 ID_NAME(IID_IOleInPlaceSite),
136 ID_NAME(IID_IOleInPlaceSiteEx),
137 ID_NAME(IID_IOleInPlaceSiteWindowless),
138 ID_NAME(IID_IOleInPlaceUIWindow),
139 ID_NAME(IID_IOleObject),
140 ID_NAME(IID_IOleWindow),
141 ID_NAME(IID_IPersist),
142 ID_NAME(IID_IPersistFile),
143 ID_NAME(IID_IPersistFolder),
144 ID_NAME(IID_IPersistFolder2),
145 ID_NAME(IID_IPersistFolder3),
146 ID_NAME(IID_IPersistHistory),
147 ID_NAME(IID_IPersistIDList),
148 ID_NAME(IID_IPersistMemory),
149 ID_NAME(IID_IPersistPropertyBag),
150 ID_NAME(IID_IPersistPropertyBag2),
151 ID_NAME(IID_IPersistStorage),
152 ID_NAME(IID_IPersistStream),
153 ID_NAME(IID_IPersistStreamInit),
154 ID_NAME(IID_IProgressDialog),
155 ID_NAME(IID_IPropertyBag),
156 ID_NAME(IID_IPropertyBag2),
157 ID_NAME(IID_IQueryAssociations),
158 ID_NAME(IID_IQueryInfo),
159 ID_NAME(IID_IRegTreeOptions),
160 ID_NAME(IID_IRunnableObject),
161 ID_NAME(IID_IServerSecurity),
162 ID_NAME(IID_IServiceProvider),
163 ID_NAME(IID_ISFHelper),
164 ID_NAME(IID_IShellBrowser),
165 ID_NAME(IID_IShellBrowserService),
166 ID_NAME(IID_IShellChangeNotify),
167 ID_NAME(IID_IShellDesktopTray),
168 ID_NAME(IID_IShellDispatch),
169 ID_NAME(IID_IShellDispatch2),
170 ID_NAME(IID_IShellDispatch3),
171 ID_NAME(IID_IShellDispatch4),
172 ID_NAME(IID_IShellDispatch5),
173 ID_NAME(IID_IShellExtInit),
174 ID_NAME(IID_IShellPropSheetExt),
175 ID_NAME(IID_IShellIconOverlayIdentifier),
176 ID_NAME(IID_IShellFolder),
177 ID_NAME(IID_IShellFolder2),
178 ID_NAME(IID_IShellFolderBand),
179 ID_NAME(IID_IShellFolderView),
180 ID_NAME(IID_IShellFolderViewCB),
181 ID_NAME(IID_IShellFolderViewDual),
182 ID_NAME(IID_IShellFolderViewDual2),
183 ID_NAME(IID_IShellIcon),
184 ID_NAME(IID_IShellItem),
185 ID_NAME(IID_IShellItem2),
186 ID_NAME(IID_IShellItemArray),
187 ID_NAME(IID_IShellItemFilter),
188 ID_NAME(IID_IShellLinkA),
189 ID_NAME(IID_IShellLinkDual),
190 ID_NAME(IID_IShellLinkDual2),
191 ID_NAME(IID_IShellLinkW),
192 ID_NAME(IID_IShellMenu),
193 ID_NAME(IID_IShellMenu2),
194 ID_NAME(IID_IShellMenuAcc),
195 ID_NAME(IID_IShellMenuCallback),
196 ID_NAME(IID_IShellNameSpace),
197 ID_NAME(IID_IShellService),
198 ID_NAME(IID_IShellView),
199 ID_NAME(IID_IShellView2),
200 ID_NAME(IID_IShellView3),
201 ID_NAME(IID_IShellWindows),
202 ID_NAME(IID_IStorage),
203 ID_NAME(IID_IStream),
204 ID_NAME(IID_ISurrogate),
205 ID_NAME(IID_ISynchronize),
206 ID_NAME(IID_ISynchronizeContainer),
207 ID_NAME(IID_ISynchronizeEvent),
208 ID_NAME(IID_ISynchronizeHandle),
209 ID_NAME(IID_ITaskbarList),
210 ID_NAME(IID_ITaskbarList2),
211 ID_NAME(IID_ITrackShellMenu),
212 ID_NAME(IID_ITrayPriv),
213 ID_NAME(IID_ITrayPriv2),
214 ID_NAME(IID_IUnknown),
215 ID_NAME(IID_IViewObject),
216 ID_NAME(IID_IViewObject2),
217 ID_NAME(IID_IViewObjectEx),
218 ID_NAME(IID_IWinEventHandler),
219
220 ID_NAME(IID_DFConstraint),
221 ID_NAME(DIID_DShellFolderViewEvents),
222
223 ID_NAME(IID_CDefView),
224 ID_NAME(IID_Folder),
225 ID_NAME(IID_Folder2),
226 ID_NAME(IID_Folder3),
227 ID_NAME(IID_FolderItem),
228 ID_NAME(IID_FolderItem2),
229 ID_NAME(IID_FolderItems),
230 ID_NAME(IID_FolderItems2),
231 ID_NAME(IID_FolderItems3),
232 ID_NAME(IID_FolderItemVerb),
233 ID_NAME(IID_FolderItemVerbs),
234
235 ID_NAME(CLSID_ShellDesktop)
236 };
237 static const INT KnownInterfaceCount = RTL_NUMBER_OF(KnownInterfaces);
238
239 static
240 PCKNOWN_INTERFACE
241 FindInterface(
242 _In_ const IID *iid)
243 {
244 INT i;
245
246 for (i = 0; i < KnownInterfaceCount; i++)
247 if (IsEqualIID(KnownInterfaces[i].iid, iid))
248 return &KnownInterfaces[i];
249 ASSERT(i != KnownInterfaceCount);
250 return NULL;
251 }
252
253 static
254 BOOLEAN
255 IsInterfaceExpected(
256 _In_ PCCLASS_AND_INTERFACES class,
257 _In_ const IID *iid)
258 {
259 INT i;
260
261 for (i = 0; class->ifaces[i].iid; i++)
262 if (IsEqualIID(class->ifaces[i].iid, iid))
263 return TRUE;
264 return FALSE;
265 }
266
267 #define INTF_NOT_EXPOSED LONG_MAX
268 static
269 LONG
270 GetInterfaceOffset(
271 _In_ PUNKNOWN pUnk,
272 _In_ const IID *iid)
273 {
274 HRESULT hr;
275 PVOID pObj;
276 PUNKNOWN pUnk2;
277 LONG offset;
278
279 hr = IUnknown_QueryInterface(pUnk, iid, &pObj);
280 ok(hr == S_OK || hr == E_NOINTERFACE, "IUnknown::QueryInterface returned 0x%lx\n", hr);
281 if (FAILED(hr))
282 return INTF_NOT_EXPOSED;
283
284 pUnk2 = pObj;
285 offset = (LONG_PTR)pObj - (LONG_PTR)pUnk;
286 IUnknown_Release(pUnk2);
287 return offset;
288 }
289
290 static
291 VOID
292 TestModuleInterfaces(
293 _In_ PCCLASS_AND_INTERFACES ExpectedInterfaces,
294 _In_ INT ExpectedInterfaceCount)
295 {
296 HRESULT hr;
297 PVOID pObj;
298 PUNKNOWN pUnk;
299 INT iClass, iIntf;
300 PCCLASS_AND_INTERFACES class;
301
302 for (iClass = 0; iClass < ExpectedInterfaceCount; iClass++)
303 {
304 class = &ExpectedInterfaces[iClass];
305 hr = CoCreateInstance(class->clsid,
306 NULL,
307 CLSCTX_INPROC_SERVER,
308 &IID_IUnknown,
309 &pObj);
310 ok(hr == S_OK, "CoCreateInstance failed. hr=0x%lx\n", hr);
311 if (FAILED(hr))
312 {
313 skip("Failed to instantiate %s.\n", class->name);
314 continue;
315 }
316
317 pUnk = pObj;
318
319 /* Check that all expected interfaces are present and have the right offset */
320 for (iIntf = 0; class->ifaces[iIntf].iid; iIntf++)
321 {
322 PCKNOWN_INTERFACE iface = FindInterface(class->ifaces[iIntf].iid);
323 LONG offset = GetInterfaceOffset(pUnk, iface->iid);
324 if (offset == INTF_NOT_EXPOSED)
325 ok(0, "%s is missing %s (offset %ld)\n", class->name, iface->name, class->ifaces[iIntf].offset);
326 else if (class->ifaces[iIntf].offset != FARAWY)
327 {
328 #ifdef FAIL_WRONG_OFFSET
329 ok(offset == class->ifaces[iIntf].offset, "%s, %s offset is %ld, expected %ld\n", class->name, iface->name, offset, class->ifaces[iIntf].offset);
330 #else
331 if (offset != class->ifaces[iIntf].offset)
332 mytrace("%s, %s offset is %ld, expected %ld\n", class->name, iface->name, offset, class->ifaces[iIntf].offset);
333 #endif
334 }
335 }
336
337 /* Check that none other than the expected interfaces are present */
338 for (iIntf = 0; iIntf < KnownInterfaceCount; iIntf++)
339 {
340 PCKNOWN_INTERFACE iface = &KnownInterfaces[iIntf];
341 LONG offset;
342 if (IsInterfaceExpected(class, iface->iid))
343 continue;
344 offset = GetInterfaceOffset(pUnk, iface->iid);
345 #ifdef GENERATE_TABLE_ENTRIES
346 ok(offset == INTF_NOT_EXPOSED, "%s: { %s%x, &%s },\n", class->name, offset < 0 ? "-" : "", offset < 0 ? -offset : offset, iface->name);
347 #else
348 ok(offset == INTF_NOT_EXPOSED, "%s exposes %s (offset %ld), but shouldn't\n", class->name, iface->name, offset);
349 #endif
350 }
351
352 // TODO: do some aggregation
353
354 IUnknown_Release(pUnk);
355 }
356 }
357
358 static
359 VOID
360 TestModuleRegistry(
361 _In_ PCWSTR ModuleName,
362 _In_ PCCLASS_AND_INTERFACES ExpectedInterfaces,
363 _In_ INT ExpectedInterfaceCount)
364 {
365 INT iClass;
366 PCCLASS_AND_INTERFACES class;
367 HKEY hKeyClasses;
368 LONG result;
369
370 result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID", 0, KEY_ENUMERATE_SUB_KEYS, &hKeyClasses);
371 ok(result == NO_ERROR, "Failed to open classes key, error %lu\n", result);
372 if (!myskip(result == NO_ERROR, "No classes key\n"))
373 {
374 for (iClass = 0; iClass < ExpectedInterfaceCount; iClass++)
375 {
376 HKEY hKey;
377 HKEY hKeyServer;
378 NTSTATUS status;
379 UNICODE_STRING clsid;
380 DWORD type;
381 WCHAR data[100];
382 DWORD dataSize;
383 PCWSTR expectedThreadingModel;
384
385 class = &ExpectedInterfaces[iClass];
386 status = RtlStringFromGUID(class->clsid, &clsid);
387 ok(status == STATUS_SUCCESS, "Failed to convert guid to string for %s, status %lx\n", class->name, status);
388 if (myskip(NT_SUCCESS(status), "No guid string\n"))
389 continue;
390
391 result = RegOpenKeyEx(hKeyClasses, clsid.Buffer, 0, KEY_ENUMERATE_SUB_KEYS, &hKey);
392 ok(result == NO_ERROR, "Failed to open key for %s, error %lu\n", class->name, result);
393 RtlFreeUnicodeString(&clsid);
394 if (myskip(result == NO_ERROR, "No key\n"))
395 continue;
396
397 result = RegOpenKeyEx(hKey, L"InProcServer32", 0, KEY_QUERY_VALUE, &hKeyServer);
398 ok(result == NO_ERROR, "Failed to open key for %s, error %lu\n", class->name, result);
399 RegCloseKey(hKey);
400 if (myskip(result == NO_ERROR, "No key\n"))
401 continue;
402
403 dataSize = sizeof(data);
404 result = RegQueryValueEx(hKeyServer, NULL, NULL, &type, (PBYTE)data, &dataSize);
405 ok(result == NO_ERROR, "Failed to query value for %s, error %lu\n", class->name, result);
406 if (!myskip(result == NO_ERROR, "No module name\n"))
407 {
408 ok(type == REG_SZ || type == REG_EXPAND_SZ, "type %lu for %s\n", type, class->name);
409 ok(dataSize % sizeof(WCHAR) == 0, "size %lu for %s\n", dataSize, class->name);
410 ok(dataSize <= sizeof(data), "size %lu for %s\n", dataSize, class->name);
411 ok(data[dataSize / sizeof(WCHAR) - 1] == UNICODE_NULL, "Not null terminated for %s\n", class->name);
412 // TODO: Use SearchPath (or assume everything's in system32) and do a proper full path compare
413 PathStripPathW(data);
414 PathRemoveExtensionW(data);
415 ok(!wcsicmp(data, ModuleName), "Server is %ls, expected %ls for %s\n", data, ModuleName, class->name);
416 }
417
418 dataSize = sizeof(data);
419 result = RegQueryValueEx(hKeyServer, L"ThreadingModel", NULL, &type, (PBYTE)data, &dataSize);
420 ok(result == NO_ERROR, "Failed to query value for %s, error %lu\n", class->name, result);
421 if (!myskip(result == NO_ERROR, "No ThreadingModel\n"))
422 {
423 ok(type == REG_SZ || type == REG_EXPAND_SZ, "type %lu for %s\n", type, class->name);
424 ok(dataSize % sizeof(WCHAR) == 0, "size %lu for %s\n", dataSize, class->name);
425 ok(dataSize <= sizeof(data), "size %lu for %s\n", dataSize, class->name);
426 ok(data[dataSize / sizeof(WCHAR) - 1] == UNICODE_NULL, "Not null terminated for %s\n", class->name);
427 expectedThreadingModel = class->ThreadingModel;
428 if (!expectedThreadingModel)
429 expectedThreadingModel = L"Apartment";
430 ok(!wcsicmp(data, expectedThreadingModel), "Server is %ls, expected %ls for %s\n", data, expectedThreadingModel, class->name);
431 }
432
433 RegCloseKey(hKeyServer);
434 }
435 RegCloseKey(hKeyClasses);
436 }
437 }
438
439 static
440 VOID
441 TestManualInstantiation(
442 _In_ PCWSTR ModuleName,
443 _In_ PCCLASS_AND_INTERFACES ExpectedInterfaces,
444 _In_ INT ExpectedInterfaceCount)
445 {
446 INT iClass;
447 PCCLASS_AND_INTERFACES class;
448 HRESULT (__stdcall *DllGetClassObject)(REFCLSID, REFIID, PVOID *);
449
450 DllGetClassObject = (PVOID)GetProcAddress(GetModuleHandle(ModuleName), "DllGetClassObject");
451 ok(DllGetClassObject != NULL, "DllGetClassObject not found in %ls, error %lu\n", ModuleName, GetLastError());
452 if (myskip(DllGetClassObject != NULL, "No DllGetClassObject\n"))
453 return;
454
455 for (iClass = 0; iClass < ExpectedInterfaceCount; iClass++)
456 {
457 PVOID pv;
458 HRESULT hr;
459 class = &ExpectedInterfaces[iClass];
460 hr = DllGetClassObject(class->clsid, &IID_IClassFactory, &pv);
461 ok(hr == S_OK, "DllGetClassObject failed for %s, hr = 0x%lx\n", class->name, hr);
462 if (!myskip(SUCCEEDED(hr), "No class factory\n"))
463 {
464 IClassFactory *pCF = pv;
465 hr = IClassFactory_CreateInstance(pCF, NULL, &IID_IUnknown, &pv);
466 ok(hr == S_OK, "IClassFactory::CreateInstance failed for %s, hr = 0x%lx\n", class->name, hr);
467 if (!myskip(SUCCEEDED(hr), "No instance\n"))
468 {
469 IUnknown *pUnk = pv;
470 IUnknown_Release(pUnk);
471 }
472 IClassFactory_Release(pCF);
473 }
474 }
475 }
476
477 VOID
478 TestClasses(
479 _In_ PCWSTR ModuleName,
480 _In_ PCCLASS_AND_INTERFACES ExpectedInterfaces,
481 _In_ INT ExpectedInterfaceCount)
482 {
483 HRESULT hr;
484
485 hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
486 ok(hr == S_OK, "CoInitializeEx failed. hr=0x%lx\n", hr);
487 if (myskip(SUCCEEDED(hr), "Failed to initialize COM. Cannot perform tests\n"))
488 return;
489
490 TestModuleInterfaces(ExpectedInterfaces, ExpectedInterfaceCount);
491 TestModuleRegistry(ModuleName, ExpectedInterfaces, ExpectedInterfaceCount);
492 TestManualInstantiation(ModuleName, ExpectedInterfaces, ExpectedInterfaceCount);
493
494 CoUninitialize();
495 }