Release 1.6-rc2.
[wine/testsucceed.git] / dlls / ole32 / tests / ole_server.c
blob9c901ee79cdf60ea89d6cf8c96d7f5fc537c8fd2
1 /*
2 * OLE client/server test suite
4 * Copyright 2013 Dmitry Timoshkov
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.
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.
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
21 #define COBJMACROS
22 #define CONST_VTABLE
24 #include <windows.h>
25 #include <exdisp.h>
26 #include <tlhelp32.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include "wine/test.h"
31 #include <initguid.h>
32 DEFINE_GUID(CLSID_WineTestObject, 0xdeadbeef,0xdead,0xbeef,0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef);
33 #ifndef CLSID_IdentityUnmarshal
34 DEFINE_GUID(CLSID_IdentityUnmarshal,0x0000001b,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
35 #endif
36 DEFINE_GUID(CLSID_UnknownUnmarshal,0x4c1e39e1,0xe3e3,0x4296,0xaa,0x86,0xec,0x93,0x8d,0x89,0x6e,0x92);
38 struct winetest_info
40 LONG child_failures;
43 static const struct
45 const GUID *guid;
46 const char *name;
47 } guid_name[] =
49 #define GUID_NAME(guid) \
50 { &IID_##guid, #guid }
51 GUID_NAME(IUnknown),
52 GUID_NAME(IClassFactory),
53 GUID_NAME(IOleObject),
54 GUID_NAME(IMarshal),
55 GUID_NAME(IStdMarshalInfo),
56 GUID_NAME(IExternalConnection),
57 GUID_NAME(IRunnableObject),
58 GUID_NAME(ICallFactory),
59 { &CLSID_IdentityUnmarshal, "CLSID_IdentityUnmarshal" },
60 { &CLSID_UnknownUnmarshal, "CLSID_UnknownUnmarshal" },
61 #undef GUID_NAME
64 static LONG obj_ref, class_ref, server_locks;
66 static const char *debugstr_guid(const GUID *guid)
68 static char buf[50];
69 int i;
71 if (!guid) return "(null)";
73 for (i = 0; i < sizeof(guid_name)/sizeof(guid_name[0]); i++)
75 if (IsEqualIID(guid, guid_name[i].guid))
76 return guid_name[i].name;
79 sprintf(buf, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
80 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
81 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
82 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
83 return buf;
86 /******************************* OLE server *******************************/
87 typedef struct
89 IUnknown IUnknown_iface;
90 LONG ref;
91 } UnknownImpl;
93 static inline UnknownImpl *impl_from_IUnknown(IUnknown *iface)
95 return CONTAINING_RECORD(iface, UnknownImpl, IUnknown_iface);
98 static HRESULT WINAPI UnknownImpl_QueryInterface(IUnknown *iface,
99 REFIID iid, void **ppv)
101 UnknownImpl *This = impl_from_IUnknown(iface);
103 trace("server: unknown_QueryInterface: %p,%s,%p\n", iface, debugstr_guid(iid), ppv);
105 if (!ppv) return E_INVALIDARG;
107 if (IsEqualIID(&IID_IUnknown, iid))
109 *ppv = &This->IUnknown_iface;
110 IUnknown_AddRef(&This->IUnknown_iface);
111 return S_OK;
114 *ppv = NULL;
115 return E_NOINTERFACE;
118 static ULONG WINAPI UnknownImpl_AddRef(IUnknown *iface)
120 UnknownImpl *This = impl_from_IUnknown(iface);
121 ULONG ref = InterlockedIncrement(&This->ref);
123 InterlockedIncrement(&obj_ref);
125 trace("server: unknown_AddRef: %p, ref %u\n", iface, ref);
126 return ref;
129 static ULONG WINAPI UnknownImpl_Release(IUnknown *iface)
131 UnknownImpl *This = impl_from_IUnknown(iface);
132 ULONG ref = InterlockedDecrement(&This->ref);
134 InterlockedDecrement(&obj_ref);
136 trace("server: unknown_Release: %p, ref %u\n", iface, ref);
137 if (ref == 0) HeapFree(GetProcessHeap(), 0, This);
138 return ref;
141 static const IUnknownVtbl UnknownImpl_Vtbl =
143 UnknownImpl_QueryInterface,
144 UnknownImpl_AddRef,
145 UnknownImpl_Release,
148 typedef struct
150 IClassFactory IClassFactory_iface;
151 LONG ref;
152 } ClassFactoryImpl;
154 static inline ClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
156 return CONTAINING_RECORD(iface, ClassFactoryImpl, IClassFactory_iface);
159 static HRESULT WINAPI ClassFactoryImpl_QueryInterface(IClassFactory *iface,
160 REFIID iid, void **ppv)
162 ClassFactoryImpl *This = impl_from_IClassFactory(iface);
164 trace("server: factory_QueryInterface: %p,%s,%p\n", iface, debugstr_guid(iid), ppv);
166 if (!ppv) return E_INVALIDARG;
168 if (IsEqualIID(&IID_IUnknown, iid) ||
169 IsEqualIID(&IID_IClassFactory, iid))
171 IClassFactory_AddRef(&This->IClassFactory_iface);
172 *ppv = &This->IClassFactory_iface;
173 return S_OK;
176 *ppv = NULL;
177 return E_NOINTERFACE;
180 static ULONG WINAPI ClassFactoryImpl_AddRef(IClassFactory *iface)
182 ClassFactoryImpl *This = impl_from_IClassFactory(iface);
183 ULONG ref = InterlockedIncrement(&This->ref);
185 InterlockedIncrement(&class_ref);
187 trace("server: factory_AddRef: %p, ref %u\n", iface, ref);
188 return ref;
191 static ULONG WINAPI ClassFactoryImpl_Release(IClassFactory *iface)
193 ClassFactoryImpl *This = impl_from_IClassFactory(iface);
194 ULONG ref = InterlockedDecrement(&This->ref);
196 InterlockedDecrement(&class_ref);
198 trace("server: factory_Release: %p, ref %u\n", iface, ref);
199 return ref;
202 static HRESULT WINAPI ClassFactoryImpl_CreateInstance(IClassFactory *iface,
203 IUnknown *punkouter, REFIID iid, void **ppv)
205 UnknownImpl *unknown;
206 HRESULT hr;
208 trace("server: factory_CreateInstance: %p,%s,%p\n", iface, debugstr_guid(iid), ppv);
210 if (punkouter) return CLASS_E_NOAGGREGATION;
212 unknown = HeapAlloc(GetProcessHeap(), 0, sizeof(*unknown));
213 if (!unknown) return E_OUTOFMEMORY;
215 unknown->IUnknown_iface.lpVtbl = &UnknownImpl_Vtbl;
216 unknown->ref = 0;
217 IUnknown_AddRef(&unknown->IUnknown_iface);
219 hr = IUnknown_QueryInterface(&unknown->IUnknown_iface, iid, ppv);
220 IUnknown_Release(&unknown->IUnknown_iface);
222 return hr;
225 static HRESULT WINAPI ClassFactoryImpl_LockServer(IClassFactory *iface, BOOL lock)
227 ULONG ref = lock ? InterlockedIncrement(&server_locks) : InterlockedDecrement(&server_locks);
229 trace("server: factory_LockServer: %p,%d, ref %u\n", iface, lock, ref);
230 return S_OK;
233 static const IClassFactoryVtbl ClassFactoryImpl_Vtbl =
235 ClassFactoryImpl_QueryInterface,
236 ClassFactoryImpl_AddRef,
237 ClassFactoryImpl_Release,
238 ClassFactoryImpl_CreateInstance,
239 ClassFactoryImpl_LockServer
242 static ClassFactoryImpl factory = { { &ClassFactoryImpl_Vtbl }, 0 };
244 static void ole_server(void)
246 HRESULT hr;
247 DWORD key;
249 trace("server: starting %u\n", GetCurrentProcessId());
251 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
252 if (hr == S_OK)
254 trace("server: registering class object\n");
255 hr = CoRegisterClassObject(&CLSID_WineTestObject, (IUnknown *)&factory,
256 CLSCTX_SERVER, REGCLS_MULTIPLEUSE, &key);
257 if (hr == S_OK)
259 HANDLE done_event, init_done_event;
261 done_event = OpenEvent(SYNCHRONIZE, FALSE, "ole_server_done_event");
262 ok(done_event != 0, "server: OpenEvent error %d\n", GetLastError());
263 init_done_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, "ole_server_init_done_event");
264 ok(init_done_event != 0, "server: OpenEvent error %d\n", GetLastError());
266 SetEvent(init_done_event);
268 trace("server: waiting for requests\n");
269 WaitForSingleObject(done_event, INFINITE);
271 /* 1 remainining class ref is supposed to be cleared by CoRevokeClassObject */
272 ok(class_ref == 1, "expected 1 class refs, got %d\n", class_ref);
273 ok(!obj_ref, "expected 0 object refs, got %d\n", obj_ref);
274 ok(!server_locks, "expected 0 server locks, got %d\n", server_locks);
276 CloseHandle(done_event);
277 CloseHandle(init_done_event);
278 if (0)
280 /* calling CoRevokeClassObject terminates process under Win7 */
281 trace("call CoRevokeClassObject\n");
282 CoRevokeClassObject(key);
283 trace("ret CoRevokeClassObject\n");
286 trace("server: call CoUninitialize\n");
287 CoUninitialize();
288 trace("server: ret CoUninitialize\n");
291 trace("server: exiting %u\n", GetCurrentProcessId());
294 /******************************* OLE client *******************************/
295 static BOOL register_server(const char *server, BOOL inproc_handler)
297 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
298 DWORD ret;
299 HKEY root;
300 WCHAR buf[39 + 6];
301 char server_path[MAX_PATH];
303 lstrcpy(server_path, server);
304 lstrcat(server_path, " ole_server");
306 lstrcpyW(buf, clsidW);
307 StringFromGUID2(&CLSID_WineTestObject, buf + 6, 39);
309 ret = RegCreateKeyExW(HKEY_CLASSES_ROOT, buf, 0, NULL, 0,
310 KEY_READ | KEY_WRITE | KEY_CREATE_SUB_KEY, NULL, &root, NULL);
311 if (ret == ERROR_SUCCESS)
313 ret = RegSetValue(root, "LocalServer32", REG_SZ, server_path, strlen(server_path));
314 ok(ret == ERROR_SUCCESS, "RegSetValue error %u\n", ret);
316 if (inproc_handler)
318 ret = RegSetValue(root, "InprocHandler32", REG_SZ, "ole32.dll", 9);
319 ok(ret == ERROR_SUCCESS, "RegSetValue error %u\n", ret);
322 RegCloseKey(root);
325 return ret == ERROR_SUCCESS;
328 static void unregister_server(void)
330 static const WCHAR clsidW[] = {'C','L','S','I','D','\\',0};
331 DWORD ret;
332 HKEY root;
333 WCHAR buf[39 + 6];
335 lstrcpyW(buf, clsidW);
336 StringFromGUID2(&CLSID_WineTestObject, buf + 6, 39);
338 ret = RegCreateKeyExW(HKEY_CLASSES_ROOT, buf, 0, NULL, 0,
339 DELETE, NULL, &root, NULL);
340 if (ret == ERROR_SUCCESS)
342 ret = RegDeleteKey(root, "InprocHandler32");
343 ok(ret == ERROR_SUCCESS, "RegDeleteKey error %u\n", ret);
344 ret = RegDeleteKey(root, "LocalServer32");
345 ok(ret == ERROR_SUCCESS, "RegDeleteKey error %u\n", ret);
346 ret = RegDeleteKey(root, "");
347 ok(ret == ERROR_SUCCESS, "RegDeleteKey error %u\n", ret);
348 RegCloseKey(root);
352 static HANDLE start_server(const char *argv0)
354 PROCESS_INFORMATION pi;
355 STARTUPINFO si;
356 SECURITY_ATTRIBUTES sa;
357 char cmdline[MAX_PATH * 2];
358 BOOL ret;
360 memset(&si, 0, sizeof(si));
361 si.cb = sizeof(si);
362 si.dwFlags = STARTF_USESTDHANDLES;
363 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
364 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
365 si.hStdError = si.hStdOutput;
367 sa.nLength = sizeof(sa);
368 sa.lpSecurityDescriptor = NULL;
369 sa.bInheritHandle = TRUE;
371 sprintf(cmdline, "\"%s\" ole_server -server", argv0);
372 ret = CreateProcess(argv0, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
373 ok(ret, "CreateProcess(%s) error %d\n", cmdline, GetLastError());
374 if (!ret) return 0;
376 CloseHandle(pi.hThread);
377 return pi.hProcess;
380 START_TEST(ole_server)
382 CLSID clsid = CLSID_WineTestObject;
383 HRESULT hr;
384 IClassFactory *factory;
385 IUnknown *unknown;
386 IOleObject *oleobj;
387 IRunnableObject *runobj;
388 DWORD ret;
389 HANDLE mapping, done_event, init_done_event, process;
390 struct winetest_info *info;
391 int argc;
392 char **argv;
394 mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, "winetest_ole_server");
395 ok(mapping != 0, "CreateFileMapping failed\n");
396 info = MapViewOfFile(mapping, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 4096);
398 argc = winetest_get_mainargs(&argv);
400 done_event = CreateEvent(NULL, TRUE, FALSE, "ole_server_done_event");
401 ok(done_event != 0, "CreateEvent error %d\n", GetLastError());
402 init_done_event = CreateEvent(NULL, TRUE, FALSE, "ole_server_init_done_event");
403 ok(init_done_event != 0, "CreateEvent error %d\n", GetLastError());
405 if (argc > 2)
407 if (!lstrcmpi(argv[2], "-Embedding"))
409 trace("server: Refusing to be run by ole32\n");
410 return;
413 if (!lstrcmpi(argv[2], "-server"))
415 info->child_failures = 0;
416 ole_server();
417 info->child_failures = winetest_get_failures();
418 return;
421 trace("server: Unknown parameter: %s\n", argv[2]);
422 return;
425 if (!register_server(argv[0], FALSE))
427 win_skip("not enough permissions to create a server CLSID key\n");
428 return;
431 if (!(process = start_server(argv[0])))
433 unregister_server();
434 return;
436 WaitForSingleObject(init_done_event, 5000);
438 hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
439 ok(hr == S_OK, "OleInitialize error %#x\n", hr);
441 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&unknown);
442 ok(hr == REGDB_E_CLASSNOTREG, "expected REGDB_E_CLASSNOTREG, got %#x\n", hr);
444 if (!register_server(argv[0], TRUE))
446 win_skip("not enough permissions to create a server CLSID key\n");
447 unregister_server();
448 return;
451 trace("call CoCreateInstance(&IID_NULL)\n");
452 hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_NULL, (void **)&unknown);
453 trace("ret CoCreateInstance(&IID_NULL)\n");
454 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
456 /* in-process handler supports IID_IUnknown starting from Vista */
457 trace("call CoCreateInstance(&IID_IUnknown)\n");
458 hr = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_HANDLER, &IID_IUnknown, (void **)&unknown);
459 trace("ret CoCreateInstance(&IID_IUnknown)\n");
460 ok(hr == S_OK || broken(hr == REGDB_E_CLASSNOTREG) /* XP,win2000 and earlier */, "CoCreateInstance(IID_IUnknown) error %#x\n", hr);
461 if (hr != S_OK)
463 win_skip("In-process handler doesn't support IID_IUnknown on this platform\n");
464 goto test_local_server;
467 trace("call CoCreateInstance(&IID_IOleObject)\n");
468 hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IOleObject, (void **)&oleobj);
469 trace("ret CoCreateInstance(&IID_IOleObject)\n");
470 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
472 trace("call IUnknown_QueryInterface(&IID_IRunnableObject)\n");
473 hr = IUnknown_QueryInterface(unknown, &IID_IRunnableObject, (void **)&runobj);
474 trace("ret IUnknown_QueryInterface(&IID_IRunnableObject)\n");
475 ok(hr == S_OK, "QueryInterface(&IID_IRunnableObject) error %#x\n", hr);
477 ret = IRunnableObject_IsRunning(runobj);
478 ok(!ret, "expected 0, got %d\n", ret);
480 trace("call OleRun\n");
481 hr = OleRun(unknown);
482 trace("ret OleRun\n");
483 todo_wine
484 ok(hr == S_OK, "OleRun error %#x\n", hr);
486 ret = IRunnableObject_IsRunning(runobj);
487 todo_wine
488 ok(ret == 1, "expected 1, got %d\n", ret);
490 trace("call IRunnableObject_Release\n");
491 ret = IRunnableObject_Release(runobj);
492 trace("ret IRunnableObject_Release\n");
493 ok(ret == 1, "expected ref 1, got %u\n", ret);
495 trace("call IUnknown_QueryInterface(&IID_IOleObject)\n");
496 hr = IUnknown_QueryInterface(unknown, &IID_IOleObject, (void **)&oleobj);
497 trace("ret IUnknown_QueryInterface(&IID_IOleObject)\n");
498 ok(hr == S_OK, "QueryInterface(&IID_IOleObject) error %#x\n", hr);
500 trace("call IOleObject_Release\n");
501 ret = IOleObject_Release(oleobj);
502 trace("ret IOleObject_Release\n");
503 ok(ret == 1, "expected ref 1, got %u\n", ret);
505 trace("call IUnknown_Release\n");
506 ret = IUnknown_Release(unknown);
507 trace("ret IUnknown_Release\n");
508 ok(!ret, "expected ref 0, got %u\n", ret);
510 test_local_server:
511 /* local server supports IID_IUnknown */
512 trace("call CoCreateInstance(&IID_IUnknown)\n");
513 hr = CoCreateInstance(&clsid, NULL, CLSCTX_LOCAL_SERVER, &IID_IUnknown, (void **)&unknown);
514 trace("ret CoCreateInstance(&IID_IUnknown)\n");
515 ok(hr == S_OK, "CoCreateInstance(IID_IUnknown) error %#x\n", hr);
517 trace("call IUnknown_QueryInterface(&IID_IRunnableObject)\n");
518 hr = IUnknown_QueryInterface(unknown, &IID_IRunnableObject, (void **)&runobj);
519 trace("ret IUnknown_QueryInterface(&IID_IRunnableObject)\n");
520 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
522 trace("call OleRun\n");
523 hr = OleRun(unknown);
524 trace("ret OleRun\n");
525 ok(hr == S_OK, "OleRun error %#x\n", hr);
527 trace("call IUnknown_QueryInterface(&IID_IOleObject)\n");
528 hr = IUnknown_QueryInterface(unknown, &IID_IOleObject, (void **)&oleobj);
529 trace("ret IUnknown_QueryInterface(&IID_IOleObject)\n");
530 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
532 trace("call IUnknown_Release\n");
533 ret = IUnknown_Release(unknown);
534 trace("ret IUnknown_Release\n");
535 ok(!ret, "expected ref 0, got %u\n", ret);
537 trace("call CoGetClassObject(&IID_IClassFactory)\n");
538 hr = CoGetClassObject(&clsid, CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void **)&factory);
539 trace("ret CoGetClassObject(&IID_IClassFactory)\n");
540 ok(hr == S_OK, "CoGetClassObject error %#x\n", hr);
542 trace("call IClassFactory_CreateInstance(&IID_NULL)\n");
543 hr = IClassFactory_CreateInstance(factory, NULL, &IID_NULL, (void **)&oleobj);
544 trace("ret IClassFactory_CreateInstance(&IID_NULL)\n");
545 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
547 trace("call IClassFactory_CreateInstance(&IID_IOleObject)\n");
548 hr = IClassFactory_CreateInstance(factory, NULL, &IID_IOleObject, (void **)&oleobj);
549 trace("ret IClassFactory_CreateInstance(&IID_IOleObject)\n");
550 ok(hr == E_NOINTERFACE, "expected E_NOINTERFACE, got %#x\n", hr);
552 trace("call IClassFactory_Release\n");
553 ret = IClassFactory_Release(factory);
554 trace("ret IClassFactory_Release\n");
555 ok(!ret, "expected ref 0, got %u\n", ret);
557 trace("signalling termination\n");
558 SetEvent(done_event);
559 ret = WaitForSingleObject(process, 10000);
560 ok(ret == WAIT_OBJECT_0, "server failed to terminate\n");
562 OleUninitialize();
564 unregister_server();
566 if (info->child_failures)
568 trace("%d failures in child process\n", info->child_failures);
569 winetest_add_failures(info->child_failures);