oleaut32: Do no check for dispatchable flag on dual interfaces.
[wine] / dlls / oledb32 / tests / marshal.c
1 /*
2  * OLE DB Marshaling Tests
3  *
4  * Copyright 2009 Robert Shearman
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 #define _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "initguid.h"
31 #include "objbase.h"
32 #include "oledb.h"
33
34 #include "wine/test.h"
35
36 #define RELEASEMARSHALDATA WM_USER
37
38 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
39
40 struct host_object_data
41 {
42     IStream *stream;
43     IID iid;
44     IUnknown *object;
45     MSHLFLAGS marshal_flags;
46     HANDLE marshal_event;
47     IMessageFilter *filter;
48 };
49
50 static DWORD CALLBACK host_object_proc(LPVOID p)
51 {
52     struct host_object_data *data = p;
53     HRESULT hr;
54     MSG msg;
55
56     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
57
58     if (data->filter)
59     {
60         IMessageFilter * prev_filter = NULL;
61         hr = CoRegisterMessageFilter(data->filter, &prev_filter);
62         if (prev_filter) IMessageFilter_Release(prev_filter);
63         ok_ole_success(hr, CoRegisterMessageFilter);
64     }
65
66     hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
67     ok_ole_success(hr, CoMarshalInterface);
68
69     /* force the message queue to be created before signaling parent thread */
70     PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
71
72     SetEvent(data->marshal_event);
73
74     while (GetMessage(&msg, NULL, 0, 0))
75     {
76         if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
77         {
78             CoReleaseMarshalData(data->stream);
79             SetEvent((HANDLE)msg.lParam);
80         }
81         else
82             DispatchMessage(&msg);
83     }
84
85     HeapFree(GetProcessHeap(), 0, data);
86
87     CoUninitialize();
88
89     return hr;
90 }
91
92 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
93 {
94     DWORD tid = 0;
95     HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
96     struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
97
98     data->stream = stream;
99     data->iid = *riid;
100     data->object = object;
101     data->marshal_flags = marshal_flags;
102     data->marshal_event = marshal_event;
103     data->filter = filter;
104
105     *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
106
107     /* wait for marshaling to complete before returning */
108     ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
109     CloseHandle(marshal_event);
110
111     return tid;
112 }
113
114 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
115 {
116     return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
117 }
118
119 static void end_host_object(DWORD tid, HANDLE thread)
120 {
121     BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
122     ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
123     /* be careful of races - don't return until hosting thread has terminated */
124     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
125     CloseHandle(thread);
126 }
127
128 #define TEST_PROPID 0xdead
129 static const WCHAR wszDBPropertyTestString[] = {'D','B','P','r','o','p','e','r','t','y','T','e','s','t','S','t','r','i','n','g',0};
130 static const WCHAR wszDBPropertyColumnName[] = {'C','o','l','u','m','n',0};
131
132 static HRESULT WINAPI Test_DBProperties_QueryInterface(
133     IDBProperties* iface,
134     REFIID riid,
135     void **ppvObject)
136 {
137     if (IsEqualIID(riid, &IID_IUnknown) ||
138         IsEqualIID(riid, &IID_IDBProperties))
139     {
140         *ppvObject = iface;
141         return S_OK;
142     }
143     *ppvObject = NULL;
144     return E_NOINTERFACE;
145 }
146
147 static ULONG WINAPI Test_DBProperties_AddRef(
148         IDBProperties* iface)
149 {
150     return 2;
151 }
152
153 static ULONG WINAPI Test_DBProperties_Release(
154         IDBProperties* iface)
155 {
156     return 1;
157 }
158
159     /*** IDBProperties methods ***/
160 static HRESULT WINAPI Test_DBProperties_GetProperties(
161         IDBProperties* iface,
162         ULONG cPropertyIDSets,
163         const DBPROPIDSET rgPropertyIDSets[],
164         ULONG *pcPropertySets,
165         DBPROPSET **prgPropertySets)
166 {
167     ok(cPropertyIDSets == 0, "Expected cPropertyIDSets to be 0 instead of %d\n", cPropertyIDSets);
168     ok(*pcPropertySets == 1, "Expected *pcPropertySets to be 1 instead of %d\n", *pcPropertySets);
169     prgPropertySets[0] = CoTaskMemAlloc(sizeof(DBPROPSET));
170     prgPropertySets[0]->rgProperties = CoTaskMemAlloc(sizeof(DBPROP));
171     prgPropertySets[0]->rgProperties[0].dwPropertyID = TEST_PROPID;
172     prgPropertySets[0]->rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
173     prgPropertySets[0]->rgProperties[0].dwStatus = S_OK;
174     prgPropertySets[0]->rgProperties[0].colid.eKind = DBKIND_GUID_NAME;
175     /* colid contents */
176     prgPropertySets[0]->rgProperties[0].colid.uGuid.guid = IID_IDBProperties;
177     prgPropertySets[0]->rgProperties[0].colid.uName.pwszName = CoTaskMemAlloc(sizeof(wszDBPropertyColumnName));
178     memcpy(prgPropertySets[0]->rgProperties[0].colid.uName.pwszName, wszDBPropertyColumnName, sizeof(wszDBPropertyColumnName));
179     /* vValue contents */
180     V_VT(&prgPropertySets[0]->rgProperties[0].vValue) = VT_BSTR;
181     V_BSTR(&prgPropertySets[0]->rgProperties[0].vValue) = SysAllocString(wszDBPropertyTestString);
182     prgPropertySets[0]->cProperties = 1;
183     prgPropertySets[0]->guidPropertySet = IID_IDBProperties;
184
185     return S_OK;
186 }
187
188 static HRESULT WINAPI Test_DBProperties_GetPropertyInfo(
189         IDBProperties* iface,
190         ULONG cPropertyIDSets,
191         const DBPROPIDSET rgPropertyIDSets[],
192         ULONG *pcPropertyInfoSets,
193         DBPROPINFOSET **prgPropertyInfoSets,
194         OLECHAR **ppDescBuffer)
195 {
196     return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI Test_DBProperties_SetProperties(
200         IDBProperties* iface,
201         ULONG cPropertySets,
202         DBPROPSET rgPropertySets[])
203 {
204     return E_NOTIMPL;
205 }
206
207 static const IDBPropertiesVtbl Test_DBProperties_Vtbl =
208 {
209     Test_DBProperties_QueryInterface,
210     Test_DBProperties_AddRef,
211     Test_DBProperties_Release,
212     Test_DBProperties_GetProperties,
213     Test_DBProperties_GetPropertyInfo,
214     Test_DBProperties_SetProperties,
215 };
216
217 static IDBProperties Test_DBProperties =
218 {
219     &Test_DBProperties_Vtbl
220 };
221
222 static void test_IDBProperties(void)
223 {
224     HRESULT hr;
225     IStream *pStream = NULL;
226     IDBProperties *pProxy = NULL;
227     DWORD tid;
228     HANDLE thread;
229     static const LARGE_INTEGER ullZero;
230     ULONG propset_count;
231     DBPROPSET *propsets = NULL;
232
233     hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
234     ok_ole_success(hr, "CreateStreamOnHGlobal");
235     tid = start_host_object(pStream, &IID_IDBProperties, (IUnknown*)&Test_DBProperties, MSHLFLAGS_NORMAL, &thread);
236
237     IStream_Seek(pStream, ullZero, STREAM_SEEK_SET, NULL);
238     hr = CoUnmarshalInterface(pStream, &IID_IDBProperties, (void **)&pProxy);
239     ok_ole_success(hr, "CoUnmarshalInterface");
240     IStream_Release(pStream);
241
242     propset_count = 1;
243     hr = IDBProperties_GetProperties(pProxy, 0, NULL, &propset_count, &propsets);
244     ok_ole_success(hr, "IDBProperties_GetProperties");
245
246     ok(propset_count == 1, "Expected propset_count of 1 but got %d\n", propset_count);
247     ok(propsets->rgProperties[0].dwPropertyID == TEST_PROPID, "Expected property ID of 0x%x, but got 0x%x\n", TEST_PROPID, propsets->rgProperties[0].dwPropertyID);
248     ok(propsets->rgProperties[0].dwOptions == DBPROPOPTIONS_REQUIRED, "Expected property options of 0x%x, but got 0x%x\n", DBPROPOPTIONS_REQUIRED, propsets->rgProperties[0].dwOptions);
249     ok(propsets->rgProperties[0].dwStatus == S_OK, "Expected property options of 0x%x, but got 0x%x\n", S_OK, propsets->rgProperties[0].dwStatus);
250     ok(propsets->rgProperties[0].colid.eKind == DBKIND_GUID_NAME, "Expected property colid kind of DBKIND_GUID_NAME, but got %d\n", propsets->rgProperties[0].colid.eKind);
251     /* colid contents */
252     ok(IsEqualGUID(&propsets->rgProperties[0].colid.uGuid.guid, &IID_IDBProperties), "Unexpected property colid guid\n");
253     ok(!lstrcmpW(propsets->rgProperties[0].colid.uName.pwszName, wszDBPropertyColumnName), "Unexpected property colid name\n");
254     /* vValue contents */
255     ok(V_VT(&propsets->rgProperties[0].vValue) == VT_BSTR, "Expected property value vt of VT_BSTR, but got %d\n", V_VT(&propsets->rgProperties[0].vValue));
256     ok(!lstrcmpW(V_BSTR(&propsets->rgProperties[0].vValue), wszDBPropertyTestString), "Unexpected property value string\n");
257     ok(propsets->cProperties == 1, "Expected property count of 1 but got %d\n", propsets->cProperties);
258     ok(IsEqualGUID(&propsets->guidPropertySet, &IID_IDBProperties), "Unexpected guid for property set\n");
259
260     IDBProperties_Release(pProxy);
261
262     end_host_object(tid, thread);
263 }
264
265 START_TEST(marshal)
266 {
267     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
268
269     test_IDBProperties();
270     CoUninitialize();
271 }