push 6495605bb529da27066f1c178d57d902552737a0
[wine/hacks.git] / dlls / rpcrt4 / tests / ndr_marshall.c
blob7c538c60cdf1a8549b9f9d6d43ddfd21ba2eba57
1 /*
2 * Unit test suite for ndr marshalling functions
4 * Copyright 2006 Huw Davies
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.
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.
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
21 #include <stdarg.h>
23 #define NTDDI_WIN2K 0x05000000
24 #define NTDDI_VERSION NTDDI_WIN2K /* for some MIDL_STUB_MESSAGE fields */
26 #include "wine/test.h"
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winnt.h>
30 #include <winerror.h>
32 #include "rpc.h"
33 #include "rpcdce.h"
34 #include "rpcproxy.h"
37 static int my_alloc_called;
38 static int my_free_called;
39 static void * CALLBACK my_alloc(size_t size)
41 my_alloc_called++;
42 return NdrOleAllocate(size);
45 static void CALLBACK my_free(void *ptr)
47 my_free_called++;
48 NdrOleFree(ptr);
51 static const MIDL_STUB_DESC Object_StubDesc =
53 NULL,
54 my_alloc,
55 my_free,
56 { 0 },
61 NULL, /* format string, filled in by tests */
62 1, /* -error bounds_check flag */
63 0x20000, /* Ndr library version */
65 0x50100a4, /* MIDL Version 5.1.164 */
67 NULL,
68 0, /* notify & notify_flag routine table */
69 1, /* Flags */
70 0, /* Reserved3 */
71 0, /* Reserved4 */
72 0 /* Reserved5 */
75 static RPC_DISPATCH_FUNCTION IFoo_table[] =
80 static RPC_DISPATCH_TABLE IFoo_v0_0_DispatchTable =
83 IFoo_table
86 static const RPC_SERVER_INTERFACE IFoo___RpcServerInterface =
88 sizeof(RPC_SERVER_INTERFACE),
89 {{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x34}},{0,0}},
90 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
91 &IFoo_v0_0_DispatchTable,
99 static RPC_IF_HANDLE IFoo_v0_0_s_ifspec = (RPC_IF_HANDLE)& IFoo___RpcServerInterface;
100 static BOOL use_pointer_ids = FALSE;
102 static void determine_pointer_marshalling_style(void)
104 RPC_MESSAGE RpcMessage;
105 MIDL_STUB_MESSAGE StubMsg;
106 MIDL_STUB_DESC StubDesc;
107 char ch = 0xde;
109 static const unsigned char fmtstr_up_char[] =
111 0x12, 0x8, /* FC_UP [simple_pointer] */
112 0x2, /* FC_CHAR */
113 0x5c, /* FC_PAD */
116 StubDesc = Object_StubDesc;
117 StubDesc.pFormatTypes = NULL;
119 NdrClientInitializeNew(
120 &RpcMessage,
121 &StubMsg,
122 &StubDesc,
125 StubMsg.BufferLength = 8;
126 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
127 NdrPointerMarshall(&StubMsg, (unsigned char*)&ch, fmtstr_up_char);
128 ok(StubMsg.Buffer == StubMsg.BufferStart + 5, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
130 use_pointer_ids = (*(unsigned int *)StubMsg.BufferStart != (unsigned int)&ch);
131 trace("Pointer marshalling using %s\n", use_pointer_ids ? "pointer ids" : "pointer value");
133 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
136 static void test_ndr_simple_type(void)
138 RPC_MESSAGE RpcMessage;
139 MIDL_STUB_MESSAGE StubMsg;
140 MIDL_STUB_DESC StubDesc;
141 long l, l2 = 0;
143 StubDesc = Object_StubDesc;
144 StubDesc.pFormatTypes = NULL;
146 NdrClientInitializeNew(
147 &RpcMessage,
148 &StubMsg,
149 &StubDesc,
152 StubMsg.BufferLength = 16;
153 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
154 l = 0xcafebabe;
155 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
156 ok(StubMsg.Buffer == StubMsg.BufferStart + 4, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
157 ok(*(long*)StubMsg.BufferStart == l, "%ld\n", *(long*)StubMsg.BufferStart);
159 StubMsg.Buffer = StubMsg.BufferStart + 1;
160 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
161 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
162 ok(*(long*)(StubMsg.BufferStart + 4) == l, "%ld\n", *(long*)StubMsg.BufferStart);
164 StubMsg.Buffer = StubMsg.BufferStart + 1;
165 NdrSimpleTypeUnmarshall(&StubMsg, (unsigned char*)&l2, 8 /* FC_LONG */);
166 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
167 ok(l2 == l, "%ld\n", l2);
169 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
172 static void test_pointer_marshal(const unsigned char *formattypes,
173 void *memsrc,
174 long srcsize,
175 const void *wiredata,
176 ULONG wiredatalen,
177 int(*cmp)(const void*,const void*,size_t),
178 long num_additional_allocs,
179 const char *msgpfx)
181 RPC_MESSAGE RpcMessage;
182 MIDL_STUB_MESSAGE StubMsg;
183 MIDL_STUB_DESC StubDesc;
184 DWORD size;
185 void *ptr;
186 unsigned char *mem, *mem_orig;
188 my_alloc_called = my_free_called = 0;
189 if(!cmp)
190 cmp = memcmp;
192 StubDesc = Object_StubDesc;
193 StubDesc.pFormatTypes = formattypes;
195 NdrClientInitializeNew(
196 &RpcMessage,
197 &StubMsg,
198 &StubDesc,
201 StubMsg.BufferLength = 0;
202 NdrPointerBufferSize( &StubMsg,
203 memsrc,
204 formattypes );
205 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
207 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
208 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
209 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
211 memset(StubMsg.BufferStart, 0x0, StubMsg.BufferLength); /* This is a hack to clear the padding between the ptr and longlong/double */
213 ptr = NdrPointerMarshall( &StubMsg, memsrc, formattypes );
214 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
215 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
216 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled\n", msgpfx);
218 StubMsg.Buffer = StubMsg.BufferStart;
219 StubMsg.MemorySize = 0;
221 if (0)
223 /* NdrPointerMemorySize crashes under Wine */
224 size = NdrPointerMemorySize( &StubMsg, formattypes );
225 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
226 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
227 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
228 ok(size == srcsize + 4, "%s: mem size %u\n", msgpfx, size);
229 else
230 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
232 StubMsg.Buffer = StubMsg.BufferStart;
233 StubMsg.MemorySize = 16;
234 size = NdrPointerMemorySize( &StubMsg, formattypes );
235 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
236 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
237 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
238 ok(size == srcsize + 4 + 16, "%s: mem size %u\n", msgpfx, size);
239 else
240 ok(size == srcsize + 16, "%s: mem size %u\n", msgpfx, size);
242 StubMsg.Buffer = StubMsg.BufferStart;
243 StubMsg.MemorySize = 1;
244 size = NdrPointerMemorySize( &StubMsg, formattypes );
245 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
246 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
247 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
248 ok(size == srcsize + 4 + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
249 else
250 ok(size == srcsize + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
253 size = srcsize;
254 if(formattypes[1] & 0x10) size += 4;
256 StubMsg.Buffer = StubMsg.BufferStart;
257 StubMsg.MemorySize = 0;
258 mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, size);
260 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
261 *(void**)mem = NULL;
262 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
263 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
264 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
265 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
266 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
267 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
268 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
269 my_alloc_called = 0;
271 /* reset the buffer and call with must alloc */
272 StubMsg.Buffer = StubMsg.BufferStart;
273 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
274 *(void**)mem = NULL;
275 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 1 );
276 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
277 /* doesn't allocate mem in this case */
278 todo_wine {
279 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
281 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
282 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
283 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
285 todo_wine {
286 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
288 my_alloc_called = 0;
289 if(formattypes[0] != 0x11 /* FC_RP */)
291 /* now pass the address of a NULL ptr */
292 mem = NULL;
293 StubMsg.Buffer = StubMsg.BufferStart;
294 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
295 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
296 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
297 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
298 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
299 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
300 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
301 my_alloc_called = 0;
302 NdrPointerFree(&StubMsg, mem, formattypes);
304 /* again pass address of NULL ptr, but pretend we're a server */
305 mem = NULL;
306 StubMsg.Buffer = StubMsg.BufferStart;
307 StubMsg.IsClient = 0;
308 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
309 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
310 if (formattypes[2] == 0xd /* FC_ENUM16 */)
311 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
312 else
313 ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
314 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
315 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
316 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
317 if (formattypes[2] != 0xd /* FC_ENUM16 */) {
318 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
319 my_alloc_called = 0;
322 HeapFree(GetProcessHeap(), 0, mem_orig);
323 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
326 static int deref_cmp(const void *s1, const void *s2, size_t num)
328 return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
332 static void test_simple_types(void)
334 unsigned char wiredata[16];
335 unsigned char ch;
336 unsigned char *ch_ptr;
337 unsigned short s;
338 unsigned int i;
339 unsigned long l;
340 ULONGLONG ll;
341 float f;
342 double d;
344 static const unsigned char fmtstr_up_char[] =
346 0x12, 0x8, /* FC_UP [simple_pointer] */
347 0x2, /* FC_CHAR */
348 0x5c, /* FC_PAD */
350 static const unsigned char fmtstr_up_byte[] =
352 0x12, 0x8, /* FC_UP [simple_pointer] */
353 0x1, /* FC_BYTE */
354 0x5c, /* FC_PAD */
356 static const unsigned char fmtstr_up_small[] =
358 0x12, 0x8, /* FC_UP [simple_pointer] */
359 0x3, /* FC_SMALL */
360 0x5c, /* FC_PAD */
362 static const unsigned char fmtstr_up_usmall[] =
364 0x12, 0x8, /* FC_UP [simple_pointer] */
365 0x4, /* FC_USMALL */
366 0x5c, /* FC_PAD */
368 static const unsigned char fmtstr_rp_char[] =
370 0x11, 0x8, /* FC_RP [simple_pointer] */
371 0x2, /* FC_CHAR */
372 0x5c, /* FC_PAD */
374 static const unsigned char fmtstr_rpup_char[] =
376 0x11, 0x14, /* FC_RP [alloced_on_stack] */
377 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
378 0x12, 0x8, /* FC_UP [simple_pointer] */
379 0x2, /* FC_CHAR */
380 0x5c, /* FC_PAD */
382 static const unsigned char fmtstr_rpup_char2[] =
384 0x11, 0x04, /* FC_RP [alloced_on_stack] */
385 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
386 0x12, 0x8, /* FC_UP [simple_pointer] */
387 0x2, /* FC_CHAR */
388 0x5c, /* FC_PAD */
391 static const unsigned char fmtstr_up_wchar[] =
393 0x12, 0x8, /* FC_UP [simple_pointer] */
394 0x5, /* FC_WCHAR */
395 0x5c, /* FC_PAD */
397 static const unsigned char fmtstr_up_short[] =
399 0x12, 0x8, /* FC_UP [simple_pointer] */
400 0x6, /* FC_SHORT */
401 0x5c, /* FC_PAD */
403 static const unsigned char fmtstr_up_ushort[] =
405 0x12, 0x8, /* FC_UP [simple_pointer] */
406 0x7, /* FC_USHORT */
407 0x5c, /* FC_PAD */
409 static const unsigned char fmtstr_up_enum16[] =
411 0x12, 0x8, /* FC_UP [simple_pointer] */
412 0xd, /* FC_ENUM16 */
413 0x5c, /* FC_PAD */
415 static const unsigned char fmtstr_up_long[] =
417 0x12, 0x8, /* FC_UP [simple_pointer] */
418 0x8, /* FC_LONG */
419 0x5c, /* FC_PAD */
421 static const unsigned char fmtstr_up_ulong[] =
423 0x12, 0x8, /* FC_UP [simple_pointer] */
424 0x9, /* FC_ULONG */
425 0x5c, /* FC_PAD */
427 static const unsigned char fmtstr_up_enum32[] =
429 0x12, 0x8, /* FC_UP [simple_pointer] */
430 0xe, /* FC_ENUM32 */
431 0x5c, /* FC_PAD */
433 static const unsigned char fmtstr_up_errorstatus[] =
435 0x12, 0x8, /* FC_UP [simple_pointer] */
436 0x10, /* FC_ERROR_STATUS_T */
437 0x5c, /* FC_PAD */
440 static const unsigned char fmtstr_up_longlong[] =
442 0x12, 0x8, /* FC_UP [simple_pointer] */
443 0xb, /* FC_HYPER */
444 0x5c, /* FC_PAD */
446 static const unsigned char fmtstr_up_float[] =
448 0x12, 0x8, /* FC_UP [simple_pointer] */
449 0xa, /* FC_FLOAT */
450 0x5c, /* FC_PAD */
452 static const unsigned char fmtstr_up_double[] =
454 0x12, 0x8, /* FC_UP [simple_pointer] */
455 0xc, /* FC_DOUBLE */
456 0x5c, /* FC_PAD */
459 ch = 0xa5;
460 ch_ptr = &ch;
461 if (use_pointer_ids)
462 *(unsigned int *)wiredata = 0x20000;
463 else
464 *(unsigned int *)wiredata = (unsigned int)ch_ptr;
465 wiredata[4] = ch;
467 test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
468 test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
469 test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0, "up_small");
470 test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
472 test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
474 test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
475 test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
477 s = 0xa597;
478 if (use_pointer_ids)
479 *(unsigned int *)wiredata = 0x20000;
480 else
481 *(unsigned int *)wiredata = (unsigned int)&s;
482 *(unsigned short*)(wiredata + 4) = s;
484 test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
485 test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
486 test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
488 i = 0x7fff;
489 if (use_pointer_ids)
490 *(unsigned int *)wiredata = 0x20000;
491 else
492 *(unsigned int *)wiredata = (unsigned int)&i;
493 *(unsigned short*)(wiredata + 4) = i;
494 test_pointer_marshal(fmtstr_up_enum16, &i, 2, wiredata, 6, NULL, 0, "up_enum16");
496 l = 0xcafebabe;
497 if (use_pointer_ids)
498 *(unsigned int *)wiredata = 0x20000;
499 else
500 *(unsigned int *)wiredata = (unsigned int)&l;
501 *(unsigned long*)(wiredata + 4) = l;
503 test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
504 test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0, "up_ulong");
505 test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0, "up_emun32");
506 test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0, "up_errorstatus");
508 ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
509 if (use_pointer_ids)
510 *(unsigned int *)wiredata = 0x20000;
511 else
512 *(unsigned int *)wiredata = (unsigned int)&ll;
513 *(unsigned int **)(wiredata + 4) = 0;
514 *(ULONGLONG*)(wiredata + 8) = ll;
515 test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
517 f = 3.1415f;
518 if (use_pointer_ids)
519 *(unsigned int *)wiredata = 0x20000;
520 else
521 *(unsigned int *)wiredata = (unsigned int)&f;
522 *(float*)(wiredata + 4) = f;
523 test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
525 d = 3.1415;
526 if (use_pointer_ids)
527 *(unsigned int *)wiredata = 0x20000;
528 else
529 *(unsigned int *)wiredata = (unsigned int)&d;
530 *(unsigned int *)(wiredata + 4) = 0;
531 *(double*)(wiredata + 8) = d;
532 test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0, "up_double");
536 static void test_nontrivial_pointer_types(void)
538 RPC_MESSAGE RpcMessage;
539 MIDL_STUB_MESSAGE StubMsg;
540 MIDL_STUB_DESC StubDesc;
541 void *ptr;
542 char **p1;
543 char *p2;
544 char ch;
545 unsigned char *mem, *mem_orig;
547 static const unsigned char fmtstr_ref_unique_out[] =
549 0x12, 0x8, /* FC_UP [simple_pointer] */
550 0x2, /* FC_CHAR */
551 0x5c, /* FC_PAD */
552 0x11, 0x14, /* FC_RP [alloced_on_stack] [pointer_deref] */
553 NdrFcShort( 0xfffffffa ), /* Offset= -6 (0) */
556 p1 = &p2;
557 p2 = &ch;
558 ch = 0x22;
560 StubDesc = Object_StubDesc;
561 StubDesc.pFormatTypes = fmtstr_ref_unique_out;
563 NdrClientInitializeNew(
564 &RpcMessage,
565 &StubMsg,
566 &StubDesc,
569 StubMsg.BufferLength = 0;
570 NdrPointerBufferSize( &StubMsg,
571 (unsigned char *)p1,
572 &fmtstr_ref_unique_out[4] );
574 /* Windows overestimates the buffer size */
575 ok(StubMsg.BufferLength >= 5, "length %d\n", StubMsg.BufferLength);
577 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
578 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
579 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
581 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)p1, &fmtstr_ref_unique_out[4] );
582 ok(ptr == NULL, "ret %p\n", ptr);
583 ok(StubMsg.Buffer - StubMsg.BufferStart == 5, "Buffer %p Start %p len %d\n",
584 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
585 ok(*(unsigned int *)StubMsg.BufferStart != 0, "pointer ID marshalled incorrectly\n");
586 ok(*(unsigned char *)(StubMsg.BufferStart + 4) == 0x22, "char data marshalled incorrectly: 0x%x\n",
587 *(unsigned char *)(StubMsg.BufferStart + 4));
589 StubMsg.Buffer = StubMsg.BufferStart;
590 StubMsg.MemorySize = 0;
591 mem = NULL;
593 /* Client */
594 my_alloc_called = 0;
595 StubMsg.Buffer = StubMsg.BufferStart;
596 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(void *));
597 *(void **)mem = NULL;
598 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
599 ok(mem == mem_orig, "mem alloced\n");
600 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
602 my_alloc_called = 0;
603 StubMsg.Buffer = StubMsg.BufferStart;
604 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
605 todo_wine {
606 ok(mem == mem_orig, "mem alloced\n");
607 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
610 my_free_called = 0;
611 StubMsg.Buffer = StubMsg.BufferStart;
612 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
613 ok(my_free_called == 1, "free called %d\n", my_free_called);
615 mem = my_alloc(sizeof(void *));
616 *(void **)mem = NULL;
617 my_free_called = 0;
618 StubMsg.Buffer = StubMsg.BufferStart;
619 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
620 ok(my_free_called == 0, "free called %d\n", my_free_called);
621 my_free(mem);
623 mem = my_alloc(sizeof(void *));
624 *(void **)mem = my_alloc(sizeof(char));
625 my_free_called = 0;
626 StubMsg.Buffer = StubMsg.BufferStart;
627 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
628 ok(my_free_called == 1, "free called %d\n", my_free_called);
629 my_free(mem);
631 /* Server */
632 my_alloc_called = 0;
633 StubMsg.IsClient = 0;
634 mem = NULL;
635 StubMsg.Buffer = StubMsg.BufferStart;
636 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
637 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
638 todo_wine
639 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
640 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
642 my_alloc_called = 0;
643 mem = NULL;
644 StubMsg.Buffer = StubMsg.BufferStart;
645 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
646 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
647 todo_wine
648 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
649 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
651 my_alloc_called = 0;
652 mem = mem_orig;
653 *(void **)mem = NULL;
654 StubMsg.Buffer = StubMsg.BufferStart;
655 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
656 todo_wine {
657 ok(mem == mem_orig, "mem alloced\n");
658 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
661 my_alloc_called = 0;
662 mem = mem_orig;
663 *(void **)mem = NULL;
664 StubMsg.Buffer = StubMsg.BufferStart;
665 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
666 todo_wine {
667 ok(mem == mem_orig, "mem alloced\n");
668 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
671 mem = my_alloc(sizeof(void *));
672 *(void **)mem = NULL;
673 my_free_called = 0;
674 StubMsg.Buffer = StubMsg.BufferStart;
675 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
676 ok(my_free_called == 0, "free called %d\n", my_free_called);
677 my_free(mem);
679 mem = my_alloc(sizeof(void *));
680 *(void **)mem = my_alloc(sizeof(char));
681 my_free_called = 0;
682 StubMsg.Buffer = StubMsg.BufferStart;
683 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
684 ok(my_free_called == 1, "free called %d\n", my_free_called);
685 my_free(mem);
687 HeapFree(GetProcessHeap(), 0, mem_orig);
688 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
691 static void test_simple_struct_marshal(const unsigned char *formattypes,
692 void *memsrc,
693 long srcsize,
694 const void *wiredata,
695 ULONG wiredatalen,
696 int(*cmp)(const void*,const void*,size_t),
697 long num_additional_allocs,
698 const char *msgpfx)
700 RPC_MESSAGE RpcMessage;
701 MIDL_STUB_MESSAGE StubMsg;
702 MIDL_STUB_DESC StubDesc;
703 DWORD size;
704 void *ptr;
705 unsigned char *mem, *mem_orig;
707 my_alloc_called = my_free_called = 0;
708 if(!cmp)
709 cmp = memcmp;
711 StubDesc = Object_StubDesc;
712 StubDesc.pFormatTypes = formattypes;
714 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
716 StubMsg.BufferLength = 0;
717 NdrSimpleStructBufferSize( &StubMsg, (unsigned char *)memsrc, formattypes );
718 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
719 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
720 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
721 ptr = NdrSimpleStructMarshall( &StubMsg, (unsigned char*)memsrc, formattypes );
722 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
723 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
724 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled %08x %08x %08x\n", msgpfx, *(DWORD*)StubMsg.BufferStart,*((DWORD*)StubMsg.BufferStart+1),*((DWORD*)StubMsg.BufferStart+2));
726 if (0)
728 /* FIXME: Causes Wine to crash */
729 StubMsg.Buffer = StubMsg.BufferStart;
730 StubMsg.MemorySize = 0;
731 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
732 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
733 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
734 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
736 StubMsg.Buffer = StubMsg.BufferStart;
737 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
738 todo_wine {
739 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
741 ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
742 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
744 size = srcsize;
745 /*** Unmarshalling first with must_alloc false ***/
747 StubMsg.Buffer = StubMsg.BufferStart;
748 StubMsg.MemorySize = 0;
749 mem_orig = mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, srcsize);
750 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
751 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
752 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
753 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
754 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
755 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
756 my_alloc_called = 0;
757 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
759 /* If we're a server we still use the supplied memory */
760 StubMsg.Buffer = StubMsg.BufferStart;
761 StubMsg.IsClient = 0;
762 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
763 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
764 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
765 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
766 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
767 my_alloc_called = 0;
768 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
770 /* ...unless we pass a NULL ptr, then the buffer is used.
771 Passing a NULL ptr while we're a client && !must_alloc
772 crashes on Windows, so we won't do that. */
774 mem = NULL;
775 StubMsg.IsClient = 0;
776 StubMsg.Buffer = StubMsg.BufferStart;
777 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
778 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
779 ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
780 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
781 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
782 my_alloc_called = 0;
783 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
785 /*** now must_alloc is true ***/
787 /* with must_alloc set we always allocate new memory whether or not we're
788 a server and also when passing NULL */
789 mem = mem_orig;
790 StubMsg.IsClient = 1;
791 StubMsg.Buffer = StubMsg.BufferStart;
792 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
793 ok(ptr == NULL, "ret %p\n", ptr);
794 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
795 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
796 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
797 my_alloc_called = 0;
798 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
800 mem = NULL;
801 StubMsg.Buffer = StubMsg.BufferStart;
802 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
803 ok(ptr == NULL, "ret %p\n", ptr);
804 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
805 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
806 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
807 my_alloc_called = 0;
808 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
810 mem = mem_orig;
811 StubMsg.Buffer = StubMsg.BufferStart;
812 StubMsg.IsClient = 0;
813 StubMsg.ReuseBuffer = 1;
814 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
815 ok(ptr == NULL, "ret %p\n", ptr);
816 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
817 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
818 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
819 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
820 my_alloc_called = 0;
821 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
823 mem = NULL;
824 StubMsg.Buffer = StubMsg.BufferStart;
825 StubMsg.IsClient = 0;
826 StubMsg.ReuseBuffer = 1;
827 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
828 ok(ptr == NULL, "ret %p\n", ptr);
829 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
830 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
831 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
832 my_alloc_called = 0;
833 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
835 HeapFree(GetProcessHeap(), 0, mem_orig);
836 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
839 typedef struct
841 long l1;
842 long *pl1;
843 char *pc1;
844 } ps1_t;
846 static int ps1_cmp(const void *s1, const void *s2, size_t num)
848 const ps1_t *p1, *p2;
850 p1 = s1;
851 p2 = s2;
853 if(p1->l1 != p2->l1)
854 return 1;
856 if(p1->pl1 && p2->pl1)
858 if(*p1->pl1 != *p2->pl1)
859 return 1;
861 else if(p1->pl1 || p1->pl1)
862 return 1;
864 if(p1->pc1 && p2->pc1)
866 if(*p1->pc1 != *p2->pc1)
867 return 1;
869 else if(p1->pc1 || p1->pc1)
870 return 1;
872 return 0;
875 static void test_simple_struct(void)
877 unsigned char wiredata[28];
878 unsigned long wiredatalen;
879 long l;
880 char c;
881 ps1_t ps1;
883 static const unsigned char fmtstr_simple_struct[] =
885 0x12, 0x0, /* FC_UP */
886 NdrFcShort( 0x2 ), /* Offset=2 */
887 0x15, 0x3, /* FC_STRUCT [align 4] */
888 NdrFcShort( 0x18 ), /* [size 24] */
889 0x6, /* FC_SHORT */
890 0x2, /* FC_CHAR */
891 0x38, /* FC_ALIGNM4 */
892 0x8, /* FC_LONG */
893 0x8, /* FC_LONG */
894 0x39, /* FC_ALIGNM8 */
895 0xb, /* FC_HYPER */
896 0x5b, /* FC_END */
898 struct {
899 short s;
900 char c;
901 long l1, l2;
902 LONGLONG ll;
903 } s1;
905 static const unsigned char fmtstr_pointer_struct[] =
907 0x12, 0x0, /* FC_UP */
908 NdrFcShort( 0x2 ), /* Offset=2 */
909 0x16, 0x3, /* FC_PSTRUCT [align 4] */
910 NdrFcShort( 0xc ), /* [size 12] */
911 0x4b, /* FC_PP */
912 0x5c, /* FC_PAD */
913 0x46, /* FC_NO_REPEAT */
914 0x5c, /* FC_PAD */
915 NdrFcShort( 0x4 ), /* 4 */
916 NdrFcShort( 0x4 ), /* 4 */
917 0x13, 0x8, /* FC_OP [simple_pointer] */
918 0x8, /* FC_LONG */
919 0x5c, /* FC_PAD */
920 0x46, /* FC_NO_REPEAT */
921 0x5c, /* FC_PAD */
922 NdrFcShort( 0x8 ), /* 8 */
923 NdrFcShort( 0x8 ), /* 8 */
924 0x13, 0x8, /* FC_OP [simple_pointer] */
925 0x2, /* FC_CHAR */
926 0x5c, /* FC_PAD */
927 0x5b, /* FC_END */
928 0x8, /* FC_LONG */
929 0x8, /* FC_LONG */
930 0x8, /* FC_LONG */
931 0x5c, /* FC_PAD */
932 0x5b, /* FC_END */
936 /* zero the entire structure, including the holes */
937 memset(&s1, 0, sizeof(s1));
939 /* FC_STRUCT */
940 s1.s = 0x1234;
941 s1.c = 0xa5;
942 s1.l1 = 0xdeadbeef;
943 s1.l2 = 0xcafebabe;
944 s1.ll = ((LONGLONG) 0xbadefeed << 32) | 0x2468ace0;
946 wiredatalen = 24;
947 memcpy(wiredata, &s1, wiredatalen);
948 test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
950 if (use_pointer_ids)
951 *(unsigned int *)wiredata = 0x20000;
952 else
953 *(unsigned int *)wiredata = (unsigned int)&s1;
954 memcpy(wiredata + 4, &s1, wiredatalen);
955 if (0)
957 /* one of the unmarshallings crashes Wine */
958 test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
961 /* zero the entire structure, including the hole */
962 memset(&ps1, 0, sizeof(ps1));
964 /* FC_PSTRUCT */
965 ps1.l1 = 0xdeadbeef;
966 l = 0xcafebabe;
967 ps1.pl1 = &l;
968 c = 'a';
969 ps1.pc1 = &c;
970 *(unsigned int *)(wiredata + 4) = 0xdeadbeef;
971 if (use_pointer_ids)
973 *(unsigned int *)(wiredata + 8) = 0x20000;
974 *(unsigned int *)(wiredata + 12) = 0x20004;
976 else
978 *(unsigned int *)(wiredata + 8) = (unsigned int)&l;
979 *(unsigned int *)(wiredata + 12) = (unsigned int)&c;
981 memcpy(wiredata + 16, &l, 4);
982 memcpy(wiredata + 20, &c, 1);
984 test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
985 if (use_pointer_ids)
986 *(unsigned int *)wiredata = 0x20000;
987 else
988 *(unsigned int *)wiredata = (unsigned int)&ps1;
989 if (0)
991 /* one of the unmarshallings crashes Wine */
992 test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
996 static void test_fullpointer_xlat(void)
998 PFULL_PTR_XLAT_TABLES pXlatTables;
999 ULONG RefId;
1000 int ret;
1001 void *Pointer;
1003 pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
1005 /* "marshaling" phase */
1007 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1008 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1009 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1011 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1012 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1013 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1015 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1016 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1017 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1019 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1020 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1021 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1023 ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
1024 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1025 ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
1027 /* "unmarshaling" phase */
1029 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1030 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1031 ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
1033 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
1034 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1035 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1037 NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
1039 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
1040 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1041 ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
1043 NdrFullPointerXlatFree(pXlatTables);
1045 pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
1047 /* "unmarshaling" phase */
1049 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1050 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1051 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1053 NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
1055 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1056 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1057 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1059 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1060 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1061 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1063 /* "marshaling" phase */
1065 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1066 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1067 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1069 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1070 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1071 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1073 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1074 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1075 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1077 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1078 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1079 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1081 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1082 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1083 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1085 /* "freeing" phase */
1087 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
1088 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1090 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
1091 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1092 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1094 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1095 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1096 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1098 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
1099 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1101 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1102 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1104 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
1105 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1106 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1108 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1109 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1110 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1112 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1113 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1114 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1116 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1117 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1119 NdrFullPointerXlatFree(pXlatTables);
1122 static void test_client_init(void)
1124 MIDL_STUB_MESSAGE stubMsg;
1125 RPC_MESSAGE rpcMsg;
1127 memset(&rpcMsg, 0xcc, sizeof(rpcMsg));
1128 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1130 NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
1132 #define TEST_POINTER_UNSET(field) ok(rpcMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", rpcMsg.field)
1134 ok(rpcMsg.Handle == NULL, "rpcMsg.Handle should have been NULL instead of %p\n", rpcMsg.Handle);
1135 TEST_POINTER_UNSET(Buffer);
1136 ok(rpcMsg.BufferLength == 0xcccccccc, "rpcMsg.BufferLength should have been unset instead of %d\n", rpcMsg.BufferLength);
1137 ok(rpcMsg.ProcNum == 0x8001, "rpcMsg.ProcNum should have been 0x8001 instead of 0x%x\n", rpcMsg.ProcNum);
1138 TEST_POINTER_UNSET(TransferSyntax);
1139 ok(rpcMsg.RpcInterfaceInformation == Object_StubDesc.RpcInterfaceInformation,
1140 "rpcMsg.RpcInterfaceInformation should have been %p instead of %p\n",
1141 Object_StubDesc.RpcInterfaceInformation, rpcMsg.RpcInterfaceInformation);
1142 /* Note: ReservedForRuntime not tested */
1143 TEST_POINTER_UNSET(ManagerEpv);
1144 TEST_POINTER_UNSET(ImportContext);
1145 ok(rpcMsg.RpcFlags == 0, "rpcMsg.RpcFlags should have been 0 instead of 0x%lx\n", rpcMsg.RpcFlags);
1146 #undef TEST_POINTER_UNSET
1148 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1149 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", stubMsg.field)
1150 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1151 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1153 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1154 TEST_POINTER_UNSET(Buffer);
1155 TEST_ZERO(BufferStart, "%p");
1156 TEST_ZERO(BufferEnd, "%p");
1157 TEST_POINTER_UNSET(BufferMark);
1158 TEST_ZERO(BufferLength, "%d");
1159 TEST_ULONG_UNSET(MemorySize);
1160 TEST_POINTER_UNSET(Memory);
1161 ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
1162 TEST_ZERO(ReuseBuffer, "%d");
1163 TEST_ZERO(pAllocAllNodesContext, "%p");
1164 ok(stubMsg.pPointerQueueState == 0 ||
1165 broken(stubMsg.pPointerQueueState == (void *)0xcccccccc), /* win2k */
1166 "stubMsg.pPointerQueueState should have been unset instead of %p\n", stubMsg.pPointerQueueState);
1167 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1168 TEST_ZERO(PointerBufferMark, "%p");
1169 TEST_ZERO(CorrDespIncrement, "%d");
1170 TEST_ZERO(uFlags, "%d");
1171 /* FIXME: UniquePtrCount */
1172 TEST_ULONG_PTR_UNSET(MaxCount);
1173 TEST_ULONG_UNSET(Offset);
1174 TEST_ULONG_UNSET(ActualCount);
1175 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1176 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1177 TEST_ZERO(StackTop, "%p");
1178 TEST_POINTER_UNSET(pPresentedType);
1179 TEST_POINTER_UNSET(pTransmitType);
1180 TEST_POINTER_UNSET(SavedHandle);
1181 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1182 TEST_POINTER_UNSET(FullPtrXlatTables);
1183 TEST_ZERO(FullPtrRefId, "%d");
1184 TEST_ZERO(PointerLength, "%d");
1185 TEST_ZERO(fInDontFree, "%d");
1186 TEST_ZERO(fDontCallFreeInst, "%d");
1187 TEST_ZERO(fInOnlyParam, "%d");
1188 TEST_ZERO(fHasReturn, "%d");
1189 TEST_ZERO(fHasExtensions, "%d");
1190 TEST_ZERO(fHasNewCorrDesc, "%d");
1191 TEST_ZERO(fIsIn, "%d");
1192 TEST_ZERO(fIsOut, "%d");
1193 TEST_ZERO(fIsOicf, "%d");
1194 TEST_ZERO(fBufferValid, "%d");
1195 TEST_ZERO(fHasMemoryValidateCallback, "%d");
1196 TEST_ZERO(fInFree, "%d");
1197 TEST_ZERO(fNeedMCCP, "%d");
1198 TEST_ZERO(fUnused, "0x%x");
1199 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xffffcccc instead of 0x%x\n", stubMsg.fUnused2);
1200 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1201 TEST_ZERO(pvDestContext, "%p");
1202 TEST_POINTER_UNSET(SavedContextHandles);
1203 TEST_ULONG_UNSET(ParamNumber);
1204 TEST_ZERO(pRpcChannelBuffer, "%p");
1205 TEST_ZERO(pArrayInfo, "%p");
1206 TEST_POINTER_UNSET(SizePtrCountArray);
1207 TEST_POINTER_UNSET(SizePtrOffsetArray);
1208 TEST_POINTER_UNSET(SizePtrLengthArray);
1209 TEST_POINTER_UNSET(pArgQueue);
1210 TEST_ZERO(dwStubPhase, "%d");
1211 /* FIXME: where does this value come from? */
1212 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1213 TEST_ZERO(pAsyncMsg, "%p");
1214 TEST_ZERO(pCorrInfo, "%p");
1215 TEST_ZERO(pCorrMemory, "%p");
1216 TEST_ZERO(pMemoryList, "%p");
1217 TEST_POINTER_UNSET(pCSInfo);
1218 TEST_POINTER_UNSET(ConformanceMark);
1219 TEST_POINTER_UNSET(VarianceMark);
1220 ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1221 TEST_POINTER_UNSET(pContext);
1222 TEST_POINTER_UNSET(ContextHandleHash);
1223 TEST_POINTER_UNSET(pUserMarshalList);
1224 TEST_ULONG_PTR_UNSET(Reserved51_3);
1225 TEST_ULONG_PTR_UNSET(Reserved51_4);
1226 TEST_ULONG_PTR_UNSET(Reserved51_5);
1227 #undef TEST_ULONG_UNSET
1228 #undef TEST_POINTER_UNSET
1229 #undef TEST_ZERO
1233 static void test_server_init(void)
1235 MIDL_STUB_MESSAGE stubMsg;
1236 RPC_MESSAGE rpcMsg;
1237 unsigned char *ret;
1238 unsigned char buffer[256];
1240 memset(&rpcMsg, 0, sizeof(rpcMsg));
1241 rpcMsg.Buffer = buffer;
1242 rpcMsg.BufferLength = sizeof(buffer);
1243 rpcMsg.RpcFlags = RPC_BUFFER_COMPLETE;
1245 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1247 ret = NdrServerInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc);
1248 ok(ret == NULL, "NdrServerInitializeNew should have returned NULL instead of %p\n", ret);
1250 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1251 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", stubMsg.field)
1252 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1253 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1255 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1256 ok(stubMsg.Buffer == buffer, "stubMsg.Buffer should have been %p instead of %p\n", buffer, stubMsg.Buffer);
1257 ok(stubMsg.BufferStart == buffer, "stubMsg.BufferStart should have been %p instead of %p\n", buffer, stubMsg.BufferStart);
1258 ok(stubMsg.BufferEnd == buffer + sizeof(buffer), "stubMsg.BufferEnd should have been %p instead of %p\n", buffer + sizeof(buffer), stubMsg.BufferEnd);
1259 TEST_POINTER_UNSET(BufferMark);
1260 todo_wine
1261 TEST_ZERO(BufferLength, "%d");
1262 TEST_ULONG_UNSET(MemorySize);
1263 TEST_POINTER_UNSET(Memory);
1264 ok(stubMsg.IsClient == 0, "stubMsg.IsClient should have been 0 instead of %u\n", stubMsg.IsClient);
1265 ok(stubMsg.ReuseBuffer == 0 ||
1266 broken(stubMsg.ReuseBuffer == 1), /* win2k */
1267 "stubMsg.ReuseBuffer should have been set to zero instead of %d\n", stubMsg.ReuseBuffer);
1268 TEST_ZERO(pAllocAllNodesContext, "%p");
1269 ok(stubMsg.pPointerQueueState == 0 ||
1270 broken(stubMsg.pPointerQueueState == (void *)0xcccccccc), /* win2k */
1271 "stubMsg.pPointerQueueState should have been unset instead of %p\n", stubMsg.pPointerQueueState);
1272 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1273 TEST_ZERO(PointerBufferMark, "%p");
1274 ok(stubMsg.CorrDespIncrement == 0xcc, "CorrDespIncrement should have been unset instead of 0x%x\n", stubMsg.CorrDespIncrement);
1275 TEST_ZERO(uFlags, "%d");
1276 /* FIXME: UniquePtrCount */
1277 TEST_ULONG_PTR_UNSET(MaxCount);
1278 TEST_ULONG_UNSET(Offset);
1279 TEST_ULONG_UNSET(ActualCount);
1280 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1281 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1282 TEST_ZERO(StackTop, "%p");
1283 TEST_POINTER_UNSET(pPresentedType);
1284 TEST_POINTER_UNSET(pTransmitType);
1285 TEST_POINTER_UNSET(SavedHandle);
1286 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1287 TEST_ZERO(FullPtrXlatTables, "%p");
1288 TEST_ZERO(FullPtrRefId, "%d");
1289 TEST_ZERO(PointerLength, "%d");
1290 TEST_ZERO(fInDontFree, "%d");
1291 TEST_ZERO(fDontCallFreeInst, "%d");
1292 TEST_ZERO(fInOnlyParam, "%d");
1293 TEST_ZERO(fHasReturn, "%d");
1294 TEST_ZERO(fHasExtensions, "%d");
1295 TEST_ZERO(fHasNewCorrDesc, "%d");
1296 TEST_ZERO(fIsIn, "%d");
1297 TEST_ZERO(fIsOut, "%d");
1298 TEST_ZERO(fIsOicf, "%d");
1299 trace("fBufferValid = %d\n", stubMsg.fBufferValid);
1300 TEST_ZERO(fHasMemoryValidateCallback, "%d");
1301 TEST_ZERO(fInFree, "%d");
1302 TEST_ZERO(fNeedMCCP, "%d");
1303 TEST_ZERO(fUnused, "0x%x");
1304 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xffffcccc instead of 0x%x\n", stubMsg.fUnused2);
1305 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1306 TEST_ZERO(pvDestContext, "%p");
1307 TEST_POINTER_UNSET(SavedContextHandles);
1308 TEST_ULONG_UNSET(ParamNumber);
1309 TEST_ZERO(pRpcChannelBuffer, "%p");
1310 TEST_ZERO(pArrayInfo, "%p");
1311 TEST_POINTER_UNSET(SizePtrCountArray);
1312 TEST_POINTER_UNSET(SizePtrOffsetArray);
1313 TEST_POINTER_UNSET(SizePtrLengthArray);
1314 TEST_POINTER_UNSET(pArgQueue);
1315 TEST_ZERO(dwStubPhase, "%d");
1316 /* FIXME: where does this value come from? */
1317 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1318 TEST_ZERO(pAsyncMsg, "%p");
1319 TEST_ZERO(pCorrInfo, "%p");
1320 TEST_ZERO(pCorrMemory, "%p");
1321 TEST_ZERO(pMemoryList, "%p");
1322 TEST_POINTER_UNSET(pCSInfo);
1323 TEST_POINTER_UNSET(ConformanceMark);
1324 TEST_POINTER_UNSET(VarianceMark);
1325 ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1326 TEST_POINTER_UNSET(pContext);
1327 TEST_POINTER_UNSET(ContextHandleHash);
1328 TEST_POINTER_UNSET(pUserMarshalList);
1329 TEST_ULONG_PTR_UNSET(Reserved51_3);
1330 TEST_ULONG_PTR_UNSET(Reserved51_4);
1331 TEST_ULONG_PTR_UNSET(Reserved51_5);
1332 #undef TEST_ULONG_UNSET
1333 #undef TEST_POINTER_UNSET
1334 #undef TEST_ZERO
1338 static void test_ndr_allocate(void)
1340 RPC_MESSAGE RpcMessage;
1341 MIDL_STUB_MESSAGE StubMsg;
1342 MIDL_STUB_DESC StubDesc;
1343 void *p1, *p2;
1344 struct tag_mem_list_v1_t
1346 DWORD magic;
1347 void *ptr;
1348 struct tag_mem_list_v1_t *next;
1349 } *mem_list_v1;
1350 struct tag_mem_list_v2_t
1352 DWORD magic;
1353 DWORD size;
1354 DWORD unknown;
1355 struct tag_mem_list_v2_t *next;
1356 } *mem_list_v2;
1357 const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
1359 StubDesc = Object_StubDesc;
1360 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
1362 ok(StubMsg.pMemoryList == NULL, "memlist %p\n", StubMsg.pMemoryList);
1363 my_alloc_called = my_free_called = 0;
1364 p1 = NdrAllocate(&StubMsg, 10);
1365 p2 = NdrAllocate(&StubMsg, 24);
1366 ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
1367 ok(StubMsg.pMemoryList != NULL, "StubMsg.pMemoryList NULL\n");
1368 if(StubMsg.pMemoryList)
1370 mem_list_v2 = StubMsg.pMemoryList;
1371 if (mem_list_v2->size == 24)
1373 trace("v2 mem list format\n");
1374 ok((char *)mem_list_v2 == (char *)p2 + 24, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p2 + 24, mem_list_v2);
1375 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1376 ok(mem_list_v2->size == 24, "wrong size for p2 %d\n", mem_list_v2->size);
1377 ok(mem_list_v2->unknown == 0, "wrong unknown for p2 0x%x\n", mem_list_v2->unknown);
1378 ok(mem_list_v2->next != NULL, "next NULL\n");
1379 mem_list_v2 = mem_list_v2->next;
1380 if(mem_list_v2)
1382 ok((char *)mem_list_v2 == (char *)p1 + 16, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p1 + 16, mem_list_v2);
1383 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1384 ok(mem_list_v2->size == 16, "wrong size for p1 %d\n", mem_list_v2->size);
1385 ok(mem_list_v2->unknown == 0, "wrong unknown for p1 0x%x\n", mem_list_v2->unknown);
1386 ok(mem_list_v2->next == NULL, "next %p\n", mem_list_v2->next);
1389 else
1391 trace("v1 mem list format\n");
1392 mem_list_v1 = StubMsg.pMemoryList;
1393 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1394 ok(mem_list_v1->ptr == p2, "ptr != p2\n");
1395 ok(mem_list_v1->next != NULL, "next NULL\n");
1396 mem_list_v1 = mem_list_v1->next;
1397 if(mem_list_v1)
1399 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1400 ok(mem_list_v1->ptr == p1, "ptr != p1\n");
1401 ok(mem_list_v1->next == NULL, "next %p\n", mem_list_v1->next);
1405 /* NdrFree isn't exported so we can't test free'ing */
1408 static void test_conformant_array(void)
1410 RPC_MESSAGE RpcMessage;
1411 MIDL_STUB_MESSAGE StubMsg;
1412 MIDL_STUB_DESC StubDesc;
1413 void *ptr;
1414 unsigned char *mem, *mem_orig;
1415 unsigned char memsrc[20];
1416 unsigned int i;
1418 static const unsigned char fmtstr_conf_array[] =
1420 0x1b, /* FC_CARRAY */
1421 0x0, /* align */
1422 NdrFcShort( 0x1 ), /* elem size */
1423 0x40, /* Corr desc: const */
1424 0x0,
1425 NdrFcShort(0x10), /* const = 0x10 */
1426 0x1, /* FC_BYTE */
1427 0x5b /* FC_END */
1430 for (i = 0; i < sizeof(memsrc); i++)
1431 memsrc[i] = i * i;
1433 StubDesc = Object_StubDesc;
1434 StubDesc.pFormatTypes = fmtstr_conf_array;
1436 NdrClientInitializeNew(
1437 &RpcMessage,
1438 &StubMsg,
1439 &StubDesc,
1442 StubMsg.BufferLength = 0;
1443 NdrConformantArrayBufferSize( &StubMsg,
1444 memsrc,
1445 fmtstr_conf_array );
1446 ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1448 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1449 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1450 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1452 ptr = NdrConformantArrayMarshall( &StubMsg, memsrc, fmtstr_conf_array );
1453 ok(ptr == NULL, "ret %p\n", ptr);
1454 ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1455 ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1457 StubMsg.Buffer = StubMsg.BufferStart;
1458 StubMsg.MemorySize = 0;
1459 mem = NULL;
1461 /* Client */
1462 my_alloc_called = 0;
1463 /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1464 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1465 ok(mem != NULL, "mem not alloced\n");
1466 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1467 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1469 my_alloc_called = 0;
1470 StubMsg.Buffer = StubMsg.BufferStart;
1471 mem_orig = mem;
1472 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1473 ok(mem == mem_orig, "mem alloced\n");
1474 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1475 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1477 my_alloc_called = 0;
1478 StubMsg.Buffer = StubMsg.BufferStart;
1479 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1480 ok(mem != mem_orig, "mem not alloced\n");
1481 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1482 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1484 my_free_called = 0;
1485 StubMsg.Buffer = StubMsg.BufferStart;
1486 NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1487 ok(my_free_called == 0, "free called %d\n", my_free_called);
1488 StubMsg.pfnFree(mem);
1490 /* Server */
1491 my_alloc_called = 0;
1492 StubMsg.IsClient = 0;
1493 mem = NULL;
1494 StubMsg.Buffer = StubMsg.BufferStart;
1495 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1496 ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1497 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1498 my_alloc_called = 0;
1499 mem = NULL;
1500 StubMsg.Buffer = StubMsg.BufferStart;
1501 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1502 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1503 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1504 StubMsg.pfnFree(mem);
1506 my_alloc_called = 0;
1507 mem = mem_orig;
1508 StubMsg.Buffer = StubMsg.BufferStart;
1509 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1510 ok(mem == mem_orig, "mem alloced\n");
1511 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1513 my_alloc_called = 0;
1514 mem = mem_orig;
1515 StubMsg.Buffer = StubMsg.BufferStart;
1516 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1517 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1518 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1519 StubMsg.pfnFree(mem);
1520 StubMsg.pfnFree(mem_orig);
1522 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1525 static void test_conformant_string(void)
1527 RPC_MESSAGE RpcMessage;
1528 MIDL_STUB_MESSAGE StubMsg;
1529 MIDL_STUB_DESC StubDesc;
1530 void *ptr;
1531 unsigned char *mem, *mem_orig;
1532 char memsrc[] = "This is a test string";
1534 static const unsigned char fmtstr_conf_str[] =
1536 0x11, 0x8, /* FC_RP [simple_pointer] */
1537 0x22, /* FC_C_CSTRING */
1538 0x5c, /* FC_PAD */
1541 StubDesc = Object_StubDesc;
1542 StubDesc.pFormatTypes = fmtstr_conf_str;
1544 NdrClientInitializeNew(
1545 &RpcMessage,
1546 &StubMsg,
1547 &StubDesc,
1550 StubMsg.BufferLength = 0;
1551 NdrPointerBufferSize( &StubMsg,
1552 (unsigned char *)memsrc,
1553 fmtstr_conf_str );
1554 ok(StubMsg.BufferLength >= sizeof(memsrc) + 12, "length %d\n", StubMsg.BufferLength);
1556 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1557 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1558 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1560 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_conf_str );
1561 ok(ptr == NULL, "ret %p\n", ptr);
1562 ok(StubMsg.Buffer - StubMsg.BufferStart == sizeof(memsrc) + 12, "Buffer %p Start %p len %d\n",
1563 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1564 ok(!memcmp(StubMsg.BufferStart + 12, memsrc, sizeof(memsrc)), "incorrectly marshaled\n");
1566 StubMsg.Buffer = StubMsg.BufferStart;
1567 StubMsg.MemorySize = 0;
1568 mem = NULL;
1570 /* Client */
1571 my_alloc_called = 0;
1572 StubMsg.Buffer = StubMsg.BufferStart;
1573 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1574 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1575 ok(mem == mem_orig, "mem not alloced\n");
1576 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1578 my_alloc_called = 0;
1579 StubMsg.Buffer = StubMsg.BufferStart;
1580 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1581 todo_wine {
1582 ok(mem == mem_orig, "mem not alloced\n");
1583 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1586 my_free_called = 0;
1587 StubMsg.Buffer = StubMsg.BufferStart;
1588 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1589 ok(my_free_called == 1, "free called %d\n", my_free_called);
1591 mem = my_alloc(10);
1592 my_free_called = 0;
1593 StubMsg.Buffer = StubMsg.BufferStart;
1594 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1595 ok(my_free_called == 1, "free called %d\n", my_free_called);
1597 /* Server */
1598 my_alloc_called = 0;
1599 StubMsg.IsClient = 0;
1600 mem = NULL;
1601 StubMsg.Buffer = StubMsg.BufferStart;
1602 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1603 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1604 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1606 my_alloc_called = 0;
1607 mem = NULL;
1608 StubMsg.Buffer = StubMsg.BufferStart;
1609 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1610 todo_wine {
1611 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1612 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1615 my_alloc_called = 0;
1616 mem = mem_orig;
1617 StubMsg.Buffer = StubMsg.BufferStart;
1618 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1619 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1620 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1622 my_alloc_called = 0;
1623 mem = mem_orig;
1624 StubMsg.Buffer = StubMsg.BufferStart;
1625 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1626 todo_wine {
1627 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1628 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1631 mem = my_alloc(10);
1632 my_free_called = 0;
1633 StubMsg.Buffer = StubMsg.BufferStart;
1634 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1635 ok(my_free_called == 1, "free called %d\n", my_free_called);
1637 HeapFree(GetProcessHeap(), 0, mem_orig);
1638 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1641 static void test_nonconformant_string(void)
1643 RPC_MESSAGE RpcMessage;
1644 MIDL_STUB_MESSAGE StubMsg;
1645 MIDL_STUB_DESC StubDesc;
1646 void *ptr;
1647 unsigned char *mem, *mem_orig;
1648 unsigned char memsrc[10] = "This is";
1649 unsigned char memsrc2[10] = "This is a";
1651 static const unsigned char fmtstr_nonconf_str[] =
1653 0x26, /* FC_CSTRING */
1654 0x5c, /* FC_PAD */
1655 NdrFcShort( 0xa ), /* 10 */
1658 StubDesc = Object_StubDesc;
1659 StubDesc.pFormatTypes = fmtstr_nonconf_str;
1661 /* length < size */
1662 NdrClientInitializeNew(
1663 &RpcMessage,
1664 &StubMsg,
1665 &StubDesc,
1668 StubMsg.BufferLength = 0;
1670 NdrNonConformantStringBufferSize( &StubMsg,
1671 (unsigned char *)memsrc,
1672 fmtstr_nonconf_str );
1673 ok(StubMsg.BufferLength >= strlen((char *)memsrc) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1675 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1676 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1677 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1679 ptr = NdrNonConformantStringMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_nonconf_str );
1680 ok(ptr == NULL, "ret %p\n", ptr);
1681 ok(StubMsg.Buffer - StubMsg.BufferStart == strlen((char *)memsrc) + 1 + 8, "Buffer %p Start %p len %d\n",
1682 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1683 ok(!memcmp(StubMsg.BufferStart + 8, memsrc, strlen((char *)memsrc) + 1), "incorrectly marshaled\n");
1685 StubMsg.Buffer = StubMsg.BufferStart;
1686 StubMsg.MemorySize = 0;
1687 mem = NULL;
1689 /* Client */
1690 my_alloc_called = 0;
1691 StubMsg.Buffer = StubMsg.BufferStart;
1692 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1693 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1694 ok(mem == mem_orig, "mem alloced\n");
1695 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1697 my_alloc_called = 0;
1698 StubMsg.Buffer = StubMsg.BufferStart;
1699 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1700 todo_wine
1701 ok(mem == mem_orig, "mem alloced\n");
1702 todo_wine
1703 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1705 /* Server */
1706 my_alloc_called = 0;
1707 StubMsg.IsClient = 0;
1708 mem = NULL;
1709 StubMsg.Buffer = StubMsg.BufferStart;
1710 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1711 ok(mem != mem_orig, "mem not alloced\n");
1712 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1713 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1714 NdrOleFree(mem);
1716 my_alloc_called = 0;
1717 mem = mem_orig;
1718 StubMsg.Buffer = StubMsg.BufferStart;
1719 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1720 ok(mem == mem_orig, "mem alloced\n");
1721 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1723 my_alloc_called = 0;
1724 mem = mem_orig;
1725 StubMsg.Buffer = StubMsg.BufferStart;
1726 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1727 todo_wine
1728 ok(mem == mem_orig, "mem alloced\n");
1729 todo_wine
1730 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1732 HeapFree(GetProcessHeap(), 0, mem_orig);
1733 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1735 /* length = size */
1736 NdrClientInitializeNew(
1737 &RpcMessage,
1738 &StubMsg,
1739 &StubDesc,
1742 StubMsg.BufferLength = 0;
1744 NdrNonConformantStringBufferSize( &StubMsg,
1745 (unsigned char *)memsrc2,
1746 fmtstr_nonconf_str );
1747 ok(StubMsg.BufferLength >= strlen((char *)memsrc2) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1749 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1750 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1751 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1753 ptr = NdrNonConformantStringMarshall( &StubMsg, (unsigned char *)memsrc2, fmtstr_nonconf_str );
1754 ok(ptr == NULL, "ret %p\n", ptr);
1755 ok(StubMsg.Buffer - StubMsg.BufferStart == strlen((char *)memsrc2) + 1 + 8, "Buffer %p Start %p len %d\n",
1756 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1757 ok(!memcmp(StubMsg.BufferStart + 8, memsrc2, strlen((char *)memsrc2) + 1), "incorrectly marshaled\n");
1759 StubMsg.Buffer = StubMsg.BufferStart;
1760 StubMsg.MemorySize = 0;
1761 mem = NULL;
1763 /* Client */
1764 my_alloc_called = 0;
1765 StubMsg.Buffer = StubMsg.BufferStart;
1766 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1767 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1768 ok(mem == mem_orig, "mem alloced\n");
1769 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1771 my_alloc_called = 0;
1772 StubMsg.Buffer = StubMsg.BufferStart;
1773 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1774 todo_wine
1775 ok(mem == mem_orig, "mem alloced\n");
1776 todo_wine
1777 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1779 /* Server */
1780 my_alloc_called = 0;
1781 StubMsg.IsClient = 0;
1782 mem = NULL;
1783 StubMsg.Buffer = StubMsg.BufferStart;
1784 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1785 ok(mem != mem_orig, "mem not alloced\n");
1786 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1787 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1788 NdrOleFree(mem);
1790 my_alloc_called = 0;
1791 mem = mem_orig;
1792 StubMsg.Buffer = StubMsg.BufferStart;
1793 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1794 ok(mem == mem_orig, "mem alloced\n");
1795 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1797 my_alloc_called = 0;
1798 mem = mem_orig;
1799 StubMsg.Buffer = StubMsg.BufferStart;
1800 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1801 todo_wine
1802 ok(mem == mem_orig, "mem alloced\n");
1803 todo_wine
1804 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1806 HeapFree(GetProcessHeap(), 0, mem_orig);
1807 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1810 static void test_conf_complex_struct(void)
1812 RPC_MESSAGE RpcMessage;
1813 MIDL_STUB_MESSAGE StubMsg;
1814 MIDL_STUB_DESC StubDesc;
1815 void *ptr;
1816 unsigned int i;
1817 struct conf_complex
1819 unsigned int size;
1820 unsigned int *array[1];
1822 struct conf_complex *memsrc;
1823 struct conf_complex *mem;
1825 static const unsigned char fmtstr_complex_struct[] =
1827 /* 0 */
1828 0x1b, /* FC_CARRAY */
1829 0x3, /* 3 */
1830 /* 2 */ NdrFcShort( 0x4 ), /* 4 */
1831 /* 4 */ 0x8, /* Corr desc: FC_LONG */
1832 0x0, /* */
1833 /* 6 */ NdrFcShort( 0xfffc ), /* -4 */
1834 /* 8 */
1835 0x4b, /* FC_PP */
1836 0x5c, /* FC_PAD */
1837 /* 10 */
1838 0x48, /* FC_VARIABLE_REPEAT */
1839 0x49, /* FC_FIXED_OFFSET */
1840 /* 12 */ NdrFcShort( 0x4 ), /* 4 */
1841 /* 14 */ NdrFcShort( 0x0 ), /* 0 */
1842 /* 16 */ NdrFcShort( 0x1 ), /* 1 */
1843 /* 18 */ NdrFcShort( 0x0 ), /* 0 */
1844 /* 20 */ NdrFcShort( 0x0 ), /* 0 */
1845 /* 22 */ 0x12, 0x8, /* FC_UP [simple_pointer] */
1846 /* 24 */ 0x8, /* FC_LONG */
1847 0x5c, /* FC_PAD */
1848 /* 26 */
1849 0x5b, /* FC_END */
1851 0x8, /* FC_LONG */
1852 /* 28 */ 0x5c, /* FC_PAD */
1853 0x5b, /* FC_END */
1854 /* 30 */
1855 0x1a, /* FC_BOGUS_STRUCT */
1856 0x3, /* 3 */
1857 /* 32 */ NdrFcShort( 0x4 ), /* 4 */
1858 /* 34 */ NdrFcShort( 0xffffffde ), /* Offset= -34 (0) */
1859 /* 36 */ NdrFcShort( 0x0 ), /* Offset= 0 (36) */
1860 /* 38 */ 0x8, /* FC_LONG */
1861 0x5b, /* FC_END */
1864 memsrc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1865 FIELD_OFFSET(struct conf_complex, array[20]));
1866 memsrc->size = 20;
1868 StubDesc = Object_StubDesc;
1869 StubDesc.pFormatTypes = fmtstr_complex_struct;
1871 NdrClientInitializeNew(
1872 &RpcMessage,
1873 &StubMsg,
1874 &StubDesc,
1877 StubMsg.BufferLength = 0;
1878 NdrComplexStructBufferSize( &StubMsg,
1879 (unsigned char *)memsrc,
1880 &fmtstr_complex_struct[30] );
1881 ok(StubMsg.BufferLength >= 28, "length %d\n", StubMsg.BufferLength);
1883 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1884 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1885 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1887 ptr = NdrComplexStructMarshall( &StubMsg, (unsigned char *)memsrc,
1888 &fmtstr_complex_struct[30] );
1889 ok(ptr == NULL, "ret %p\n", ptr);
1890 ok(*(unsigned int *)StubMsg.BufferStart == 20, "Conformance should have been 20 instead of %d\n", (unsigned int)StubMsg.BufferStart);
1891 ok(*(unsigned int *)(StubMsg.BufferStart + 4) == 20, "conf_complex.size should have been 20 instead of %d\n", (unsigned int)(StubMsg.BufferStart + 4));
1892 for (i = 0; i < 20; i++)
1893 ok(*(unsigned int *)(StubMsg.BufferStart + 8 + i * 4) == 0, "pointer id for conf_complex.array[%d] should have been 0 instead of 0x%x\n", i, *(unsigned int *)(StubMsg.BufferStart + 8 + i * 4));
1895 /* Server */
1896 my_alloc_called = 0;
1897 StubMsg.IsClient = 0;
1898 mem = NULL;
1899 StubMsg.Buffer = StubMsg.BufferStart;
1900 ptr = NdrComplexStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_struct[30], 0);
1901 ok(ptr == NULL, "ret %p\n", ptr);
1902 ok(mem->size == 20, "mem->size wasn't unmarshalled correctly (%d)\n", mem->size);
1903 ok(mem->array[0] == NULL, "mem->array[0] wasn't unmarshalled correctly (%p)\n", mem->array[0]);
1904 StubMsg.pfnFree(mem);
1906 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1909 static void test_ndr_buffer(void)
1911 static unsigned char ncalrpc[] = "ncalrpc";
1912 static unsigned char endpoint[] = "winetest:test_ndr_buffer";
1913 RPC_MESSAGE RpcMessage;
1914 MIDL_STUB_MESSAGE StubMsg;
1915 MIDL_STUB_DESC StubDesc = Object_StubDesc;
1916 unsigned char *ret;
1917 unsigned char *binding;
1918 RPC_BINDING_HANDLE Handle;
1919 RPC_STATUS status;
1920 ULONG prev_buffer_length;
1921 BOOL old_buffer_valid_location;
1923 StubDesc.RpcInterfaceInformation = (void *)&IFoo___RpcServerInterface;
1925 status = RpcServerUseProtseqEp(ncalrpc, 20, endpoint, NULL);
1926 ok(RPC_S_OK == status, "RpcServerUseProtseqEp failed with status %lu\n", status);
1927 status = RpcServerRegisterIf(IFoo_v0_0_s_ifspec, NULL, NULL);
1928 ok(RPC_S_OK == status, "RpcServerRegisterIf failed with status %lu\n", status);
1929 status = RpcServerListen(1, 20, TRUE);
1930 ok(RPC_S_OK == status, "RpcServerListen failed with status %lu\n", status);
1931 if (status != RPC_S_OK)
1933 /* Failed to create a server, running client tests is useless */
1934 return;
1937 status = RpcStringBindingCompose(NULL, ncalrpc, NULL, endpoint, NULL, &binding);
1938 ok(status == RPC_S_OK, "RpcStringBindingCompose failed (%lu)\n", status);
1940 status = RpcBindingFromStringBinding(binding, &Handle);
1941 ok(status == RPC_S_OK, "RpcBindingFromStringBinding failed (%lu)\n", status);
1942 RpcStringFree(&binding);
1944 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 5);
1946 ret = NdrGetBuffer(&StubMsg, 10, Handle);
1947 ok(ret == StubMsg.Buffer, "NdrGetBuffer should have returned the same value as StubMsg.Buffer instead of %p\n", ret);
1948 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1949 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1950 ok(RpcMessage.BufferLength == 10 ||
1951 broken(RpcMessage.BufferLength == 12), /* win2k */
1952 "RpcMessage.BufferLength should have been 10 instead of %d\n", RpcMessage.BufferLength);
1953 ok(RpcMessage.RpcFlags == 0, "RpcMessage.RpcFlags should have been 0x0 instead of 0x%lx\n", RpcMessage.RpcFlags);
1954 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1955 ok(!StubMsg.BufferStart, "BufferStart should have been NULL instead of %p\n", StubMsg.BufferStart);
1956 ok(!StubMsg.BufferEnd, "BufferEnd should have been NULL instead of %p\n", StubMsg.BufferEnd);
1957 todo_wine
1958 ok(StubMsg.BufferLength == 0, "BufferLength should have left as 0 instead of being set to %d\n", StubMsg.BufferLength);
1959 old_buffer_valid_location = !StubMsg.fBufferValid;
1960 if (old_buffer_valid_location)
1961 ok(broken(StubMsg.CorrDespIncrement == TRUE), "fBufferValid should have been TRUE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1962 else
1963 ok(StubMsg.fBufferValid, "fBufferValid should have been non-zero instead of 0x%x\n", StubMsg.fBufferValid);
1965 prev_buffer_length = RpcMessage.BufferLength;
1966 StubMsg.BufferLength = 1;
1967 NdrFreeBuffer(&StubMsg);
1968 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1969 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1970 ok(RpcMessage.BufferLength == prev_buffer_length, "RpcMessage.BufferLength should have been left as %d instead of %d\n", prev_buffer_length, RpcMessage.BufferLength);
1971 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1972 ok(StubMsg.BufferLength == 1, "BufferLength should have left as 1 instead of being set to %d\n", StubMsg.BufferLength);
1973 if (old_buffer_valid_location)
1974 ok(broken(StubMsg.CorrDespIncrement == FALSE), "fBufferValid should have been FALSE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1975 else
1976 ok(!StubMsg.fBufferValid, "fBufferValid should have been FALSE instead of %d\n", StubMsg.fBufferValid);
1978 /* attempt double-free */
1979 NdrFreeBuffer(&StubMsg);
1981 RpcBindingFree(&Handle);
1983 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
1984 ok(status == RPC_S_OK, "RpcServerUnregisterIf failed (%lu)\n", status);
1987 static void test_NdrMapCommAndFaultStatus(void)
1989 RPC_STATUS rpc_status;
1990 MIDL_STUB_MESSAGE StubMsg;
1991 RPC_MESSAGE RpcMessage;
1993 NdrClientInitializeNew(&RpcMessage, &StubMsg, &Object_StubDesc, 5);
1995 for (rpc_status = 0; rpc_status < 10000; rpc_status++)
1997 RPC_STATUS status;
1998 ULONG comm_status = 0;
1999 ULONG fault_status = 0;
2000 ULONG expected_comm_status = 0;
2001 ULONG expected_fault_status = 0;
2002 status = NdrMapCommAndFaultStatus(&StubMsg, &comm_status, &fault_status, rpc_status);
2003 ok(status == RPC_S_OK, "NdrMapCommAndFaultStatus failed with error %ld\n", status);
2004 switch (rpc_status)
2006 case ERROR_INVALID_HANDLE:
2007 case RPC_S_INVALID_BINDING:
2008 case RPC_S_UNKNOWN_IF:
2009 case RPC_S_SERVER_UNAVAILABLE:
2010 case RPC_S_SERVER_TOO_BUSY:
2011 case RPC_S_CALL_FAILED_DNE:
2012 case RPC_S_PROTOCOL_ERROR:
2013 case RPC_S_UNSUPPORTED_TRANS_SYN:
2014 case RPC_S_UNSUPPORTED_TYPE:
2015 case RPC_S_PROCNUM_OUT_OF_RANGE:
2016 case EPT_S_NOT_REGISTERED:
2017 case RPC_S_COMM_FAILURE:
2018 expected_comm_status = rpc_status;
2019 break;
2020 default:
2021 expected_fault_status = rpc_status;
2023 ok(comm_status == expected_comm_status, "NdrMapCommAndFaultStatus should have mapped %ld to comm status %d instead of %d\n",
2024 rpc_status, expected_comm_status, comm_status);
2025 ok(fault_status == expected_fault_status, "NdrMapCommAndFaultStatus should have mapped %ld to fault status %d instead of %d\n",
2026 rpc_status, expected_fault_status, fault_status);
2030 START_TEST( ndr_marshall )
2032 determine_pointer_marshalling_style();
2034 test_ndr_simple_type();
2035 test_simple_types();
2036 test_nontrivial_pointer_types();
2037 test_simple_struct();
2038 test_fullpointer_xlat();
2039 test_client_init();
2040 test_server_init();
2041 test_ndr_allocate();
2042 test_conformant_array();
2043 test_conformant_string();
2044 test_nonconformant_string();
2045 test_conf_complex_struct();
2046 test_ndr_buffer();
2047 test_NdrMapCommAndFaultStatus();