c9e6af98b98ebec6a85b26504832929283b4b3f8
[reactos.git] / modules / rostests / winetests / ole32 / defaulthandler.c
1 /*
2 * Default Handler Tests
3 *
4 * Copyright 2008 Huw Davies
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "precomp.h"
22
23 #define DEFINE_EXPECT(func) \
24 static BOOL expect_ ## func = FALSE, called_ ## func = FALSE
25
26 #define SET_EXPECT(func) \
27 expect_ ## func = TRUE
28
29 #define CHECK_EXPECT2(func) \
30 do { \
31 ok(expect_ ##func, "unexpected call " #func "\n"); \
32 called_ ## func = TRUE; \
33 }while(0)
34
35 #define CHECK_EXPECT(func) \
36 do { \
37 CHECK_EXPECT2(func); \
38 expect_ ## func = FALSE; \
39 }while(0)
40
41 #define CHECK_CALLED(func) \
42 do { \
43 ok(called_ ## func, "expected " #func "\n"); \
44 expect_ ## func = called_ ## func = FALSE; \
45 }while(0)
46
47 #define CHECK_NOT_CALLED(func) \
48 do { \
49 ok(!called_ ## func, "unexpected " #func "\n"); \
50 expect_ ## func = called_ ## func = FALSE; \
51 }while(0)
52
53 DEFINE_EXPECT(CF_QueryInterface_ClassFactory);
54 DEFINE_EXPECT(CF_CreateInstance);
55 DEFINE_EXPECT(CF_QueryInterface_IMarshal);
56
57 static HRESULT create_storage(IStorage **stg)
58 {
59 HRESULT hr;
60 ILockBytes *lock_bytes;
61
62 hr = CreateILockBytesOnHGlobal(NULL, TRUE, &lock_bytes);
63 if(SUCCEEDED(hr))
64 {
65 hr = StgCreateDocfileOnILockBytes(lock_bytes,
66 STGM_CREATE | STGM_SHARE_EXCLUSIVE | STGM_READWRITE, 0, stg);
67 ILockBytes_Release(lock_bytes);
68 }
69 return hr;
70 }
71
72 typedef struct
73 {
74 DWORD version;
75 DWORD flags;
76 DWORD link_update_opt;
77 DWORD res;
78 DWORD moniker_size;
79 } ole_stream_header_t;
80
81 static void test_olestream(void)
82 {
83 HRESULT hr;
84 const CLSID non_existent_class = {0xa5f1772f, 0x3772, 0x490f, {0x9e, 0xc6, 0x77, 0x13, 0xe8, 0xb3, 0x4b, 0x5d}};
85 IOleObject *ole_obj;
86 IPersistStorage *persist;
87 IStorage *stg;
88 IStream *stm;
89 static const WCHAR olestream[] = {1,'O','l','e',0};
90 ULONG read;
91 ole_stream_header_t header;
92
93 hr = create_storage(&stg);
94 ok(hr == S_OK, "got %08x\n", hr);
95
96 hr = IStorage_OpenStream(stg, olestream, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stm);
97 ok(hr == STG_E_FILENOTFOUND, "got %08x\n", hr);
98
99 hr = OleCreateDefaultHandler(&non_existent_class, 0, &IID_IOleObject, (void**)&ole_obj);
100 ok(hr == S_OK, "got %08x\n", hr);
101
102 hr = IOleObject_QueryInterface(ole_obj, &IID_IPersistStorage, (void**)&persist);
103 ok(hr == S_OK, "got %08x\n", hr);
104
105 hr = IPersistStorage_InitNew(persist, stg);
106 ok(hr == S_OK, "got %08x\n", hr);
107
108 hr = IStorage_OpenStream(stg, olestream, NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stm);
109 ok(hr == S_OK, "got %08x\n", hr);
110 hr = IStream_Read(stm, &header, sizeof(header), &read);
111 ok(hr == S_OK, "got %08x\n", hr);
112 ok(read == sizeof(header), "read %d\n", read);
113 ok(header.version == 0x02000001, "got version %08x\n", header.version);
114 ok(header.flags == 0x0, "got flags %08x\n", header.flags);
115 ok(header.link_update_opt == 0x0, "got link update option %08x\n", header.link_update_opt);
116 ok(header.res == 0x0, "got reserved %08x\n", header.res);
117 ok(header.moniker_size == 0x0, "got moniker size %08x\n", header.moniker_size);
118
119 IStream_Release(stm);
120
121 IPersistStorage_Release(persist);
122 IOleObject_Release(ole_obj);
123
124 IStorage_Release(stg);
125 }
126
127 static HRESULT WINAPI test_class_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
128 {
129 if(IsEqualGUID(riid, &IID_IUnknown)) {
130 *ppv = iface;
131 return S_OK;
132 }else if(IsEqualGUID(riid, &IID_IOleObject)) {
133 ok(0, "unexpected query for IOleObject interface\n");
134 *ppv = NULL;
135 return E_NOINTERFACE;
136 }
137
138 *ppv = NULL;
139 return E_NOINTERFACE;
140 }
141
142 static ULONG WINAPI test_class_AddRef(IUnknown *iface)
143 {
144 return 2;
145 }
146
147 static ULONG WINAPI test_class_Release(IUnknown *iface)
148 {
149 return 1;
150 }
151
152 static const IUnknownVtbl test_class_vtbl = {
153 test_class_QueryInterface,
154 test_class_AddRef,
155 test_class_Release,
156 };
157
158 static IUnknown test_class = { &test_class_vtbl };
159
160 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
161 {
162 if(IsEqualGUID(riid, &IID_IUnknown)) {
163 *ppv = iface;
164 return S_OK;
165 }else if(IsEqualGUID(riid, &IID_IMarshal)) {
166 CHECK_EXPECT(CF_QueryInterface_IMarshal);
167 *ppv = NULL;
168 return E_NOINTERFACE;
169 }else if(IsEqualGUID(riid, &IID_IClassFactory)) {
170 CHECK_EXPECT(CF_QueryInterface_ClassFactory);
171 *ppv = iface;
172 return S_OK;
173 }
174
175 ok(0, "unexpected interface: %s\n", wine_dbgstr_guid(riid));
176 *ppv = NULL;
177 return E_NOINTERFACE;
178 }
179
180 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
181 {
182 return 2;
183 }
184
185 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
186 {
187 return 1;
188 }
189
190 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface,
191 IUnknown *pUnkOuter, REFIID riid, void **ppv)
192 {
193 CHECK_EXPECT(CF_CreateInstance);
194
195 ok(pUnkOuter == NULL, "pUnkOuter != NULL\n");
196 todo_wine ok(IsEqualGUID(riid, &IID_IUnknown), "riid = %s\n", wine_dbgstr_guid(riid));
197 if(IsEqualGUID(riid, &IID_IOleObject)) {
198 *ppv = NULL;
199 return E_NOINTERFACE;
200 }
201
202 *ppv = &test_class;
203 return S_OK;
204 }
205
206 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
207 {
208 ok(0, "unexpected call\n");
209 return E_NOTIMPL;
210 }
211
212 static const IClassFactoryVtbl ClassFactoryVtbl = {
213 ClassFactory_QueryInterface,
214 ClassFactory_AddRef,
215 ClassFactory_Release,
216 ClassFactory_CreateInstance,
217 ClassFactory_LockServer
218 };
219
220 static IClassFactory ClassFactory = { &ClassFactoryVtbl };
221
222 static void test_default_handler_run(void)
223 {
224 const CLSID test_server_clsid = {0x0f77e570,0x80c3,0x11e2,{0x9e,0x96,0x08,0x00,0x20,0x0c,0x9a,0x66}};
225
226 IUnknown *unk;
227 IRunnableObject *ro;
228 IOleObject *oleobj;
229 IPersistStorage *persist;
230 DWORD class_reg;
231 HRESULT hres;
232
233 if(!GetProcAddress(GetModuleHandleA("ole32"), "CoRegisterSurrogateEx")) {
234 win_skip("skipping OleCreateDefaultHandler tests\n");
235 return;
236 }
237
238 hres = CoRegisterClassObject(&test_server_clsid, (IUnknown*)&ClassFactory,
239 CLSCTX_INPROC_SERVER, 0, &class_reg);
240 ok(hres == S_OK, "CoRegisterClassObject failed: %x\n", hres);
241
242 hres = OleCreateDefaultHandler(&test_server_clsid, NULL, &IID_IUnknown, (void**)&unk);
243 ok(hres == S_OK, "OleCreateDefaultHandler failed: %x\n", hres);
244
245 hres = IUnknown_QueryInterface(unk, &IID_IRunnableObject, (void**)&ro);
246 ok(hres == S_OK, "QueryInterface(IRunnableObject) failed: %x\n", hres);
247 IUnknown_Release(unk);
248
249 hres = IRunnableObject_Run(ro, NULL);
250 ok(hres == REGDB_E_CLASSNOTREG, "Run returned: %x, expected REGDB_E_CLASSNOTREG\n", hres);
251 IRunnableObject_Release(ro);
252
253 SET_EXPECT(CF_QueryInterface_IMarshal);
254 CoRevokeClassObject(class_reg);
255 todo_wine CHECK_CALLED(CF_QueryInterface_IMarshal);
256
257 hres = CoRegisterClassObject(&test_server_clsid, (IUnknown*)&ClassFactory,
258 CLSCTX_LOCAL_SERVER, 0, &class_reg);
259 ok(hres == S_OK, "CoRegisterClassObject failed: %x\n", hres);
260
261 hres = OleCreateDefaultHandler(&test_server_clsid, NULL, &IID_IUnknown, (void**)&unk);
262 ok(hres == S_OK, "OleCreateDefaultHandler failed: %x\n", hres);
263
264 hres = IUnknown_QueryInterface(unk, &IID_IOleObject, (void**)&oleobj);
265 ok(hres == S_OK, "QueryInterface(IID_IOleObject) failed: %x\n", hres);
266
267 hres = IOleObject_QueryInterface(oleobj, &IID_IPersistStorage, (void**)&persist);
268 ok(hres == S_OK, "QueryInterface(IID_IPersistStorage) failed: %x\n", hres);
269 IPersistStorage_Release(persist);
270 IOleObject_Release(oleobj);
271
272 hres = IUnknown_QueryInterface(unk, &IID_IRunnableObject, (void**)&ro);
273 ok(hres == S_OK, "QueryInterface(IRunnableObject) failed: %x\n", hres);
274 IUnknown_Release(unk);
275
276 SET_EXPECT(CF_QueryInterface_ClassFactory);
277 SET_EXPECT(CF_CreateInstance);
278 hres = IRunnableObject_Run(ro, NULL);
279 todo_wine
280 ok(hres == S_OK, "Run failed: %x\n", hres);
281 CHECK_CALLED(CF_QueryInterface_ClassFactory);
282 CHECK_CALLED(CF_CreateInstance);
283 IRunnableObject_Release(ro);
284
285 SET_EXPECT(CF_QueryInterface_ClassFactory);
286 SET_EXPECT(CF_CreateInstance);
287 hres = CoCreateInstance(&test_server_clsid, NULL, CLSCTX_LOCAL_SERVER,
288 &IID_IOleObject, (void**)&oleobj);
289 todo_wine
290 ok(hres == REGDB_E_CLASSNOTREG, "expected REGDB_E_CLASSNOTREG, got %x\n", hres);
291 todo_wine
292 CHECK_NOT_CALLED(CF_QueryInterface_ClassFactory);
293 todo_wine
294 CHECK_NOT_CALLED(CF_CreateInstance);
295
296 SET_EXPECT(CF_QueryInterface_IMarshal);
297 CoRevokeClassObject(class_reg);
298 todo_wine CHECK_CALLED(CF_QueryInterface_IMarshal);
299 }
300
301 START_TEST(defaulthandler)
302 {
303 OleInitialize(NULL);
304
305 test_olestream();
306 test_default_handler_run();
307
308 OleUninitialize();
309 }