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