rpcrt4: Implement NdrPointerMemorySize and enable the corresponding tests.
[wine.git] / dlls / rpcrt4 / tests / ndr_marshall.c
blob0b8b6559f421b9b25e81a728b059a9895649b2a6
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 static void test_client_init(void)
1115 MIDL_STUB_MESSAGE stubMsg;
1116 RPC_MESSAGE rpcMsg;
1117 void *unset_ptr;
1119 memset(&rpcMsg, 0xcc, sizeof(rpcMsg));
1120 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1121 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1123 NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
1125 #define TEST_POINTER_UNSET(field) ok(rpcMsg.field == unset_ptr, #field " should have been unset instead of %p\n", rpcMsg.field)
1127 ok(rpcMsg.Handle == NULL, "rpcMsg.Handle should have been NULL instead of %p\n", rpcMsg.Handle);
1128 TEST_POINTER_UNSET(Buffer);
1129 ok(rpcMsg.BufferLength == 0xcccccccc, "rpcMsg.BufferLength should have been unset instead of %d\n", rpcMsg.BufferLength);
1130 ok(rpcMsg.ProcNum == 0x8001, "rpcMsg.ProcNum should have been 0x8001 instead of 0x%x\n", rpcMsg.ProcNum);
1131 TEST_POINTER_UNSET(TransferSyntax);
1132 ok(rpcMsg.RpcInterfaceInformation == Object_StubDesc.RpcInterfaceInformation,
1133 "rpcMsg.RpcInterfaceInformation should have been %p instead of %p\n",
1134 Object_StubDesc.RpcInterfaceInformation, rpcMsg.RpcInterfaceInformation);
1135 /* Note: ReservedForRuntime not tested */
1136 TEST_POINTER_UNSET(ManagerEpv);
1137 TEST_POINTER_UNSET(ImportContext);
1138 ok(rpcMsg.RpcFlags == 0, "rpcMsg.RpcFlags should have been 0 instead of 0x%x\n", rpcMsg.RpcFlags);
1139 #undef TEST_POINTER_UNSET
1141 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1142 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == unset_ptr, #field " should have been unset instead of %p\n", stubMsg.field)
1143 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1144 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == (ULONG_PTR)unset_ptr, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1146 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1147 TEST_POINTER_UNSET(Buffer);
1148 TEST_ZERO(BufferStart, "%p");
1149 TEST_ZERO(BufferEnd, "%p");
1150 TEST_POINTER_UNSET(BufferMark);
1151 TEST_ZERO(BufferLength, "%d");
1152 TEST_ULONG_UNSET(MemorySize);
1153 TEST_POINTER_UNSET(Memory);
1154 ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
1155 TEST_ZERO(ReuseBuffer, "%d");
1156 TEST_ZERO(pAllocAllNodesContext, "%p");
1157 ok(stubMsg.pPointerQueueState == 0 ||
1158 broken(stubMsg.pPointerQueueState == unset_ptr), /* win2k */
1159 "stubMsg.pPointerQueueState should have been unset instead of %p\n", stubMsg.pPointerQueueState);
1160 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1161 TEST_ZERO(PointerBufferMark, "%p");
1162 TEST_ZERO(CorrDespIncrement, "%d");
1163 TEST_ZERO(uFlags, "%d");
1164 /* FIXME: UniquePtrCount */
1165 TEST_ULONG_PTR_UNSET(MaxCount);
1166 TEST_ULONG_UNSET(Offset);
1167 TEST_ULONG_UNSET(ActualCount);
1168 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1169 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1170 TEST_ZERO(StackTop, "%p");
1171 TEST_POINTER_UNSET(pPresentedType);
1172 TEST_POINTER_UNSET(pTransmitType);
1173 TEST_POINTER_UNSET(SavedHandle);
1174 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1175 TEST_POINTER_UNSET(FullPtrXlatTables);
1176 TEST_ZERO(FullPtrRefId, "%d");
1177 TEST_ZERO(PointerLength, "%d");
1178 TEST_ZERO(fInDontFree, "%d");
1179 TEST_ZERO(fDontCallFreeInst, "%d");
1180 ok(stubMsg.fInOnlyParam == 0 ||
1181 stubMsg.fInOnlyParam == -1, /* Vista */
1182 "fInOnlyParam should have been set to 0 or -1 instead of %d\n", stubMsg.fInOnlyParam);
1183 TEST_ZERO(fHasReturn, "%d");
1184 TEST_ZERO(fHasExtensions, "%d");
1185 TEST_ZERO(fHasNewCorrDesc, "%d");
1186 TEST_ZERO(fIsIn, "%d");
1187 ok(stubMsg.fIsOut == 0 ||
1188 stubMsg.fIsOut == -1, /* XP-SP3 */
1189 "fIsOut should have been set to 0 or -1 instead of %d\n", stubMsg.fIsOut);
1190 TEST_ZERO(fIsOicf, "%d");
1191 trace("NdrClientInitializeNew: fBufferValid = %d\n", stubMsg.fBufferValid);
1192 ok(stubMsg.fHasMemoryValidateCallback == 0 ||
1193 stubMsg.fHasMemoryValidateCallback == -1, /* XP-SP3 */
1194 "fHasMemoryValidateCallback should have been set to 0 or -1 instead of %d\n", stubMsg.fHasMemoryValidateCallback);
1195 ok(stubMsg.fInFree == 0 ||
1196 stubMsg.fInFree == -1, /* XP-SP3 */
1197 "fInFree should have been set to 0 or -1 instead of %d\n", stubMsg.fInFree);
1198 TEST_ZERO(fNeedMCCP, "%d");
1199 ok(stubMsg.fUnused == 0 ||
1200 stubMsg.fUnused == -2, /* Vista */
1201 "fUnused should have been set to 0 or -2 instead of %d\n", stubMsg.fUnused);
1202 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xffffcccc instead of 0x%x\n", stubMsg.fUnused2);
1203 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1204 TEST_ZERO(pvDestContext, "%p");
1205 TEST_POINTER_UNSET(SavedContextHandles);
1206 TEST_ULONG_UNSET(ParamNumber);
1207 TEST_ZERO(pRpcChannelBuffer, "%p");
1208 TEST_ZERO(pArrayInfo, "%p");
1209 TEST_POINTER_UNSET(SizePtrCountArray);
1210 TEST_POINTER_UNSET(SizePtrOffsetArray);
1211 TEST_POINTER_UNSET(SizePtrLengthArray);
1212 TEST_POINTER_UNSET(pArgQueue);
1213 TEST_ZERO(dwStubPhase, "%d");
1214 /* FIXME: where does this value come from? */
1215 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1216 TEST_ZERO(pAsyncMsg, "%p");
1217 TEST_ZERO(pCorrInfo, "%p");
1218 TEST_ZERO(pCorrMemory, "%p");
1219 TEST_ZERO(pMemoryList, "%p");
1220 TEST_POINTER_UNSET(pCSInfo);
1221 TEST_POINTER_UNSET(ConformanceMark);
1222 TEST_POINTER_UNSET(VarianceMark);
1223 ok(stubMsg.Unused == (ULONG_PTR)unset_ptr, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1224 TEST_POINTER_UNSET(pContext);
1225 TEST_POINTER_UNSET(ContextHandleHash);
1226 TEST_POINTER_UNSET(pUserMarshalList);
1227 TEST_ULONG_PTR_UNSET(Reserved51_3);
1228 TEST_ULONG_PTR_UNSET(Reserved51_4);
1229 TEST_ULONG_PTR_UNSET(Reserved51_5);
1230 #undef TEST_ULONG_UNSET
1231 #undef TEST_POINTER_UNSET
1232 #undef TEST_ZERO
1236 static void test_server_init(void)
1238 MIDL_STUB_MESSAGE stubMsg;
1239 RPC_MESSAGE rpcMsg;
1240 unsigned char *ret;
1241 unsigned char buffer[256];
1242 void *unset_ptr;
1244 memset(&rpcMsg, 0, sizeof(rpcMsg));
1245 rpcMsg.Buffer = buffer;
1246 rpcMsg.BufferLength = sizeof(buffer);
1247 rpcMsg.RpcFlags = RPC_BUFFER_COMPLETE;
1249 memset(&stubMsg, 0xcc, sizeof(stubMsg));
1250 memset(&unset_ptr, 0xcc, sizeof(unset_ptr));
1252 ret = NdrServerInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc);
1253 ok(ret == NULL, "NdrServerInitializeNew should have returned NULL instead of %p\n", ret);
1255 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have been set to zero instead of " fmt "\n", stubMsg.field)
1256 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == unset_ptr, #field " should have been unset instead of %p\n", stubMsg.field)
1257 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have been unset instead of 0x%x\n", stubMsg.field)
1258 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == (ULONG_PTR)unset_ptr, #field " should have been unset instead of 0x%lx\n", stubMsg.field)
1260 ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
1261 ok(stubMsg.Buffer == buffer, "stubMsg.Buffer should have been %p instead of %p\n", buffer, stubMsg.Buffer);
1262 ok(stubMsg.BufferStart == buffer, "stubMsg.BufferStart should have been %p instead of %p\n", buffer, stubMsg.BufferStart);
1263 ok(stubMsg.BufferEnd == buffer + sizeof(buffer), "stubMsg.BufferEnd should have been %p instead of %p\n", buffer + sizeof(buffer), stubMsg.BufferEnd);
1264 TEST_POINTER_UNSET(BufferMark);
1265 todo_wine
1266 TEST_ZERO(BufferLength, "%d");
1267 TEST_ULONG_UNSET(MemorySize);
1268 TEST_POINTER_UNSET(Memory);
1269 ok(stubMsg.IsClient == 0, "stubMsg.IsClient should have been 0 instead of %u\n", stubMsg.IsClient);
1270 ok(stubMsg.ReuseBuffer == 0 ||
1271 broken(stubMsg.ReuseBuffer == 1), /* win2k */
1272 "stubMsg.ReuseBuffer should have been set to zero instead of %d\n", stubMsg.ReuseBuffer);
1273 TEST_ZERO(pAllocAllNodesContext, "%p");
1274 ok(stubMsg.pPointerQueueState == 0 ||
1275 broken(stubMsg.pPointerQueueState == unset_ptr), /* win2k */
1276 "stubMsg.pPointerQueueState should have been unset instead of %p\n", stubMsg.pPointerQueueState);
1277 TEST_ZERO(IgnoreEmbeddedPointers, "%d");
1278 TEST_ZERO(PointerBufferMark, "%p");
1279 ok(stubMsg.CorrDespIncrement == 0xcc ||
1280 stubMsg.CorrDespIncrement == 0,
1281 "CorrDespIncrement should have been unset instead of 0x%x\n", stubMsg.CorrDespIncrement);
1282 TEST_ZERO(uFlags, "%d");
1283 /* FIXME: UniquePtrCount */
1284 TEST_ULONG_PTR_UNSET(MaxCount);
1285 TEST_ULONG_UNSET(Offset);
1286 TEST_ULONG_UNSET(ActualCount);
1287 ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
1288 ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
1289 TEST_ZERO(StackTop, "%p");
1290 TEST_POINTER_UNSET(pPresentedType);
1291 TEST_POINTER_UNSET(pTransmitType);
1292 TEST_POINTER_UNSET(SavedHandle);
1293 ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
1294 TEST_ZERO(FullPtrXlatTables, "%p");
1295 TEST_ZERO(FullPtrRefId, "%d");
1296 TEST_ZERO(PointerLength, "%d");
1297 TEST_ZERO(fInDontFree, "%d");
1298 TEST_ZERO(fDontCallFreeInst, "%d");
1299 ok(stubMsg.fInOnlyParam == 0 ||
1300 stubMsg.fInOnlyParam == -1, /* Vista */
1301 "fInOnlyParam should have been set to 0 or -1 instead of %d\n", stubMsg.fInOnlyParam);
1302 TEST_ZERO(fHasReturn, "%d");
1303 TEST_ZERO(fHasExtensions, "%d");
1304 TEST_ZERO(fHasNewCorrDesc, "%d");
1305 TEST_ZERO(fIsIn, "%d");
1306 ok(stubMsg.fIsOut == 0 ||
1307 stubMsg.fIsOut == -1, /* XP-SP3 */
1308 "fIsOut should have been set to 0 or -1 instead of %d\n", stubMsg.fIsOut);
1309 TEST_ZERO(fIsOicf, "%d");
1310 trace("NdrServerInitializeNew: fBufferValid = %d\n", stubMsg.fBufferValid);
1311 ok(stubMsg.fHasMemoryValidateCallback == 0 ||
1312 stubMsg.fHasMemoryValidateCallback == -1, /* XP-SP3 */
1313 "fHasMemoryValidateCallback should have been set to 0 or -1 instead of %d\n", stubMsg.fHasMemoryValidateCallback);
1314 ok(stubMsg.fInFree == 0 ||
1315 stubMsg.fInFree == -1, /* XP-SP3 */
1316 "fInFree should have been set to 0 or -1 instead of %d\n", stubMsg.fInFree);
1317 TEST_ZERO(fNeedMCCP, "%d");
1318 ok(stubMsg.fUnused == 0 ||
1319 stubMsg.fUnused == -2, /* Vista */
1320 "fUnused should have been set to 0 or -2 instead of %d\n", stubMsg.fUnused);
1321 ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xffffcccc instead of 0x%x\n", stubMsg.fUnused2);
1322 ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
1323 TEST_ZERO(pvDestContext, "%p");
1324 TEST_POINTER_UNSET(SavedContextHandles);
1325 TEST_ULONG_UNSET(ParamNumber);
1326 TEST_ZERO(pRpcChannelBuffer, "%p");
1327 TEST_ZERO(pArrayInfo, "%p");
1328 TEST_POINTER_UNSET(SizePtrCountArray);
1329 TEST_POINTER_UNSET(SizePtrOffsetArray);
1330 TEST_POINTER_UNSET(SizePtrLengthArray);
1331 TEST_POINTER_UNSET(pArgQueue);
1332 TEST_ZERO(dwStubPhase, "%d");
1333 /* FIXME: where does this value come from? */
1334 trace("LowStackMark is %p\n", stubMsg.LowStackMark);
1335 TEST_ZERO(pAsyncMsg, "%p");
1336 TEST_ZERO(pCorrInfo, "%p");
1337 TEST_ZERO(pCorrMemory, "%p");
1338 TEST_ZERO(pMemoryList, "%p");
1339 TEST_POINTER_UNSET(pCSInfo);
1340 TEST_POINTER_UNSET(ConformanceMark);
1341 TEST_POINTER_UNSET(VarianceMark);
1342 ok(stubMsg.Unused == (ULONG_PTR)unset_ptr, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
1343 TEST_POINTER_UNSET(pContext);
1344 TEST_POINTER_UNSET(ContextHandleHash);
1345 TEST_POINTER_UNSET(pUserMarshalList);
1346 TEST_ULONG_PTR_UNSET(Reserved51_3);
1347 TEST_ULONG_PTR_UNSET(Reserved51_4);
1348 TEST_ULONG_PTR_UNSET(Reserved51_5);
1349 #undef TEST_ULONG_UNSET
1350 #undef TEST_POINTER_UNSET
1351 #undef TEST_ZERO
1355 static void test_ndr_allocate(void)
1357 RPC_MESSAGE RpcMessage;
1358 MIDL_STUB_MESSAGE StubMsg;
1359 MIDL_STUB_DESC StubDesc;
1360 void *p1, *p2;
1361 struct tag_mem_list_v1_t
1363 DWORD magic;
1364 void *ptr;
1365 struct tag_mem_list_v1_t *next;
1366 } *mem_list_v1;
1367 struct tag_mem_list_v2_t
1369 DWORD magic;
1370 DWORD size;
1371 DWORD unknown;
1372 struct tag_mem_list_v2_t *next;
1373 } *mem_list_v2;
1374 const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
1376 StubDesc = Object_StubDesc;
1377 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
1379 ok(StubMsg.pMemoryList == NULL, "memlist %p\n", StubMsg.pMemoryList);
1380 my_alloc_called = my_free_called = 0;
1381 p1 = NdrAllocate(&StubMsg, 10);
1382 p2 = NdrAllocate(&StubMsg, 24);
1383 ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
1384 ok(StubMsg.pMemoryList != NULL, "StubMsg.pMemoryList NULL\n");
1385 if(StubMsg.pMemoryList)
1387 mem_list_v2 = StubMsg.pMemoryList;
1388 if (mem_list_v2->size == 24)
1390 trace("v2 mem list format\n");
1391 ok((char *)mem_list_v2 == (char *)p2 + 24, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p2 + 24, mem_list_v2);
1392 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1393 ok(mem_list_v2->size == 24, "wrong size for p2 %d\n", mem_list_v2->size);
1394 ok(mem_list_v2->unknown == 0, "wrong unknown for p2 0x%x\n", mem_list_v2->unknown);
1395 ok(mem_list_v2->next != NULL, "next NULL\n");
1396 mem_list_v2 = mem_list_v2->next;
1397 if(mem_list_v2)
1399 ok((char *)mem_list_v2 == (char *)p1 + 16, "expected mem_list_v2 pointer %p, but got %p\n", (char *)p1 + 16, mem_list_v2);
1400 ok(mem_list_v2->magic == magic_MEML, "magic %08x\n", mem_list_v2->magic);
1401 ok(mem_list_v2->size == 16, "wrong size for p1 %d\n", mem_list_v2->size);
1402 ok(mem_list_v2->unknown == 0, "wrong unknown for p1 0x%x\n", mem_list_v2->unknown);
1403 ok(mem_list_v2->next == NULL, "next %p\n", mem_list_v2->next);
1406 else
1408 trace("v1 mem list format\n");
1409 mem_list_v1 = StubMsg.pMemoryList;
1410 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1411 ok(mem_list_v1->ptr == p2, "ptr != p2\n");
1412 ok(mem_list_v1->next != NULL, "next NULL\n");
1413 mem_list_v1 = mem_list_v1->next;
1414 if(mem_list_v1)
1416 ok(mem_list_v1->magic == magic_MEML, "magic %08x\n", mem_list_v1->magic);
1417 ok(mem_list_v1->ptr == p1, "ptr != p1\n");
1418 ok(mem_list_v1->next == NULL, "next %p\n", mem_list_v1->next);
1422 /* NdrFree isn't exported so we can't test free'ing */
1425 static void test_conformant_array(void)
1427 RPC_MESSAGE RpcMessage;
1428 MIDL_STUB_MESSAGE StubMsg;
1429 MIDL_STUB_DESC StubDesc;
1430 void *ptr;
1431 unsigned char *mem, *mem_orig;
1432 unsigned char memsrc[20];
1433 unsigned int i;
1435 static const unsigned char fmtstr_conf_array[] =
1437 0x1b, /* FC_CARRAY */
1438 0x0, /* align */
1439 NdrFcShort( 0x1 ), /* elem size */
1440 0x40, /* Corr desc: const */
1441 0x0,
1442 NdrFcShort(0x10), /* const = 0x10 */
1443 0x1, /* FC_BYTE */
1444 0x5b /* FC_END */
1447 for (i = 0; i < sizeof(memsrc); i++)
1448 memsrc[i] = i * i;
1450 StubDesc = Object_StubDesc;
1451 StubDesc.pFormatTypes = fmtstr_conf_array;
1453 NdrClientInitializeNew(
1454 &RpcMessage,
1455 &StubMsg,
1456 &StubDesc,
1459 StubMsg.BufferLength = 0;
1460 NdrConformantArrayBufferSize( &StubMsg,
1461 memsrc,
1462 fmtstr_conf_array );
1463 ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1465 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1466 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1467 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1469 ptr = NdrConformantArrayMarshall( &StubMsg, memsrc, fmtstr_conf_array );
1470 ok(ptr == NULL, "ret %p\n", ptr);
1471 ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1472 ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1474 StubMsg.Buffer = StubMsg.BufferStart;
1475 StubMsg.MemorySize = 0;
1476 mem = NULL;
1478 /* Client */
1479 my_alloc_called = 0;
1480 /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1481 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1482 ok(mem != NULL, "mem not alloced\n");
1483 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1484 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1486 my_alloc_called = 0;
1487 StubMsg.Buffer = StubMsg.BufferStart;
1488 mem_orig = mem;
1489 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1490 ok(mem == mem_orig, "mem alloced\n");
1491 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1492 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1494 my_alloc_called = 0;
1495 StubMsg.Buffer = StubMsg.BufferStart;
1496 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1497 ok(mem != mem_orig, "mem not alloced\n");
1498 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1499 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1501 my_free_called = 0;
1502 StubMsg.Buffer = StubMsg.BufferStart;
1503 NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1504 ok(my_free_called == 0, "free called %d\n", my_free_called);
1505 StubMsg.pfnFree(mem);
1507 /* Server */
1508 my_alloc_called = 0;
1509 StubMsg.IsClient = 0;
1510 mem = NULL;
1511 StubMsg.Buffer = StubMsg.BufferStart;
1512 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1513 ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1514 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1515 my_alloc_called = 0;
1516 mem = NULL;
1517 StubMsg.Buffer = StubMsg.BufferStart;
1518 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1519 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1520 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1521 StubMsg.pfnFree(mem);
1523 my_alloc_called = 0;
1524 mem = mem_orig;
1525 StubMsg.Buffer = StubMsg.BufferStart;
1526 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1527 ok(mem == mem_orig, "mem alloced\n");
1528 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1530 my_alloc_called = 0;
1531 mem = mem_orig;
1532 StubMsg.Buffer = StubMsg.BufferStart;
1533 NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1534 ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1535 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1536 StubMsg.pfnFree(mem);
1537 StubMsg.pfnFree(mem_orig);
1539 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1542 static void test_conformant_string(void)
1544 RPC_MESSAGE RpcMessage;
1545 MIDL_STUB_MESSAGE StubMsg;
1546 MIDL_STUB_DESC StubDesc;
1547 DWORD size;
1548 void *ptr;
1549 unsigned char *mem, *mem_orig;
1550 char memsrc[] = "This is a test string";
1552 static const unsigned char fmtstr_conf_str[] =
1554 0x11, 0x8, /* FC_RP [simple_pointer] */
1555 0x22, /* FC_C_CSTRING */
1556 0x5c, /* FC_PAD */
1559 StubDesc = Object_StubDesc;
1560 StubDesc.pFormatTypes = fmtstr_conf_str;
1562 NdrClientInitializeNew(
1563 &RpcMessage,
1564 &StubMsg,
1565 &StubDesc,
1568 StubMsg.BufferLength = 0;
1569 NdrPointerBufferSize( &StubMsg,
1570 (unsigned char *)memsrc,
1571 fmtstr_conf_str );
1572 ok(StubMsg.BufferLength >= sizeof(memsrc) + 12, "length %d\n", StubMsg.BufferLength);
1574 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1575 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1576 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1578 ptr = NdrPointerMarshall( &StubMsg, (unsigned char *)memsrc, fmtstr_conf_str );
1579 ok(ptr == NULL, "ret %p\n", ptr);
1580 size = StubMsg.Buffer - StubMsg.BufferStart;
1581 ok(size == sizeof(memsrc) + 12, "Buffer %p Start %p len %d\n",
1582 StubMsg.Buffer, StubMsg.BufferStart, size);
1583 ok(!memcmp(StubMsg.BufferStart + 12, memsrc, sizeof(memsrc)), "incorrectly marshaled\n");
1585 StubMsg.Buffer = StubMsg.BufferStart;
1586 StubMsg.MemorySize = 0;
1587 mem = NULL;
1589 /* Client */
1590 my_alloc_called = 0;
1591 StubMsg.Buffer = StubMsg.BufferStart;
1592 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1593 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1594 ok(mem == mem_orig, "mem not alloced\n");
1595 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1597 my_alloc_called = 0;
1598 StubMsg.Buffer = StubMsg.BufferStart;
1599 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1600 todo_wine {
1601 ok(mem == mem_orig, "mem not alloced\n");
1602 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1605 my_free_called = 0;
1606 StubMsg.Buffer = StubMsg.BufferStart;
1607 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1608 ok(my_free_called == 1, "free called %d\n", my_free_called);
1610 mem = my_alloc(10);
1611 my_free_called = 0;
1612 StubMsg.Buffer = StubMsg.BufferStart;
1613 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1614 ok(my_free_called == 1, "free called %d\n", my_free_called);
1616 /* Server */
1617 my_alloc_called = 0;
1618 StubMsg.IsClient = 0;
1619 mem = NULL;
1620 StubMsg.Buffer = StubMsg.BufferStart;
1621 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1622 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1623 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1625 my_alloc_called = 0;
1626 mem = NULL;
1627 StubMsg.Buffer = StubMsg.BufferStart;
1628 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1629 todo_wine {
1630 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1631 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1634 my_alloc_called = 0;
1635 mem = mem_orig;
1636 StubMsg.Buffer = StubMsg.BufferStart;
1637 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 0);
1638 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1639 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1641 my_alloc_called = 0;
1642 mem = mem_orig;
1643 StubMsg.Buffer = StubMsg.BufferStart;
1644 NdrPointerUnmarshall( &StubMsg, &mem, fmtstr_conf_str, 1);
1645 todo_wine {
1646 ok(mem == StubMsg.BufferStart + 12, "mem not pointing at buffer\n");
1647 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1650 mem = my_alloc(10);
1651 my_free_called = 0;
1652 StubMsg.Buffer = StubMsg.BufferStart;
1653 NdrPointerFree( &StubMsg, mem, fmtstr_conf_str );
1654 ok(my_free_called == 1, "free called %d\n", my_free_called);
1656 HeapFree(GetProcessHeap(), 0, mem_orig);
1657 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1660 static void test_nonconformant_string(void)
1662 RPC_MESSAGE RpcMessage;
1663 MIDL_STUB_MESSAGE StubMsg;
1664 MIDL_STUB_DESC StubDesc;
1665 DWORD size;
1666 void *ptr;
1667 unsigned char *mem, *mem_orig;
1668 unsigned char memsrc[10] = "This is";
1669 unsigned char memsrc2[10] = "This is a";
1671 static const unsigned char fmtstr_nonconf_str[] =
1673 0x26, /* FC_CSTRING */
1674 0x5c, /* FC_PAD */
1675 NdrFcShort( 0xa ), /* 10 */
1678 StubDesc = Object_StubDesc;
1679 StubDesc.pFormatTypes = fmtstr_nonconf_str;
1681 /* length < size */
1682 NdrClientInitializeNew(
1683 &RpcMessage,
1684 &StubMsg,
1685 &StubDesc,
1688 StubMsg.BufferLength = 0;
1690 NdrNonConformantStringBufferSize( &StubMsg, memsrc, fmtstr_nonconf_str );
1691 ok(StubMsg.BufferLength >= strlen((char *)memsrc) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1693 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1694 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1695 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1697 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc, fmtstr_nonconf_str );
1698 ok(ptr == NULL, "ret %p\n", ptr);
1699 size = StubMsg.Buffer - StubMsg.BufferStart;
1700 ok(size == strlen((char *)memsrc) + 1 + 8, "Buffer %p Start %p len %d\n",
1701 StubMsg.Buffer, StubMsg.BufferStart, size);
1702 ok(!memcmp(StubMsg.BufferStart + 8, memsrc, strlen((char *)memsrc) + 1), "incorrectly marshaled\n");
1704 StubMsg.Buffer = StubMsg.BufferStart;
1705 StubMsg.MemorySize = 0;
1706 mem = NULL;
1708 /* Client */
1709 my_alloc_called = 0;
1710 StubMsg.Buffer = StubMsg.BufferStart;
1711 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1712 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1713 ok(mem == mem_orig, "mem alloced\n");
1714 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1716 my_alloc_called = 0;
1717 StubMsg.Buffer = StubMsg.BufferStart;
1718 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1719 todo_wine
1720 ok(mem == mem_orig, "mem alloced\n");
1721 todo_wine
1722 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1724 /* Server */
1725 my_alloc_called = 0;
1726 StubMsg.IsClient = 0;
1727 mem = NULL;
1728 StubMsg.Buffer = StubMsg.BufferStart;
1729 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1730 ok(mem != mem_orig, "mem not alloced\n");
1731 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1732 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1733 NdrOleFree(mem);
1735 my_alloc_called = 0;
1736 mem = mem_orig;
1737 StubMsg.Buffer = StubMsg.BufferStart;
1738 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1739 ok(mem == mem_orig, "mem alloced\n");
1740 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1742 my_alloc_called = 0;
1743 mem = mem_orig;
1744 StubMsg.Buffer = StubMsg.BufferStart;
1745 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1746 todo_wine
1747 ok(mem == mem_orig, "mem alloced\n");
1748 todo_wine
1749 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1751 HeapFree(GetProcessHeap(), 0, mem_orig);
1752 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1754 /* length = size */
1755 NdrClientInitializeNew(
1756 &RpcMessage,
1757 &StubMsg,
1758 &StubDesc,
1761 StubMsg.BufferLength = 0;
1763 NdrNonConformantStringBufferSize( &StubMsg, memsrc2, fmtstr_nonconf_str );
1764 ok(StubMsg.BufferLength >= strlen((char *)memsrc2) + 1 + 8, "length %d\n", StubMsg.BufferLength);
1766 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1767 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1768 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1770 ptr = NdrNonConformantStringMarshall( &StubMsg, memsrc2, fmtstr_nonconf_str );
1771 ok(ptr == NULL, "ret %p\n", ptr);
1772 size = StubMsg.Buffer - StubMsg.BufferStart;
1773 ok(size == strlen((char *)memsrc2) + 1 + 8, "Buffer %p Start %p len %d\n",
1774 StubMsg.Buffer, StubMsg.BufferStart, size);
1775 ok(!memcmp(StubMsg.BufferStart + 8, memsrc2, strlen((char *)memsrc2) + 1), "incorrectly marshaled\n");
1777 StubMsg.Buffer = StubMsg.BufferStart;
1778 StubMsg.MemorySize = 0;
1779 mem = NULL;
1781 /* Client */
1782 my_alloc_called = 0;
1783 StubMsg.Buffer = StubMsg.BufferStart;
1784 mem = mem_orig = HeapAlloc(GetProcessHeap(), 0, sizeof(memsrc));
1785 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1786 ok(mem == mem_orig, "mem alloced\n");
1787 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1789 my_alloc_called = 0;
1790 StubMsg.Buffer = StubMsg.BufferStart;
1791 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1792 todo_wine
1793 ok(mem == mem_orig, "mem alloced\n");
1794 todo_wine
1795 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1797 /* Server */
1798 my_alloc_called = 0;
1799 StubMsg.IsClient = 0;
1800 mem = NULL;
1801 StubMsg.Buffer = StubMsg.BufferStart;
1802 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1803 ok(mem != mem_orig, "mem not alloced\n");
1804 ok(mem != StubMsg.BufferStart + 8, "mem pointing at buffer\n");
1805 ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1806 NdrOleFree(mem);
1808 my_alloc_called = 0;
1809 mem = mem_orig;
1810 StubMsg.Buffer = StubMsg.BufferStart;
1811 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 0);
1812 ok(mem == mem_orig, "mem alloced\n");
1813 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1815 my_alloc_called = 0;
1816 mem = mem_orig;
1817 StubMsg.Buffer = StubMsg.BufferStart;
1818 NdrNonConformantStringUnmarshall( &StubMsg, &mem, fmtstr_nonconf_str, 1);
1819 todo_wine
1820 ok(mem == mem_orig, "mem alloced\n");
1821 todo_wine
1822 ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1824 HeapFree(GetProcessHeap(), 0, mem_orig);
1825 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1828 static void test_conf_complex_struct(void)
1830 RPC_MESSAGE RpcMessage;
1831 MIDL_STUB_MESSAGE StubMsg;
1832 MIDL_STUB_DESC StubDesc;
1833 void *ptr;
1834 unsigned int i;
1835 struct conf_complex
1837 unsigned int size;
1838 unsigned int *array[1];
1840 struct conf_complex *memsrc;
1841 struct conf_complex *mem;
1843 static const unsigned char fmtstr_complex_struct[] =
1845 /* 0 */
1846 0x1b, /* FC_CARRAY */
1847 0x3, /* 3 */
1848 /* 2 */ NdrFcShort( 0x4 ), /* 4 */
1849 /* 4 */ 0x8, /* Corr desc: FC_LONG */
1850 0x0, /* */
1851 /* 6 */ NdrFcShort( 0xfffc ), /* -4 */
1852 /* 8 */
1853 0x4b, /* FC_PP */
1854 0x5c, /* FC_PAD */
1855 /* 10 */
1856 0x48, /* FC_VARIABLE_REPEAT */
1857 0x49, /* FC_FIXED_OFFSET */
1858 /* 12 */ NdrFcShort( 0x4 ), /* 4 */
1859 /* 14 */ NdrFcShort( 0x0 ), /* 0 */
1860 /* 16 */ NdrFcShort( 0x1 ), /* 1 */
1861 /* 18 */ NdrFcShort( 0x0 ), /* 0 */
1862 /* 20 */ NdrFcShort( 0x0 ), /* 0 */
1863 /* 22 */ 0x12, 0x8, /* FC_UP [simple_pointer] */
1864 /* 24 */ 0x8, /* FC_LONG */
1865 0x5c, /* FC_PAD */
1866 /* 26 */
1867 0x5b, /* FC_END */
1869 0x8, /* FC_LONG */
1870 /* 28 */ 0x5c, /* FC_PAD */
1871 0x5b, /* FC_END */
1872 /* 30 */
1873 0x1a, /* FC_BOGUS_STRUCT */
1874 0x3, /* 3 */
1875 /* 32 */ NdrFcShort( 0x4 ), /* 4 */
1876 /* 34 */ NdrFcShort( 0xffffffde ), /* Offset= -34 (0) */
1877 /* 36 */ NdrFcShort( 0x0 ), /* Offset= 0 (36) */
1878 /* 38 */ 0x8, /* FC_LONG */
1879 0x5b, /* FC_END */
1882 memsrc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1883 FIELD_OFFSET(struct conf_complex, array[20]));
1884 memsrc->size = 20;
1886 StubDesc = Object_StubDesc;
1887 StubDesc.pFormatTypes = fmtstr_complex_struct;
1889 NdrClientInitializeNew(
1890 &RpcMessage,
1891 &StubMsg,
1892 &StubDesc,
1895 StubMsg.BufferLength = 0;
1896 NdrComplexStructBufferSize( &StubMsg,
1897 (unsigned char *)memsrc,
1898 &fmtstr_complex_struct[30] );
1899 ok(StubMsg.BufferLength >= 28, "length %d\n", StubMsg.BufferLength);
1901 /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1902 StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1903 StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1905 ptr = NdrComplexStructMarshall( &StubMsg, (unsigned char *)memsrc,
1906 &fmtstr_complex_struct[30] );
1907 ok(ptr == NULL, "ret %p\n", ptr);
1908 ok(*(unsigned int *)StubMsg.BufferStart == 20, "Conformance should have been 20 instead of %d\n", *(unsigned int *)StubMsg.BufferStart);
1909 ok(*(unsigned int *)(StubMsg.BufferStart + 4) == 20, "conf_complex.size should have been 20 instead of %d\n", *(unsigned int *)(StubMsg.BufferStart + 4));
1910 for (i = 0; i < 20; i++)
1911 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));
1913 /* Server */
1914 my_alloc_called = 0;
1915 StubMsg.IsClient = 0;
1916 mem = NULL;
1917 StubMsg.Buffer = StubMsg.BufferStart;
1918 ptr = NdrComplexStructUnmarshall( &StubMsg, (unsigned char **)&mem, &fmtstr_complex_struct[30], 0);
1919 ok(ptr == NULL, "ret %p\n", ptr);
1920 ok(mem->size == 20, "mem->size wasn't unmarshalled correctly (%d)\n", mem->size);
1921 ok(mem->array[0] == NULL, "mem->array[0] wasn't unmarshalled correctly (%p)\n", mem->array[0]);
1922 StubMsg.pfnFree(mem);
1924 HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1927 static void test_ndr_buffer(void)
1929 static unsigned char ncalrpc[] = "ncalrpc";
1930 static unsigned char endpoint[] = "winetest:test_ndr_buffer";
1931 RPC_MESSAGE RpcMessage;
1932 MIDL_STUB_MESSAGE StubMsg;
1933 MIDL_STUB_DESC StubDesc = Object_StubDesc;
1934 unsigned char *ret;
1935 unsigned char *binding;
1936 RPC_BINDING_HANDLE Handle;
1937 RPC_STATUS status;
1938 ULONG prev_buffer_length;
1939 BOOL old_buffer_valid_location;
1941 StubDesc.RpcInterfaceInformation = (void *)&IFoo___RpcServerInterface;
1943 status = RpcServerUseProtseqEp(ncalrpc, 20, endpoint, NULL);
1944 ok(RPC_S_OK == status, "RpcServerUseProtseqEp failed with status %u\n", status);
1945 status = RpcServerRegisterIf(IFoo_v0_0_s_ifspec, NULL, NULL);
1946 ok(RPC_S_OK == status, "RpcServerRegisterIf failed with status %u\n", status);
1947 status = RpcServerListen(1, 20, TRUE);
1948 ok(RPC_S_OK == status, "RpcServerListen failed with status %u\n", status);
1949 if (status != RPC_S_OK)
1951 /* Failed to create a server, running client tests is useless */
1952 return;
1955 status = RpcStringBindingCompose(NULL, ncalrpc, NULL, endpoint, NULL, &binding);
1956 ok(status == RPC_S_OK, "RpcStringBindingCompose failed (%u)\n", status);
1958 status = RpcBindingFromStringBinding(binding, &Handle);
1959 ok(status == RPC_S_OK, "RpcBindingFromStringBinding failed (%u)\n", status);
1960 RpcStringFree(&binding);
1962 NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 5);
1964 ret = NdrGetBuffer(&StubMsg, 10, Handle);
1965 ok(ret == StubMsg.Buffer, "NdrGetBuffer should have returned the same value as StubMsg.Buffer instead of %p\n", ret);
1966 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1967 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1968 ok(RpcMessage.BufferLength == 10 ||
1969 broken(RpcMessage.BufferLength == 12), /* win2k */
1970 "RpcMessage.BufferLength should have been 10 instead of %d\n", RpcMessage.BufferLength);
1971 ok(RpcMessage.RpcFlags == 0, "RpcMessage.RpcFlags should have been 0x0 instead of 0x%x\n", RpcMessage.RpcFlags);
1972 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1973 ok(!StubMsg.BufferStart, "BufferStart should have been NULL instead of %p\n", StubMsg.BufferStart);
1974 ok(!StubMsg.BufferEnd, "BufferEnd should have been NULL instead of %p\n", StubMsg.BufferEnd);
1975 todo_wine
1976 ok(StubMsg.BufferLength == 0, "BufferLength should have left as 0 instead of being set to %d\n", StubMsg.BufferLength);
1977 old_buffer_valid_location = !StubMsg.fBufferValid;
1978 if (old_buffer_valid_location)
1979 ok(broken(StubMsg.CorrDespIncrement == TRUE), "fBufferValid should have been TRUE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1980 else
1981 ok(StubMsg.fBufferValid, "fBufferValid should have been non-zero instead of 0x%x\n", StubMsg.fBufferValid);
1983 prev_buffer_length = RpcMessage.BufferLength;
1984 StubMsg.BufferLength = 1;
1985 NdrFreeBuffer(&StubMsg);
1986 ok(RpcMessage.Handle != NULL, "RpcMessage.Handle should not have been NULL\n");
1987 ok(RpcMessage.Buffer != NULL, "RpcMessage.Buffer should not have been NULL\n");
1988 ok(RpcMessage.BufferLength == prev_buffer_length, "RpcMessage.BufferLength should have been left as %d instead of %d\n", prev_buffer_length, RpcMessage.BufferLength);
1989 ok(StubMsg.Buffer != NULL, "Buffer should not have been NULL\n");
1990 ok(StubMsg.BufferLength == 1, "BufferLength should have left as 1 instead of being set to %d\n", StubMsg.BufferLength);
1991 if (old_buffer_valid_location)
1992 ok(broken(StubMsg.CorrDespIncrement == FALSE), "fBufferValid should have been FALSE instead of 0x%x\n", StubMsg.CorrDespIncrement);
1993 else
1994 ok(!StubMsg.fBufferValid, "fBufferValid should have been FALSE instead of %d\n", StubMsg.fBufferValid);
1996 /* attempt double-free */
1997 NdrFreeBuffer(&StubMsg);
1999 RpcBindingFree(&Handle);
2001 status = RpcServerUnregisterIf(NULL, NULL, FALSE);
2002 ok(status == RPC_S_OK, "RpcServerUnregisterIf failed (%u)\n", status);
2005 static void test_NdrMapCommAndFaultStatus(void)
2007 RPC_STATUS rpc_status;
2008 MIDL_STUB_MESSAGE StubMsg;
2009 RPC_MESSAGE RpcMessage;
2011 NdrClientInitializeNew(&RpcMessage, &StubMsg, &Object_StubDesc, 5);
2013 for (rpc_status = 0; rpc_status < 10000; rpc_status++)
2015 RPC_STATUS status;
2016 ULONG comm_status = 0;
2017 ULONG fault_status = 0;
2018 ULONG expected_comm_status = 0;
2019 ULONG expected_fault_status = 0;
2020 status = NdrMapCommAndFaultStatus(&StubMsg, &comm_status, &fault_status, rpc_status);
2021 ok(status == RPC_S_OK, "NdrMapCommAndFaultStatus failed with error %d\n", status);
2022 switch (rpc_status)
2024 case ERROR_INVALID_HANDLE:
2025 case RPC_S_INVALID_BINDING:
2026 case RPC_S_UNKNOWN_IF:
2027 case RPC_S_SERVER_UNAVAILABLE:
2028 case RPC_S_SERVER_TOO_BUSY:
2029 case RPC_S_CALL_FAILED_DNE:
2030 case RPC_S_PROTOCOL_ERROR:
2031 case RPC_S_UNSUPPORTED_TRANS_SYN:
2032 case RPC_S_UNSUPPORTED_TYPE:
2033 case RPC_S_PROCNUM_OUT_OF_RANGE:
2034 case EPT_S_NOT_REGISTERED:
2035 case RPC_S_COMM_FAILURE:
2036 expected_comm_status = rpc_status;
2037 break;
2038 default:
2039 expected_fault_status = rpc_status;
2041 ok(comm_status == expected_comm_status, "NdrMapCommAndFaultStatus should have mapped %d to comm status %d instead of %d\n",
2042 rpc_status, expected_comm_status, comm_status);
2043 ok(fault_status == expected_fault_status, "NdrMapCommAndFaultStatus should have mapped %d to fault status %d instead of %d\n",
2044 rpc_status, expected_fault_status, fault_status);
2048 static void test_NdrGetUserMarshalInfo(void)
2050 RPC_STATUS status;
2051 MIDL_STUB_MESSAGE stubmsg;
2052 USER_MARSHAL_CB umcb;
2053 NDR_USER_MARSHAL_INFO umi;
2054 unsigned char buffer[16];
2055 void *rpc_channel_buffer = (void *)(ULONG_PTR)0xcafebabe;
2056 RPC_MESSAGE rpc_msg;
2057 RPC_STATUS (RPC_ENTRY *pNdrGetUserMarshalInfo)(ULONG *,ULONG,NDR_USER_MARSHAL_INFO *);
2059 pNdrGetUserMarshalInfo = (void *)GetProcAddress(GetModuleHandle("rpcrt4.dll"), "NdrGetUserMarshalInfo");
2060 if (!pNdrGetUserMarshalInfo)
2062 skip("NdrGetUserMarshalInfo not exported\n");
2063 return;
2066 /* unmarshall */
2068 memset(&rpc_msg, 0xcc, sizeof(rpc_msg));
2069 rpc_msg.Buffer = buffer;
2070 rpc_msg.BufferLength = 16;
2072 memset(&stubmsg, 0xcc, sizeof(stubmsg));
2073 stubmsg.RpcMsg = &rpc_msg;
2074 stubmsg.dwDestContext = MSHCTX_INPROC;
2075 stubmsg.pvDestContext = NULL;
2076 stubmsg.Buffer = buffer + 15;
2077 stubmsg.BufferLength = 0;
2078 stubmsg.BufferEnd = NULL;
2079 stubmsg.pRpcChannelBuffer = rpc_channel_buffer;
2080 stubmsg.StubDesc = NULL;
2081 stubmsg.pfnAllocate = my_alloc;
2082 stubmsg.pfnFree = my_free;
2084 memset(&umcb, 0xcc, sizeof(umcb));
2085 umcb.Flags = MAKELONG(MSHCTX_INPROC, NDR_LOCAL_DATA_REPRESENTATION);
2086 umcb.pStubMsg = &stubmsg;
2087 umcb.Signature = USER_MARSHAL_CB_SIGNATURE;
2088 umcb.CBType = USER_MARSHAL_CB_UNMARSHALL;
2090 memset(&umi, 0xaa, sizeof(umi));
2092 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2093 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2094 ok( umi.InformationLevel == 1,
2095 "umi.InformationLevel was %u instead of 1\n",
2096 umi.InformationLevel);
2097 ok( U(umi.Level1).Buffer == buffer + 15,
2098 "U(umi.Level1).Buffer was %p instead of %p\n",
2099 U(umi.Level1).Buffer, buffer);
2100 ok( U(umi.Level1).BufferSize == 1,
2101 "U(umi.Level1).BufferSize was %u instead of 1\n",
2102 U(umi.Level1).BufferSize);
2103 ok( U(umi.Level1).pfnAllocate == my_alloc,
2104 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2105 U(umi.Level1).pfnAllocate, my_alloc);
2106 ok( U(umi.Level1).pfnFree == my_free,
2107 "U(umi.Level1).pfnFree was %p instead of %p\n",
2108 U(umi.Level1).pfnFree, my_free);
2109 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2110 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2111 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2113 /* buffer size */
2115 rpc_msg.Buffer = buffer;
2116 rpc_msg.BufferLength = 16;
2118 stubmsg.Buffer = buffer;
2119 stubmsg.BufferLength = 16;
2120 stubmsg.BufferEnd = NULL;
2122 umcb.CBType = USER_MARSHAL_CB_BUFFER_SIZE;
2124 memset(&umi, 0xaa, sizeof(umi));
2126 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2127 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2128 ok( umi.InformationLevel == 1,
2129 "umi.InformationLevel was %u instead of 1\n",
2130 umi.InformationLevel);
2131 ok( U(umi.Level1).Buffer == NULL,
2132 "U(umi.Level1).Buffer was %p instead of NULL\n",
2133 U(umi.Level1).Buffer);
2134 ok( U(umi.Level1).BufferSize == 0,
2135 "U(umi.Level1).BufferSize was %u instead of 0\n",
2136 U(umi.Level1).BufferSize);
2137 ok( U(umi.Level1).pfnAllocate == my_alloc,
2138 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2139 U(umi.Level1).pfnAllocate, my_alloc);
2140 ok( U(umi.Level1).pfnFree == my_free,
2141 "U(umi.Level1).pfnFree was %p instead of %p\n",
2142 U(umi.Level1).pfnFree, my_free);
2143 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2144 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2145 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2147 /* marshall */
2149 rpc_msg.Buffer = buffer;
2150 rpc_msg.BufferLength = 16;
2152 stubmsg.Buffer = buffer + 15;
2153 stubmsg.BufferLength = 0;
2154 stubmsg.BufferEnd = NULL;
2156 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2158 memset(&umi, 0xaa, sizeof(umi));
2160 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2161 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2162 ok( umi.InformationLevel == 1,
2163 "umi.InformationLevel was %u instead of 1\n",
2164 umi.InformationLevel);
2165 ok( U(umi.Level1).Buffer == buffer + 15,
2166 "U(umi.Level1).Buffer was %p instead of %p\n",
2167 U(umi.Level1).Buffer, buffer);
2168 ok( U(umi.Level1).BufferSize == 1,
2169 "U(umi.Level1).BufferSize was %u instead of 1\n",
2170 U(umi.Level1).BufferSize);
2171 ok( U(umi.Level1).pfnAllocate == my_alloc,
2172 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2173 U(umi.Level1).pfnAllocate, my_alloc);
2174 ok( U(umi.Level1).pfnFree == my_free,
2175 "U(umi.Level1).pfnFree was %p instead of %p\n",
2176 U(umi.Level1).pfnFree, my_free);
2177 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2178 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2179 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2181 /* free */
2183 rpc_msg.Buffer = buffer;
2184 rpc_msg.BufferLength = 16;
2186 stubmsg.Buffer = buffer;
2187 stubmsg.BufferLength = 16;
2188 stubmsg.BufferEnd = NULL;
2190 umcb.CBType = USER_MARSHAL_CB_FREE;
2192 memset(&umi, 0xaa, sizeof(umi));
2194 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2195 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2196 ok( umi.InformationLevel == 1,
2197 "umi.InformationLevel was %u instead of 1\n",
2198 umi.InformationLevel);
2199 ok( U(umi.Level1).Buffer == NULL,
2200 "U(umi.Level1).Buffer was %p instead of NULL\n",
2201 U(umi.Level1).Buffer);
2202 ok( U(umi.Level1).BufferSize == 0,
2203 "U(umi.Level1).BufferSize was %u instead of 0\n",
2204 U(umi.Level1).BufferSize);
2205 ok( U(umi.Level1).pfnAllocate == my_alloc,
2206 "U(umi.Level1).pfnAllocate was %p instead of %p\n",
2207 U(umi.Level1).pfnAllocate, my_alloc);
2208 ok( U(umi.Level1).pfnFree == my_free,
2209 "U(umi.Level1).pfnFree was %p instead of %p\n",
2210 U(umi.Level1).pfnFree, my_free);
2211 ok( U(umi.Level1).pRpcChannelBuffer == rpc_channel_buffer,
2212 "U(umi.Level1).pRpcChannelBuffer was %p instead of %p\n",
2213 U(umi.Level1).pRpcChannelBuffer, rpc_channel_buffer);
2215 /* boundary test */
2217 rpc_msg.Buffer = buffer;
2218 rpc_msg.BufferLength = 15;
2220 stubmsg.Buffer = buffer + 15;
2221 stubmsg.BufferLength = 0;
2222 stubmsg.BufferEnd = NULL;
2224 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2226 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2227 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2228 ok( U(umi.Level1).BufferSize == 0,
2229 "U(umi.Level1).BufferSize was %u instead of 0\n",
2230 U(umi.Level1).BufferSize);
2232 /* error conditions */
2234 rpc_msg.BufferLength = 14;
2235 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2236 ok(status == ERROR_INVALID_USER_BUFFER,
2237 "NdrGetUserMarshalInfo should have failed with ERROR_INVALID_USER_BUFFER instead of %d\n", status);
2239 rpc_msg.BufferLength = 15;
2240 status = pNdrGetUserMarshalInfo(&umcb.Flags, 9999, &umi);
2241 ok(status == RPC_S_INVALID_ARG,
2242 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2244 umcb.CBType = 9999;
2245 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2246 ok(status == RPC_S_OK, "NdrGetUserMarshalInfo failed with error %d\n", status);
2248 umcb.CBType = USER_MARSHAL_CB_MARSHALL;
2249 umcb.Signature = 0;
2250 status = pNdrGetUserMarshalInfo(&umcb.Flags, 1, &umi);
2251 ok(status == RPC_S_INVALID_ARG,
2252 "NdrGetUserMarshalInfo should have failed with RPC_S_INVALID_ARG instead of %d\n", status);
2255 START_TEST( ndr_marshall )
2257 determine_pointer_marshalling_style();
2259 test_ndr_simple_type();
2260 test_simple_types();
2261 test_nontrivial_pointer_types();
2262 test_simple_struct();
2263 test_fullpointer_xlat();
2264 test_client_init();
2265 test_server_init();
2266 test_ndr_allocate();
2267 test_conformant_array();
2268 test_conformant_string();
2269 test_nonconformant_string();
2270 test_conf_complex_struct();
2271 test_ndr_buffer();
2272 test_NdrMapCommAndFaultStatus();
2273 test_NdrGetUserMarshalInfo();