Release 1.6-rc2.
[wine/testsucceed.git] / dlls / ole32 / tests / defaulthandler.c
blob67fc5e814dc6837170f8d20a57d9b285cf9de38f
1 /*
2 * Default Handler Tests
4 * Copyright 2008 Huw Davies
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 <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "objbase.h"
31 #include "wine/test.h"
33 #define DEFINE_EXPECT(func) \
34 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
36 #define SET_EXPECT(func) \
37 expect_ ## func = TRUE
39 #define CHECK_EXPECT2(func) \
40 do { \
41 ok(expect_ ##func, "unexpected call " #func "\n"); \
42 called_ ## func = TRUE; \
43 }while(0)
45 #define CHECK_EXPECT(func) \
46 do { \
47 CHECK_EXPECT2(func); \
48 expect_ ## func = FALSE; \
49 }while(0)
51 #define CHECK_CALLED(func) \
52 do { \
53 ok(called_ ## func, "expected " #func "\n"); \
54 expect_ ## func = called_ ## func = FALSE; \
55 }while(0)
57 #define CHECK_NOT_CALLED(func) \
58 do { \
59 ok(!called_ ## func, "unexpected " #func "\n"); \
60 expect_ ## func = called_ ## func = FALSE; \
61 }while(0)
63 DEFINE_EXPECT(CF_QueryInterface_ClassFactory);
64 DEFINE_EXPECT(CF_CreateInstance);
65 DEFINE_EXPECT(CF_QueryInterface_IMarshal);
67 static const char *debugstr_guid(REFIID riid)
69 static char buf[50];
71 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
72 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
73 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
74 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
76 return buf;
79 static HRESULT create_storage(IStorage **stg)
81 HRESULT hr;
82 ILockBytes *lock_bytes;
84 hr = CreateILockBytesOnHGlobal(NULL, TRUE, &lock_bytes);
85 if(SUCCEEDED(hr))
87 hr = StgCreateDocfileOnILockBytes(lock_bytes,
88 STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, stg);
89 ILockBytes_Release(lock_bytes);
91 return hr;
94 typedef struct
96 DWORD version;
97 DWORD flags;
98 DWORD link_update_opt;
99 DWORD res;
100 DWORD moniker_size;
101 } ole_stream_header_t;
103 static void test_olestream(void)
105 HRESULT hr;
106 const CLSID non_existent_class = {0xa5f1772f, 0x3772, 0x490f, {0x9e, 0xc6, 0x77, 0x13, 0xe8, 0xb3, 0x4b, 0x5d}};
107 IOleObject *ole_obj;
108 IPersistStorage *persist;
109 IStorage *stg;
110 IStream *stm;
111 static const WCHAR olestream[] = {1,'O','l','e',0};
112 ULONG read;
113 ole_stream_header_t header;
115 hr = create_storage(&stg);
116 ok(hr == S_OK, "got %08x\n", hr);
118 hr = IStorage_OpenStream(stg, olestream, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stm);
119 ok(hr == STG_E_FILENOTFOUND, "got %08x\n", hr);
121 hr = OleCreateDefaultHandler(&non_existent_class, 0, &IID_IOleObject, (void**)&ole_obj);
122 ok(hr == S_OK, "got %08x\n", hr);
124 hr = IOleObject_QueryInterface(ole_obj, &IID_IPersistStorage, (void**)&persist);
125 ok(hr == S_OK, "got %08x\n", hr);
127 hr = IPersistStorage_InitNew(persist, stg);
128 ok(hr == S_OK, "got %08x\n", hr);
130 hr = IStorage_OpenStream(stg, olestream, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stm);
131 ok(hr == S_OK, "got %08x\n", hr);
132 hr = IStream_Read(stm, &header, sizeof(header), &read);
133 ok(hr == S_OK, "got %08x\n", hr);
134 ok(read == sizeof(header), "read %d\n", read);
135 ok(header.version == 0x02000001, "got version %08x\n", header.version);
136 ok(header.flags == 0x0, "got flags %08x\n", header.flags);
137 ok(header.link_update_opt == 0x0, "got link update option %08x\n", header.link_update_opt);
138 ok(header.res == 0x0, "got reserved %08x\n", header.res);
139 ok(header.moniker_size == 0x0, "got moniker size %08x\n", header.moniker_size);
141 IStream_Release(stm);
143 IPersistStorage_Release(persist);
144 IOleObject_Release(ole_obj);
146 IStorage_Release(stg);
149 static HRESULT WINAPI test_class_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
151 if(IsEqualGUID(riid, &IID_IUnknown)) {
152 *ppv = iface;
153 return S_OK;
154 }else if(IsEqualGUID(riid, &IID_IOleObject)) {
155 ok(0, "unexpected query for IOleObject interface\n");
156 *ppv = NULL;
157 return E_NOINTERFACE;
160 *ppv = NULL;
161 return E_NOINTERFACE;
164 static ULONG WINAPI test_class_AddRef(IUnknown *iface)
166 return 2;
169 static ULONG WINAPI test_class_Release(IUnknown *iface)
171 return 1;
174 static const IUnknownVtbl test_class_vtbl = {
175 test_class_QueryInterface,
176 test_class_AddRef,
177 test_class_Release,
180 static IUnknown test_class = { &test_class_vtbl };
182 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
184 if(IsEqualGUID(riid, &IID_IUnknown)) {
185 *ppv = iface;
186 return S_OK;
187 }else if(IsEqualGUID(riid, &IID_IMarshal)) {
188 CHECK_EXPECT(CF_QueryInterface_IMarshal);
189 *ppv = NULL;
190 return E_NOINTERFACE;
191 }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
192 CHECK_EXPECT(CF_QueryInterface_ClassFactory);
193 *ppv = iface;
194 return S_OK;
197 ok(0, "unexpected interface: %s\n", debugstr_guid(riid));
198 *ppv = NULL;
199 return E_NOINTERFACE;
202 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
204 return 2;
207 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
209 return 1;
212 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface,
213 IUnknown *pUnkOuter, REFIID riid, void **ppv)
215 CHECK_EXPECT(CF_CreateInstance);
217 ok(pUnkOuter == NULL, "pUnkOuter != NULL\n");
218 todo_wine ok(IsEqualGUID(riid, &IID_IUnknown), "riid = %s\n", debugstr_guid(riid));
219 if(IsEqualGUID(riid, &IID_IOleObject)) {
220 *ppv = NULL;
221 return E_NOINTERFACE;
224 *ppv = &test_class;
225 return S_OK;
228 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
230 ok(0, "unexpected call\n");
231 return E_NOTIMPL;
234 static const IClassFactoryVtbl ClassFactoryVtbl = {
235 ClassFactory_QueryInterface,
236 ClassFactory_AddRef,
237 ClassFactory_Release,
238 ClassFactory_CreateInstance,
239 ClassFactory_LockServer
242 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
244 static void test_default_handler_run(void)
246 const CLSID test_server_clsid = {0x0f77e570,0x80c3,0x11e2,{0x9e,0x96,0x08,0x00,0x20,0x0c,0x9a,0x66}};
248 IUnknown *unk;
249 IRunnableObject *ro;
250 IOleObject *oleobj;
251 IPersistStorage *persist;
252 DWORD class_reg;
253 HRESULT hres;
255 if(!GetProcAddress(GetModuleHandle("ole32"), "CoRegisterSurrogateEx")) {
256 win_skip("skipping OleCreateDefaultHandler tests\n");
257 return;
260 hres = CoRegisterClassObject(&test_server_clsid, (IUnknown*)&ClassFactory,
261 CLSCTX_INPROC_SERVER, 0, &class_reg);
262 ok(hres == S_OK, "CoRegisterClassObject failed: %x\n", hres);
264 hres = OleCreateDefaultHandler(&test_server_clsid, NULL, &IID_IUnknown, (void**)&unk);
265 ok(hres == S_OK, "OleCreateDefaultHandler failed: %x\n", hres);
267 hres = IUnknown_QueryInterface(unk, &IID_IRunnableObject, (void**)&ro);
268 ok(hres == S_OK, "QueryInterface(IRunnableObject) failed: %x\n", hres);
269 IUnknown_Release(unk);
271 hres = IRunnableObject_Run(ro, NULL);
272 ok(hres == REGDB_E_CLASSNOTREG, "Run returned: %x, expected REGDB_E_CLASSNOTREG\n", hres);
273 IRunnableObject_Release(ro);
275 SET_EXPECT(CF_QueryInterface_IMarshal);
276 CoRevokeClassObject(class_reg);
277 todo_wine CHECK_CALLED(CF_QueryInterface_IMarshal);
279 hres = CoRegisterClassObject(&test_server_clsid, (IUnknown*)&ClassFactory,
280 CLSCTX_LOCAL_SERVER, 0, &class_reg);
281 ok(hres == S_OK, "CoRegisterClassObject failed: %x\n", hres);
283 hres = OleCreateDefaultHandler(&test_server_clsid, NULL, &IID_IUnknown, (void**)&unk);
284 ok(hres == S_OK, "OleCreateDefaultHandler failed: %x\n", hres);
286 hres = IUnknown_QueryInterface(unk, &IID_IOleObject, (void**)&oleobj);
287 ok(hres == S_OK, "QueryInterface(IID_IOleObject) failed: %x\n", hres);
289 hres = IOleObject_QueryInterface(oleobj, &IID_IPersistStorage, (void**)&persist);
290 ok(hres == S_OK, "QueryInterface(IID_IPersistStorage) failed: %x\n", hres);
291 IPersistStorage_Release(persist);
292 IOleObject_Release(oleobj);
294 hres = IUnknown_QueryInterface(unk, &IID_IRunnableObject, (void**)&ro);
295 ok(hres == S_OK, "QueryInterface(IRunnableObject) failed: %x\n", hres);
296 IUnknown_Release(unk);
298 SET_EXPECT(CF_QueryInterface_ClassFactory);
299 SET_EXPECT(CF_CreateInstance);
300 hres = IRunnableObject_Run(ro, NULL);
301 todo_wine
302 ok(hres == S_OK, "Run failed: %x\n", hres);
303 CHECK_CALLED(CF_QueryInterface_ClassFactory);
304 CHECK_CALLED(CF_CreateInstance);
305 IRunnableObject_Release(ro);
307 SET_EXPECT(CF_QueryInterface_ClassFactory);
308 SET_EXPECT(CF_CreateInstance);
309 hres = CoCreateInstance(&test_server_clsid, NULL, CLSCTX_LOCAL_SERVER,
310 &IID_IOleObject, (void**)&oleobj);
311 todo_wine
312 ok(hres == REGDB_E_CLASSNOTREG, "expected REGDB_E_CLASSNOTREG, got %x\n", hres);
313 todo_wine
314 CHECK_NOT_CALLED(CF_QueryInterface_ClassFactory);
315 todo_wine
316 CHECK_NOT_CALLED(CF_CreateInstance);
318 SET_EXPECT(CF_QueryInterface_IMarshal);
319 CoRevokeClassObject(class_reg);
320 todo_wine CHECK_CALLED(CF_QueryInterface_IMarshal);
323 START_TEST(defaulthandler)
325 OleInitialize(NULL);
327 test_olestream();
328 test_default_handler_run();
330 OleUninitialize();