rpcrt4/tests: Remove tests for v1 memory list, the layout is wrong and we don't care...
[wine.git] / dlls / rpcrt4 / tests / ndr_marshall.c
bloba4f8bb4985ba7659bdad4bedae0a810451a9c9f9
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 != (UINT_PTR)&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, "%d\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, "%d\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, "%d\n", l2);
169 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
172 static void test_pointer_marshal(const unsigned char *formattypes,
173 void *memsrc, DWORD srcsize,
174 const void *wiredata,
175 ULONG wiredatalen,
176 int(*cmp)(const void*,const void*,size_t),
177 int num_additional_allocs,
178 const char *msgpfx)
180 RPC_MESSAGE RpcMessage;
181 MIDL_STUB_MESSAGE StubMsg;
182 MIDL_STUB_DESC StubDesc;
183 DWORD size;
184 void *ptr;
185 unsigned char *mem, *mem_orig;
187 my_alloc_called = my_free_called = 0;
188 if(!cmp)
189 cmp = memcmp;
191 StubDesc = Object_StubDesc;
192 StubDesc.pFormatTypes = formattypes;
194 NdrClientInitializeNew(
195 &RpcMessage,
196 &StubMsg,
197 &StubDesc,
200 StubMsg.BufferLength = 0;
201 NdrPointerBufferSize( &StubMsg,
202 memsrc,
203 formattypes );
204 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
206 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
207 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
208 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
210 memset(StubMsg.BufferStart, 0x0, StubMsg.BufferLength); /* This is a hack to clear the padding between the ptr and longlong/double */
212 ptr = NdrPointerMarshall( &StubMsg, memsrc, formattypes );
213 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
214 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
215 ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled\n", msgpfx);
217 StubMsg.Buffer = StubMsg.BufferStart;
218 StubMsg.MemorySize = 0;
220 size = NdrPointerMemorySize( &StubMsg, formattypes );
221 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
222 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
223 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
224 ok(size == srcsize + 4, "%s: mem size %u\n", msgpfx, size);
225 else
226 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
228 StubMsg.Buffer = StubMsg.BufferStart;
229 StubMsg.MemorySize = 16;
230 size = NdrPointerMemorySize( &StubMsg, formattypes );
231 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
232 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
233 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
234 ok(size == srcsize + 4 + 16, "%s: mem size %u\n", msgpfx, size);
235 else
236 ok(size == srcsize + 16, "%s: mem size %u\n", msgpfx, size);
238 StubMsg.Buffer = StubMsg.BufferStart;
239 StubMsg.MemorySize = 1;
240 size = NdrPointerMemorySize( &StubMsg, formattypes );
241 ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
242 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
243 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
244 ok(size == srcsize + 4 + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
245 else
246 ok(size == srcsize + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
248 size = srcsize;
249 if(formattypes[1] & 0x10) size += 4;
251 StubMsg.Buffer = StubMsg.BufferStart;
252 StubMsg.MemorySize = 0;
253 mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, size);
255 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
256 *(void**)mem = NULL;
257 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
258 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
259 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
260 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
261 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
262 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
263 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
264 my_alloc_called = 0;
266 /* reset the buffer and call with must alloc */
267 StubMsg.Buffer = StubMsg.BufferStart;
268 if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
269 *(void**)mem = NULL;
270 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 1 );
271 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
272 /* doesn't allocate mem in this case */
273 todo_wine {
274 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
276 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
277 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
278 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
280 todo_wine {
281 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
283 my_alloc_called = 0;
284 if(formattypes[0] != 0x11 /* FC_RP */)
286 /* now pass the address of a NULL ptr */
287 mem = NULL;
288 StubMsg.Buffer = StubMsg.BufferStart;
289 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
290 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
291 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
292 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
293 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
294 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
295 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
296 my_alloc_called = 0;
297 NdrPointerFree(&StubMsg, mem, formattypes);
299 /* again pass address of NULL ptr, but pretend we're a server */
300 if (0) /* crashes on Win9x and NT4 */
302 mem = NULL;
303 StubMsg.Buffer = StubMsg.BufferStart;
304 StubMsg.IsClient = 0;
305 ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
306 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
307 if (formattypes[2] == 0xd /* FC_ENUM16 */)
308 ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
309 else
310 ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
311 ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
312 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
313 ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
314 if (formattypes[2] != 0xd /* FC_ENUM16 */) {
315 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
316 my_alloc_called = 0;
320 HeapFree(GetProcessHeap(), 0, mem_orig);
321 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
324 static int deref_cmp(const void *s1, const void *s2, size_t num)
326 return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
330 static void test_simple_types(void)
332 unsigned char wiredata[16];
333 unsigned char ch;
334 unsigned char *ch_ptr;
335 unsigned short s;
336 unsigned int i;
337 ULONG l;
338 ULONGLONG ll;
339 float f;
340 double d;
342 static const unsigned char fmtstr_up_char[] =
344 0x12, 0x8, /* FC_UP [simple_pointer] */
345 0x2, /* FC_CHAR */
346 0x5c, /* FC_PAD */
348 static const unsigned char fmtstr_up_byte[] =
350 0x12, 0x8, /* FC_UP [simple_pointer] */
351 0x1, /* FC_BYTE */
352 0x5c, /* FC_PAD */
354 static const unsigned char fmtstr_up_small[] =
356 0x12, 0x8, /* FC_UP [simple_pointer] */
357 0x3, /* FC_SMALL */
358 0x5c, /* FC_PAD */
360 static const unsigned char fmtstr_up_usmall[] =
362 0x12, 0x8, /* FC_UP [simple_pointer] */
363 0x4, /* FC_USMALL */
364 0x5c, /* FC_PAD */
366 static const unsigned char fmtstr_rp_char[] =
368 0x11, 0x8, /* FC_RP [simple_pointer] */
369 0x2, /* FC_CHAR */
370 0x5c, /* FC_PAD */
372 static const unsigned char fmtstr_rpup_char[] =
374 0x11, 0x14, /* FC_RP [alloced_on_stack] */
375 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
376 0x12, 0x8, /* FC_UP [simple_pointer] */
377 0x2, /* FC_CHAR */
378 0x5c, /* FC_PAD */
380 static const unsigned char fmtstr_rpup_char2[] =
382 0x11, 0x04, /* FC_RP [alloced_on_stack] */
383 NdrFcShort( 0x2 ), /* Offset= 2 (4) */
384 0x12, 0x8, /* FC_UP [simple_pointer] */
385 0x2, /* FC_CHAR */
386 0x5c, /* FC_PAD */
389 static const unsigned char fmtstr_up_wchar[] =
391 0x12, 0x8, /* FC_UP [simple_pointer] */
392 0x5, /* FC_WCHAR */
393 0x5c, /* FC_PAD */
395 static const unsigned char fmtstr_up_short[] =
397 0x12, 0x8, /* FC_UP [simple_pointer] */
398 0x6, /* FC_SHORT */
399 0x5c, /* FC_PAD */
401 static const unsigned char fmtstr_up_ushort[] =
403 0x12, 0x8, /* FC_UP [simple_pointer] */
404 0x7, /* FC_USHORT */
405 0x5c, /* FC_PAD */
407 static const unsigned char fmtstr_up_enum16[] =
409 0x12, 0x8, /* FC_UP [simple_pointer] */
410 0xd, /* FC_ENUM16 */
411 0x5c, /* FC_PAD */
413 static const unsigned char fmtstr_up_long[] =
415 0x12, 0x8, /* FC_UP [simple_pointer] */
416 0x8, /* FC_LONG */
417 0x5c, /* FC_PAD */
419 static const unsigned char fmtstr_up_ulong[] =
421 0x12, 0x8, /* FC_UP [simple_pointer] */
422 0x9, /* FC_ULONG */
423 0x5c, /* FC_PAD */
425 static const unsigned char fmtstr_up_enum32[] =
427 0x12, 0x8, /* FC_UP [simple_pointer] */
428 0xe, /* FC_ENUM32 */
429 0x5c, /* FC_PAD */
431 static const unsigned char fmtstr_up_errorstatus[] =
433 0x12, 0x8, /* FC_UP [simple_pointer] */
434 0x10, /* FC_ERROR_STATUS_T */
435 0x5c, /* FC_PAD */
438 static const unsigned char fmtstr_up_longlong[] =
440 0x12, 0x8, /* FC_UP [simple_pointer] */
441 0xb, /* FC_HYPER */
442 0x5c, /* FC_PAD */
444 static const unsigned char fmtstr_up_float[] =
446 0x12, 0x8, /* FC_UP [simple_pointer] */
447 0xa, /* FC_FLOAT */
448 0x5c, /* FC_PAD */
450 static const unsigned char fmtstr_up_double[] =
452 0x12, 0x8, /* FC_UP [simple_pointer] */
453 0xc, /* FC_DOUBLE */
454 0x5c, /* FC_PAD */
457 ch = 0xa5;
458 ch_ptr = &ch;
459 if (use_pointer_ids)
460 *(unsigned int *)wiredata = 0x20000;
461 else
462 *(unsigned int *)wiredata = (UINT_PTR)ch_ptr;
463 wiredata[4] = ch;
465 test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
466 test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
467 test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0, "up_small");
468 test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
470 test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
472 test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
473 test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
475 s = 0xa597;
476 if (use_pointer_ids)
477 *(unsigned int *)wiredata = 0x20000;
478 else
479 *(unsigned int *)wiredata = (UINT_PTR)&s;
480 *(unsigned short*)(wiredata + 4) = s;
482 test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
483 test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
484 test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
486 i = 0x7fff;
487 if (use_pointer_ids)
488 *(unsigned int *)wiredata = 0x20000;
489 else
490 *(unsigned int *)wiredata = (UINT_PTR)&i;
491 *(unsigned short*)(wiredata + 4) = i;
492 test_pointer_marshal(fmtstr_up_enum16, &i, 4, wiredata, 6, NULL, 0, "up_enum16");
494 l = 0xcafebabe;
495 if (use_pointer_ids)
496 *(unsigned int *)wiredata = 0x20000;
497 else
498 *(unsigned int *)wiredata = (UINT_PTR)&l;
499 *(ULONG*)(wiredata + 4) = l;
501 test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
502 test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0, "up_ulong");
503 test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0, "up_emun32");
504 test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0, "up_errorstatus");
506 ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
507 if (use_pointer_ids)
508 *(unsigned int *)wiredata = 0x20000;
509 else
510 *(unsigned int *)wiredata = (UINT_PTR)&ll;
511 *(unsigned int *)(wiredata + 4) = 0;
512 *(ULONGLONG*)(wiredata + 8) = ll;
513 test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
515 f = 3.1415f;
516 if (use_pointer_ids)
517 *(unsigned int *)wiredata = 0x20000;
518 else
519 *(unsigned int *)wiredata = (UINT_PTR)&f;
520 *(float*)(wiredata + 4) = f;
521 test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
523 d = 3.1415;
524 if (use_pointer_ids)
525 *(unsigned int *)wiredata = 0x20000;
526 else
527 *(unsigned int *)wiredata = (UINT_PTR)&d;
528 *(unsigned int *)(wiredata + 4) = 0;
529 *(double*)(wiredata + 8) = d;
530 test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0, "up_double");
534 static void test_nontrivial_pointer_types(void)
536 RPC_MESSAGE RpcMessage;
537 MIDL_STUB_MESSAGE StubMsg;
538 MIDL_STUB_DESC StubDesc;
539 DWORD size;
540 void *ptr;
541 char **p1;
542 char *p2;
543 char ch;
544 unsigned char *mem, *mem_orig;
546 static const unsigned char fmtstr_ref_unique_out[] =
548 0x12, 0x8, /* FC_UP [simple_pointer] */
549 0x2, /* FC_CHAR */
550 0x5c, /* FC_PAD */
551 0x11, 0x14, /* FC_RP [alloced_on_stack] [pointer_deref] */
552 NdrFcShort( 0xfffffffa ), /* Offset= -6 (0) */
555 p1 = &p2;
556 p2 = &ch;
557 ch = 0x22;
559 StubDesc = Object_StubDesc;
560 StubDesc.pFormatTypes = fmtstr_ref_unique_out;
562 NdrClientInitializeNew(
563 &RpcMessage,
564 &StubMsg,
565 &StubDesc,
568 StubMsg.BufferLength = 0;
569 NdrPointerBufferSize( &StubMsg,
570 (unsigned char *)p1,
571 &fmtstr_ref_unique_out[4] );
573 /* Windows overestimates the buffer size */
574 ok(StubMsg.BufferLength >= 5, "length %d\n", StubMsg.BufferLength);
576 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
577 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
578 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
580 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)p1, &fmtstr_ref_unique_out[4] );
581 ok(ptr == NULL, "ret %p\n", ptr);
582 size = StubMsg.Buffer - StubMsg.BufferStart;
583 ok(size == 5, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, size);
584 ok(*(unsigned int *)StubMsg.BufferStart != 0, "pointer ID marshalled incorrectly\n");
585 ok(*(unsigned char *)(StubMsg.BufferStart + 4) == 0x22, "char data marshalled incorrectly: 0x%x\n",
586 *(unsigned char *)(StubMsg.BufferStart + 4));
588 StubMsg.Buffer = StubMsg.BufferStart;
589 StubMsg.MemorySize = 0;
590 mem = NULL;
592 /* Client */
593 my_alloc_called = 0;
594 StubMsg.Buffer = StubMsg.BufferStart;
595 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(void *));
596 *(void **)mem = NULL;
597 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
598 ok(mem == mem_orig, "mem alloced\n");
599 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
601 my_alloc_called = 0;
602 StubMsg.Buffer = StubMsg.BufferStart;
603 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
604 todo_wine {
605 ok(mem == mem_orig, "mem alloced\n");
606 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
609 my_free_called = 0;
610 StubMsg.Buffer = StubMsg.BufferStart;
611 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
612 ok(my_free_called == 1, "free called %d\n", my_free_called);
614 mem = my_alloc(sizeof(void *));
615 *(void **)mem = NULL;
616 my_free_called = 0;
617 StubMsg.Buffer = StubMsg.BufferStart;
618 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
619 ok(my_free_called == 0, "free called %d\n", my_free_called);
620 my_free(mem);
622 mem = my_alloc(sizeof(void *));
623 *(void **)mem = my_alloc(sizeof(char));
624 my_free_called = 0;
625 StubMsg.Buffer = StubMsg.BufferStart;
626 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
627 ok(my_free_called == 1, "free called %d\n", my_free_called);
628 my_free(mem);
630 /* Server */
631 my_alloc_called = 0;
632 StubMsg.IsClient = 0;
633 mem = NULL;
634 StubMsg.Buffer = StubMsg.BufferStart;
635 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
636 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
637 todo_wine
638 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
639 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
641 my_alloc_called = 0;
642 mem = NULL;
643 StubMsg.Buffer = StubMsg.BufferStart;
644 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
645 ok(mem != StubMsg.BufferStart, "mem pointing at buffer\n");
646 todo_wine
647 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
648 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
650 my_alloc_called = 0;
651 mem = mem_orig;
652 *(void **)mem = NULL;
653 StubMsg.Buffer = StubMsg.BufferStart;
654 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 0);
655 todo_wine {
656 ok(mem == mem_orig, "mem alloced\n");
657 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
660 my_alloc_called = 0;
661 mem = mem_orig;
662 *(void **)mem = NULL;
663 StubMsg.Buffer = StubMsg.BufferStart;
664 NdrPointerUnmarshall( &StubMsg, &mem, &fmtstr_ref_unique_out[4], 1);
665 todo_wine {
666 ok(mem == mem_orig, "mem alloced\n");
667 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
670 mem = my_alloc(sizeof(void *));
671 *(void **)mem = NULL;
672 my_free_called = 0;
673 StubMsg.Buffer = StubMsg.BufferStart;
674 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
675 ok(my_free_called == 0, "free called %d\n", my_free_called);
676 my_free(mem);
678 mem = my_alloc(sizeof(void *));
679 *(void **)mem = my_alloc(sizeof(char));
680 my_free_called = 0;
681 StubMsg.Buffer = StubMsg.BufferStart;
682 NdrPointerFree( &StubMsg, mem, &fmtstr_ref_unique_out[4] );
683 ok(my_free_called == 1, "free called %d\n", my_free_called);
684 my_free(mem);
686 HeapFree(GetProcessHeap(), 0, mem_orig);
687 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
690 static void test_simple_struct_marshal(const unsigned char *formattypes,
691 void *memsrc, DWORD srcsize,
692 const void *wiredata,
693 ULONG wiredatalen,
694 int(*cmp)(const void*,const void*,size_t),
695 int num_additional_allocs,
696 const char *msgpfx)
698 RPC_MESSAGE RpcMessage;
699 MIDL_STUB_MESSAGE StubMsg;
700 MIDL_STUB_DESC StubDesc;
701 DWORD size;
702 void *ptr;
703 unsigned char *mem, *mem_orig;
705 my_alloc_called = my_free_called = 0;
706 if(!cmp)
707 cmp = memcmp;
709 StubDesc = Object_StubDesc;
710 StubDesc.pFormatTypes = formattypes;
712 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
714 StubMsg.BufferLength = 0;
715 NdrSimpleStructBufferSize( &StubMsg, memsrc, formattypes );
716 ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
717 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
718 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
719 ptr = NdrSimpleStructMarshall( &StubMsg, memsrc, formattypes );
720 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
721 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
722 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));
724 StubMsg.Buffer = StubMsg.BufferStart;
725 StubMsg.MemorySize = 0;
726 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
727 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
728 ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
729 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
731 StubMsg.Buffer = StubMsg.BufferStart;
732 size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
733 ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
734 ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
735 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
736 size = srcsize;
737 /*** Unmarshalling first with must_alloc false ***/
739 StubMsg.Buffer = StubMsg.BufferStart;
740 StubMsg.MemorySize = 0;
741 mem_orig = mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, srcsize);
742 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
743 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
744 ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
745 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
746 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
747 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
748 my_alloc_called = 0;
749 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
751 /* If we're a server we still use the supplied memory */
752 StubMsg.Buffer = StubMsg.BufferStart;
753 StubMsg.IsClient = 0;
754 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
755 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
756 ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
757 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
758 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
759 my_alloc_called = 0;
760 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
762 /* ...unless we pass a NULL ptr, then the buffer is used.
763 Passing a NULL ptr while we're a client && !must_alloc
764 crashes on Windows, so we won't do that. */
766 if (0) /* crashes on Win9x and NT4 */
768 mem = NULL;
769 StubMsg.IsClient = 0;
770 StubMsg.Buffer = StubMsg.BufferStart;
771 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, FALSE );
772 ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
773 ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
774 ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
775 ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
776 my_alloc_called = 0;
777 ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
780 /*** now must_alloc is true ***/
782 /* with must_alloc set we always allocate new memory whether or not we're
783 a server and also when passing NULL */
784 mem = mem_orig;
785 StubMsg.IsClient = 1;
786 StubMsg.Buffer = StubMsg.BufferStart;
787 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
788 ok(ptr == NULL, "ret %p\n", ptr);
789 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
790 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
791 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
792 my_alloc_called = 0;
793 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
795 mem = NULL;
796 StubMsg.Buffer = StubMsg.BufferStart;
797 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
798 ok(ptr == NULL, "ret %p\n", ptr);
799 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
800 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
801 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
802 my_alloc_called = 0;
803 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
805 mem = mem_orig;
806 StubMsg.Buffer = StubMsg.BufferStart;
807 StubMsg.IsClient = 0;
808 StubMsg.ReuseBuffer = 1;
809 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
810 ok(ptr == NULL, "ret %p\n", ptr);
811 ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
812 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
813 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
814 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
815 my_alloc_called = 0;
816 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
818 mem = NULL;
819 StubMsg.Buffer = StubMsg.BufferStart;
820 StubMsg.IsClient = 0;
821 StubMsg.ReuseBuffer = 1;
822 ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
823 ok(ptr == NULL, "ret %p\n", ptr);
824 ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
825 ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
826 ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
827 my_alloc_called = 0;
828 ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
830 HeapFree(GetProcessHeap(), 0, mem_orig);
831 HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
834 typedef struct
836 LONG l1;
837 LONG *pl1;
838 char *pc1;
839 } ps1_t;
841 static int ps1_cmp(const void *s1, const void *s2, size_t num)
843 const ps1_t *p1, *p2;
845 p1 = s1;
846 p2 = s2;
848 if(p1->l1 != p2->l1)
849 return 1;
851 if(p1->pl1 && p2->pl1)
853 if(*p1->pl1 != *p2->pl1)
854 return 1;
856 else if(p1->pl1 || p1->pl1)
857 return 1;
859 if(p1->pc1 && p2->pc1)
861 if(*p1->pc1 != *p2->pc1)
862 return 1;
864 else if(p1->pc1 || p1->pc1)
865 return 1;
867 return 0;
870 static void test_simple_struct(void)
872 unsigned char wiredata[28];
873 ULONG wiredatalen;
874 LONG l;
875 char c;
876 ps1_t ps1;
878 static const unsigned char fmtstr_simple_struct[] =
880 0x12, 0x0, /* FC_UP */
881 NdrFcShort( 0x2 ), /* Offset=2 */
882 0x15, 0x3, /* FC_STRUCT [align 4] */
883 NdrFcShort( 0x18 ), /* [size 24] */
884 0x6, /* FC_SHORT */
885 0x2, /* FC_CHAR */
886 0x38, /* FC_ALIGNM4 */
887 0x8, /* FC_LONG */
888 0x8, /* FC_LONG */
889 0x39, /* FC_ALIGNM8 */
890 0xb, /* FC_HYPER */
891 0x5b, /* FC_END */
893 struct {
894 short s;
895 char c;
896 LONG l1, l2;
897 LONGLONG ll;
898 } s1;
900 static const unsigned char fmtstr_pointer_struct[] =
902 0x12, 0x0, /* FC_UP */
903 NdrFcShort( 0x2 ), /* Offset=2 */
904 0x16, 0x3, /* FC_PSTRUCT [align 4] */
905 NdrFcShort( 0xc ), /* [size 12] */
906 0x4b, /* FC_PP */
907 0x5c, /* FC_PAD */
908 0x46, /* FC_NO_REPEAT */
909 0x5c, /* FC_PAD */
910 NdrFcShort( 0x4 ), /* 4 */
911 NdrFcShort( 0x4 ), /* 4 */
912 0x13, 0x8, /* FC_OP [simple_pointer] */
913 0x8, /* FC_LONG */
914 0x5c, /* FC_PAD */
915 0x46, /* FC_NO_REPEAT */
916 0x5c, /* FC_PAD */
917 NdrFcShort( 0x8 ), /* 8 */
918 NdrFcShort( 0x8 ), /* 8 */
919 0x13, 0x8, /* FC_OP [simple_pointer] */
920 0x2, /* FC_CHAR */
921 0x5c, /* FC_PAD */
922 0x5b, /* FC_END */
923 0x8, /* FC_LONG */
924 0x8, /* FC_LONG */
925 0x8, /* FC_LONG */
926 0x5c, /* FC_PAD */
927 0x5b, /* FC_END */
931 /* zero the entire structure, including the holes */
932 memset(&s1, 0, sizeof(s1));
934 /* FC_STRUCT */
935 s1.s = 0x1234;
936 s1.c = 0xa5;
937 s1.l1 = 0xdeadbeef;
938 s1.l2 = 0xcafebabe;
939 s1.ll = ((LONGLONG) 0xbadefeed << 32) | 0x2468ace0;
941 wiredatalen = 24;
942 memcpy(wiredata, &s1, wiredatalen);
943 test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
945 if (use_pointer_ids)
946 *(unsigned int *)wiredata = 0x20000;
947 else
948 *(unsigned int *)wiredata = (UINT_PTR)&s1;
949 memcpy(wiredata + 4, &s1, wiredatalen);
950 test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
952 /* zero the entire structure, including the hole */
953 memset(&ps1, 0, sizeof(ps1));
955 /* FC_PSTRUCT */
956 ps1.l1 = 0xdeadbeef;
957 l = 0xcafebabe;
958 ps1.pl1 = &l;
959 c = 'a';
960 ps1.pc1 = &c;
961 *(unsigned int *)(wiredata + 4) = 0xdeadbeef;
962 if (use_pointer_ids)
964 *(unsigned int *)(wiredata + 8) = 0x20000;
965 *(unsigned int *)(wiredata + 12) = 0x20004;
967 else
969 *(unsigned int *)(wiredata + 8) = (UINT_PTR)&l;
970 *(unsigned int *)(wiredata + 12) = (UINT_PTR)&c;
972 memcpy(wiredata + 16, &l, 4);
973 memcpy(wiredata + 20, &c, 1);
975 test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
976 if (use_pointer_ids)
978 *(unsigned int *)wiredata = 0x20000;
979 *(unsigned int *)(wiredata + 8) = 0x20004;
980 *(unsigned int *)(wiredata + 12) = 0x20008;
982 else
983 *(unsigned int *)wiredata = (UINT_PTR)&ps1;
984 test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
987 static void test_fullpointer_xlat(void)
989 PFULL_PTR_XLAT_TABLES pXlatTables;
990 ULONG RefId;
991 int ret;
992 void *Pointer;
994 pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
996 /* "marshaling" phase */
998 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
999 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1000 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1002 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1003 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1004 ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
1006 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1007 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1008 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1010 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1011 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1012 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1014 ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
1015 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1016 ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
1018 /* "unmarshaling" phase */
1020 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1021 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1022 ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
1024 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
1025 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1026 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1028 NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
1030 ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
1031 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1032 ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
1034 NdrFullPointerXlatFree(pXlatTables);
1036 pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
1038 /* "unmarshaling" phase */
1040 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1041 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1042 ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
1044 NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
1046 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
1047 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1048 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1050 ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
1051 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1052 ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
1054 /* "marshaling" phase */
1056 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1057 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1058 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1060 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1061 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1062 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1064 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
1065 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1066 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1068 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
1069 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1070 ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
1072 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
1073 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1074 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1076 /* "freeing" phase */
1078 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
1079 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1081 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
1082 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1083 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1085 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
1086 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1087 ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
1089 ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
1090 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1092 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1093 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1095 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
1096 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1097 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1099 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1100 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1101 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1103 ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
1104 ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
1105 ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
1107 ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
1108 ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
1110 NdrFullPointerXlatFree(pXlatTables);
1113 /* verify stub data that is identical between client and server */
1114 static void test_common_stub_data( const char *prefix, const MIDL_STUB_MESSAGE *stubMsg )
1116 void *unset_ptr;
1118 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1120 #define TEST_ZERO(field, fmt) ok(stubMsg->field == 0, "%s: " #field " should have been set to zero instead of " fmt "\n", prefix, stubMsg->field)
1121 #define TEST_POINTER_UNSET(field) ok(stubMsg->field == unset_ptr, "%s: " #field " should have been unset instead of %p\n", prefix, stubMsg->field)
1122 #define TEST_ULONG_UNSET(field) ok(stubMsg->field == 0xcccccccc, "%s: " #field " should have been unset instead of 0x%x\n", prefix, stubMsg->field)
1123 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg->field == (ULONG_PTR)unset_ptr, "%s: " #field " should have been unset instead of 0x%lx\n", prefix, stubMsg->field)
1125 TEST_POINTER_UNSET(BufferMark);
1126 TEST_ULONG_UNSET(MemorySize);
1127 TEST_POINTER_UNSET(Memory);
1128 TEST_ZERO(pAllocAllNodesContext, "%p");
1129 ok(stubMsg->pPointerQueueState == 0 ||
1130 broken(stubMsg->pPointerQueueState == unset_ptr), /* win2k */
1131 "%s: pPointerQueueState should have been unset instead of %p\n",
1132 prefix, stubMsg->pPointerQueueState);
1133 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1134 TEST_ZERO(PointerBufferMark, "%p");
1135 ok( stubMsg->uFlags == 0 ||
1136 broken(stubMsg->uFlags == 0xcc), /* win9x */
1137 "%s: uFlags should have been set to zero instead of 0x%x\n", prefix, stubMsg->uFlags );
1138 /* FIXME: UniquePtrCount */
1139 TEST_ULONG_PTR_UNSET(MaxCount);
1140 TEST_ULONG_UNSET(Offset);
1141 TEST_ULONG_UNSET(ActualCount);
1142 ok(stubMsg->pfnAllocate == my_alloc, "%s: pfnAllocate should have been %p instead of %p\n",
1143 prefix, my_alloc, stubMsg->pfnAllocate);
1144 ok(stubMsg->pfnFree == my_free, "%s: pfnFree should have been %p instead of %p\n",
1145 prefix, my_free, stubMsg->pfnFree);
1146 TEST_ZERO(StackTop, "%p");
1147 TEST_POINTER_UNSET(pPresentedType);
1148 TEST_POINTER_UNSET(pTransmitType);
1149 TEST_POINTER_UNSET(SavedHandle);
1150 ok(stubMsg->StubDesc == &Object_StubDesc, "%s: StubDesc should have been %p instead of %p\n",
1151 prefix, &Object_StubDesc, stubMsg->StubDesc);
1152 TEST_ZERO(FullPtrRefId, "%d");
1153 ok( stubMsg->PointerLength == 0 ||
1154 broken(stubMsg->PointerLength == 1), /* win9x, nt4 */
1155 "%s: pAsyncMsg should have been set to zero instead of %d\n", prefix, stubMsg->PointerLength );
1156 TEST_ZERO(fInDontFree, "%d");
1157 TEST_ZERO(fDontCallFreeInst, "%d");
1158 ok(stubMsg->fInOnlyParam == 0 ||
1159 stubMsg->fInOnlyParam == -1, /* Vista */
1160 "%s: fInOnlyParam should have been set to 0 or -1 instead of %d\n", prefix, stubMsg->fInOnlyParam);
1161 ok( stubMsg->fHasReturn == 0 ||
1162 broken(stubMsg->fHasReturn == -1), /* win9x, nt4 */
1163 "%s: fHasReturn should have been set to zero instead of %d\n", prefix, stubMsg->fHasReturn );
1164 TEST_ZERO(fHasExtensions, "%d");
1165 TEST_ZERO(fHasNewCorrDesc, "%d");
1166 ok(stubMsg->fIsIn == 0 ||
1167 broken(stubMsg->fIsIn == -1), /* win9x, nt4 */
1168 "%s: fIsIn should have been set to 0 instead of %d\n", prefix, stubMsg->fIsIn);
1169 ok(stubMsg->fIsOut == 0 ||
1170 stubMsg->fIsOut == -1, /* XP-SP3 */
1171 "%s: fIsOut should have been set to 0 or -1 instead of %d\n", prefix, stubMsg->fIsOut);
1172 TEST_ZERO(fIsOicf, "%d");
1173 ok(stubMsg->fBufferValid == 0,
1174 "%s: fBufferValid should have been set to 0 instead of %d\n", prefix, stubMsg->fBufferValid);
1175 ok(stubMsg->fHasMemoryValidateCallback == 0 ||
1176 stubMsg->fHasMemoryValidateCallback == -1, /* XP-SP3 */
1177 "%s: fHasMemoryValidateCallback should have been set to 0 or -1 instead of %d\n",
1178 prefix, stubMsg->fHasMemoryValidateCallback);
1179 ok(stubMsg->fInFree == 0 ||
1180 stubMsg->fInFree == -1, /* XP-SP3 */
1181 "%s: fInFree should have been set to 0 or -1 instead of %d\n", prefix, stubMsg->fInFree);
1182 TEST_ZERO(fNeedMCCP, "%d");
1183 ok(stubMsg->fUnused == 0 ||
1184 stubMsg->fUnused == -2, /* Vista */
1185 "%s: fUnused should have been set to 0 or -2 instead of %d\n", prefix, stubMsg->fUnused);
1186 ok(stubMsg->fUnused2 == 0xffffcccc, "%s: fUnused2 should have been 0xffffcccc instead of 0x%x\n",
1187 prefix, stubMsg->fUnused2);
1188 ok(stubMsg->dwDestContext == MSHCTX_DIFFERENTMACHINE,
1189 "%s: dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n",
1190 prefix, stubMsg->dwDestContext);
1191 TEST_ZERO(pvDestContext, "%p");
1192 TEST_POINTER_UNSET(SavedContextHandles);
1193 TEST_ULONG_UNSET(ParamNumber);
1194 TEST_ZERO(pRpcChannelBuffer, "%p");
1195 TEST_ZERO(pArrayInfo, "%p");
1196 TEST_POINTER_UNSET(SizePtrCountArray);
1197 TEST_POINTER_UNSET(SizePtrOffsetArray);
1198 TEST_POINTER_UNSET(SizePtrLengthArray);
1199 TEST_POINTER_UNSET(pArgQueue);
1200 TEST_ZERO(dwStubPhase, "%d");
1201 /* FIXME: where does this value come from? */
1202 trace("%s: LowStackMark is %p\n", prefix, stubMsg->LowStackMark);
1203 ok( stubMsg->pAsyncMsg == 0 || broken(stubMsg->pAsyncMsg == unset_ptr), /* win9x, nt4 */
1204 "%s: pAsyncMsg should have been set to zero instead of %p\n", prefix, stubMsg->pAsyncMsg );
1205 ok( stubMsg->pCorrInfo == 0 || broken(stubMsg->pCorrInfo == unset_ptr), /* win9x, nt4 */
1206 "%s: pCorrInfo should have been set to zero instead of %p\n", prefix, stubMsg->pCorrInfo );
1207 ok( stubMsg->pCorrMemory == 0 || broken(stubMsg->pCorrMemory == unset_ptr), /* win9x, nt4 */
1208 "%s: pCorrMemory should have been set to zero instead of %p\n", prefix, stubMsg->pCorrMemory );
1209 ok( stubMsg->pMemoryList == 0 || broken(stubMsg->pMemoryList == unset_ptr), /* win9x, nt4 */
1210 "%s: pMemoryList should have been set to zero instead of %p\n", prefix, stubMsg->pMemoryList );
1211 TEST_POINTER_UNSET(pCSInfo);
1212 TEST_POINTER_UNSET(ConformanceMark);
1213 TEST_POINTER_UNSET(VarianceMark);
1214 ok(stubMsg->Unused == (ULONG_PTR)unset_ptr, "%s: Unused should have be unset instead of 0x%lx\n",
1215 prefix, stubMsg->Unused);
1216 TEST_POINTER_UNSET(pContext);
1217 TEST_POINTER_UNSET(ContextHandleHash);
1218 TEST_POINTER_UNSET(pUserMarshalList);
1219 TEST_ULONG_PTR_UNSET(Reserved51_3);
1220 TEST_ULONG_PTR_UNSET(Reserved51_4);
1221 TEST_ULONG_PTR_UNSET(Reserved51_5);
1223 #undef TEST_ULONG_PTR_UNSET
1224 #undef TEST_ULONG_UNSET
1225 #undef TEST_POINTER_UNSET
1226 #undef TEST_ZERO
1229 static void test_client_init(void)
1231 MIDL_STUB_MESSAGE stubMsg;
1232 RPC_MESSAGE rpcMsg;
1233 void *unset_ptr;
1235 memset(&rpcMsg, 0xcc, sizeof(rpcMsg));
1236 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1237 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1239 NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
1241 test_common_stub_data( "NdrClientInitializeNew", &stubMsg );
1243 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1244 ok(rpcMsg.Handle == NULL, "rpcMsg.Handle should have been NULL instead of %p\n", rpcMsg.Handle);
1245 ok(rpcMsg.Buffer == unset_ptr, "rpcMsg.Buffer should have been unset instead of %p\n",
1246 rpcMsg.Buffer);
1247 ok(rpcMsg.BufferLength == 0xcccccccc, "rpcMsg.BufferLength should have been unset instead of %d\n", rpcMsg.BufferLength);
1248 ok(rpcMsg.ProcNum == 0x8001, "rpcMsg.ProcNum should have been 0x8001 instead of 0x%x\n", rpcMsg.ProcNum);
1249 ok(rpcMsg.TransferSyntax == unset_ptr, "rpcMsg.TransferSyntax should have been unset instead of %p\n", rpcMsg.TransferSyntax);
1250 ok(rpcMsg.RpcInterfaceInformation == Object_StubDesc.RpcInterfaceInformation,
1251 "rpcMsg.RpcInterfaceInformation should have been %p instead of %p\n",
1252 Object_StubDesc.RpcInterfaceInformation, rpcMsg.RpcInterfaceInformation);
1253 /* Note: ReservedForRuntime not tested */
1254 ok(rpcMsg.ManagerEpv == unset_ptr, "rpcMsg.ManagerEpv should have been unset instead of %p\n", rpcMsg.ManagerEpv);
1255 ok(rpcMsg.ImportContext == unset_ptr, "rpcMsg.ImportContext should have been unset instead of %p\n", rpcMsg.ImportContext);
1256 ok(rpcMsg.RpcFlags == 0, "rpcMsg.RpcFlags should have been 0 instead of 0x%x\n", rpcMsg.RpcFlags);
1258 ok(stubMsg.Buffer == unset_ptr, "stubMsg.Buffer should have been unset instead of %p\n",
1259 stubMsg.Buffer);
1260 ok(stubMsg.BufferStart == NULL, "stubMsg.BufferStart should have been NULL instead of %p\n",
1261 stubMsg.BufferStart);
1262 ok(stubMsg.BufferEnd == NULL, "stubMsg.BufferEnd should have been NULL instead of %p\n",
1263 stubMsg.BufferEnd);
1264 ok(stubMsg.BufferLength == 0, "stubMsg.BufferLength should have been 0 instead of %u\n",
1265 stubMsg.BufferLength);
1266 ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
1267 ok(stubMsg.ReuseBuffer == 0, "stubMsg.ReuseBuffer should have been 0 instead of %d\n",
1268 stubMsg.ReuseBuffer);
1269 ok(stubMsg.CorrDespIncrement == 0, "stubMsg.CorrDespIncrement should have been 0 instead of %d\n",
1270 stubMsg.CorrDespIncrement);
1271 ok(stubMsg.FullPtrXlatTables == unset_ptr, "stubMsg.FullPtrXlatTables should have been unset instead of %p\n",
1272 stubMsg.FullPtrXlatTables);
1275 static void test_server_init(void)
1277 MIDL_STUB_MESSAGE stubMsg;
1278 RPC_MESSAGE rpcMsg;
1279 unsigned char *ret;
1280 unsigned char buffer[256];
1282 memset(&rpcMsg, 0, sizeof(rpcMsg));
1283 rpcMsg.Buffer = buffer;
1284 rpcMsg.BufferLength = sizeof(buffer);
1285 rpcMsg.RpcFlags = RPC_BUFFER_COMPLETE;
1287 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1289 ret = NdrServerInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc);
1290 ok(ret == NULL, "NdrServerInitializeNew should have returned NULL instead of %p\n", ret);
1292 test_common_stub_data( "NdrServerInitializeNew", &stubMsg );
1294 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1295 ok(stubMsg.Buffer == buffer, "stubMsg.Buffer should have been %p instead of %p\n", buffer, stubMsg.Buffer);
1296 ok(stubMsg.BufferStart == buffer, "stubMsg.BufferStart should have been %p instead of %p\n", buffer, stubMsg.BufferStart);
1297 ok(stubMsg.BufferEnd == buffer + sizeof(buffer), "stubMsg.BufferEnd should have been %p instead of %p\n", buffer + sizeof(buffer), stubMsg.BufferEnd);
1298 todo_wine
1299 ok(stubMsg.BufferLength == 0, "stubMsg.BufferLength should have been 0 instead of %u\n", stubMsg.BufferLength);
1300 ok(stubMsg.IsClient == 0, "stubMsg.IsClient should have been 0 instead of %u\n", stubMsg.IsClient);
1301 ok(stubMsg.ReuseBuffer == 0 ||
1302 broken(stubMsg.ReuseBuffer == 1), /* win2k */
1303 "stubMsg.ReuseBuffer should have been set to zero instead of %d\n", stubMsg.ReuseBuffer);
1304 ok(stubMsg.CorrDespIncrement == 0xcc ||
1305 stubMsg.CorrDespIncrement == 0,
1306 "CorrDespIncrement should have been unset instead of 0x%x\n", stubMsg.CorrDespIncrement);
1307 ok(stubMsg.FullPtrXlatTables == 0, "stubMsg.BufferLength should have been 0 instead of %p\n", stubMsg.FullPtrXlatTables);
1310 static void test_ndr_allocate(void)
1312 RPC_MESSAGE RpcMessage;
1313 MIDL_STUB_MESSAGE StubMsg;
1314 MIDL_STUB_DESC StubDesc;
1315 void *p1, *p2;
1316 struct tag_mem_list_v2_t
1318 DWORD magic;
1319 DWORD size;
1320 DWORD unknown;
1321 struct tag_mem_list_v2_t *next;
1322 } *mem_list_v2;
1323 const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
1325 StubDesc = Object_StubDesc;
1326 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
1328 my_alloc_called = my_free_called = 0;
1329 p1 = NdrAllocate(&StubMsg, 10);
1330 p2 = NdrAllocate(&StubMsg, 24);
1331 ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
1332 ok(StubMsg.pMemoryList != NULL, "StubMsg.pMemoryList NULL\n");
1333 if(StubMsg.pMemoryList)
1335 mem_list_v2 = StubMsg.pMemoryList;
1336 if (mem_list_v2->size == 24)
1338 trace("v2 mem list format\n");
1339 ok((char *)mem_list_v2 == (char *)p2 + 24, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p2 + 24, mem_list_v2);
1340 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1341 ok(mem_list_v2->size == 24, "wrong size for p2 %d\n", mem_list_v2->size);
1342 ok(mem_list_v2->unknown == 0, "wrong unknown for p2 0x%x\n", mem_list_v2->unknown);
1343 ok(mem_list_v2->next != NULL, "next NULL\n");
1344 mem_list_v2 = mem_list_v2->next;
1345 if(mem_list_v2)
1347 ok((char *)mem_list_v2 == (char *)p1 + 16, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p1 + 16, mem_list_v2);
1348 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1349 ok(mem_list_v2->size == 16, "wrong size for p1 %d\n", mem_list_v2->size);
1350 ok(mem_list_v2->unknown == 0, "wrong unknown for p1 0x%x\n", mem_list_v2->unknown);
1351 ok(mem_list_v2->next == NULL, "next %p\n", mem_list_v2->next);
1354 else win_skip("v1 mem list format\n");
1356 /* NdrFree isn't exported so we can't test free'ing */
1359 static void test_conformant_array(void)
1361 RPC_MESSAGE RpcMessage;
1362 MIDL_STUB_MESSAGE StubMsg;
1363 MIDL_STUB_DESC StubDesc;
1364 void *ptr;
1365 unsigned char *mem, *mem_orig;
1366 unsigned char memsrc[20];
1367 unsigned int i;
1369 static const unsigned char fmtstr_conf_array[] =
1371 0x1b, /* FC_CARRAY */
1372 0x0, /* align */
1373 NdrFcShort( 0x1 ), /* elem size */
1374 0x40, /* Corr desc: const */
1375 0x0,
1376 NdrFcShort(0x10), /* const = 0x10 */
1377 0x1, /* FC_BYTE */
1378 0x5b /* FC_END */
1381 for (i = 0; i < sizeof(memsrc); i++)
1382 memsrc[i] = i * i;
1384 StubDesc = Object_StubDesc;
1385 StubDesc.pFormatTypes = fmtstr_conf_array;
1387 NdrClientInitializeNew(
1388 &RpcMessage,
1389 &StubMsg,
1390 &StubDesc,
1393 StubMsg.BufferLength = 0;
1394 NdrConformantArrayBufferSize( &StubMsg,
1395 memsrc,
1396 fmtstr_conf_array );
1397 ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1399 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1400 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1401 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1403 ptr = NdrConformantArrayMarshall( &StubMsg, memsrc, fmtstr_conf_array );
1404 ok(ptr == NULL, "ret %p\n", ptr);
1405 ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1406 ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1408 StubMsg.Buffer = StubMsg.BufferStart;
1409 StubMsg.MemorySize = 0;
1410 mem = NULL;
1412 /* Client */
1413 my_alloc_called = 0;
1414 /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1415 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1416 ok(mem != NULL, "mem not alloced\n");
1417 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1418 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1420 my_alloc_called = 0;
1421 StubMsg.Buffer = StubMsg.BufferStart;
1422 mem_orig = mem;
1423 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1424 ok(mem == mem_orig, "mem alloced\n");
1425 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1426 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1428 my_alloc_called = 0;
1429 StubMsg.Buffer = StubMsg.BufferStart;
1430 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1431 ok(mem != mem_orig, "mem not alloced\n");
1432 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1433 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1435 my_free_called = 0;
1436 StubMsg.Buffer = StubMsg.BufferStart;
1437 NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1438 ok(my_free_called == 0, "free called %d\n", my_free_called);
1439 StubMsg.pfnFree(mem);
1441 /* Server */
1442 my_alloc_called = 0;
1443 StubMsg.IsClient = 0;
1444 mem = NULL;
1445 StubMsg.Buffer = StubMsg.BufferStart;
1446 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1447 ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1448 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1449 my_alloc_called = 0;
1450 mem = NULL;
1451 StubMsg.Buffer = StubMsg.BufferStart;
1452 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1453 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1454 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1455 StubMsg.pfnFree(mem);
1457 my_alloc_called = 0;
1458 mem = mem_orig;
1459 StubMsg.Buffer = StubMsg.BufferStart;
1460 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1461 ok(mem == mem_orig, "mem alloced\n");
1462 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1464 my_alloc_called = 0;
1465 mem = mem_orig;
1466 StubMsg.Buffer = StubMsg.BufferStart;
1467 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1468 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1469 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1470 StubMsg.pfnFree(mem);
1471 StubMsg.pfnFree(mem_orig);
1473 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1476 static void test_conformant_string(void)
1478 RPC_MESSAGE RpcMessage;
1479 MIDL_STUB_MESSAGE StubMsg;
1480 MIDL_STUB_DESC StubDesc;
1481 DWORD size;
1482 void *ptr;
1483 unsigned char *mem, *mem_orig;
1484 char memsrc[] = "This is a test string";
1486 static const unsigned char fmtstr_conf_str[] =
1488 0x11, 0x8, /* FC_RP [simple_pointer] */
1489 0x22, /* FC_C_CSTRING */
1490 0x5c, /* FC_PAD */
1493 StubDesc = Object_StubDesc;
1494 StubDesc.pFormatTypes = fmtstr_conf_str;
1496 NdrClientInitializeNew(
1497 &RpcMessage,
1498 &StubMsg,
1499 &StubDesc,
1502 StubMsg.BufferLength = 0;
1503 NdrPointerBufferSize( &StubMsg,
1504 (unsigned char *)memsrc,
1505 fmtstr_conf_str );
1506 ok(StubMsg.BufferLength >= sizeof(memsrc) + 12, "length %d\n", StubMsg.BufferLength);
1508 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1509 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1510 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1512 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_conf_str );
1513 ok(ptr == NULL, "ret %p\n", ptr);
1514 size = StubMsg.Buffer - StubMsg.BufferStart;
1515 ok(size == sizeof(memsrc) + 12, "Buffer %p Start %p len %d\n",
1516 StubMsg.Buffer, StubMsg.BufferStart, size);
1517 ok(!memcmp(StubMsg.BufferStart + 12, memsrc, sizeof(memsrc)), "incorrectly marshaled\n");
1519 StubMsg.Buffer = StubMsg.BufferStart;
1520 StubMsg.MemorySize = 0;
1521 mem = NULL;
1523 /* Client */
1524 my_alloc_called = 0;
1525 StubMsg.Buffer = StubMsg.BufferStart;
1526 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1527 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1528 ok(mem == mem_orig, "mem not alloced\n");
1529 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1531 my_alloc_called = 0;
1532 StubMsg.Buffer = StubMsg.BufferStart;
1533 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1534 todo_wine {
1535 ok(mem == mem_orig, "mem not alloced\n");
1536 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1539 my_free_called = 0;
1540 StubMsg.Buffer = StubMsg.BufferStart;
1541 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1542 ok(my_free_called == 1, "free called %d\n", my_free_called);
1544 mem = my_alloc(10);
1545 my_free_called = 0;
1546 StubMsg.Buffer = StubMsg.BufferStart;
1547 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1548 ok(my_free_called == 1, "free called %d\n", my_free_called);
1550 /* Server */
1551 my_alloc_called = 0;
1552 StubMsg.IsClient = 0;
1553 mem = NULL;
1554 StubMsg.Buffer = StubMsg.BufferStart;
1555 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1556 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1557 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1559 my_alloc_called = 0;
1560 mem = NULL;
1561 StubMsg.Buffer = StubMsg.BufferStart;
1562 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1563 todo_wine {
1564 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1565 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1568 my_alloc_called = 0;
1569 mem = mem_orig;
1570 StubMsg.Buffer = StubMsg.BufferStart;
1571 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1572 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1573 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1575 my_alloc_called = 0;
1576 mem = mem_orig;
1577 StubMsg.Buffer = StubMsg.BufferStart;
1578 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1579 todo_wine {
1580 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1581 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1584 mem = my_alloc(10);
1585 my_free_called = 0;
1586 StubMsg.Buffer = StubMsg.BufferStart;
1587 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1588 ok(my_free_called == 1, "free called %d\n", my_free_called);
1590 HeapFree(GetProcessHeap(), 0, mem_orig);
1591 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1594 static void test_nonconformant_string(void)
1596 RPC_MESSAGE RpcMessage;
1597 MIDL_STUB_MESSAGE StubMsg;
1598 MIDL_STUB_DESC StubDesc;
1599 DWORD size;
1600 void *ptr;
1601 unsigned char *mem, *mem_orig;
1602 unsigned char memsrc[10] = "This is";
1603 unsigned char memsrc2[10] = "This is a";
1605 static const unsigned char fmtstr_nonconf_str[] =
1607 0x26, /* FC_CSTRING */
1608 0x5c, /* FC_PAD */
1609 NdrFcShort( 0xa ), /* 10 */
1612 StubDesc = Object_StubDesc;
1613 StubDesc.pFormatTypes = fmtstr_nonconf_str;
1615 /* length < size */
1616 NdrClientInitializeNew(
1617 &RpcMessage,
1618 &StubMsg,
1619 &StubDesc,
1622 StubMsg.BufferLength = 0;
1624 NdrNonConformantStringBufferSize( &StubMsg, memsrc, fmtstr_nonconf_str );
1625 ok(StubMsg.BufferLength >= strlen((char *)memsrc) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1627 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1628 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1629 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1631 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc, fmtstr_nonconf_str );
1632 ok(ptr == NULL, "ret %p\n", ptr);
1633 size = StubMsg.Buffer - StubMsg.BufferStart;
1634 ok(size == strlen((char *)memsrc) + 1 + 8, "Buffer %p Start %p len %d\n",
1635 StubMsg.Buffer, StubMsg.BufferStart, size);
1636 ok(!memcmp(StubMsg.BufferStart + 8, memsrc, strlen((char *)memsrc) + 1), "incorrectly marshaled\n");
1638 StubMsg.Buffer = StubMsg.BufferStart;
1639 StubMsg.MemorySize = 0;
1640 mem = NULL;
1642 /* Client */
1643 my_alloc_called = 0;
1644 StubMsg.Buffer = StubMsg.BufferStart;
1645 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1646 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1647 ok(mem == mem_orig, "mem alloced\n");
1648 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1650 my_alloc_called = 0;
1651 StubMsg.Buffer = StubMsg.BufferStart;
1652 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1653 todo_wine
1654 ok(mem == mem_orig, "mem alloced\n");
1655 todo_wine
1656 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1658 /* Server */
1659 my_alloc_called = 0;
1660 StubMsg.IsClient = 0;
1661 mem = NULL;
1662 StubMsg.Buffer = StubMsg.BufferStart;
1663 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1664 ok(mem != mem_orig, "mem not alloced\n");
1665 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1666 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1667 NdrOleFree(mem);
1669 my_alloc_called = 0;
1670 mem = mem_orig;
1671 StubMsg.Buffer = StubMsg.BufferStart;
1672 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1673 ok(mem == mem_orig, "mem alloced\n");
1674 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1676 my_alloc_called = 0;
1677 mem = mem_orig;
1678 StubMsg.Buffer = StubMsg.BufferStart;
1679 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1680 todo_wine
1681 ok(mem == mem_orig, "mem alloced\n");
1682 todo_wine
1683 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1685 HeapFree(GetProcessHeap(), 0, mem_orig);
1686 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1688 /* length = size */
1689 NdrClientInitializeNew(
1690 &RpcMessage,
1691 &StubMsg,
1692 &StubDesc,
1695 StubMsg.BufferLength = 0;
1697 NdrNonConformantStringBufferSize( &StubMsg, memsrc2, fmtstr_nonconf_str );
1698 ok(StubMsg.BufferLength >= strlen((char *)memsrc2) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1700 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1701 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1702 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1704 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc2, fmtstr_nonconf_str );
1705 ok(ptr == NULL, "ret %p\n", ptr);
1706 size = StubMsg.Buffer - StubMsg.BufferStart;
1707 ok(size == strlen((char *)memsrc2) + 1 + 8, "Buffer %p Start %p len %d\n",
1708 StubMsg.Buffer, StubMsg.BufferStart, size);
1709 ok(!memcmp(StubMsg.BufferStart + 8, memsrc2, strlen((char *)memsrc2) + 1), "incorrectly marshaled\n");
1711 StubMsg.Buffer = StubMsg.BufferStart;
1712 StubMsg.MemorySize = 0;
1713 mem = NULL;
1715 /* Client */
1716 my_alloc_called = 0;
1717 StubMsg.Buffer = StubMsg.BufferStart;
1718 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
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 StubMsg.Buffer = StubMsg.BufferStart;
1725 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1726 todo_wine
1727 ok(mem == mem_orig, "mem alloced\n");
1728 todo_wine
1729 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1731 /* Server */
1732 my_alloc_called = 0;
1733 StubMsg.IsClient = 0;
1734 mem = NULL;
1735 StubMsg.Buffer = StubMsg.BufferStart;
1736 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1737 ok(mem != mem_orig, "mem not alloced\n");
1738 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1739 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1740 NdrOleFree(mem);
1742 my_alloc_called = 0;
1743 mem = mem_orig;
1744 StubMsg.Buffer = StubMsg.BufferStart;
1745 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1746 ok(mem == mem_orig, "mem alloced\n");
1747 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1749 my_alloc_called = 0;
1750 mem = mem_orig;
1751 StubMsg.Buffer = StubMsg.BufferStart;
1752 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1753 todo_wine
1754 ok(mem == mem_orig, "mem alloced\n");
1755 todo_wine
1756 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1758 HeapFree(GetProcessHeap(), 0, mem_orig);
1759 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1762 static void test_conf_complex_struct(void)
1764 RPC_MESSAGE RpcMessage;
1765 MIDL_STUB_MESSAGE StubMsg;
1766 MIDL_STUB_DESC StubDesc;
1767 void *ptr;
1768 unsigned int i;
1769 struct conf_complex
1771 unsigned int size;
1772 unsigned int *array[1];
1774 struct conf_complex *memsrc;
1775 struct conf_complex *mem;
1777 static const unsigned char fmtstr_complex_struct[] =
1779 /* 0 */
1780 0x1b, /* FC_CARRAY */
1781 0x3, /* 3 */
1782 /* 2 */ NdrFcShort( 0x4 ), /* 4 */
1783 /* 4 */ 0x8, /* Corr desc: FC_LONG */
1784 0x0, /* */
1785 /* 6 */ NdrFcShort( 0xfffc ), /* -4 */
1786 /* 8 */
1787 0x4b, /* FC_PP */
1788 0x5c, /* FC_PAD */
1789 /* 10 */
1790 0x48, /* FC_VARIABLE_REPEAT */
1791 0x49, /* FC_FIXED_OFFSET */
1792 /* 12 */ NdrFcShort( 0x4 ), /* 4 */
1793 /* 14 */ NdrFcShort( 0x0 ), /* 0 */
1794 /* 16 */ NdrFcShort( 0x1 ), /* 1 */
1795 /* 18 */ NdrFcShort( 0x0 ), /* 0 */
1796 /* 20 */ NdrFcShort( 0x0 ), /* 0 */
1797 /* 22 */ 0x12, 0x8, /* FC_UP [simple_pointer] */
1798 /* 24 */ 0x8, /* FC_LONG */
1799 0x5c, /* FC_PAD */
1800 /* 26 */
1801 0x5b, /* FC_END */
1803 0x8, /* FC_LONG */
1804 /* 28 */ 0x5c, /* FC_PAD */
1805 0x5b, /* FC_END */
1806 /* 30 */
1807 0x1a, /* FC_BOGUS_STRUCT */
1808 0x3, /* 3 */
1809 /* 32 */ NdrFcShort( 0x4 ), /* 4 */
1810 /* 34 */ NdrFcShort( 0xffffffde ), /* Offset= -34 (0) */
1811 /* 36 */ NdrFcShort( 0x0 ), /* Offset= 0 (36) */
1812 /* 38 */ 0x8, /* FC_LONG */
1813 0x5b, /* FC_END */
1816 memsrc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1817 FIELD_OFFSET(struct conf_complex, array[20]));
1818 memsrc->size = 20;
1820 StubDesc = Object_StubDesc;
1821 StubDesc.pFormatTypes = fmtstr_complex_struct;
1823 NdrClientInitializeNew(
1824 &RpcMessage,
1825 &StubMsg,
1826 &StubDesc,
1829 StubMsg.BufferLength = 0;
1830 NdrComplexStructBufferSize( &StubMsg,
1831 (unsigned char *)memsrc,
1832 &fmtstr_complex_struct[30] );
1833 ok(StubMsg.BufferLength >= 28, "length %d\n", StubMsg.BufferLength);
1835 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1836 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1837 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1839 ptr = NdrComplexStructMarshall( &StubMsg, (unsigned char *)memsrc,
1840 &fmtstr_complex_struct[30] );
1841 ok(ptr == NULL, "ret %p\n", ptr);
1842 ok(*(unsigned int *)StubMsg.BufferStart == 20, "Conformance should have been 20 instead of %d\n", *(unsigned int *)StubMsg.BufferStart);
1843 ok(*(unsigned int *)(StubMsg.BufferStart + 4) == 20, "conf_complex.size should have been 20 instead of %d\n", *(unsigned int *)(StubMsg.BufferStart + 4));
1844 for (i = 0; i < 20; i++)
1845 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));
1847 /* Server */
1848 my_alloc_called = 0;
1849 StubMsg.IsClient = 0;
1850 mem = NULL;
1851 StubMsg.Buffer = StubMsg.BufferStart;
1852 ptr = NdrComplexStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_struct[30], 0);
1853 ok(ptr == NULL, "ret %p\n", ptr);
1854 ok(mem->size == 20, "mem->size wasn't unmarshalled correctly (%d)\n", mem->size);
1855 ok(mem->array[0] == NULL, "mem->array[0] wasn't unmarshalled correctly (%p)\n", mem->array[0]);
1856 StubMsg.pfnFree(mem);
1858 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1861 static void test_ndr_buffer(void)
1863 static unsigned char ncalrpc[] = "ncalrpc";
1864 static unsigned char endpoint[] = "winetest:test_ndr_buffer";
1865 RPC_MESSAGE RpcMessage;
1866 MIDL_STUB_MESSAGE StubMsg;
1867 MIDL_STUB_DESC StubDesc = Object_StubDesc;
1868 unsigned char *ret;
1869 unsigned char *binding;
1870 RPC_BINDING_HANDLE Handle;
1871 RPC_STATUS status;
1872 ULONG prev_buffer_length;
1873 BOOL old_buffer_valid_location;
1875 StubDesc.RpcInterfaceInformation = (void *)&IFoo___RpcServerInterface;
1877 status = RpcServerUseProtseqEp(ncalrpc, 20, endpoint, NULL);
1878 ok(RPC_S_OK == status, "RpcServerUseProtseqEp failed with status %u\n", status);
1879 status = RpcServerRegisterIf(IFoo_v0_0_s_ifspec, NULL, NULL);
1880 ok(RPC_S_OK == status, "RpcServerRegisterIf failed with status %u\n", status);
1881 status = RpcServerListen(1, 20, TRUE);
1882 ok(RPC_S_OK == status, "RpcServerListen failed with status %u\n", status);
1883 if (status != RPC_S_OK)
1885 /* Failed to create a server, running client tests is useless */
1886 return;
1889 status = RpcStringBindingCompose(NULL, ncalrpc, NULL, endpoint, NULL, &binding);
1890 ok(status == RPC_S_OK, "RpcStringBindingCompose failed (%u)\n", status);
1892 status = RpcBindingFromStringBinding(binding, &Handle);
1893 ok(status == RPC_S_OK, "RpcBindingFromStringBinding failed (%u)\n", status);
1894 RpcStringFree(&binding);
1896 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 5);
1898 ret = NdrGetBuffer(&StubMsg, 10, Handle);
1899 ok(ret == StubMsg.Buffer, "NdrGetBuffer should have returned the same value as StubMsg.Buffer instead of %p\n", ret);
1900 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1901 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1902 ok(RpcMessage.BufferLength == 10 ||
1903 broken(RpcMessage.BufferLength == 12), /* win2k */
1904 "RpcMessage.BufferLength should have been 10 instead of %d\n", RpcMessage.BufferLength);
1905 ok(RpcMessage.RpcFlags == 0, "RpcMessage.RpcFlags should have been 0x0 instead of 0x%x\n", RpcMessage.RpcFlags);
1906 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1907 ok(!StubMsg.BufferStart, "BufferStart should have been NULL instead of %p\n", StubMsg.BufferStart);
1908 ok(!StubMsg.BufferEnd, "BufferEnd should have been NULL instead of %p\n", StubMsg.BufferEnd);
1909 todo_wine
1910 ok(StubMsg.BufferLength == 0, "BufferLength should have left as 0 instead of being set to %d\n", StubMsg.BufferLength);
1911 old_buffer_valid_location = !StubMsg.fBufferValid;
1912 if (old_buffer_valid_location)
1913 ok(broken(StubMsg.CorrDespIncrement == TRUE), "fBufferValid should have been TRUE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1914 else
1915 ok(StubMsg.fBufferValid, "fBufferValid should have been non-zero instead of 0x%x\n", StubMsg.fBufferValid);
1917 prev_buffer_length = RpcMessage.BufferLength;
1918 StubMsg.BufferLength = 1;
1919 NdrFreeBuffer(&StubMsg);
1920 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1921 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1922 ok(RpcMessage.BufferLength == prev_buffer_length, "RpcMessage.BufferLength should have been left as %d instead of %d\n", prev_buffer_length, RpcMessage.BufferLength);
1923 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1924 ok(StubMsg.BufferLength == 1, "BufferLength should have left as 1 instead of being set to %d\n", StubMsg.BufferLength);
1925 if (old_buffer_valid_location)
1926 ok(broken(StubMsg.CorrDespIncrement == FALSE), "fBufferValid should have been FALSE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1927 else
1928 ok(!StubMsg.fBufferValid, "fBufferValid should have been FALSE instead of %d\n", StubMsg.fBufferValid);
1930 /* attempt double-free */
1931 NdrFreeBuffer(&StubMsg);
1933 RpcBindingFree(&Handle);
1935 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
1936 ok(status == RPC_S_OK, "RpcServerUnregisterIf failed (%u)\n", status);
1939 static void test_NdrMapCommAndFaultStatus(void)
1941 RPC_STATUS rpc_status;
1942 MIDL_STUB_MESSAGE StubMsg;
1943 RPC_MESSAGE RpcMessage;
1945 NdrClientInitializeNew(&RpcMessage, &StubMsg, &Object_StubDesc, 5);
1947 for (rpc_status = 0; rpc_status < 10000; rpc_status++)
1949 RPC_STATUS status;
1950 ULONG comm_status = 0;
1951 ULONG fault_status = 0;
1952 ULONG expected_comm_status = 0;
1953 ULONG expected_fault_status = 0;
1954 status = NdrMapCommAndFaultStatus(&StubMsg, &comm_status, &fault_status, rpc_status);
1955 ok(status == RPC_S_OK, "NdrMapCommAndFaultStatus failed with error %d\n", status);
1956 switch (rpc_status)
1958 case ERROR_INVALID_HANDLE:
1959 case RPC_S_INVALID_BINDING:
1960 case RPC_S_UNKNOWN_IF:
1961 case RPC_S_SERVER_UNAVAILABLE:
1962 case RPC_S_SERVER_TOO_BUSY:
1963 case RPC_S_CALL_FAILED_DNE:
1964 case RPC_S_PROTOCOL_ERROR:
1965 case RPC_S_UNSUPPORTED_TRANS_SYN:
1966 case RPC_S_UNSUPPORTED_TYPE:
1967 case RPC_S_PROCNUM_OUT_OF_RANGE:
1968 case EPT_S_NOT_REGISTERED:
1969 case RPC_S_COMM_FAILURE:
1970 expected_comm_status = rpc_status;
1971 break;
1972 default:
1973 expected_fault_status = rpc_status;
1975 ok(comm_status == expected_comm_status, "NdrMapCommAndFaultStatus should have mapped %d to comm status %d instead of %d\n",
1976 rpc_status, expected_comm_status, comm_status);
1977 ok(fault_status == expected_fault_status, "NdrMapCommAndFaultStatus should have mapped %d to fault status %d instead of %d\n",
1978 rpc_status, expected_fault_status, fault_status);
1982 static void test_NdrGetUserMarshalInfo(void)
1984 RPC_STATUS status;
1985 MIDL_STUB_MESSAGE stubmsg;
1986 USER_MARSHAL_CB umcb;
1987 NDR_USER_MARSHAL_INFO umi;
1988 unsigned char buffer[16];
1989 void *rpc_channel_buffer = (void *)(ULONG_PTR)0xcafebabe;
1990 RPC_MESSAGE rpc_msg;
1991 RPC_STATUS (RPC_ENTRY *pNdrGetUserMarshalInfo)(ULONG *,ULONG,NDR_USER_MARSHAL_INFO *);
1993 pNdrGetUserMarshalInfo = (void *)GetProcAddress(GetModuleHandle("rpcrt4.dll"), "NdrGetUserMarshalInfo");
1994 if (!pNdrGetUserMarshalInfo)
1996 skip("NdrGetUserMarshalInfo not exported\n");
1997 return;
2000 /* unmarshall */
2002 memset(&rpc_msg, 0xcc, sizeof(rpc_msg));
2003 rpc_msg.Buffer = buffer;
2004 rpc_msg.BufferLength = 16;
2006 memset(&stubmsg, 0xcc, sizeof(stubmsg));
2007 stubmsg.RpcMsg = &rpc_msg;
2008 stubmsg.dwDestContext = MSHCTX_INPROC;
2009 stubmsg.pvDestContext = NULL;
2010 stubmsg.Buffer = buffer + 15;
2011 stubmsg.BufferLength = 0;
2012 stubmsg.BufferEnd = NULL;
2013 stubmsg.pRpcChannelBuffer = rpc_channel_buffer;
2014 stubmsg.StubDesc = NULL;
2015 stubmsg.pfnAllocate = my_alloc;
2016 stubmsg.pfnFree = my_free;
2018 memset(&umcb, 0xcc, sizeof(umcb));
2019 umcb.Flags = MAKELONG(MSHCTX_INPROC, NDR_LOCAL_DATA_REPRESENTATION);
2020 umcb.pStubMsg = &stubmsg;
2021 umcb.Signature = USER_MARSHAL_CB_SIGNATURE;
2022 umcb.CBType = USER_MARSHAL_CB_UNMARSHALL;
2024 memset(&umi, 0xaa, sizeof(umi));
2026 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2027 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2028 ok( umi.InformationLevel == 1,
2029 "umi.InformationLevel was %u instead of 1\n",
2030 umi.InformationLevel);
2031 ok( U(umi.Level1).Buffer == buffer + 15,
2032 "U(umi.Level1).Buffer was %p instead of %p\n",
2033 U(umi.Level1).Buffer, buffer);
2034 ok( U(umi.Level1).BufferSize == 1,
2035 "U(umi.Level1).BufferSize was %u instead of 1\n",
2036 U(umi.Level1).BufferSize);
2037 ok( U(umi.Level1).pfnAllocate == my_alloc,
2038 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2039 U(umi.Level1).pfnAllocate, my_alloc);
2040 ok( U(umi.Level1).pfnFree == my_free,
2041 "U(umi.Level1).pfnFree was %p instead of %p\n",
2042 U(umi.Level1).pfnFree, my_free);
2043 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2044 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2045 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2047 /* buffer size */
2049 rpc_msg.Buffer = buffer;
2050 rpc_msg.BufferLength = 16;
2052 stubmsg.Buffer = buffer;
2053 stubmsg.BufferLength = 16;
2054 stubmsg.BufferEnd = NULL;
2056 umcb.CBType = USER_MARSHAL_CB_BUFFER_SIZE;
2058 memset(&umi, 0xaa, sizeof(umi));
2060 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2061 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2062 ok( umi.InformationLevel == 1,
2063 "umi.InformationLevel was %u instead of 1\n",
2064 umi.InformationLevel);
2065 ok( U(umi.Level1).Buffer == NULL,
2066 "U(umi.Level1).Buffer was %p instead of NULL\n",
2067 U(umi.Level1).Buffer);
2068 ok( U(umi.Level1).BufferSize == 0,
2069 "U(umi.Level1).BufferSize was %u instead of 0\n",
2070 U(umi.Level1).BufferSize);
2071 ok( U(umi.Level1).pfnAllocate == my_alloc,
2072 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2073 U(umi.Level1).pfnAllocate, my_alloc);
2074 ok( U(umi.Level1).pfnFree == my_free,
2075 "U(umi.Level1).pfnFree was %p instead of %p\n",
2076 U(umi.Level1).pfnFree, my_free);
2077 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2078 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2079 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2081 /* marshall */
2083 rpc_msg.Buffer = buffer;
2084 rpc_msg.BufferLength = 16;
2086 stubmsg.Buffer = buffer + 15;
2087 stubmsg.BufferLength = 0;
2088 stubmsg.BufferEnd = NULL;
2090 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2092 memset(&umi, 0xaa, sizeof(umi));
2094 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2095 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2096 ok( umi.InformationLevel == 1,
2097 "umi.InformationLevel was %u instead of 1\n",
2098 umi.InformationLevel);
2099 ok( U(umi.Level1).Buffer == buffer + 15,
2100 "U(umi.Level1).Buffer was %p instead of %p\n",
2101 U(umi.Level1).Buffer, buffer);
2102 ok( U(umi.Level1).BufferSize == 1,
2103 "U(umi.Level1).BufferSize was %u instead of 1\n",
2104 U(umi.Level1).BufferSize);
2105 ok( U(umi.Level1).pfnAllocate == my_alloc,
2106 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2107 U(umi.Level1).pfnAllocate, my_alloc);
2108 ok( U(umi.Level1).pfnFree == my_free,
2109 "U(umi.Level1).pfnFree was %p instead of %p\n",
2110 U(umi.Level1).pfnFree, my_free);
2111 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2112 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2113 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2115 /* free */
2117 rpc_msg.Buffer = buffer;
2118 rpc_msg.BufferLength = 16;
2120 stubmsg.Buffer = buffer;
2121 stubmsg.BufferLength = 16;
2122 stubmsg.BufferEnd = NULL;
2124 umcb.CBType = USER_MARSHAL_CB_FREE;
2126 memset(&umi, 0xaa, sizeof(umi));
2128 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2129 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2130 ok( umi.InformationLevel == 1,
2131 "umi.InformationLevel was %u instead of 1\n",
2132 umi.InformationLevel);
2133 ok( U(umi.Level1).Buffer == NULL,
2134 "U(umi.Level1).Buffer was %p instead of NULL\n",
2135 U(umi.Level1).Buffer);
2136 ok( U(umi.Level1).BufferSize == 0,
2137 "U(umi.Level1).BufferSize was %u instead of 0\n",
2138 U(umi.Level1).BufferSize);
2139 ok( U(umi.Level1).pfnAllocate == my_alloc,
2140 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2141 U(umi.Level1).pfnAllocate, my_alloc);
2142 ok( U(umi.Level1).pfnFree == my_free,
2143 "U(umi.Level1).pfnFree was %p instead of %p\n",
2144 U(umi.Level1).pfnFree, my_free);
2145 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2146 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2147 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2149 /* boundary test */
2151 rpc_msg.Buffer = buffer;
2152 rpc_msg.BufferLength = 15;
2154 stubmsg.Buffer = buffer + 15;
2155 stubmsg.BufferLength = 0;
2156 stubmsg.BufferEnd = NULL;
2158 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2160 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2161 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2162 ok( U(umi.Level1).BufferSize == 0,
2163 "U(umi.Level1).BufferSize was %u instead of 0\n",
2164 U(umi.Level1).BufferSize);
2166 /* error conditions */
2168 rpc_msg.BufferLength = 14;
2169 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2170 ok(status == ERROR_INVALID_USER_BUFFER,
2171 "NdrGetUserMarshalInfo should have failed with ERROR_INVALID_USER_BUFFER instead of %d\n", status);
2173 rpc_msg.BufferLength = 15;
2174 status = pNdrGetUserMarshalInfo(&umcb.Flags, 9999, &umi);
2175 ok(status == RPC_S_INVALID_ARG,
2176 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2178 umcb.CBType = 9999;
2179 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2180 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2182 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2183 umcb.Signature = 0;
2184 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2185 ok(status == RPC_S_INVALID_ARG,
2186 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2189 START_TEST( ndr_marshall )
2191 determine_pointer_marshalling_style();
2193 test_ndr_simple_type();
2194 test_simple_types();
2195 test_nontrivial_pointer_types();
2196 test_simple_struct();
2197 test_fullpointer_xlat();
2198 test_client_init();
2199 test_server_init();
2200 test_ndr_allocate();
2201 test_conformant_array();
2202 test_conformant_string();
2203 test_nonconformant_string();
2204 test_conf_complex_struct();
2205 test_ndr_buffer();
2206 test_NdrMapCommAndFaultStatus();
2207 test_NdrGetUserMarshalInfo();