f601e62f9c7495892bcb3c0ab81fc71ce9f5955b
[reactos.git] / rostests / winetests / ole32 / propvariant.c
1 /*
2 * PropVariant Tests
3 *
4 * Copyright 2004 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 //#include "windows.h"
22
23 #define WIN32_NO_STATUS
24 #define _INC_WINDOWS
25 #define COM_NO_WINDOWS_H
26
27 #include <stdarg.h>
28
29 #include <windef.h>
30 #include <winbase.h>
31 #include <winnls.h>
32 #include <ddeml.h>
33 #include <ole2.h>
34
35 #include <wine/test.h>
36
37 /* invalid in all versions */
38 #define PROP_INV 0x7f
39 /* valid in v0 and above (NT4+) */
40 #define PROP_V0 0
41 /* valid in v1 and above (Win2k+) */
42 #define PROP_V1 1
43 /* valid in v1a and above (WinXP+) */
44 #define PROP_V1A 2
45 #define PROP_TODO 0x80
46
47 static const struct valid_mapping
48 {
49 BYTE simple;
50 BYTE with_array;
51 BYTE with_vector;
52 BYTE byref;
53 } valid_types[] =
54 {
55 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_EMPTY */
56 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_NULL */
57 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_I2 */
58 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_I4 */
59 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_R4 */
60 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_R8 */
61 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_CY */
62 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_DATE */
63 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_BSTR */
64 { PROP_V1 | PROP_TODO , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_DISPATCH */
65 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_ERROR */
66 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_BOOL */
67 { PROP_V1 | PROP_TODO , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_VARIANT */
68 { PROP_V1 | PROP_TODO , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_UNKNOWN */
69 { PROP_V1 , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_DECIMAL */
70 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 15 */
71 { PROP_V1 , PROP_V1 | PROP_TODO , PROP_V1 , PROP_V1 | PROP_TODO }, /* VT_I1 */
72 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_UI1 */
73 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_UI2 */
74 { PROP_V0 , PROP_V1 | PROP_TODO , PROP_V0 , PROP_V1 | PROP_TODO }, /* VT_UI4 */
75 { PROP_V0 , PROP_V1A | PROP_TODO, PROP_V0 , PROP_V1A | PROP_TODO }, /* VT_I8 */
76 { PROP_V0 , PROP_V1A | PROP_TODO, PROP_V0 , PROP_V1A | PROP_TODO }, /* VT_UI8 */
77 { PROP_V1 , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_INT */
78 { PROP_V1 , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_UINT */
79 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_VOID */
80 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_HRESULT */
81 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_PTR */
82 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_SAFEARRAY */
83 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_CARRAY */
84 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_USERDEFINED */
85 { PROP_V0 , PROP_INV, PROP_V0 , PROP_INV }, /* VT_LPSTR */
86 { PROP_V0 , PROP_INV, PROP_V0 , PROP_INV }, /* VT_LPWSTR */
87 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 32 */
88 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 33 */
89 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 34 */
90 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 35 */
91 { PROP_V1 | PROP_TODO , PROP_V1 | PROP_TODO , PROP_INV, PROP_V1 | PROP_TODO }, /* VT_RECORD */
92 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_INT_PTR */
93 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* VT_UINT_PTR */
94 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 39 */
95 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 40 */
96 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 41 */
97 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 42 */
98 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 43 */
99 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 44 */
100 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 45 */
101 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 46 */
102 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 47 */
103 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 48 */
104 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 49 */
105 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 50 */
106 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 51 */
107 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 52 */
108 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 53 */
109 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 54 */
110 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 55 */
111 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 56 */
112 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 57 */
113 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 58 */
114 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 59 */
115 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 60 */
116 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 61 */
117 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 62 */
118 { PROP_INV, PROP_INV, PROP_INV, PROP_INV }, /* 63 */
119 { PROP_V0 , PROP_INV, PROP_V0 , PROP_INV }, /* VT_FILETIME */
120 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_BLOB */
121 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_STREAM */
122 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_STORAGE */
123 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_STREAMED_OBJECT */
124 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_STORED_OBJECT */
125 { PROP_V0 , PROP_INV, PROP_INV, PROP_INV }, /* VT_BLOB_OBJECT */
126 { PROP_V0 , PROP_INV, PROP_V0 , PROP_INV } /* VT_CF */
127 };
128
129 static const char* wine_vtypes[VT_CLSID+1] =
130 {
131 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
132 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
133 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
134 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
135 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
136 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
137 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
138 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
139 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID"
140 };
141
142
143 static void expect(HRESULT hr, VARTYPE vt, BOOL copy)
144 {
145 int idx = vt & VT_TYPEMASK;
146 BYTE flags;
147 const char *modifier;
148
149 if(vt & VT_BYREF)
150 {
151 flags = valid_types[idx].byref;
152 modifier = "byref";
153 }
154 else if(vt & VT_ARRAY)
155 {
156 flags = valid_types[idx].with_array;
157 modifier = "array";
158 }
159 else if(vt & VT_VECTOR)
160 {
161 flags = valid_types[idx].with_vector;
162 modifier = "vector";
163 }
164 else
165 {
166 flags = valid_types[idx].simple;
167 modifier = "simple";
168 }
169
170 if(flags == PROP_INV)
171 ok(hr == copy ? DISP_E_BADVARTYPE : STG_E_INVALIDPARAMETER, "%s (%s): got %08x\n", wine_vtypes[idx], modifier, hr);
172 else if(flags == PROP_V0)
173 ok(hr == S_OK, "%s (%s): got %08x\n", wine_vtypes[idx], modifier, hr);
174 else if(flags & PROP_TODO)
175 {
176 todo_wine
177 {
178 if(hr != S_OK)
179 win_skip("%s (%s): unsupported\n", wine_vtypes[idx], modifier);
180 else ok(hr == S_OK, "%s (%s): got %08x\n", wine_vtypes[idx], modifier, hr);
181 }
182 }
183 else
184 {
185 if(hr != S_OK)
186 win_skip("%s (%s): unsupported\n", wine_vtypes[idx], modifier);
187 else ok(hr == S_OK, "%s (%s): got %08x\n", wine_vtypes[idx], modifier, hr);
188 }
189 }
190
191 static void test_validtypes(void)
192 {
193 PROPVARIANT propvar, copy, uninit;
194 HRESULT hr;
195 unsigned int i, ret;
196
197 memset(&uninit, 0x77, sizeof(uninit));
198
199 memset(&propvar, 0x55, sizeof(propvar));
200 hr = PropVariantClear(&propvar);
201 ok(hr == STG_E_INVALIDPARAMETER, "expected STG_E_INVALIDPARAMETER, got %08x\n", hr);
202 ok(propvar.vt == 0, "expected 0, got %d\n", propvar.vt);
203 ok(U(propvar).uhVal.QuadPart == 0, "expected 0, got %#x/%#x\n",
204 U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart);
205
206 for (i = 0; i < sizeof(valid_types)/sizeof(valid_types[0]); i++)
207 {
208 VARTYPE vt;
209
210 memset(&propvar, 0x55, sizeof(propvar));
211 if (i == VT_RECORD)
212 memset(&propvar, 0, sizeof(propvar));
213 else if (i == VT_BLOB || i == VT_BLOB_OBJECT)
214 {
215 U(propvar).blob.cbSize = 0;
216 U(propvar).blob.pBlobData = NULL;
217 }
218 else
219 U(propvar).pszVal = NULL;
220 vt = propvar.vt = i;
221 memset(&copy, 0x77, sizeof(copy));
222 hr = PropVariantCopy(&copy, &propvar);
223 expect(hr, vt, TRUE);
224 if (hr == S_OK)
225 {
226 ok(copy.vt == propvar.vt, "expected %d, got %d\n", propvar.vt, copy.vt);
227 ok(U(copy).uhVal.QuadPart == U(propvar).uhVal.QuadPart, "%u: expected %#x/%#x, got %#x/%#x\n",
228 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart,
229 U(copy).uhVal.u.LowPart, U(copy).uhVal.u.HighPart);
230 }
231 else
232 {
233 ret = memcmp(&copy, &uninit, sizeof(copy));
234 ok(!ret || broken(ret) /* win2000 */, "%d: copy should stay unchanged\n", i);
235 }
236 hr = PropVariantClear(&propvar);
237 expect(hr, vt, FALSE);
238 ok(propvar.vt == 0, "expected 0, got %d\n", propvar.vt);
239 ok(U(propvar).uhVal.QuadPart == 0, "%u: expected 0, got %#x/%#x\n",
240 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart);
241
242 memset(&propvar, 0x55, sizeof(propvar));
243 U(propvar).pszVal = NULL;
244 vt = propvar.vt = i | VT_ARRAY;
245 memset(&copy, 0x77, sizeof(copy));
246 hr = PropVariantCopy(&copy, &propvar);
247 expect(hr, vt, TRUE);
248 if (hr == S_OK)
249 {
250 ok(copy.vt == propvar.vt, "expected %d, got %d\n", propvar.vt, copy.vt);
251 ok(U(copy).uhVal.QuadPart == 0, "%u: expected 0, got %#x/%#x\n",
252 i, U(copy).uhVal.u.LowPart, U(copy).uhVal.u.HighPart);
253 }
254 else
255 {
256 ret = memcmp(&copy, &uninit, sizeof(copy));
257 ok(!ret || broken(ret) /* win2000 */, "%d: copy should stay unchanged\n", i);
258 }
259 hr = PropVariantClear(&propvar);
260 expect(hr, vt, FALSE);
261 ok(propvar.vt == 0, "expected 0, got %d\n", propvar.vt);
262 ok(U(propvar).uhVal.QuadPart == 0, "%u: expected 0, got %#x/%#x\n",
263 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart);
264
265 memset(&propvar, 0x55, sizeof(propvar));
266 U(propvar).caub.cElems = 0;
267 U(propvar).caub.pElems = NULL;
268 vt = propvar.vt = i | VT_VECTOR;
269 memset(&copy, 0x77, sizeof(copy));
270 hr = PropVariantCopy(&copy, &propvar);
271 expect(hr, vt, TRUE);
272 if (hr == S_OK)
273 {
274 ok(copy.vt == propvar.vt, "expected %d, got %d\n", propvar.vt, copy.vt);
275 ok(!U(copy).caub.cElems, "%u: expected 0, got %d\n", i, U(copy).caub.cElems);
276 ok(!U(copy).caub.pElems, "%u: expected NULL, got %p\n", i, U(copy).caub.pElems);
277 }
278 else
279 {
280 ret = memcmp(&copy, &uninit, sizeof(copy));
281 ok(!ret || broken(ret) /* win2000 */, "%d: copy should stay unchanged\n", i);
282 }
283 hr = PropVariantClear(&propvar);
284 expect(hr, vt, FALSE);
285 ok(propvar.vt == 0, "expected 0, got %d\n", propvar.vt);
286 ok(U(propvar).uhVal.QuadPart == 0, "%u: expected 0, got %#x/%#x\n",
287 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart);
288
289 memset(&propvar, 0x55, sizeof(propvar));
290 U(propvar).pszVal = NULL;
291 vt = propvar.vt = i | VT_BYREF;
292 memset(&copy, 0x77, sizeof(copy));
293 hr = PropVariantCopy(&copy, &propvar);
294 expect(hr, vt, TRUE);
295 if (hr == S_OK)
296 {
297 ok(copy.vt == propvar.vt, "expected %d, got %d\n", propvar.vt, copy.vt);
298 ok(U(copy).uhVal.QuadPart == U(propvar).uhVal.QuadPart, "%u: expected %#x/%#x, got %#x/%#x\n",
299 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart,
300 U(copy).uhVal.u.LowPart, U(copy).uhVal.u.HighPart);
301 }
302 else
303 {
304 ret = memcmp(&copy, &uninit, sizeof(copy));
305 ok(!ret || broken(ret) /* win2000 */, "%d: copy should stay unchanged\n", i);
306 }
307 hr = PropVariantClear(&propvar);
308 expect(hr, vt, FALSE);
309 ok(propvar.vt == 0, "expected 0, got %d\n", propvar.vt);
310 ok(U(propvar).uhVal.QuadPart == 0, "%u: expected 0, got %#x/%#x\n",
311 i, U(propvar).uhVal.u.LowPart, U(propvar).uhVal.u.HighPart);
312 }
313 }
314
315 static void test_copy(void)
316 {
317 static char szTestString[] = "Test String";
318 static WCHAR wszTestString[] = {'T','e','s','t',' ','S','t','r','i','n','g',0};
319 PROPVARIANT propvarSrc;
320 PROPVARIANT propvarDst;
321 HRESULT hr;
322
323 propvarSrc.vt = VT_BSTR;
324 U(propvarSrc).bstrVal = SysAllocString(wszTestString);
325
326 hr = PropVariantCopy(&propvarDst, &propvarSrc);
327 ok(hr == S_OK, "PropVariantCopy(...VT_BSTR...) failed\n");
328 ok(!lstrcmpW(U(propvarSrc).bstrVal, U(propvarDst).bstrVal), "BSTR not copied properly\n");
329 hr = PropVariantClear(&propvarSrc);
330 ok(hr == S_OK, "PropVariantClear(...VT_BSTR...) failed\n");
331 hr = PropVariantClear(&propvarDst);
332 ok(hr == S_OK, "PropVariantClear(...VT_BSTR...) failed\n");
333
334 propvarSrc.vt = VT_LPWSTR;
335 U(propvarSrc).pwszVal = wszTestString;
336 hr = PropVariantCopy(&propvarDst, &propvarSrc);
337 ok(hr == S_OK, "PropVariantCopy(...VT_LPWSTR...) failed\n");
338 ok(!lstrcmpW(U(propvarSrc).pwszVal, U(propvarDst).pwszVal), "Wide string not copied properly\n");
339 hr = PropVariantClear(&propvarDst);
340 ok(hr == S_OK, "PropVariantClear(...VT_LPWSTR...) failed\n");
341 memset(&propvarSrc, 0, sizeof(propvarSrc));
342
343 propvarSrc.vt = VT_LPSTR;
344 U(propvarSrc).pszVal = szTestString;
345 hr = PropVariantCopy(&propvarDst, &propvarSrc);
346 ok(hr == S_OK, "PropVariantCopy(...VT_LPSTR...) failed\n");
347 ok(!strcmp(U(propvarSrc).pszVal, U(propvarDst).pszVal), "String not copied properly\n");
348 hr = PropVariantClear(&propvarDst);
349 ok(hr == S_OK, "PropVariantClear(...VT_LPSTR...) failed\n");
350 memset(&propvarSrc, 0, sizeof(propvarSrc));
351 }
352
353 struct _PMemoryAllocator_vtable {
354 void *Allocate; /* virtual void* Allocate(ULONG cbSize); */
355 void *Free; /* virtual void Free(void *pv); */
356 };
357
358 typedef struct _PMemoryAllocator {
359 struct _PMemoryAllocator_vtable *vt;
360 } PMemoryAllocator;
361
362 #ifdef __i386__
363 #define __thiscall_wrapper __stdcall
364 #else
365 #define __thiscall_wrapper __cdecl
366 #endif
367
368 static void * __thiscall_wrapper PMemoryAllocator_Allocate(PMemoryAllocator *_this, ULONG cbSize)
369 {
370 return CoTaskMemAlloc(cbSize);
371 }
372
373 static void __thiscall_wrapper PMemoryAllocator_Free(PMemoryAllocator *_this, void *pv)
374 {
375 CoTaskMemFree(pv);
376 }
377
378 #ifdef __i386__
379
380 #include "pshpack1.h"
381 typedef struct
382 {
383 BYTE pop_eax; /* popl %eax */
384 BYTE push_ecx; /* pushl %ecx */
385 BYTE push_eax; /* pushl %eax */
386 BYTE jmp_func; /* jmp $func */
387 DWORD func;
388 } THISCALL_TO_STDCALL_THUNK;
389 #include "poppack.h"
390
391 static THISCALL_TO_STDCALL_THUNK *wrapperCodeMem = NULL;
392
393 static void fill_thunk(THISCALL_TO_STDCALL_THUNK *thunk, void *fn)
394 {
395 thunk->pop_eax = 0x58;
396 thunk->push_ecx = 0x51;
397 thunk->push_eax = 0x50;
398 thunk->jmp_func = 0xe9;
399 thunk->func = (char*)fn - (char*)(&thunk->func + 1);
400 }
401
402 static void setup_vtable(struct _PMemoryAllocator_vtable *vtable)
403 {
404 wrapperCodeMem = VirtualAlloc(NULL, 2 * sizeof(*wrapperCodeMem),
405 MEM_COMMIT, PAGE_EXECUTE_READWRITE);
406
407 fill_thunk(&wrapperCodeMem[0], PMemoryAllocator_Allocate);
408 fill_thunk(&wrapperCodeMem[1], PMemoryAllocator_Free);
409
410 vtable->Allocate = &wrapperCodeMem[0];
411 vtable->Free = &wrapperCodeMem[1];
412 }
413
414 #else
415
416 static void setup_vtable(struct _PMemoryAllocator_vtable *vtable)
417 {
418 vtable->Allocate = PMemoryAllocator_Allocate;
419 vtable->Free = PMemoryAllocator_Free;
420 }
421
422 #endif
423
424 static const char serialized_empty[] = {
425 0,0, /* VT_EMPTY */
426 0,0, /* padding */
427 };
428
429 static const char serialized_null[] = {
430 1,0, /* VT_NULL */
431 0,0, /* padding */
432 };
433
434 static const char serialized_i4[] = {
435 3,0, /* VT_I4 */
436 0,0, /* padding */
437 0xef,0xcd,0xab,0xfe
438 };
439
440 static const char serialized_bstr_wc[] = {
441 8,0, /* VT_BSTR */
442 0,0, /* padding */
443 10,0,0,0, /* size */
444 't',0,'e',0,
445 's',0,'t',0,
446 0,0,0,0
447 };
448
449 static const char serialized_bstr_mb[] = {
450 8,0, /* VT_BSTR */
451 0,0, /* padding */
452 5,0,0,0, /* size */
453 't','e','s','t',
454 0,0,0,0
455 };
456
457 static void test_propertytovariant(void)
458 {
459 HANDLE hole32;
460 BOOLEAN (__stdcall *pStgConvertPropertyToVariant)(const SERIALIZEDPROPERTYVALUE*,USHORT,PROPVARIANT*,PMemoryAllocator*);
461 PROPVARIANT propvar;
462 PMemoryAllocator allocator;
463 struct _PMemoryAllocator_vtable vtable;
464 BOOLEAN ret;
465 static const WCHAR test_string[] = {'t','e','s','t',0};
466
467 hole32 = GetModuleHandleA("ole32");
468
469 pStgConvertPropertyToVariant = (void*)GetProcAddress(hole32, "StgConvertPropertyToVariant");
470
471 if (!pStgConvertPropertyToVariant)
472 {
473 win_skip("StgConvertPropertyToVariant not available\n");
474 return;
475 }
476
477 setup_vtable(&vtable);
478 allocator.vt = &vtable;
479
480 ret = pStgConvertPropertyToVariant((SERIALIZEDPROPERTYVALUE*)serialized_empty,
481 CP_WINUNICODE, &propvar, &allocator);
482
483 ok(ret == 0, "StgConvertPropertyToVariant returned %i\n", ret);
484 ok(propvar.vt == VT_EMPTY, "unexpected vt %x\n", propvar.vt);
485
486 ret = pStgConvertPropertyToVariant((SERIALIZEDPROPERTYVALUE*)serialized_null,
487 CP_WINUNICODE, &propvar, &allocator);
488
489 ok(ret == 0, "StgConvertPropertyToVariant returned %i\n", ret);
490 ok(propvar.vt == VT_NULL, "unexpected vt %x\n", propvar.vt);
491
492 ret = pStgConvertPropertyToVariant((SERIALIZEDPROPERTYVALUE*)serialized_i4,
493 CP_WINUNICODE, &propvar, &allocator);
494
495 ok(ret == 0, "StgConvertPropertyToVariant returned %i\n", ret);
496 ok(propvar.vt == VT_I4, "unexpected vt %x\n", propvar.vt);
497 ok(U(propvar).lVal == 0xfeabcdef, "unexpected lVal %x\n", U(propvar).lVal);
498
499 ret = pStgConvertPropertyToVariant((SERIALIZEDPROPERTYVALUE*)serialized_bstr_wc,
500 CP_WINUNICODE, &propvar, &allocator);
501
502 ok(ret == 0, "StgConvertPropertyToVariant returned %i\n", ret);
503 ok(propvar.vt == VT_BSTR, "unexpected vt %x\n", propvar.vt);
504 ok(!lstrcmpW(U(propvar).bstrVal, test_string), "unexpected string value\n");
505 PropVariantClear(&propvar);
506
507 ret = pStgConvertPropertyToVariant((SERIALIZEDPROPERTYVALUE*)serialized_bstr_mb,
508 CP_UTF8, &propvar, &allocator);
509
510 ok(ret == 0, "StgConvertPropertyToVariant returned %i\n", ret);
511 ok(propvar.vt == VT_BSTR, "unexpected vt %x\n", propvar.vt);
512 ok(!lstrcmpW(U(propvar).bstrVal, test_string), "unexpected string value\n");
513 PropVariantClear(&propvar);
514 }
515
516 static void test_varianttoproperty(void)
517 {
518 HANDLE hole32;
519 PROPVARIANT propvar;
520 SERIALIZEDPROPERTYVALUE *propvalue, *own_propvalue;
521 SERIALIZEDPROPERTYVALUE* (__stdcall *pStgConvertVariantToProperty)(
522 const PROPVARIANT*,USHORT,SERIALIZEDPROPERTYVALUE*,ULONG*,PROPID,BOOLEAN,ULONG*);
523 ULONG len;
524 static const WCHAR test_string[] = {'t','e','s','t',0};
525 BSTR test_string_bstr;
526
527 hole32 = GetModuleHandleA("ole32");
528
529 pStgConvertVariantToProperty = (void*)GetProcAddress(hole32, "StgConvertVariantToProperty");
530
531 if (!pStgConvertVariantToProperty)
532 {
533 win_skip("StgConvertVariantToProperty not available\n");
534 return;
535 }
536
537 own_propvalue = HeapAlloc(GetProcessHeap(), 0, sizeof(SERIALIZEDPROPERTYVALUE) + 20);
538
539 PropVariantInit(&propvar);
540
541 propvar.vt = VT_I4;
542 U(propvar).lVal = 0xfeabcdef;
543
544 len = 0xdeadbeef;
545 propvalue = pStgConvertVariantToProperty(&propvar, CP_WINUNICODE, NULL, &len,
546 0, FALSE, 0);
547
548 ok(propvalue == NULL, "got nonnull propvalue\n");
549 todo_wine ok(len == 8, "unexpected length %d\n", len);
550
551 if (len == 0xdeadbeef)
552 {
553 HeapFree(GetProcessHeap(), 0, own_propvalue);
554 return;
555 }
556
557 len = 20;
558 propvalue = pStgConvertVariantToProperty(&propvar, CP_WINUNICODE, own_propvalue, &len,
559 0, FALSE, 0);
560
561 ok(propvalue == own_propvalue, "unexpected propvalue %p\n", propvalue);
562 ok(len == 8, "unexpected length %d\n", len);
563 ok(!memcmp(propvalue, serialized_i4, 8), "got wrong data\n");
564
565 propvar.vt = VT_EMPTY;
566 len = 20;
567 own_propvalue->dwType = 0xdeadbeef;
568 propvalue = pStgConvertVariantToProperty(&propvar, CP_WINUNICODE, own_propvalue, &len,
569 0, FALSE, 0);
570
571 ok(propvalue == own_propvalue, "unexpected propvalue %p\n", propvalue);
572 ok(len == 4 || broken(len == 0) /* before Vista */, "unexpected length %d\n", len);
573 if (len) ok(!memcmp(propvalue, serialized_empty, 4), "got wrong data\n");
574 else ok(propvalue->dwType == 0xdeadbeef, "unexpected type %d\n", propvalue->dwType);
575
576 propvar.vt = VT_NULL;
577 len = 20;
578 propvalue = pStgConvertVariantToProperty(&propvar, CP_WINUNICODE, own_propvalue, &len,
579 0, FALSE, 0);
580
581 ok(propvalue == own_propvalue, "unexpected propvalue %p\n", propvalue);
582 ok(len == 4, "unexpected length %d\n", len);
583 ok(!memcmp(propvalue, serialized_null, 4), "got wrong data\n");
584
585 test_string_bstr = SysAllocString(test_string);
586
587 propvar.vt = VT_BSTR;
588 U(propvar).bstrVal = test_string_bstr;
589 len = 20;
590 propvalue = pStgConvertVariantToProperty(&propvar, CP_WINUNICODE, own_propvalue, &len,
591 0, FALSE, 0);
592
593 ok(propvalue == own_propvalue, "unexpected propvalue %p\n", propvalue);
594 ok(len == 20, "unexpected length %d\n", len);
595 ok(!memcmp(propvalue, serialized_bstr_wc, 20), "got wrong data\n");
596
597 len = 20;
598 propvalue = pStgConvertVariantToProperty(&propvar, CP_UTF8, own_propvalue, &len,
599 0, FALSE, 0);
600
601 ok(propvalue == own_propvalue, "unexpected propvalue %p\n", propvalue);
602 ok(len == 16, "unexpected length %d\n", len);
603 ok(!memcmp(propvalue, serialized_bstr_mb, 16), "got wrong data\n");
604
605 SysFreeString(test_string_bstr);
606
607 HeapFree(GetProcessHeap(), 0, own_propvalue);
608 }
609
610 START_TEST(propvariant)
611 {
612 test_validtypes();
613 test_copy();
614 test_propertytovariant();
615 test_varianttoproperty();
616 }