rpcrt4: Fix ndr_marshall test failures.
[wine/testsucceed.git] / dlls / rpcrt4 / tests / ndr_marshall.c
blob1ff5e3b04380a9a0084c414ed4e89097a74ac4da
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;
101 static void test_ndr_simple_type(void)
103 RPC_MESSAGE RpcMessage;
104 MIDL_STUB_MESSAGE StubMsg;
105 MIDL_STUB_DESC StubDesc;
106 long l, l2 = 0;
108 StubDesc = Object_StubDesc;
109 StubDesc.pFormatTypes = NULL;
111 NdrClientInitializeNew(
112 &RpcMessage,
113 &StubMsg,
114 &StubDesc,
117 StubMsg.BufferLength = 16;
118 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
119 l = 0xcafebabe;
120 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
121 ok(StubMsg.Buffer == StubMsg.BufferStart + 4, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
122 ok(*(long*)StubMsg.BufferStart == l, "%ld\n", *(long*)StubMsg.BufferStart);
124 StubMsg.Buffer = StubMsg.BufferStart + 1;
125 NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
126 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
127 ok(*(long*)(StubMsg.BufferStart + 4) == l, "%ld\n", *(long*)StubMsg.BufferStart);
129 StubMsg.Buffer = StubMsg.BufferStart + 1;
130 NdrSimpleTypeUnmarshall(&StubMsg, (unsigned char*)&l2, 8 /* FC_LONG */);
131 ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
132 ok(l2 == l, "%ld\n", l2);
134 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
137 static void test_pointer_marshal(const unsigned char *formattypes,
138 void *memsrc,
139 long srcsize,
140 const void *wiredata,
141 ULONG wiredatalen,
142 int(*cmp)(const void*,const void*,size_t),
143 long num_additional_allocs,
144 const char *msgpfx)
146 RPC_MESSAGE RpcMessage;
147 MIDL_STUB_MESSAGE StubMsg;
148 MIDL_STUB_DESC StubDesc;
149 DWORD size;
150 void *ptr;
151 unsigned char *mem, *mem_orig;
153 my_alloc_called = my_free_called = 0;
154 if(!cmp)
155 cmp = memcmp;
157 StubDesc = Object_StubDesc;
158 StubDesc.pFormatTypes = formattypes;
160 NdrClientInitializeNew(
161 &RpcMessage,
162 &StubMsg,
163 &StubDesc,
166 StubMsg.BufferLength = 0;
167 NdrPointerBufferSize( &StubMsg,
168 memsrc,
169 formattypes );
170 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
172 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
173 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
174 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
176 memset(StubMsg.BufferStart, 0x0, StubMsg.BufferLength); /* This is a hack to clear the padding between the ptr and longlong/double */
178 ptr = NdrPointerMarshall( &StubMsg, memsrc, formattypes );
179 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
180 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
181 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled\n", msgpfx);
183 StubMsg.Buffer = StubMsg.BufferStart;
184 StubMsg.MemorySize = 0;
186 if (0)
188 /* NdrPointerMemorySize crashes under Wine */
189 size = NdrPointerMemorySize( &StubMsg, formattypes );
190 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
191 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
192 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
193 ok(size == srcsize + 4, "%s: mem size %u\n", msgpfx, size);
194 else
195 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
197 StubMsg.Buffer = StubMsg.BufferStart;
198 StubMsg.MemorySize = 16;
199 size = NdrPointerMemorySize( &StubMsg, formattypes );
200 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
201 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
202 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
203 ok(size == srcsize + 4 + 16, "%s: mem size %u\n", msgpfx, size);
204 else
205 ok(size == srcsize + 16, "%s: mem size %u\n", msgpfx, size);
207 StubMsg.Buffer = StubMsg.BufferStart;
208 StubMsg.MemorySize = 1;
209 size = NdrPointerMemorySize( &StubMsg, formattypes );
210 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
211 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
212 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
213 ok(size == srcsize + 4 + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
214 else
215 ok(size == srcsize + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
218 size = srcsize;
219 if(formattypes[1] & 0x10) size += 4;
221 StubMsg.Buffer = StubMsg.BufferStart;
222 StubMsg.MemorySize = 0;
223 mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, size);
225 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
226 *(void**)mem = NULL;
227 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
228 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
229 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
230 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
231 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
232 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
233 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
234 my_alloc_called = 0;
236 /* reset the buffer and call with must alloc */
237 StubMsg.Buffer = StubMsg.BufferStart;
238 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
239 *(void**)mem = NULL;
240 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 1 );
241 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
242 /* doesn't allocate mem in this case */
243 todo_wine {
244 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
246 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
247 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
248 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
250 todo_wine {
251 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
253 my_alloc_called = 0;
254 if(formattypes[0] != 0x11 /* FC_RP */)
256 /* now pass the address of a NULL ptr */
257 mem = NULL;
258 StubMsg.Buffer = StubMsg.BufferStart;
259 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
260 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
261 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
262 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
263 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
264 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
265 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
266 my_alloc_called = 0;
267 NdrPointerFree(&StubMsg, mem, formattypes);
269 /* again pass address of NULL ptr, but pretend we're a server */
270 mem = NULL;
271 StubMsg.Buffer = StubMsg.BufferStart;
272 StubMsg.IsClient = 0;
273 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
274 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
275 if (formattypes[2] == 0xd /* FC_ENUM16 */)
276 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
277 else
278 ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
279 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
280 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
281 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
282 if (formattypes[2] != 0xd /* FC_ENUM16 */) {
283 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
284 my_alloc_called = 0;
287 HeapFree(GetProcessHeap(), 0, mem_orig);
288 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
291 static int deref_cmp(const void *s1, const void *s2, size_t num)
293 return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
297 static void test_simple_types(void)
299 unsigned char wiredata[16];
300 unsigned char ch;
301 unsigned char *ch_ptr;
302 unsigned short s;
303 unsigned int i;
304 unsigned long l;
305 ULONGLONG ll;
306 float f;
307 double d;
309 static const unsigned char fmtstr_up_char[] =
311 0x12, 0x8, /* FC_UP [simple_pointer] */
312 0x2, /* FC_CHAR */
313 0x5c, /* FC_PAD */
315 static const unsigned char fmtstr_up_byte[] =
317 0x12, 0x8, /* FC_UP [simple_pointer] */
318 0x1, /* FC_BYTE */
319 0x5c, /* FC_PAD */
321 static const unsigned char fmtstr_up_small[] =
323 0x12, 0x8, /* FC_UP [simple_pointer] */
324 0x3, /* FC_SMALL */
325 0x5c, /* FC_PAD */
327 static const unsigned char fmtstr_up_usmall[] =
329 0x12, 0x8, /* FC_UP [simple_pointer] */
330 0x4, /* FC_USMALL */
331 0x5c, /* FC_PAD */
333 static const unsigned char fmtstr_rp_char[] =
335 0x11, 0x8, /* FC_RP [simple_pointer] */
336 0x2, /* FC_CHAR */
337 0x5c, /* FC_PAD */
339 static const unsigned char fmtstr_rpup_char[] =
341 0x11, 0x14, /* FC_RP [alloced_on_stack] */
342 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
343 0x12, 0x8, /* FC_UP [simple_pointer] */
344 0x2, /* FC_CHAR */
345 0x5c, /* FC_PAD */
347 static const unsigned char fmtstr_rpup_char2[] =
349 0x11, 0x04, /* FC_RP [alloced_on_stack] */
350 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
351 0x12, 0x8, /* FC_UP [simple_pointer] */
352 0x2, /* FC_CHAR */
353 0x5c, /* FC_PAD */
356 static const unsigned char fmtstr_up_wchar[] =
358 0x12, 0x8, /* FC_UP [simple_pointer] */
359 0x5, /* FC_WCHAR */
360 0x5c, /* FC_PAD */
362 static const unsigned char fmtstr_up_short[] =
364 0x12, 0x8, /* FC_UP [simple_pointer] */
365 0x6, /* FC_SHORT */
366 0x5c, /* FC_PAD */
368 static const unsigned char fmtstr_up_ushort[] =
370 0x12, 0x8, /* FC_UP [simple_pointer] */
371 0x7, /* FC_USHORT */
372 0x5c, /* FC_PAD */
374 static const unsigned char fmtstr_up_enum16[] =
376 0x12, 0x8, /* FC_UP [simple_pointer] */
377 0xd, /* FC_ENUM16 */
378 0x5c, /* FC_PAD */
380 static const unsigned char fmtstr_up_long[] =
382 0x12, 0x8, /* FC_UP [simple_pointer] */
383 0x8, /* FC_LONG */
384 0x5c, /* FC_PAD */
386 static const unsigned char fmtstr_up_ulong[] =
388 0x12, 0x8, /* FC_UP [simple_pointer] */
389 0x9, /* FC_ULONG */
390 0x5c, /* FC_PAD */
392 static const unsigned char fmtstr_up_enum32[] =
394 0x12, 0x8, /* FC_UP [simple_pointer] */
395 0xe, /* FC_ENUM32 */
396 0x5c, /* FC_PAD */
398 static const unsigned char fmtstr_up_errorstatus[] =
400 0x12, 0x8, /* FC_UP [simple_pointer] */
401 0x10, /* FC_ERROR_STATUS_T */
402 0x5c, /* FC_PAD */
405 static const unsigned char fmtstr_up_longlong[] =
407 0x12, 0x8, /* FC_UP [simple_pointer] */
408 0xb, /* FC_HYPER */
409 0x5c, /* FC_PAD */
411 static const unsigned char fmtstr_up_float[] =
413 0x12, 0x8, /* FC_UP [simple_pointer] */
414 0xa, /* FC_FLOAT */
415 0x5c, /* FC_PAD */
417 static const unsigned char fmtstr_up_double[] =
419 0x12, 0x8, /* FC_UP [simple_pointer] */
420 0xc, /* FC_DOUBLE */
421 0x5c, /* FC_PAD */
424 ch = 0xa5;
425 ch_ptr = &ch;
426 *(void**)wiredata = ch_ptr;
427 wiredata[sizeof(void*)] = ch;
429 test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
430 test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
431 test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0, "up_small");
432 test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
434 test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
436 test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
437 test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
439 s = 0xa597;
440 *(void**)wiredata = &s;
441 *(unsigned short*)(wiredata + sizeof(void*)) = s;
443 test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
444 test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
445 test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
447 i = 0x7fff;
448 *(void**)wiredata = &i;
449 *(unsigned short*)(wiredata + sizeof(void*)) = i;
450 test_pointer_marshal(fmtstr_up_enum16, &i, 2, wiredata, 6, NULL, 0, "up_enum16");
452 l = 0xcafebabe;
453 *(void**)wiredata = &l;
454 *(unsigned long*)(wiredata + sizeof(void*)) = l;
456 test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
457 test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0, "up_ulong");
458 test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0, "up_emun32");
459 test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0, "up_errorstatus");
461 ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
462 *(void**)wiredata = &ll;
463 *(void**)(wiredata + sizeof(void*)) = NULL;
464 *(ULONGLONG*)(wiredata + 2 * sizeof(void*)) = ll;
465 test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
467 f = 3.1415f;
468 *(void**)wiredata = &f;
469 *(float*)(wiredata + sizeof(void*)) = f;
470 test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
472 d = 3.1415;
473 *(void**)wiredata = &d;
474 *(void**)(wiredata + sizeof(void*)) = NULL;
475 *(double*)(wiredata + 2 * sizeof(void*)) = d;
476 test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0, "up_double");
480 static void test_nontrivial_pointer_types(void)
482 RPC_MESSAGE RpcMessage;
483 MIDL_STUB_MESSAGE StubMsg;
484 MIDL_STUB_DESC StubDesc;
485 void *ptr;
486 char **p1;
487 char *p2;
488 char ch;
489 unsigned char *mem, *mem_orig;
491 static const unsigned char fmtstr_ref_unique_out[] =
493 0x12, 0x8, /* FC_UP [simple_pointer] */
494 0x2, /* FC_CHAR */
495 0x5c, /* FC_PAD */
496 0x11, 0x14, /* FC_RP [alloced_on_stack] [pointer_deref] */
497 NdrFcShort( 0xfffffffa ), /* Offset= -6 (0) */
500 p1 = &p2;
501 p2 = &ch;
502 ch = 0x22;
504 StubDesc = Object_StubDesc;
505 StubDesc.pFormatTypes = fmtstr_ref_unique_out;
507 NdrClientInitializeNew(
508 &RpcMessage,
509 &StubMsg,
510 &StubDesc,
513 StubMsg.BufferLength = 0;
514 NdrPointerBufferSize( &StubMsg,
515 (unsigned char *)p1,
516 &fmtstr_ref_unique_out[4] );
518 /* Windows overestimates the buffer size */
519 ok(StubMsg.BufferLength >= 5, "length %d\n", StubMsg.BufferLength);
521 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
522 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
523 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
525 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)p1, &fmtstr_ref_unique_out[4] );
526 ok(ptr == NULL, "ret %p\n", ptr);
527 ok(StubMsg.Buffer - StubMsg.BufferStart == 5, "Buffer %p Start %p len %d\n",
528 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
529 ok(*(unsigned int *)StubMsg.BufferStart != 0, "pointer ID marshalled incorrectly\n");
530 ok(*(unsigned char *)(StubMsg.BufferStart + 4) == 0x22, "char data marshalled incorrectly: 0x%x\n",
531 *(unsigned char *)(StubMsg.BufferStart + 4));
533 StubMsg.Buffer = StubMsg.BufferStart;
534 StubMsg.MemorySize = 0;
535 mem = NULL;
537 /* Client */
538 my_alloc_called = 0;
539 StubMsg.Buffer = StubMsg.BufferStart;
540 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(void *));
541 *(void **)mem = NULL;
542 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
543 ok(mem == mem_orig, "mem alloced\n");
544 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
546 my_alloc_called = 0;
547 StubMsg.Buffer = StubMsg.BufferStart;
548 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
549 todo_wine {
550 ok(mem == mem_orig, "mem alloced\n");
551 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
554 my_free_called = 0;
555 StubMsg.Buffer = StubMsg.BufferStart;
556 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
557 ok(my_free_called == 1, "free called %d\n", my_free_called);
559 mem = my_alloc(sizeof(void *));
560 *(void **)mem = NULL;
561 my_free_called = 0;
562 StubMsg.Buffer = StubMsg.BufferStart;
563 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
564 ok(my_free_called == 0, "free called %d\n", my_free_called);
565 my_free(mem);
567 mem = my_alloc(sizeof(void *));
568 *(void **)mem = my_alloc(sizeof(char));
569 my_free_called = 0;
570 StubMsg.Buffer = StubMsg.BufferStart;
571 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
572 ok(my_free_called == 1, "free called %d\n", my_free_called);
573 my_free(mem);
575 /* Server */
576 my_alloc_called = 0;
577 StubMsg.IsClient = 0;
578 mem = NULL;
579 StubMsg.Buffer = StubMsg.BufferStart;
580 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
581 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
582 todo_wine
583 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
584 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
586 my_alloc_called = 0;
587 mem = NULL;
588 StubMsg.Buffer = StubMsg.BufferStart;
589 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
590 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
591 todo_wine
592 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
593 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
595 my_alloc_called = 0;
596 mem = mem_orig;
597 *(void **)mem = NULL;
598 StubMsg.Buffer = StubMsg.BufferStart;
599 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
600 todo_wine {
601 ok(mem == mem_orig, "mem alloced\n");
602 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
605 my_alloc_called = 0;
606 mem = mem_orig;
607 *(void **)mem = NULL;
608 StubMsg.Buffer = StubMsg.BufferStart;
609 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
610 todo_wine {
611 ok(mem == mem_orig, "mem alloced\n");
612 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_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 HeapFree(GetProcessHeap(), 0, mem_orig);
632 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
635 static void test_simple_struct_marshal(const unsigned char *formattypes,
636 void *memsrc,
637 long srcsize,
638 const void *wiredata,
639 ULONG wiredatalen,
640 int(*cmp)(const void*,const void*,size_t),
641 long num_additional_allocs,
642 const char *msgpfx)
644 RPC_MESSAGE RpcMessage;
645 MIDL_STUB_MESSAGE StubMsg;
646 MIDL_STUB_DESC StubDesc;
647 DWORD size;
648 void *ptr;
649 unsigned char *mem, *mem_orig;
651 my_alloc_called = my_free_called = 0;
652 if(!cmp)
653 cmp = memcmp;
655 StubDesc = Object_StubDesc;
656 StubDesc.pFormatTypes = formattypes;
658 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
660 StubMsg.BufferLength = 0;
661 NdrSimpleStructBufferSize( &StubMsg, (unsigned char *)memsrc, formattypes );
662 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
663 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
664 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
665 ptr = NdrSimpleStructMarshall( &StubMsg, (unsigned char*)memsrc, formattypes );
666 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
667 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
668 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));
670 if (0)
672 /* FIXME: Causes Wine to crash */
673 StubMsg.Buffer = StubMsg.BufferStart;
674 StubMsg.MemorySize = 0;
675 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
676 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
677 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
678 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
680 StubMsg.Buffer = StubMsg.BufferStart;
681 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
682 todo_wine {
683 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
685 ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
686 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
688 size = srcsize;
689 /*** Unmarshalling first with must_alloc false ***/
691 StubMsg.Buffer = StubMsg.BufferStart;
692 StubMsg.MemorySize = 0;
693 mem_orig = mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, srcsize);
694 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
695 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
696 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
697 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
698 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
699 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
700 my_alloc_called = 0;
701 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
703 /* If we're a server we still use the supplied memory */
704 StubMsg.Buffer = StubMsg.BufferStart;
705 StubMsg.IsClient = 0;
706 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
707 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
708 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
709 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
710 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
711 my_alloc_called = 0;
712 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
714 /* ...unless we pass a NULL ptr, then the buffer is used.
715 Passing a NULL ptr while we're a client && !must_alloc
716 crashes on Windows, so we won't do that. */
718 mem = NULL;
719 StubMsg.IsClient = 0;
720 StubMsg.Buffer = StubMsg.BufferStart;
721 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
722 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
723 ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
724 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
725 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
726 my_alloc_called = 0;
727 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
729 /*** now must_alloc is true ***/
731 /* with must_alloc set we always allocate new memory whether or not we're
732 a server and also when passing NULL */
733 mem = mem_orig;
734 StubMsg.IsClient = 1;
735 StubMsg.Buffer = StubMsg.BufferStart;
736 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
737 ok(ptr == NULL, "ret %p\n", ptr);
738 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
739 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
740 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
741 my_alloc_called = 0;
742 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
744 mem = NULL;
745 StubMsg.Buffer = StubMsg.BufferStart;
746 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
747 ok(ptr == NULL, "ret %p\n", ptr);
748 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
749 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
750 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
751 my_alloc_called = 0;
752 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
754 mem = mem_orig;
755 StubMsg.Buffer = StubMsg.BufferStart;
756 StubMsg.IsClient = 0;
757 StubMsg.ReuseBuffer = 1;
758 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
759 ok(ptr == NULL, "ret %p\n", ptr);
760 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
761 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
762 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
763 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
764 my_alloc_called = 0;
765 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
767 mem = NULL;
768 StubMsg.Buffer = StubMsg.BufferStart;
769 StubMsg.IsClient = 0;
770 StubMsg.ReuseBuffer = 1;
771 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
772 ok(ptr == NULL, "ret %p\n", ptr);
773 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
774 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
775 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
776 my_alloc_called = 0;
777 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
779 HeapFree(GetProcessHeap(), 0, mem_orig);
780 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
783 typedef struct
785 long l1;
786 long *pl1;
787 char *pc1;
788 } ps1_t;
790 static int ps1_cmp(const void *s1, const void *s2, size_t num)
792 const ps1_t *p1, *p2;
794 p1 = s1;
795 p2 = s2;
797 if(p1->l1 != p2->l1)
798 return 1;
800 if(p1->pl1 && p2->pl1)
802 if(*p1->pl1 != *p2->pl1)
803 return 1;
805 else if(p1->pl1 || p1->pl1)
806 return 1;
808 if(p1->pc1 && p2->pc1)
810 if(*p1->pc1 != *p2->pc1)
811 return 1;
813 else if(p1->pc1 || p1->pc1)
814 return 1;
816 return 0;
819 static void test_simple_struct(void)
821 unsigned char wiredata[28];
822 unsigned long wiredatalen;
823 long l;
824 char c;
825 ps1_t ps1;
827 static const unsigned char fmtstr_simple_struct[] =
829 0x12, 0x0, /* FC_UP */
830 NdrFcShort( 0x2 ), /* Offset=2 */
831 0x15, 0x3, /* FC_STRUCT [align 4] */
832 NdrFcShort( 0x18 ), /* [size 24] */
833 0x6, /* FC_SHORT */
834 0x2, /* FC_CHAR */
835 0x38, /* FC_ALIGNM4 */
836 0x8, /* FC_LONG */
837 0x8, /* FC_LONG */
838 0x39, /* FC_ALIGNM8 */
839 0xb, /* FC_HYPER */
840 0x5b, /* FC_END */
842 struct {
843 short s;
844 char c;
845 long l1, l2;
846 LONGLONG ll;
847 } s1;
849 static const unsigned char fmtstr_pointer_struct[] =
851 0x12, 0x0, /* FC_UP */
852 NdrFcShort( 0x2 ), /* Offset=2 */
853 0x16, 0x3, /* FC_PSTRUCT [align 4] */
854 NdrFcShort( 0xc ), /* [size 12] */
855 0x4b, /* FC_PP */
856 0x5c, /* FC_PAD */
857 0x46, /* FC_NO_REPEAT */
858 0x5c, /* FC_PAD */
859 NdrFcShort( 0x4 ), /* 4 */
860 NdrFcShort( 0x4 ), /* 4 */
861 0x13, 0x8, /* FC_OP [simple_pointer] */
862 0x8, /* FC_LONG */
863 0x5c, /* FC_PAD */
864 0x46, /* FC_NO_REPEAT */
865 0x5c, /* FC_PAD */
866 NdrFcShort( 0x8 ), /* 8 */
867 NdrFcShort( 0x8 ), /* 8 */
868 0x13, 0x8, /* FC_OP [simple_pointer] */
869 0x2, /* FC_CHAR */
870 0x5c, /* FC_PAD */
871 0x5b, /* FC_END */
872 0x8, /* FC_LONG */
873 0x8, /* FC_LONG */
874 0x8, /* FC_LONG */
875 0x5c, /* FC_PAD */
876 0x5b, /* FC_END */
880 /* FC_STRUCT */
881 s1.s = 0x1234;
882 s1.c = 0xa5;
883 s1.l1 = 0xdeadbeef;
884 s1.l2 = 0xcafebabe;
885 s1.ll = ((LONGLONG) 0xbadefeed << 32) | 0x2468ace0;
887 wiredatalen = 24;
888 memcpy(wiredata, &s1, wiredatalen);
889 test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
891 *(void**)wiredata = &s1;
892 memcpy(wiredata + 4, &s1, wiredatalen);
893 if (0)
895 /* one of the unmarshallings crashes Wine */
896 test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
899 /* FC_PSTRUCT */
900 ps1.l1 = 0xdeadbeef;
901 l = 0xcafebabe;
902 ps1.pl1 = &l;
903 c = 'a';
904 ps1.pc1 = &c;
905 memcpy(wiredata + 4, &ps1, 12);
906 memcpy(wiredata + 16, &l, 4);
907 memcpy(wiredata + 20, &c, 1);
909 test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
910 *(void**)wiredata = &ps1;
911 if (0)
913 /* one of the unmarshallings crashes Wine */
914 test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
918 static void test_fullpointer_xlat(void)
920 PFULL_PTR_XLAT_TABLES pXlatTables;
921 ULONG RefId;
922 int ret;
923 void *Pointer;
925 pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
927 /* "marshaling" phase */
929 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
930 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
931 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
933 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
934 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
935 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
937 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
938 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
939 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
941 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
942 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
943 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
945 ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
946 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
947 ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
949 /* "unmarshaling" phase */
951 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
952 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
953 ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
955 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
956 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
957 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
959 NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
961 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
962 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
963 ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
965 NdrFullPointerXlatFree(pXlatTables);
967 pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
969 /* "unmarshaling" phase */
971 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
972 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
973 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
975 NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
977 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
978 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
979 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
981 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
982 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
983 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
985 /* "marshaling" phase */
987 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
988 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
989 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
991 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
992 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
993 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
995 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
996 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
997 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
999 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1000 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1001 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1003 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1004 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1005 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1007 /* "freeing" phase */
1009 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
1010 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1012 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
1013 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1014 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1016 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1017 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1018 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1020 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
1021 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1023 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1024 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1026 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
1027 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1028 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1030 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1031 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1032 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1034 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1035 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1036 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1038 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1039 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1041 NdrFullPointerXlatFree(pXlatTables);
1044 static void test_client_init(void)
1046 MIDL_STUB_MESSAGE stubMsg;
1047 RPC_MESSAGE rpcMsg;
1049 memset(&rpcMsg, 0xcc, sizeof(rpcMsg));
1050 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1052 NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
1054 #define TEST_POINTER_UNSET(field) ok(rpcMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", rpcMsg.field)
1056 ok(rpcMsg.Handle == NULL, "rpcMsg.Handle should have been NULL instead of %p\n", rpcMsg.Handle);
1057 TEST_POINTER_UNSET(Buffer);
1058 ok(rpcMsg.BufferLength == 0xcccccccc, "rpcMsg.BufferLength should have been unset instead of %d\n", rpcMsg.BufferLength);
1059 ok(rpcMsg.ProcNum == 0x8001, "rpcMsg.ProcNum should have been 0x8001 instead of 0x%x\n", rpcMsg.ProcNum);
1060 TEST_POINTER_UNSET(TransferSyntax);
1061 ok(rpcMsg.RpcInterfaceInformation == Object_StubDesc.RpcInterfaceInformation,
1062 "rpcMsg.RpcInterfaceInformation should have been %p instead of %p\n",
1063 Object_StubDesc.RpcInterfaceInformation, rpcMsg.RpcInterfaceInformation);
1064 /* Note: ReservedForRuntime not tested */
1065 TEST_POINTER_UNSET(ManagerEpv);
1066 TEST_POINTER_UNSET(ImportContext);
1067 ok(rpcMsg.RpcFlags == 0, "rpcMsg.RpcFlags should have been 0 instead of 0x%lx\n", rpcMsg.RpcFlags);
1068 #undef TEST_POINTER_UNSET
1070 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1071 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", stubMsg.field)
1072 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1073 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1075 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1076 TEST_POINTER_UNSET(Buffer);
1077 TEST_ZERO(BufferStart, "%p");
1078 TEST_ZERO(BufferEnd, "%p");
1079 TEST_POINTER_UNSET(BufferMark);
1080 TEST_ZERO(BufferLength, "%d");
1081 TEST_ULONG_UNSET(MemorySize);
1082 TEST_POINTER_UNSET(Memory);
1083 ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
1084 TEST_ZERO(ReuseBuffer, "%d");
1085 TEST_ZERO(pAllocAllNodesContext, "%p");
1086 TEST_ZERO(pPointerQueueState, "%p");
1087 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1088 TEST_ZERO(PointerBufferMark, "%p");
1089 TEST_ZERO(fBufferValid, "%d");
1090 TEST_ZERO(uFlags, "%d");
1091 /* FIXME: UniquePtrCount */
1092 TEST_ULONG_PTR_UNSET(MaxCount);
1093 TEST_ULONG_UNSET(Offset);
1094 TEST_ULONG_UNSET(ActualCount);
1095 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1096 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1097 TEST_ZERO(StackTop, "%p");
1098 TEST_POINTER_UNSET(pPresentedType);
1099 TEST_POINTER_UNSET(pTransmitType);
1100 TEST_POINTER_UNSET(SavedHandle);
1101 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1102 TEST_POINTER_UNSET(FullPtrXlatTables);
1103 TEST_ZERO(FullPtrRefId, "%d");
1104 TEST_ZERO(PointerLength, "%d");
1105 TEST_ZERO(fInDontFree, "%d");
1106 TEST_ZERO(fDontCallFreeInst, "%d");
1107 TEST_ZERO(fInOnlyParam, "%d");
1108 TEST_ZERO(fHasReturn, "%d");
1109 TEST_ZERO(fHasExtensions, "%d");
1110 TEST_ZERO(fHasNewCorrDesc, "%d");
1111 TEST_ZERO(fUnused, "%d");
1112 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xcccc instead of 0x%x\n", stubMsg.fUnused2);
1113 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1114 TEST_ZERO(pvDestContext, "%p");
1115 TEST_POINTER_UNSET(SavedContextHandles);
1116 TEST_ULONG_UNSET(ParamNumber);
1117 TEST_ZERO(pRpcChannelBuffer, "%p");
1118 TEST_ZERO(pArrayInfo, "%p");
1119 TEST_POINTER_UNSET(SizePtrCountArray);
1120 TEST_POINTER_UNSET(SizePtrOffsetArray);
1121 TEST_POINTER_UNSET(SizePtrLengthArray);
1122 TEST_POINTER_UNSET(pArgQueue);
1123 TEST_ZERO(dwStubPhase, "%d");
1124 /* FIXME: where does this value come from? */
1125 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1126 TEST_ZERO(pAsyncMsg, "%p");
1127 TEST_ZERO(pCorrInfo, "%p");
1128 TEST_ZERO(pCorrMemory, "%p");
1129 TEST_ZERO(pMemoryList, "%p");
1130 TEST_POINTER_UNSET(pCSInfo);
1131 TEST_POINTER_UNSET(ConformanceMark);
1132 TEST_POINTER_UNSET(VarianceMark);
1133 ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1134 TEST_POINTER_UNSET(pContext);
1135 TEST_POINTER_UNSET(ContextHandleHash);
1136 TEST_POINTER_UNSET(pUserMarshalList);
1137 TEST_ULONG_PTR_UNSET(Reserved51_3);
1138 TEST_ULONG_PTR_UNSET(Reserved51_4);
1139 TEST_ULONG_PTR_UNSET(Reserved51_5);
1140 #undef TEST_ULONG_UNSET
1141 #undef TEST_POINTER_UNSET
1142 #undef TEST_ZERO
1146 static void test_server_init(void)
1148 MIDL_STUB_MESSAGE stubMsg;
1149 RPC_MESSAGE rpcMsg;
1150 unsigned char *ret;
1151 unsigned char buffer[256];
1153 memset(&rpcMsg, 0, sizeof(rpcMsg));
1154 rpcMsg.Buffer = buffer;
1155 rpcMsg.BufferLength = sizeof(buffer);
1156 rpcMsg.RpcFlags = RPC_BUFFER_COMPLETE;
1158 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1160 ret = NdrServerInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc);
1161 ok(ret == NULL, "NdrServerInitializeNew should have returned NULL instead of %p\n", ret);
1163 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1164 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have been unset instead of %p\n", stubMsg.field)
1165 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1166 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1168 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1169 ok(stubMsg.Buffer == buffer, "stubMsg.Buffer should have been %p instead of %p\n", buffer, stubMsg.Buffer);
1170 ok(stubMsg.BufferStart == buffer, "stubMsg.BufferStart should have been %p instead of %p\n", buffer, stubMsg.BufferStart);
1171 ok(stubMsg.BufferEnd == buffer + sizeof(buffer), "stubMsg.BufferEnd should have been %p instead of %p\n", buffer + sizeof(buffer), stubMsg.BufferEnd);
1172 TEST_POINTER_UNSET(BufferMark);
1173 todo_wine
1174 TEST_ZERO(BufferLength, "%d");
1175 TEST_ULONG_UNSET(MemorySize);
1176 TEST_POINTER_UNSET(Memory);
1177 ok(stubMsg.IsClient == 0, "stubMsg.IsClient should have been 0 instead of %u\n", stubMsg.IsClient);
1178 TEST_ZERO(ReuseBuffer, "%d");
1179 TEST_ZERO(pAllocAllNodesContext, "%p");
1180 TEST_ZERO(pPointerQueueState, "%p");
1181 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1182 TEST_ZERO(PointerBufferMark, "%p");
1183 ok(stubMsg.fBufferValid == 0xcc, "fBufferValid should have been unset instead of 0x%x\n", stubMsg.fBufferValid);
1184 TEST_ZERO(uFlags, "%d");
1185 /* FIXME: UniquePtrCount */
1186 TEST_ULONG_PTR_UNSET(MaxCount);
1187 TEST_ULONG_UNSET(Offset);
1188 TEST_ULONG_UNSET(ActualCount);
1189 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1190 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1191 TEST_ZERO(StackTop, "%p");
1192 TEST_POINTER_UNSET(pPresentedType);
1193 TEST_POINTER_UNSET(pTransmitType);
1194 TEST_POINTER_UNSET(SavedHandle);
1195 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1196 TEST_ZERO(FullPtrXlatTables, "%p");
1197 TEST_ZERO(FullPtrRefId, "%d");
1198 TEST_ZERO(PointerLength, "%d");
1199 TEST_ZERO(fInDontFree, "%d");
1200 TEST_ZERO(fDontCallFreeInst, "%d");
1201 TEST_ZERO(fInOnlyParam, "%d");
1202 TEST_ZERO(fHasReturn, "%d");
1203 TEST_ZERO(fHasExtensions, "%d");
1204 TEST_ZERO(fHasNewCorrDesc, "%d");
1205 TEST_ZERO(fUnused, "%d");
1206 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xcccc instead of 0x%x\n", stubMsg.fUnused2);
1207 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1208 TEST_ZERO(pvDestContext, "%p");
1209 TEST_POINTER_UNSET(SavedContextHandles);
1210 TEST_ULONG_UNSET(ParamNumber);
1211 TEST_ZERO(pRpcChannelBuffer, "%p");
1212 TEST_ZERO(pArrayInfo, "%p");
1213 TEST_POINTER_UNSET(SizePtrCountArray);
1214 TEST_POINTER_UNSET(SizePtrOffsetArray);
1215 TEST_POINTER_UNSET(SizePtrLengthArray);
1216 TEST_POINTER_UNSET(pArgQueue);
1217 TEST_ZERO(dwStubPhase, "%d");
1218 /* FIXME: where does this value come from? */
1219 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1220 TEST_ZERO(pAsyncMsg, "%p");
1221 TEST_ZERO(pCorrInfo, "%p");
1222 TEST_ZERO(pCorrMemory, "%p");
1223 TEST_ZERO(pMemoryList, "%p");
1224 TEST_POINTER_UNSET(pCSInfo);
1225 TEST_POINTER_UNSET(ConformanceMark);
1226 TEST_POINTER_UNSET(VarianceMark);
1227 ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1228 TEST_POINTER_UNSET(pContext);
1229 TEST_POINTER_UNSET(ContextHandleHash);
1230 TEST_POINTER_UNSET(pUserMarshalList);
1231 TEST_ULONG_PTR_UNSET(Reserved51_3);
1232 TEST_ULONG_PTR_UNSET(Reserved51_4);
1233 TEST_ULONG_PTR_UNSET(Reserved51_5);
1234 #undef TEST_ULONG_UNSET
1235 #undef TEST_POINTER_UNSET
1236 #undef TEST_ZERO
1240 static void test_ndr_allocate(void)
1242 RPC_MESSAGE RpcMessage;
1243 MIDL_STUB_MESSAGE StubMsg;
1244 MIDL_STUB_DESC StubDesc;
1245 void *p1, *p2;
1246 struct tag_mem_list_v1_t
1248 DWORD magic;
1249 void *ptr;
1250 struct tag_mem_list_v1_t *next;
1251 } *mem_list_v1;
1252 struct tag_mem_list_v2_t
1254 DWORD magic;
1255 DWORD size;
1256 DWORD unknown;
1257 struct tag_mem_list_v2_t *next;
1258 } *mem_list_v2;
1259 const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
1261 StubDesc = Object_StubDesc;
1262 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
1264 ok(StubMsg.pMemoryList == NULL, "memlist %p\n", StubMsg.pMemoryList);
1265 my_alloc_called = my_free_called = 0;
1266 p1 = NdrAllocate(&StubMsg, 10);
1267 p2 = NdrAllocate(&StubMsg, 24);
1268 ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
1269 ok(StubMsg.pMemoryList != NULL, "StubMsg.pMemoryList NULL\n");
1270 if(StubMsg.pMemoryList)
1272 mem_list_v2 = StubMsg.pMemoryList;
1273 if (mem_list_v2->size == 24)
1275 trace("v2 mem list format\n");
1276 ok((char *)mem_list_v2 == (char *)p2 + 24, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p2 + 24, mem_list_v2);
1277 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1278 ok(mem_list_v2->size == 24, "wrong size for p2 %d\n", mem_list_v2->size);
1279 ok(mem_list_v2->unknown == 0, "wrong unknown for p2 0x%x\n", mem_list_v2->unknown);
1280 ok(mem_list_v2->next != NULL, "next NULL\n");
1281 mem_list_v2 = mem_list_v2->next;
1282 if(mem_list_v2)
1284 ok((char *)mem_list_v2 == (char *)p1 + 16, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p1 + 16, mem_list_v2);
1285 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1286 ok(mem_list_v2->size == 16, "wrong size for p1 %d\n", mem_list_v2->size);
1287 ok(mem_list_v2->unknown == 0, "wrong unknown for p1 0x%x\n", mem_list_v2->unknown);
1288 ok(mem_list_v2->next == NULL, "next %p\n", mem_list_v2->next);
1291 else
1293 trace("v1 mem list format\n");
1294 mem_list_v1 = StubMsg.pMemoryList;
1295 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1296 ok(mem_list_v1->ptr == p2, "ptr != p2\n");
1297 ok(mem_list_v1->next != NULL, "next NULL\n");
1298 mem_list_v1 = mem_list_v1->next;
1299 if(mem_list_v1)
1301 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1302 ok(mem_list_v1->ptr == p1, "ptr != p1\n");
1303 ok(mem_list_v1->next == NULL, "next %p\n", mem_list_v1->next);
1307 /* NdrFree isn't exported so we can't test free'ing */
1310 static void test_conformant_array(void)
1312 RPC_MESSAGE RpcMessage;
1313 MIDL_STUB_MESSAGE StubMsg;
1314 MIDL_STUB_DESC StubDesc;
1315 void *ptr;
1316 unsigned char *mem, *mem_orig;
1317 unsigned char memsrc[20];
1319 static const unsigned char fmtstr_conf_array[] =
1321 0x1b, /* FC_CARRAY */
1322 0x0, /* align */
1323 NdrFcShort( 0x1 ), /* elem size */
1324 0x40, /* Corr desc: const */
1325 0x0,
1326 NdrFcShort(0x10), /* const = 0x10 */
1327 0x1, /* FC_BYTE */
1328 0x5b /* FC_END */
1331 StubDesc = Object_StubDesc;
1332 StubDesc.pFormatTypes = fmtstr_conf_array;
1334 NdrClientInitializeNew(
1335 &RpcMessage,
1336 &StubMsg,
1337 &StubDesc,
1340 StubMsg.BufferLength = 0;
1341 NdrConformantArrayBufferSize( &StubMsg,
1342 memsrc,
1343 fmtstr_conf_array );
1344 ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1346 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1347 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1348 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1350 ptr = NdrConformantArrayMarshall( &StubMsg, memsrc, fmtstr_conf_array );
1351 ok(ptr == NULL, "ret %p\n", ptr);
1352 ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1353 ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1355 StubMsg.Buffer = StubMsg.BufferStart;
1356 StubMsg.MemorySize = 0;
1357 mem = NULL;
1359 /* Client */
1360 my_alloc_called = 0;
1361 /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1362 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1363 ok(mem != NULL, "mem not alloced\n");
1364 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1365 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1367 my_alloc_called = 0;
1368 StubMsg.Buffer = StubMsg.BufferStart;
1369 mem_orig = mem;
1370 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1371 ok(mem == mem_orig, "mem alloced\n");
1372 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1373 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1375 my_alloc_called = 0;
1376 StubMsg.Buffer = StubMsg.BufferStart;
1377 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1378 ok(mem != mem_orig, "mem not alloced\n");
1379 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1380 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1382 my_free_called = 0;
1383 StubMsg.Buffer = StubMsg.BufferStart;
1384 NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1385 ok(my_free_called == 0, "free called %d\n", my_free_called);
1386 StubMsg.pfnFree(mem);
1388 /* Server */
1389 my_alloc_called = 0;
1390 StubMsg.IsClient = 0;
1391 mem = NULL;
1392 StubMsg.Buffer = StubMsg.BufferStart;
1393 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1394 ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1395 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1396 my_alloc_called = 0;
1397 mem = NULL;
1398 StubMsg.Buffer = StubMsg.BufferStart;
1399 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1400 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1401 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1402 StubMsg.pfnFree(mem);
1404 my_alloc_called = 0;
1405 mem = mem_orig;
1406 StubMsg.Buffer = StubMsg.BufferStart;
1407 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1408 ok(mem == mem_orig, "mem alloced\n");
1409 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1411 my_alloc_called = 0;
1412 mem = mem_orig;
1413 StubMsg.Buffer = StubMsg.BufferStart;
1414 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1415 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1416 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1417 StubMsg.pfnFree(mem);
1418 StubMsg.pfnFree(mem_orig);
1420 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1423 static void test_conformant_string(void)
1425 RPC_MESSAGE RpcMessage;
1426 MIDL_STUB_MESSAGE StubMsg;
1427 MIDL_STUB_DESC StubDesc;
1428 void *ptr;
1429 unsigned char *mem, *mem_orig;
1430 char memsrc[] = "This is a test string";
1432 static const unsigned char fmtstr_conf_str[] =
1434 0x11, 0x8, /* FC_RP [simple_pointer] */
1435 0x22, /* FC_C_CSTRING */
1436 0x5c, /* FC_PAD */
1439 StubDesc = Object_StubDesc;
1440 StubDesc.pFormatTypes = fmtstr_conf_str;
1442 NdrClientInitializeNew(
1443 &RpcMessage,
1444 &StubMsg,
1445 &StubDesc,
1448 StubMsg.BufferLength = 0;
1449 NdrPointerBufferSize( &StubMsg,
1450 (unsigned char *)memsrc,
1451 fmtstr_conf_str );
1452 ok(StubMsg.BufferLength >= sizeof(memsrc) + 12, "length %d\n", StubMsg.BufferLength);
1454 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1455 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1456 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1458 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_conf_str );
1459 ok(ptr == NULL, "ret %p\n", ptr);
1460 ok(StubMsg.Buffer - StubMsg.BufferStart == sizeof(memsrc) + 12, "Buffer %p Start %p len %d\n",
1461 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1462 ok(!memcmp(StubMsg.BufferStart + 12, memsrc, sizeof(memsrc)), "incorrectly marshaled\n");
1464 StubMsg.Buffer = StubMsg.BufferStart;
1465 StubMsg.MemorySize = 0;
1466 mem = NULL;
1468 /* Client */
1469 my_alloc_called = 0;
1470 StubMsg.Buffer = StubMsg.BufferStart;
1471 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1472 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1473 ok(mem == mem_orig, "mem not alloced\n");
1474 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1476 my_alloc_called = 0;
1477 StubMsg.Buffer = StubMsg.BufferStart;
1478 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1479 todo_wine {
1480 ok(mem == mem_orig, "mem not alloced\n");
1481 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1484 my_free_called = 0;
1485 StubMsg.Buffer = StubMsg.BufferStart;
1486 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1487 ok(my_free_called == 1, "free called %d\n", my_free_called);
1489 mem = my_alloc(10);
1490 my_free_called = 0;
1491 StubMsg.Buffer = StubMsg.BufferStart;
1492 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1493 ok(my_free_called == 1, "free called %d\n", my_free_called);
1495 /* Server */
1496 my_alloc_called = 0;
1497 StubMsg.IsClient = 0;
1498 mem = NULL;
1499 StubMsg.Buffer = StubMsg.BufferStart;
1500 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1501 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1502 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1504 my_alloc_called = 0;
1505 mem = NULL;
1506 StubMsg.Buffer = StubMsg.BufferStart;
1507 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1508 todo_wine {
1509 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1510 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 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1517 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1518 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1520 my_alloc_called = 0;
1521 mem = mem_orig;
1522 StubMsg.Buffer = StubMsg.BufferStart;
1523 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1524 todo_wine {
1525 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1526 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1529 mem = my_alloc(10);
1530 my_free_called = 0;
1531 StubMsg.Buffer = StubMsg.BufferStart;
1532 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1533 ok(my_free_called == 1, "free called %d\n", my_free_called);
1535 HeapFree(GetProcessHeap(), 0, mem_orig);
1536 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1539 static void test_nonconformant_string(void)
1541 RPC_MESSAGE RpcMessage;
1542 MIDL_STUB_MESSAGE StubMsg;
1543 MIDL_STUB_DESC StubDesc;
1544 void *ptr;
1545 unsigned char *mem, *mem_orig;
1546 unsigned char memsrc[10] = "This is";
1547 unsigned char memsrc2[10] = "This is a";
1549 static const unsigned char fmtstr_nonconf_str[] =
1551 0x26, /* FC_CSTRING */
1552 0x5c, /* FC_PAD */
1553 NdrFcShort( 0xa ), /* 10 */
1556 StubDesc = Object_StubDesc;
1557 StubDesc.pFormatTypes = fmtstr_nonconf_str;
1559 /* length < size */
1560 NdrClientInitializeNew(
1561 &RpcMessage,
1562 &StubMsg,
1563 &StubDesc,
1566 StubMsg.BufferLength = 0;
1568 NdrNonConformantStringBufferSize( &StubMsg,
1569 (unsigned char *)memsrc,
1570 fmtstr_nonconf_str );
1571 ok(StubMsg.BufferLength >= strlen((char *)memsrc) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1573 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1574 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1575 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1577 ptr = NdrNonConformantStringMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_nonconf_str );
1578 ok(ptr == NULL, "ret %p\n", ptr);
1579 ok(StubMsg.Buffer - StubMsg.BufferStart == strlen((char *)memsrc) + 1 + 8, "Buffer %p Start %p len %d\n",
1580 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1581 ok(!memcmp(StubMsg.BufferStart + 8, memsrc, strlen((char *)memsrc) + 1), "incorrectly marshaled\n");
1583 StubMsg.Buffer = StubMsg.BufferStart;
1584 StubMsg.MemorySize = 0;
1585 mem = NULL;
1587 /* Client */
1588 my_alloc_called = 0;
1589 StubMsg.Buffer = StubMsg.BufferStart;
1590 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1591 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1592 ok(mem == mem_orig, "mem alloced\n");
1593 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1595 my_alloc_called = 0;
1596 StubMsg.Buffer = StubMsg.BufferStart;
1597 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1598 todo_wine
1599 ok(mem == mem_orig, "mem alloced\n");
1600 todo_wine
1601 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1603 /* Server */
1604 my_alloc_called = 0;
1605 StubMsg.IsClient = 0;
1606 mem = NULL;
1607 StubMsg.Buffer = StubMsg.BufferStart;
1608 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1609 ok(mem != mem_orig, "mem not alloced\n");
1610 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1611 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1612 NdrOleFree(mem);
1614 my_alloc_called = 0;
1615 mem = mem_orig;
1616 StubMsg.Buffer = StubMsg.BufferStart;
1617 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1618 ok(mem == mem_orig, "mem alloced\n");
1619 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1621 my_alloc_called = 0;
1622 mem = mem_orig;
1623 StubMsg.Buffer = StubMsg.BufferStart;
1624 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1625 todo_wine
1626 ok(mem == mem_orig, "mem alloced\n");
1627 todo_wine
1628 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1630 HeapFree(GetProcessHeap(), 0, mem_orig);
1631 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1633 /* length = size */
1634 NdrClientInitializeNew(
1635 &RpcMessage,
1636 &StubMsg,
1637 &StubDesc,
1640 StubMsg.BufferLength = 0;
1642 NdrNonConformantStringBufferSize( &StubMsg,
1643 (unsigned char *)memsrc2,
1644 fmtstr_nonconf_str );
1645 ok(StubMsg.BufferLength >= strlen((char *)memsrc2) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1647 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1648 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1649 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1651 ptr = NdrNonConformantStringMarshall( &StubMsg, (unsigned char *)memsrc2, fmtstr_nonconf_str );
1652 ok(ptr == NULL, "ret %p\n", ptr);
1653 ok(StubMsg.Buffer - StubMsg.BufferStart == strlen((char *)memsrc2) + 1 + 8, "Buffer %p Start %p len %d\n",
1654 StubMsg.Buffer, StubMsg.BufferStart, StubMsg.Buffer - StubMsg.BufferStart);
1655 ok(!memcmp(StubMsg.BufferStart + 8, memsrc2, strlen((char *)memsrc2) + 1), "incorrectly marshaled\n");
1657 StubMsg.Buffer = StubMsg.BufferStart;
1658 StubMsg.MemorySize = 0;
1659 mem = NULL;
1661 /* Client */
1662 my_alloc_called = 0;
1663 StubMsg.Buffer = StubMsg.BufferStart;
1664 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1665 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1666 ok(mem == mem_orig, "mem alloced\n");
1667 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1669 my_alloc_called = 0;
1670 StubMsg.Buffer = StubMsg.BufferStart;
1671 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1672 todo_wine
1673 ok(mem == mem_orig, "mem alloced\n");
1674 todo_wine
1675 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1677 /* Server */
1678 my_alloc_called = 0;
1679 StubMsg.IsClient = 0;
1680 mem = NULL;
1681 StubMsg.Buffer = StubMsg.BufferStart;
1682 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1683 ok(mem != mem_orig, "mem not alloced\n");
1684 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1685 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1686 NdrOleFree(mem);
1688 my_alloc_called = 0;
1689 mem = mem_orig;
1690 StubMsg.Buffer = StubMsg.BufferStart;
1691 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1692 ok(mem == mem_orig, "mem alloced\n");
1693 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1695 my_alloc_called = 0;
1696 mem = mem_orig;
1697 StubMsg.Buffer = StubMsg.BufferStart;
1698 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1699 todo_wine
1700 ok(mem == mem_orig, "mem alloced\n");
1701 todo_wine
1702 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1704 HeapFree(GetProcessHeap(), 0, mem_orig);
1705 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1708 static void test_ndr_buffer(void)
1710 static unsigned char ncalrpc[] = "ncalrpc";
1711 static unsigned char endpoint[] = "winetest:test_ndr_buffer";
1712 RPC_MESSAGE RpcMessage;
1713 MIDL_STUB_MESSAGE StubMsg;
1714 MIDL_STUB_DESC StubDesc = Object_StubDesc;
1715 unsigned char *ret;
1716 unsigned char *binding;
1717 RPC_BINDING_HANDLE Handle;
1718 RPC_STATUS status;
1720 StubDesc.RpcInterfaceInformation = (void *)&IFoo___RpcServerInterface;
1722 status = RpcServerUseProtseqEp(ncalrpc, 20, endpoint, NULL);
1723 ok(RPC_S_OK == status, "RpcServerUseProtseqEp failed with status %lu\n", status);
1724 status = RpcServerRegisterIf(IFoo_v0_0_s_ifspec, NULL, NULL);
1725 ok(RPC_S_OK == status, "RpcServerRegisterIf failed with status %lu\n", status);
1726 status = RpcServerListen(1, 20, TRUE);
1727 ok(RPC_S_OK == status, "RpcServerListen failed with status %lu\n", status);
1728 if (status != RPC_S_OK)
1730 /* Failed to create a server, running client tests is useless */
1731 return;
1734 status = RpcStringBindingCompose(NULL, ncalrpc, NULL, endpoint, NULL, &binding);
1735 ok(status == RPC_S_OK, "RpcStringBindingCompose failed (%lu)\n", status);
1737 status = RpcBindingFromStringBinding(binding, &Handle);
1738 ok(status == RPC_S_OK, "RpcBindingFromStringBinding failed (%lu)\n", status);
1739 RpcStringFree(&binding);
1741 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 5);
1743 ret = NdrGetBuffer(&StubMsg, 10, Handle);
1744 ok(ret == StubMsg.Buffer, "NdrGetBuffer should have returned the same value as StubMsg.Buffer instead of %p\n", ret);
1745 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1746 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1747 ok(RpcMessage.BufferLength == 10, "RpcMessage.BufferLength should have been 10 instead of %d\n", RpcMessage.BufferLength);
1748 ok(RpcMessage.RpcFlags == 0, "RpcMessage.RpcFlags should have been 0x0 instead of 0x%lx\n", RpcMessage.RpcFlags);
1749 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1750 ok(!StubMsg.BufferStart, "BufferStart should have been NULL instead of %p\n", StubMsg.BufferStart);
1751 ok(!StubMsg.BufferEnd, "BufferEnd should have been NULL instead of %p\n", StubMsg.BufferEnd);
1752 todo_wine
1753 ok(StubMsg.BufferLength == 0, "BufferLength should have left as 0 instead of being set to %d\n", StubMsg.BufferLength);
1754 ok(StubMsg.fBufferValid == TRUE, "fBufferValid should have been TRUE instead of 0x%x\n", StubMsg.fBufferValid);
1756 StubMsg.BufferLength = 1;
1757 NdrFreeBuffer(&StubMsg);
1758 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1759 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1760 ok(RpcMessage.BufferLength == 10, "RpcMessage.BufferLength should have been left as 10 instead of %d\n", RpcMessage.BufferLength);
1761 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1762 ok(StubMsg.BufferLength == 1, "BufferLength should have left as 1 instead of being set to %d\n", StubMsg.BufferLength);
1763 ok(StubMsg.fBufferValid == FALSE, "fBufferValid should have been FALSE instead of 0x%x\n", StubMsg.fBufferValid);
1765 /* attempt double-free */
1766 NdrFreeBuffer(&StubMsg);
1768 RpcBindingFree(&Handle);
1770 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
1771 ok(status == RPC_S_OK, "RpcServerUnregisterIf failed (%lu)\n", status);
1774 static void test_NdrMapCommAndFaultStatus(void)
1776 RPC_STATUS rpc_status;
1777 MIDL_STUB_MESSAGE StubMsg;
1778 RPC_MESSAGE RpcMessage;
1780 NdrClientInitializeNew(&RpcMessage, &StubMsg, &Object_StubDesc, 5);
1782 for (rpc_status = 0; rpc_status < 10000; rpc_status++)
1784 RPC_STATUS status;
1785 ULONG comm_status = 0;
1786 ULONG fault_status = 0;
1787 ULONG expected_comm_status = 0;
1788 ULONG expected_fault_status = 0;
1789 status = NdrMapCommAndFaultStatus(&StubMsg, &comm_status, &fault_status, rpc_status);
1790 ok(status == RPC_S_OK, "NdrMapCommAndFaultStatus failed with error %ld\n", status);
1791 switch (rpc_status)
1793 case ERROR_INVALID_HANDLE:
1794 case RPC_S_INVALID_BINDING:
1795 case RPC_S_UNKNOWN_IF:
1796 case RPC_S_SERVER_UNAVAILABLE:
1797 case RPC_S_SERVER_TOO_BUSY:
1798 case RPC_S_CALL_FAILED_DNE:
1799 case RPC_S_PROTOCOL_ERROR:
1800 case RPC_S_UNSUPPORTED_TRANS_SYN:
1801 case RPC_S_UNSUPPORTED_TYPE:
1802 case RPC_S_PROCNUM_OUT_OF_RANGE:
1803 case EPT_S_NOT_REGISTERED:
1804 case RPC_S_COMM_FAILURE:
1805 expected_comm_status = rpc_status;
1806 break;
1807 default:
1808 expected_fault_status = rpc_status;
1810 ok(comm_status == expected_comm_status, "NdrMapCommAndFaultStatus should have mapped %ld to comm status %d instead of %d\n",
1811 rpc_status, expected_comm_status, comm_status);
1812 ok(fault_status == expected_fault_status, "NdrMapCommAndFaultStatus should have mapped %ld to fault status %d instead of %d\n",
1813 rpc_status, expected_fault_status, fault_status);
1817 START_TEST( ndr_marshall )
1819 test_ndr_simple_type();
1820 test_simple_types();
1821 test_nontrivial_pointer_types();
1822 test_simple_struct();
1823 test_fullpointer_xlat();
1824 test_client_init();
1825 test_server_init();
1826 test_ndr_allocate();
1827 test_conformant_array();
1828 test_conformant_string();
1829 test_nonconformant_string();
1830 test_ndr_buffer();
1831 test_NdrMapCommAndFaultStatus();