oledb32: Implement IDataSourceLocator get/put hWnd.
[wine/wine-gecko.git] / dlls / oledb32 / tests / database.c
blob13637c15fd8f492baea57455508b38cf43607f45
1 /* OLEDB Database tests
3 * Copyright 2012 Alistair Leslie-Hughes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <stdio.h>
22 #define COBJMACROS
23 #define CONST_VTABLE
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "ole2.h"
30 #include "msdadc.h"
31 #include "msdasc.h"
32 #include "shlobj.h"
33 #include "msdaguid.h"
34 #include "initguid.h"
35 #include "oledberr.h"
37 #include "wine/test.h"
39 DEFINE_GUID(CSLID_MSDAER, 0xc8b522cf,0x5cf3,0x11ce,0xad,0xe5,0x00,0xaa,0x00,0x44,0x77,0x3d);
41 static WCHAR initstring_default[] = {'D','a','t','a',' ','S','o','u','r','c','e','=','d','u','m','m','y',';',0};
43 static const char *debugstr_guid(REFIID riid)
45 static char buf[50];
47 if(!riid)
48 return "(null)";
50 sprintf(buf, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
51 riid->Data1, riid->Data2, riid->Data3, riid->Data4[0],
52 riid->Data4[1], riid->Data4[2], riid->Data4[3], riid->Data4[4],
53 riid->Data4[5], riid->Data4[6], riid->Data4[7]);
55 return buf;
58 static void test_GetDataSource(WCHAR *initstring)
60 IDataInitialize *datainit = NULL;
61 IDBInitialize *dbinit = NULL;
62 HRESULT hr;
64 trace("Data Source: %s\n", wine_dbgstr_w(initstring));
66 hr = CoCreateInstance(&CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, &IID_IDataInitialize,(void**)&datainit);
67 ok(hr == S_OK, "got %08x\n", hr);
69 /* a failure to create data source here may indicate provider is simply not present */
70 hr = IDataInitialize_GetDataSource(datainit, NULL, CLSCTX_INPROC_SERVER, initstring, &IID_IDBInitialize, (IUnknown**)&dbinit);
71 if(SUCCEEDED(hr))
73 IDBProperties *props = NULL;
74 IMalloc *ppM = NULL;
76 hr = SHGetMalloc(&ppM);
77 if (FAILED(hr))
79 ok(0, "Couldn't get IMalloc object.\n");
80 goto end;
83 hr = IDBInitialize_QueryInterface(dbinit, &IID_IDBProperties, (void**)&props);
84 ok(hr == S_OK, "got %08x\n", hr);
85 if(SUCCEEDED(hr))
87 ULONG cnt;
88 DBPROPINFOSET *pInfoset;
89 OLECHAR *ary;
91 hr = IDBProperties_GetPropertyInfo(props, 0, NULL, &cnt, &pInfoset, &ary);
92 todo_wine ok(hr == S_OK, "got %08x\n", hr);
93 if(hr == S_OK)
95 ULONG i;
96 for(i =0; i < pInfoset->cPropertyInfos; i++)
98 trace("(0x%04x) '%s' %d\n", pInfoset->rgPropertyInfos[i].dwPropertyID, wine_dbgstr_w(pInfoset->rgPropertyInfos[i].pwszDescription),
99 pInfoset->rgPropertyInfos[i].vtType);
102 IMalloc_Free(ppM, ary);
105 IDBProperties_Release(props);
108 IMalloc_Release(ppM);
110 end:
111 IDBInitialize_Release(dbinit);
114 IDataInitialize_Release(datainit);
117 static void test_database(void)
119 static WCHAR initstring_jet[] = {'P','r','o','v','i','d','e','r','=','M','i','c','r','o','s','o','f','t','.',
120 'J','e','t','.','O','L','E','D','B','.','4','.','0',';',
121 'D','a','t','a',' ','S','o','u','r','c','e','=','d','u','m','m','y',';',
122 'P','e','r','s','i','s','t',' ','S','e','c','u','r','i','t','y',' ','I','n','f','o','=','F','a','l','s','e',';',0};
123 static WCHAR initstring_lower[] = {'d','a','t','a',' ','s','o','u','r','c','e','=','d','u','m','m','y',';',0};
124 IDataInitialize *datainit = NULL;
125 HRESULT hr;
127 hr = CoCreateInstance(&CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, &IID_IDataInitialize, (void**)&datainit);
128 if (FAILED(hr))
130 win_skip("Unable to load oledb library\n");
131 return;
133 IDataInitialize_Release(datainit);
135 test_GetDataSource(NULL);
136 test_GetDataSource(initstring_jet);
137 test_GetDataSource(initstring_default);
138 test_GetDataSource(initstring_lower);
141 static void test_errorinfo(void)
143 HRESULT hr;
144 IUnknown *unk = NULL;
146 hr = CoCreateInstance(&CSLID_MSDAER, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,(void**)&unk);
147 ok(hr == S_OK, "got %08x\n", hr);
148 if(hr == S_OK)
150 IErrorInfo *errorinfo;
151 IErrorRecords *errrecs;
153 hr = IUnknown_QueryInterface(unk, &IID_IErrorInfo, (void**)&errorinfo);
154 ok(hr == S_OK, "got %08x\n", hr);
155 if(hr == S_OK)
157 IErrorInfo_Release(errorinfo);
160 hr = IUnknown_QueryInterface(unk, &IID_IErrorRecords, (void**)&errrecs);
161 ok(hr == S_OK, "got %08x\n", hr);
162 if(hr == S_OK)
164 ERRORINFO info, info2, info3;
165 ULONG cnt = 0;
167 memset(&info, 0, sizeof(ERRORINFO));
168 info.dwMinor = 1;
169 memset(&info2, 0, sizeof(ERRORINFO));
170 info2.dwMinor = 2;
171 memset(&info3, 0, sizeof(ERRORINFO));
173 hr = IErrorRecords_AddErrorRecord(errrecs, NULL, 268435456, NULL, NULL, 0);
174 ok(hr == E_INVALIDARG, "got %08x\n", hr);
176 hr = IErrorRecords_AddErrorRecord(errrecs, &info, 1, NULL, NULL, 0);
177 ok(hr == S_OK, "got %08x\n", hr);
179 hr = IErrorRecords_GetRecordCount(errrecs, &cnt);
180 ok(hr == S_OK, "got %08x\n", hr);
181 ok(cnt == 1, "expected 1 got %d\n", cnt);
183 hr = IErrorRecords_AddErrorRecord(errrecs, &info2, 2, NULL, NULL, 0);
184 ok(hr == S_OK, "got %08x\n", hr);
186 hr = IErrorRecords_GetRecordCount(errrecs, &cnt);
187 ok(hr == S_OK, "got %08x\n", hr);
188 ok(cnt == 2, "expected 2 got %d\n", cnt);
190 hr = IErrorRecords_GetBasicErrorInfo(errrecs, 0, NULL);
191 ok(hr == E_INVALIDARG, "got %08x\n", hr);
193 hr = IErrorRecords_GetBasicErrorInfo(errrecs, 100, &info3);
194 ok(hr == DB_E_BADRECORDNUM, "got %08x\n", hr);
196 hr = IErrorRecords_GetBasicErrorInfo(errrecs, 0, &info3);
197 todo_wine ok(hr == S_OK, "got %08x\n", hr);
198 if(hr == S_OK)
200 ok(info3.dwMinor == 2, "expected 2 got %d\n", info3.dwMinor);
203 IErrorRecords_Release(errrecs);
206 IUnknown_Release(unk);
210 static void test_initializationstring(void)
212 static WCHAR initstring_msdasql[] = {'P','r','o','v','i','d','e','r','=','M','S','D','A','S','Q','L','.','1',';',
213 'D','a','t','a',' ','S','o','u','r','c','e','=','d','u','m','m','y', 0};
214 static WCHAR initstring_sqloledb[] = {'P','r','o','v','i','d','e','r','=','S','Q','L','O','L','E','D','B','.','1',';',
215 'D','a','t','a',' ','S','o','u','r','c','e','=','d','u','m','m','y', 0};
216 IDataInitialize *datainit = NULL;
217 IDBInitialize *dbinit = NULL;
218 HRESULT hr;
219 WCHAR *initstring = NULL;
221 hr = CoCreateInstance(&CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, &IID_IDataInitialize,(void**)&datainit);
222 ok(hr == S_OK, "got %08x\n", hr);
223 if(SUCCEEDED(hr))
225 hr = IDataInitialize_GetDataSource(datainit, NULL, CLSCTX_INPROC_SERVER, initstring_default,
226 &IID_IDBInitialize, (IUnknown**)&dbinit);
227 if(SUCCEEDED(hr))
229 hr = IDataInitialize_GetInitializationString(datainit, (IUnknown*)dbinit, 0, &initstring);
230 ok(hr == S_OK, "got %08x\n", hr);
231 if(hr == S_OK)
233 trace("Init String: %s\n", wine_dbgstr_w(initstring));
234 todo_wine ok(!lstrcmpW(initstring_msdasql, initstring) ||
235 !lstrcmpW(initstring_sqloledb, initstring), "got %s\n", wine_dbgstr_w(initstring));
236 CoTaskMemFree(initstring);
239 IDBInitialize_Release(dbinit);
242 IDataInitialize_Release(datainit);
246 static void test_rowposition(void)
248 IEnumConnectionPoints *enum_points;
249 IConnectionPointContainer *cpc;
250 IConnectionPoint *cp;
251 IRowPosition *rowpos;
252 HRESULT hr;
253 IID iid;
255 hr = CoCreateInstance(&CLSID_OLEDB_ROWPOSITIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, &IID_IRowPosition, (void**)&rowpos);
256 ok(hr == S_OK, "got %08x\n", hr);
258 hr = IRowPosition_QueryInterface(rowpos, &IID_IConnectionPointContainer, (void**)&cpc);
259 ok(hr == S_OK, "got 0x%08x\n", hr);
261 hr = IConnectionPointContainer_EnumConnectionPoints(cpc, &enum_points);
262 todo_wine
263 ok(hr == S_OK, "got 0x%08x\n", hr);
264 if (hr == S_OK) {
265 hr = IEnumConnectionPoints_Next(enum_points, 1, &cp, NULL);
266 ok(hr == S_OK, "got 0x%08x\n", hr);
267 hr = IConnectionPoint_GetConnectionInterface(cp, &iid);
268 ok(hr == S_OK, "got 0x%08x\n", hr);
269 ok(IsEqualIID(&iid, &IID_IRowPositionChange), "got %s\n", debugstr_guid(&iid));
270 IConnectionPoint_Release(cp);
272 hr = IEnumConnectionPoints_Next(enum_points, 1, &cp, NULL);
273 ok(hr == S_FALSE, "got 0x%08x\n", hr);
275 IEnumConnectionPoints_Release(enum_points);
277 IConnectionPointContainer_Release(cpc);
278 IRowPosition_Release(rowpos);
281 typedef struct
283 IRowset IRowset_iface;
284 IChapteredRowset IChapteredRowset_iface;
285 } test_rset_t;
287 static test_rset_t test_rset;
289 static HRESULT WINAPI rset_QI(IRowset *iface, REFIID riid, void **obj)
291 if (IsEqualIID(riid, &IID_IUnknown) ||
292 IsEqualIID(riid, &IID_IRowset))
294 *obj = &test_rset.IRowset_iface;
295 return S_OK;
297 else if (IsEqualIID(riid, &IID_IChapteredRowset))
299 *obj = &test_rset.IChapteredRowset_iface;
300 return S_OK;
303 ok(0, "unexpected riid %s\n", debugstr_guid(riid));
304 return E_NOINTERFACE;
307 static ULONG WINAPI rset_AddRef(IRowset *iface)
309 return 2;
312 static ULONG WINAPI rset_Release(IRowset *iface)
314 return 1;
317 static HRESULT WINAPI rset_AddRefRows(IRowset *iface, DBCOUNTITEM cRows,
318 const HROW rghRows[], DBREFCOUNT rgRefCounts[], DBROWSTATUS rgRowStatus[])
320 trace("AddRefRows: %ld\n", rghRows[0]);
321 return S_OK;
324 static HRESULT WINAPI rset_GetData(IRowset *iface, HROW hRow, HACCESSOR hAccessor, void *pData)
326 ok(0, "unexpected call\n");
327 return E_NOTIMPL;
330 static HRESULT WINAPI rset_GetNextRows(IRowset *iface, HCHAPTER hReserved, DBROWOFFSET lRowsOffset,
331 DBROWCOUNT cRows, DBCOUNTITEM *pcRowObtained, HROW **prghRows)
333 ok(0, "unexpected call\n");
334 return E_NOTIMPL;
337 static HRESULT WINAPI rset_ReleaseRows(IRowset *iface, DBCOUNTITEM cRows, const HROW rghRows[], DBROWOPTIONS rgRowOptions[],
338 DBREFCOUNT rgRefCounts[], DBROWSTATUS rgRowStatus[])
340 return S_OK;
343 static HRESULT WINAPI rset_RestartPosition(IRowset *iface, HCHAPTER hReserved)
345 ok(0, "unexpected call\n");
346 return E_NOTIMPL;
349 static const IRowsetVtbl rset_vtbl = {
350 rset_QI,
351 rset_AddRef,
352 rset_Release,
353 rset_AddRefRows,
354 rset_GetData,
355 rset_GetNextRows,
356 rset_ReleaseRows,
357 rset_RestartPosition
360 static HRESULT WINAPI chrset_QI(IChapteredRowset *iface, REFIID riid, void **obj)
362 return IRowset_QueryInterface(&test_rset.IRowset_iface, riid, obj);
365 static ULONG WINAPI chrset_AddRef(IChapteredRowset *iface)
367 return IRowset_AddRef(&test_rset.IRowset_iface);
370 static ULONG WINAPI chrset_Release(IChapteredRowset *iface)
372 return IRowset_Release(&test_rset.IRowset_iface);
375 static HRESULT WINAPI chrset_AddRefChapter(IChapteredRowset *iface, HCHAPTER chapter, DBREFCOUNT *refcount)
377 return S_OK;
380 static HRESULT WINAPI chrset_ReleaseChapter(IChapteredRowset *iface, HCHAPTER chapter, DBREFCOUNT *refcount)
382 return S_OK;
385 static const IChapteredRowsetVtbl chrset_vtbl = {
386 chrset_QI,
387 chrset_AddRef,
388 chrset_Release,
389 chrset_AddRefChapter,
390 chrset_ReleaseChapter
393 static void init_test_rset(void)
395 test_rset.IRowset_iface.lpVtbl = &rset_vtbl;
396 test_rset.IChapteredRowset_iface.lpVtbl = &chrset_vtbl;
399 static void test_rowpos_initialize(void)
401 IRowPosition *rowpos;
402 HRESULT hr;
404 hr = CoCreateInstance(&CLSID_OLEDB_ROWPOSITIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, &IID_IRowPosition, (void**)&rowpos);
405 ok(hr == S_OK, "got %08x\n", hr);
407 init_test_rset();
408 hr = IRowPosition_Initialize(rowpos, (IUnknown*)&test_rset.IRowset_iface);
409 ok(hr == S_OK, "got %08x\n", hr);
411 IRowPosition_Release(rowpos);
414 static HRESULT WINAPI onchange_QI(IRowPositionChange *iface, REFIID riid, void **obj)
416 if (IsEqualIID(riid, &IID_IUnknown) ||
417 IsEqualIID(riid, &IID_IRowPositionChange))
419 *obj = iface;
420 return S_OK;
423 return E_NOINTERFACE;
426 static ULONG WINAPI onchange_AddRef(IRowPositionChange *iface)
428 return 2;
431 static ULONG WINAPI onchange_Release(IRowPositionChange *iface)
433 return 1;
436 static HRESULT WINAPI onchange_OnRowPositionChange(IRowPositionChange *iface, DBREASON reason,
437 DBEVENTPHASE phase, BOOL cant_deny)
439 trace("%d %d %d\n", reason, phase, cant_deny);
440 return S_OK;
443 static const IRowPositionChangeVtbl onchange_vtbl = {
444 onchange_QI,
445 onchange_AddRef,
446 onchange_Release,
447 onchange_OnRowPositionChange
450 static IRowPositionChange onchangesink = { &onchange_vtbl };
452 static void init_onchange_sink(IRowPosition *rowpos)
454 IConnectionPointContainer *cpc;
455 IConnectionPoint *cp;
456 DWORD cookie;
457 HRESULT hr;
459 hr = IRowPosition_QueryInterface(rowpos, &IID_IConnectionPointContainer, (void**)&cpc);
460 ok(hr == S_OK, "got %08x\n", hr);
461 hr = IConnectionPointContainer_FindConnectionPoint(cpc, &IID_IRowPositionChange, &cp);
462 ok(hr == S_OK, "got %08x\n", hr);
463 hr = IConnectionPoint_Advise(cp, (IUnknown*)&onchangesink, &cookie);
464 ok(hr == S_OK, "got %08x\n", hr);
465 IConnectionPoint_Release(cp);
466 IConnectionPointContainer_Release(cpc);
469 static void test_rowpos_clearrowposition(void)
471 DBPOSITIONFLAGS flags;
472 IRowPosition *rowpos;
473 HCHAPTER chapter;
474 IUnknown *unk;
475 HRESULT hr;
476 HROW row;
478 hr = CoCreateInstance(&CLSID_OLEDB_ROWPOSITIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, &IID_IRowPosition, (void**)&rowpos);
479 ok(hr == S_OK, "got %08x\n", hr);
481 hr = IRowPosition_ClearRowPosition(rowpos);
482 ok(hr == E_UNEXPECTED, "got %08x\n", hr);
484 hr = IRowPosition_GetRowset(rowpos, &IID_IStream, &unk);
485 ok(hr == E_UNEXPECTED, "got %08x\n", hr);
487 chapter = 1;
488 row = 1;
489 flags = DBPOSITION_OK;
490 hr = IRowPosition_GetRowPosition(rowpos, &chapter, &row, &flags);
491 ok(hr == E_UNEXPECTED, "got %08x\n", hr);
492 ok(chapter == DB_NULL_HCHAPTER, "got %ld\n", chapter);
493 ok(row == DB_NULL_HROW, "got %ld\n", row);
494 ok(flags == DBPOSITION_NOROW, "got %d\n", flags);
496 init_test_rset();
497 hr = IRowPosition_Initialize(rowpos, (IUnknown*)&test_rset.IRowset_iface);
498 ok(hr == S_OK, "got %08x\n", hr);
500 chapter = 1;
501 row = 1;
502 flags = DBPOSITION_OK;
503 hr = IRowPosition_GetRowPosition(rowpos, &chapter, &row, &flags);
504 ok(hr == S_OK, "got %08x\n", hr);
505 ok(chapter == DB_NULL_HCHAPTER, "got %ld\n", chapter);
506 ok(row == DB_NULL_HROW, "got %ld\n", row);
507 ok(flags == DBPOSITION_NOROW, "got %d\n", flags);
509 hr = IRowPosition_GetRowset(rowpos, &IID_IRowset, &unk);
510 ok(hr == S_OK, "got %08x\n", hr);
512 init_onchange_sink(rowpos);
513 hr = IRowPosition_ClearRowPosition(rowpos);
514 ok(hr == S_OK, "got %08x\n", hr);
516 chapter = 1;
517 row = 1;
518 flags = DBPOSITION_OK;
519 hr = IRowPosition_GetRowPosition(rowpos, &chapter, &row, &flags);
520 ok(hr == S_OK, "got %08x\n", hr);
521 ok(chapter == DB_NULL_HCHAPTER, "got %ld\n", chapter);
522 ok(row == DB_NULL_HROW, "got %ld\n", row);
523 ok(flags == DBPOSITION_NOROW, "got %d\n", flags);
525 IRowPosition_Release(rowpos);
528 static void test_rowpos_setrowposition(void)
530 IRowPosition *rowpos;
531 HRESULT hr;
533 hr = CoCreateInstance(&CLSID_OLEDB_ROWPOSITIONLIBRARY, NULL, CLSCTX_INPROC_SERVER, &IID_IRowPosition, (void**)&rowpos);
534 ok(hr == S_OK, "got %08x\n", hr);
536 init_test_rset();
537 hr = IRowPosition_Initialize(rowpos, (IUnknown*)&test_rset.IRowset_iface);
538 ok(hr == S_OK, "got %08x\n", hr);
540 hr = IRowPosition_ClearRowPosition(rowpos);
541 ok(hr == S_OK, "got %08x\n", hr);
543 init_onchange_sink(rowpos);
544 hr = IRowPosition_SetRowPosition(rowpos, 0x123, 0x456, DBPOSITION_OK);
545 ok(hr == S_OK, "got %08x\n", hr);
547 IRowPosition_Release(rowpos);
550 static void test_dslocator(void)
552 IDataSourceLocator *dslocator = NULL;
553 HRESULT hr;
555 hr = CoCreateInstance(&CLSID_DataLinks, NULL, CLSCTX_INPROC_SERVER, &IID_IDataSourceLocator,(void**)&dslocator);
556 ok(hr == S_OK, "got %08x\n", hr);
557 if(SUCCEEDED(hr))
559 COMPATIBLE_LONG hwnd = 0;
561 /* Crashes under Window 7
562 hr = IDataSourceLocator_get_hWnd(dslocator, NULL);
563 ok(hr == E_INVALIDARG, "got %08x\n", hr);
566 hr = IDataSourceLocator_get_hWnd(dslocator, &hwnd);
567 ok(hr == S_OK, "got %08x\n", hr);
568 ok(hwnd == 0, "got %p\n", (HWND)hwnd);
570 hwnd = 0xDEADBEEF;
571 hr = IDataSourceLocator_get_hWnd(dslocator, &hwnd);
572 ok(hr == S_OK, "got %08x\n", hr);
573 ok(hwnd == 0, "got %p\n", (HWND)hwnd);
575 hwnd = 0xDEADBEEF;
576 hr = IDataSourceLocator_put_hWnd(dslocator, hwnd);
577 ok(hr == S_OK, "got %08x\n", hr);
579 hwnd = 0xDEADBEEF;
580 hr = IDataSourceLocator_get_hWnd(dslocator, &hwnd);
581 ok(hr == S_OK, "got %08x\n", hr);
582 ok(hwnd == 0xDEADBEEF, "got %p\n", (HWND)hwnd);
584 hwnd = 0;
585 hr = IDataSourceLocator_put_hWnd(dslocator, hwnd);
586 ok(hr == S_OK, "got %08x\n", hr);
588 hwnd = 0xDEADBEEF;
589 hr = IDataSourceLocator_get_hWnd(dslocator, &hwnd);
590 ok(hr == S_OK, "got %08x\n", hr);
591 ok(hwnd == 0, "got %p\n", (HWND)hwnd);
593 IDataSourceLocator_Release(dslocator);
597 START_TEST(database)
599 OleInitialize(NULL);
601 test_database();
602 test_errorinfo();
603 test_initializationstring();
604 test_dslocator();
606 /* row position */
607 test_rowposition();
608 test_rowpos_initialize();
609 test_rowpos_clearrowposition();
610 test_rowpos_setrowposition();
612 OleUninitialize();