oledb32: Add a table of init properties, support Persist Security Info switch in...
[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 "objbase.h"
31 #include "oledb.h"
32
33 #include "wine/test.h"
34
35 #define RELEASEMARSHALDATA WM_USER
36
37 #define ok_ole_success(hr, func) ok(hr == S_OK, #func " failed with error 0x%08x\n", hr)
38
39 struct host_object_data
40 {
41     IStream *stream;
42     IID iid;
43     IUnknown *object;
44     MSHLFLAGS marshal_flags;
45     HANDLE marshal_event;
46     IMessageFilter *filter;
47 };
48
49 static DWORD CALLBACK host_object_proc(LPVOID p)
50 {
51     struct host_object_data *data = p;
52     HRESULT hr;
53     MSG msg;
54
55     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
56
57     if (data->filter)
58     {
59         IMessageFilter * prev_filter = NULL;
60         hr = CoRegisterMessageFilter(data->filter, &prev_filter);
61         if (prev_filter) IMessageFilter_Release(prev_filter);
62         ok_ole_success(hr, CoRegisterMessageFilter);
63     }
64
65     hr = CoMarshalInterface(data->stream, &data->iid, data->object, MSHCTX_INPROC, NULL, data->marshal_flags);
66     ok_ole_success(hr, CoMarshalInterface);
67
68     /* force the message queue to be created before signaling parent thread */
69     PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
70
71     SetEvent(data->marshal_event);
72
73     while (GetMessage(&msg, NULL, 0, 0))
74     {
75         if (msg.hwnd == NULL && msg.message == RELEASEMARSHALDATA)
76         {
77             CoReleaseMarshalData(data->stream);
78             SetEvent((HANDLE)msg.lParam);
79         }
80         else
81             DispatchMessage(&msg);
82     }
83
84     HeapFree(GetProcessHeap(), 0, data);
85
86     CoUninitialize();
87
88     return hr;
89 }
90
91 static DWORD start_host_object2(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, IMessageFilter *filter, HANDLE *thread)
92 {
93     DWORD tid = 0;
94     HANDLE marshal_event = CreateEvent(NULL, FALSE, FALSE, NULL);
95     struct host_object_data *data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data));
96
97     data->stream = stream;
98     data->iid = *riid;
99     data->object = object;
100     data->marshal_flags = marshal_flags;
101     data->marshal_event = marshal_event;
102     data->filter = filter;
103
104     *thread = CreateThread(NULL, 0, host_object_proc, data, 0, &tid);
105
106     /* wait for marshaling to complete before returning */
107     ok( !WaitForSingleObject(marshal_event, 10000), "wait timed out\n" );
108     CloseHandle(marshal_event);
109
110     return tid;
111 }
112
113 static DWORD start_host_object(IStream *stream, REFIID riid, IUnknown *object, MSHLFLAGS marshal_flags, HANDLE *thread)
114 {
115     return start_host_object2(stream, riid, object, marshal_flags, NULL, thread);
116 }
117
118 static void end_host_object(DWORD tid, HANDLE thread)
119 {
120     BOOL ret = PostThreadMessage(tid, WM_QUIT, 0, 0);
121     ok(ret, "PostThreadMessage failed with error %d\n", GetLastError());
122     /* be careful of races - don't return until hosting thread has terminated */
123     ok( !WaitForSingleObject(thread, 10000), "wait timed out\n" );
124     CloseHandle(thread);
125 }
126
127 #define TEST_PROPID 0xdead
128 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};
129 static const WCHAR wszDBPropertyColumnName[] = {'C','o','l','u','m','n',0};
130
131 static HRESULT WINAPI Test_DBProperties_QueryInterface(
132     IDBProperties* iface,
133     REFIID riid,
134     void **ppvObject)
135 {
136     if (IsEqualIID(riid, &IID_IUnknown) ||
137         IsEqualIID(riid, &IID_IDBProperties))
138     {
139         *ppvObject = iface;
140         return S_OK;
141     }
142     *ppvObject = NULL;
143     return E_NOINTERFACE;
144 }
145
146 static ULONG WINAPI Test_DBProperties_AddRef(
147         IDBProperties* iface)
148 {
149     return 2;
150 }
151
152 static ULONG WINAPI Test_DBProperties_Release(
153         IDBProperties* iface)
154 {
155     return 1;
156 }
157
158     /*** IDBProperties methods ***/
159 static HRESULT WINAPI Test_DBProperties_GetProperties(
160         IDBProperties* iface,
161         ULONG cPropertyIDSets,
162         const DBPROPIDSET rgPropertyIDSets[],
163         ULONG *pcPropertySets,
164         DBPROPSET **prgPropertySets)
165 {
166     ok(cPropertyIDSets == 0, "Expected cPropertyIDSets to be 0 instead of %d\n", cPropertyIDSets);
167     ok(*pcPropertySets == 0, "Expected *pcPropertySets to be 0 instead of %d\n", *pcPropertySets);
168     *pcPropertySets = 1;
169     *prgPropertySets = 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(hr == S_OK, "IDBProperties_GetProperties failed with error 0x%08x\n", hr);
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 }