comctl32: We can now store binary files in the repository.
[wine] / dlls / rpcrt4 / tests / ndr_marshall.c
1 /*
2  * Unit test suite for ndr marshalling functions
3  *
4  * Copyright 2006 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #define NTDDI_WIN2K   0x05000000
24 #define NTDDI_VERSION NTDDI_WIN2K /* for some MIDL_STUB_MESSAGE fields */
25
26 #include "wine/test.h"
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winnt.h>
30 #include <winerror.h>
31
32 #include "rpc.h"
33 #include "rpcdce.h"
34 #include "rpcproxy.h"
35
36
37 static int my_alloc_called;
38 static int my_free_called;
39 static void * CALLBACK my_alloc(size_t size)
40 {
41     my_alloc_called++;
42     return NdrOleAllocate(size);
43 }
44
45 static void CALLBACK my_free(void *ptr)
46 {
47     my_free_called++;
48     NdrOleFree(ptr);
49 }
50
51 static const MIDL_STUB_DESC Object_StubDesc = 
52     {
53     NULL,
54     my_alloc,
55     my_free,
56     { 0 },
57     0,
58     0,
59     0,
60     0,
61     NULL, /* format string, filled in by tests */
62     1, /* -error bounds_check flag */
63     0x20000, /* Ndr library version */
64     0,
65     0x50100a4, /* MIDL Version 5.1.164 */
66     0,
67     NULL,
68     0,  /* notify & notify_flag routine table */
69     1,  /* Flags */
70     0,  /* Reserved3 */
71     0,  /* Reserved4 */
72     0   /* Reserved5 */
73     };
74
75
76 static void test_ndr_simple_type(void)
77 {
78     RPC_MESSAGE RpcMessage;
79     MIDL_STUB_MESSAGE StubMsg;
80     MIDL_STUB_DESC StubDesc;
81     long l, l2 = 0;
82
83     StubDesc = Object_StubDesc;
84     StubDesc.pFormatTypes = NULL;
85
86     NdrClientInitializeNew(
87                            &RpcMessage,
88                            &StubMsg,
89                            &StubDesc,
90                            0);
91
92     StubMsg.BufferLength = 16;
93     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
94     l = 0xcafebabe;
95     NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
96     ok(StubMsg.Buffer == StubMsg.BufferStart + 4, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
97     ok(*(long*)StubMsg.BufferStart == l, "%ld\n", *(long*)StubMsg.BufferStart);
98
99     StubMsg.Buffer = StubMsg.BufferStart + 1;
100     NdrSimpleTypeMarshall(&StubMsg, (unsigned char*)&l, 8 /* FC_LONG */);
101     ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
102     ok(*(long*)(StubMsg.BufferStart + 4) == l, "%ld\n", *(long*)StubMsg.BufferStart);
103
104     StubMsg.Buffer = StubMsg.BufferStart + 1;
105     NdrSimpleTypeUnmarshall(&StubMsg, (unsigned char*)&l2, 8 /* FC_LONG */);
106     ok(StubMsg.Buffer == StubMsg.BufferStart + 8, "%p %p\n", StubMsg.Buffer, StubMsg.BufferStart);
107     ok(l2 == l, "%ld\n", l2);
108
109     HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
110 }
111
112 static void test_pointer_marshal(const unsigned char *formattypes,
113                                  void *memsrc,
114                                  long srcsize,
115                                  const void *wiredata,
116                                  ULONG wiredatalen,
117                                  int(*cmp)(const void*,const void*,size_t),
118                                  long num_additional_allocs,
119                                  const char *msgpfx)
120 {
121     RPC_MESSAGE RpcMessage;
122     MIDL_STUB_MESSAGE StubMsg;
123     MIDL_STUB_DESC StubDesc;
124     DWORD size;
125     void *ptr;
126     unsigned char *mem, *mem_orig;
127
128     my_alloc_called = my_free_called = 0;
129     if(!cmp)
130         cmp = memcmp;
131
132     StubDesc = Object_StubDesc;
133     StubDesc.pFormatTypes = formattypes;
134
135     NdrClientInitializeNew(
136                            &RpcMessage,
137                            &StubMsg,
138                            &StubDesc,
139                            0);
140
141     StubMsg.BufferLength = 0;
142     NdrPointerBufferSize( &StubMsg,
143                           memsrc,
144                           formattypes );
145     ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
146
147     /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
148     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
149     StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
150
151     memset(StubMsg.BufferStart, 0x0, StubMsg.BufferLength); /* This is a hack to clear the padding between the ptr and longlong/double */
152
153     ptr = NdrPointerMarshall( &StubMsg,  memsrc, formattypes );
154     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
155     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
156     ok(!memcmp(StubMsg.BufferStart, wiredata, wiredatalen), "%s: incorrectly marshaled\n", msgpfx);
157
158     StubMsg.Buffer = StubMsg.BufferStart;
159     StubMsg.MemorySize = 0;
160
161     if (0)
162     {
163     /* NdrPointerMemorySize crashes under Wine */
164     size = NdrPointerMemorySize( &StubMsg, formattypes );
165     ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
166     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
167     if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
168         ok(size == srcsize + 4, "%s: mem size %u\n", msgpfx, size);
169     else
170         ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
171
172     StubMsg.Buffer = StubMsg.BufferStart;
173     StubMsg.MemorySize = 16;
174     size = NdrPointerMemorySize( &StubMsg, formattypes );
175     ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
176     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
177     if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
178         ok(size == srcsize + 4 + 16, "%s: mem size %u\n", msgpfx, size);
179     else
180         ok(size == srcsize + 16, "%s: mem size %u\n", msgpfx, size);
181
182     StubMsg.Buffer = StubMsg.BufferStart;
183     StubMsg.MemorySize = 1;
184     size = NdrPointerMemorySize( &StubMsg, formattypes );
185     ok(size == StubMsg.MemorySize, "%s: mem size %u size %u\n", msgpfx, StubMsg.MemorySize, size);
186     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
187     if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
188         ok(size == srcsize + 4 + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
189     else
190         ok(size == srcsize + (srcsize == 8 ? 8 : 4), "%s: mem size %u\n", msgpfx, size);
191     }
192
193     size = srcsize;
194     if(formattypes[1] & 0x10) size += 4;
195
196     StubMsg.Buffer = StubMsg.BufferStart;
197     StubMsg.MemorySize = 0;
198     mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, size); 
199
200     if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
201         *(void**)mem = NULL;
202     ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
203     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
204     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
205     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
206     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
207     ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
208     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
209     my_alloc_called = 0;
210
211     /* reset the buffer and call with must alloc */
212     StubMsg.Buffer = StubMsg.BufferStart;
213     if(formattypes[1] & 0x10 /* FC_POINTER_DEREF */)
214         *(void**)mem = NULL;
215     ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 1 );
216     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
217     /* doesn't allocate mem in this case */
218 todo_wine {
219     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
220  }
221     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
222     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
223     ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
224
225 todo_wine {
226     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
227 }
228     my_alloc_called = 0;
229     if(formattypes[0] != 0x11 /* FC_RP */)
230     {
231         /* now pass the address of a NULL ptr */
232         mem = NULL;
233         StubMsg.Buffer = StubMsg.BufferStart;
234         ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
235         ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
236         ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
237         ok(!cmp(mem, memsrc, size), "%s: incorrectly unmarshaled\n", msgpfx);
238         ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
239         ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
240         ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
241         my_alloc_called = 0;
242         NdrPointerFree(&StubMsg, mem, formattypes);
243  
244         /* again pass address of NULL ptr, but pretend we're a server */
245         mem = NULL;
246         StubMsg.Buffer = StubMsg.BufferStart;
247         StubMsg.IsClient = 0;
248         ptr = NdrPointerUnmarshall( &StubMsg, &mem, formattypes, 0 );
249         ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
250 todo_wine {
251         ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
252  }
253         ok(!cmp(mem, memsrc, size), "%s: incorrecly unmarshaled\n", msgpfx);
254         ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
255         ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
256 todo_wine {
257         ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
258         my_alloc_called = 0;
259  }
260     }
261     HeapFree(GetProcessHeap(), 0, mem_orig);
262     HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
263 }
264
265 static int deref_cmp(const void *s1, const void *s2, size_t num)
266 {
267     return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
268 }
269
270
271 static void test_simple_types(void)
272 {
273     unsigned char wiredata[16];
274     unsigned char ch;
275     unsigned char *ch_ptr;
276     unsigned short s;
277     unsigned int i;
278     unsigned long l;
279     ULONGLONG ll;
280     float f;
281     double d;
282
283     static const unsigned char fmtstr_up_char[] =
284     {
285         0x12, 0x8,      /* FC_UP [simple_pointer] */
286         0x2,            /* FC_CHAR */
287         0x5c,           /* FC_PAD */
288     };
289     static const unsigned char fmtstr_up_byte[] =
290     {
291         0x12, 0x8,      /* FC_UP [simple_pointer] */
292         0x1,            /* FC_BYTE */
293         0x5c,           /* FC_PAD */
294     };
295     static const unsigned char fmtstr_up_small[] =
296     {
297         0x12, 0x8,      /* FC_UP [simple_pointer] */
298         0x3,            /* FC_SMALL */
299         0x5c,           /* FC_PAD */
300     };
301     static const unsigned char fmtstr_up_usmall[] =
302     {
303         0x12, 0x8,      /* FC_UP [simple_pointer] */
304         0x4,            /* FC_USMALL */
305         0x5c,           /* FC_PAD */
306     };  
307     static const unsigned char fmtstr_rp_char[] =
308     {
309         0x11, 0x8,      /* FC_RP [simple_pointer] */
310         0x2,            /* FC_CHAR */
311         0x5c,           /* FC_PAD */
312     };
313     static const unsigned char fmtstr_rpup_char[] =
314     {
315         0x11, 0x14,     /* FC_RP [alloced_on_stack] */
316         NdrFcShort( 0x2 ),      /* Offset= 2 (4) */
317         0x12, 0x8,      /* FC_UP [simple_pointer] */
318         0x2,            /* FC_CHAR */
319         0x5c,           /* FC_PAD */
320     };
321     static const unsigned char fmtstr_rpup_char2[] =
322     {
323         0x11, 0x04,     /* FC_RP [alloced_on_stack] */
324         NdrFcShort( 0x2 ),      /* Offset= 2 (4) */
325         0x12, 0x8,      /* FC_UP [simple_pointer] */
326         0x2,            /* FC_CHAR */
327         0x5c,           /* FC_PAD */
328     };
329
330     static const unsigned char fmtstr_up_wchar[] =
331     {
332         0x12, 0x8,      /* FC_UP [simple_pointer] */
333         0x5,            /* FC_WCHAR */
334         0x5c,           /* FC_PAD */
335     };
336     static const unsigned char fmtstr_up_short[] =
337     {
338         0x12, 0x8,      /* FC_UP [simple_pointer] */
339         0x6,            /* FC_SHORT */
340         0x5c,           /* FC_PAD */
341     };
342     static const unsigned char fmtstr_up_ushort[] =
343     {
344         0x12, 0x8,      /* FC_UP [simple_pointer] */
345         0x7,            /* FC_USHORT */
346         0x5c,           /* FC_PAD */
347     };
348     static const unsigned char fmtstr_up_enum16[] =
349     {
350         0x12, 0x8,      /* FC_UP [simple_pointer] */
351         0xd,            /* FC_ENUM16 */
352         0x5c,           /* FC_PAD */
353     };
354     static const unsigned char fmtstr_up_long[] =
355     {
356         0x12, 0x8,      /* FC_UP [simple_pointer] */
357         0x8,            /* FC_LONG */
358         0x5c,           /* FC_PAD */
359     };
360     static const unsigned char fmtstr_up_ulong[] =
361     {
362         0x12, 0x8,      /* FC_UP [simple_pointer] */
363         0x9,            /* FC_ULONG */
364         0x5c,           /* FC_PAD */
365     };
366     static const unsigned char fmtstr_up_enum32[] =
367     {
368         0x12, 0x8,      /* FC_UP [simple_pointer] */
369         0xe,            /* FC_ENUM32 */
370         0x5c,           /* FC_PAD */
371     };
372     static const unsigned char fmtstr_up_errorstatus[] =
373     {
374         0x12, 0x8,      /* FC_UP [simple_pointer] */
375         0x10,           /* FC_ERROR_STATUS_T */
376         0x5c,           /* FC_PAD */
377     };
378
379     static const unsigned char fmtstr_up_longlong[] =
380     {
381         0x12, 0x8,      /* FC_UP [simple_pointer] */
382         0xb,            /* FC_HYPER */
383         0x5c,           /* FC_PAD */
384     };
385     static const unsigned char fmtstr_up_float[] =
386     {
387         0x12, 0x8,      /* FC_UP [simple_pointer] */
388         0xa,            /* FC_FLOAT */
389         0x5c,           /* FC_PAD */
390     };
391     static const unsigned char fmtstr_up_double[] =
392     {
393         0x12, 0x8,      /* FC_UP [simple_pointer] */
394         0xc,            /* FC_DOUBLE */
395         0x5c,           /* FC_PAD */
396     };
397
398     ch = 0xa5;
399     ch_ptr = &ch;
400     *(void**)wiredata = ch_ptr;
401     wiredata[sizeof(void*)] = ch;
402  
403     test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
404     test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
405     test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0,  "up_small");
406     test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
407
408     test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
409
410     test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
411     test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
412
413     s = 0xa597;
414     *(void**)wiredata = &s;
415     *(unsigned short*)(wiredata + sizeof(void*)) = s;
416
417     test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
418     test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
419     test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
420
421     i = 0x7fff;
422     *(void**)wiredata = &i;
423     *(unsigned short*)(wiredata + sizeof(void*)) = i;
424     test_pointer_marshal(fmtstr_up_enum16, &i, 2, wiredata, 6, NULL, 0, "up_enum16");
425
426     l = 0xcafebabe;
427     *(void**)wiredata = &l;
428     *(unsigned long*)(wiredata + sizeof(void*)) = l;
429
430     test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
431     test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0,  "up_ulong");
432     test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0,  "up_emun32");
433     test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0,  "up_errorstatus");
434
435     ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
436     *(void**)wiredata = &ll;
437     *(void**)(wiredata + sizeof(void*)) = NULL;
438     *(ULONGLONG*)(wiredata + 2 * sizeof(void*)) = ll;
439     test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
440
441     f = 3.1415f;
442     *(void**)wiredata = &f;
443     *(float*)(wiredata + sizeof(void*)) = f;
444     test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
445
446     d = 3.1415;
447     *(void**)wiredata = &d;
448     *(void**)(wiredata + sizeof(void*)) = NULL;
449     *(double*)(wiredata + 2 * sizeof(void*)) = d;
450     test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0,  "up_double");
451
452 }
453
454 static void test_simple_struct_marshal(const unsigned char *formattypes,
455                                        void *memsrc,
456                                        long srcsize,
457                                        const void *wiredata,
458                                        ULONG wiredatalen,
459                                        int(*cmp)(const void*,const void*,size_t),
460                                        long num_additional_allocs,
461                                        const char *msgpfx)
462 {
463     RPC_MESSAGE RpcMessage;
464     MIDL_STUB_MESSAGE StubMsg;
465     MIDL_STUB_DESC StubDesc;
466     DWORD size;
467     void *ptr;
468     unsigned char *mem, *mem_orig;
469
470     my_alloc_called = my_free_called = 0;
471     if(!cmp)
472         cmp = memcmp;
473
474     StubDesc = Object_StubDesc;
475     StubDesc.pFormatTypes = formattypes;
476
477     NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
478
479     StubMsg.BufferLength = 0;
480     NdrSimpleStructBufferSize( &StubMsg, (unsigned char *)memsrc, formattypes );
481     ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
482     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
483     StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
484     ptr = NdrSimpleStructMarshall( &StubMsg,  (unsigned char*)memsrc, formattypes );
485     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
486     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
487     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));
488
489     if (0)
490     {
491     /* FIXME: Causes Wine to crash */
492     StubMsg.Buffer = StubMsg.BufferStart;
493     StubMsg.MemorySize = 0;
494     size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
495     ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
496     ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
497     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
498
499     StubMsg.Buffer = StubMsg.BufferStart;
500     size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
501 todo_wine {
502     ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
503 }
504     ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
505     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
506     }
507     size = srcsize;
508     /*** Unmarshalling first with must_alloc false ***/
509
510     StubMsg.Buffer = StubMsg.BufferStart;
511     StubMsg.MemorySize = 0;
512     mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, srcsize);
513     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
514     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
515     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
516     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
517     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
518     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
519     my_alloc_called = 0;
520     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
521
522     /* if we're a server we still use the suppiled memory */
523     StubMsg.Buffer = StubMsg.BufferStart;
524     StubMsg.IsClient = 0;
525     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
526     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
527     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
528     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx); 
529     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
530     my_alloc_called = 0;
531     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
532
533     /* ...unless we pass a NULL ptr, then the buffer is used. 
534        Passing a NULL ptr while we're a client && !must_alloc
535        crashes on Windows, so we won't do that. */
536
537     mem = NULL;
538     StubMsg.IsClient = 0;
539     StubMsg.Buffer = StubMsg.BufferStart;
540     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
541     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
542     ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
543     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
544     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
545     my_alloc_called = 0;
546     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
547
548     /*** now must_alloc is true ***/
549
550     /* with must_alloc set we always allocate new memory whether or not we're
551        a server and also when passing NULL */
552     mem = mem_orig;
553     StubMsg.IsClient = 1;
554     StubMsg.Buffer = StubMsg.BufferStart;
555     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
556     ok(ptr == NULL, "ret %p\n", ptr);
557     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
558     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
559     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
560     my_alloc_called = 0;
561     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
562
563     mem = NULL;
564     StubMsg.Buffer = StubMsg.BufferStart;
565     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
566     ok(ptr == NULL, "ret %p\n", ptr);
567     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
568     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
569     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
570     my_alloc_called = 0; 
571     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
572
573     mem = mem_orig;
574     StubMsg.Buffer = StubMsg.BufferStart;
575     StubMsg.IsClient = 0;
576     StubMsg.ReuseBuffer = 1;
577     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
578     ok(ptr == NULL, "ret %p\n", ptr);
579     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
580     ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
581     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
582     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
583     my_alloc_called = 0;
584     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
585
586     mem = NULL;
587     StubMsg.Buffer = StubMsg.BufferStart;
588     StubMsg.IsClient = 0;
589     StubMsg.ReuseBuffer = 1;
590     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
591     ok(ptr == NULL, "ret %p\n", ptr);
592     ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
593     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n"); 
594     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
595     my_alloc_called = 0;
596     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
597
598 }
599
600 typedef struct
601 {
602     long l1;
603     long *pl1;
604     char *pc1;
605 } ps1_t;
606
607 static int ps1_cmp(const void *s1, const void *s2, size_t num)
608 {
609     const ps1_t *p1, *p2;
610
611     p1 = s1;
612     p2 = s2;
613
614     if(p1->l1 != p2->l1)
615         return 1;
616
617     if(p1->pl1 && p2->pl1)
618     {
619         if(*p1->pl1 != *p2->pl1)
620             return 1;
621     }
622     else if(p1->pl1 || p1->pl1)
623         return 1;
624
625     if(p1->pc1 && p2->pc1)
626     {
627         if(*p1->pc1 != *p2->pc1)
628             return 1;
629     }
630     else if(p1->pc1 || p1->pc1)
631         return 1;
632
633     return 0;
634 }
635
636 static void test_simple_struct(void)
637 {
638     unsigned char wiredata[28];
639     unsigned long wiredatalen;
640     long l;
641     char c;
642     ps1_t ps1;
643
644     static const unsigned char fmtstr_simple_struct[] =
645     {
646         0x12, 0x0,      /* FC_UP */
647         NdrFcShort( 0x2 ), /* Offset=2 */
648         0x15, 0x3,      /* FC_STRUCT [align 4] */
649         NdrFcShort( 0x18 ),      /* [size 24] */
650         0x6,            /* FC_SHORT */
651         0x2,            /* FC_CHAR */ 
652         0x38,           /* FC_ALIGNM4 */
653         0x8,            /* FC_LONG */
654         0x8,            /* FC_LONG */
655         0x39,           /* FC_ALIGNM8 */
656         0xb,            /* FC_HYPER */ 
657         0x5b,           /* FC_END */
658     };
659     struct {
660         short s;
661         char c;
662         long l1, l2;
663         LONGLONG ll;
664     } s1;
665
666     static const unsigned char fmtstr_pointer_struct[] =
667     { 
668         0x12, 0x0,      /* FC_UP */
669         NdrFcShort( 0x2 ), /* Offset=2 */
670         0x16, 0x3,      /* FC_PSTRUCT [align 4] */
671         NdrFcShort( 0xc ),      /* [size 12] */
672         0x4b,           /* FC_PP */
673         0x5c,           /* FC_PAD */
674         0x46,           /* FC_NO_REPEAT */
675         0x5c,           /* FC_PAD */
676         NdrFcShort( 0x4 ),      /* 4 */
677         NdrFcShort( 0x4 ),      /* 4 */
678         0x13, 0x8,      /* FC_OP [simple_pointer] */
679         0x8,            /* FC_LONG */
680         0x5c,           /* FC_PAD */
681         0x46,           /* FC_NO_REPEAT */
682         0x5c,           /* FC_PAD */
683         NdrFcShort( 0x8 ),      /* 8 */
684         NdrFcShort( 0x8 ),      /* 8 */
685         0x13, 0x8,      /* FC_OP [simple_pointer] */
686         0x2,            /* FC_CHAR */
687         0x5c,           /* FC_PAD */
688         0x5b,           /* FC_END */
689         0x8,            /* FC_LONG */
690         0x8,            /* FC_LONG */
691         0x8,            /* FC_LONG */
692         0x5c,           /* FC_PAD */
693         0x5b,           /* FC_END */
694
695     };
696
697     /* FC_STRUCT */
698     s1.s = 0x1234;
699     s1.c = 0xa5;
700     s1.l1 = 0xdeadbeef;
701     s1.l2 = 0xcafebabe;
702     s1.ll = ((LONGLONG) 0xbadefeed << 32) | 0x2468ace0;
703
704     wiredatalen = 24;
705     memcpy(wiredata, &s1, wiredatalen); 
706     test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
707
708     *(void**)wiredata = &s1;
709     memcpy(wiredata + 4, &s1, wiredatalen);
710     if (0)
711     {
712     /* one of the unmarshallings crashes Wine */
713     test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
714     }
715
716     /* FC_PSTRUCT */
717     ps1.l1 = 0xdeadbeef;
718     l = 0xcafebabe;
719     ps1.pl1 = &l;
720     c = 'a';
721     ps1.pc1 = &c;
722     memcpy(wiredata + 4, &ps1, 12);
723     memcpy(wiredata + 16, &l, 4);
724     memcpy(wiredata + 20, &c, 1);
725
726     test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
727     *(void**)wiredata = &ps1;
728     if (0)
729     {
730     /* one of the unmarshallings crashes Wine */
731     test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
732     }
733 }
734
735 static void test_fullpointer_xlat(void)
736 {
737     PFULL_PTR_XLAT_TABLES pXlatTables;
738     ULONG RefId;
739     int ret;
740     void *Pointer;
741
742     pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
743
744     /* "marshaling" phase */
745
746     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
747     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
748     ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
749
750     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
751     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
752     ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
753
754     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
755     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
756     ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
757
758     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
759     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
760     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
761
762     ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
763     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
764     ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
765
766     /* "unmarshaling" phase */
767
768     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
769     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
770     ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
771
772     ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
773     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
774     ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
775
776     NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
777
778     ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
779     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
780     ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
781
782     NdrFullPointerXlatFree(pXlatTables);
783
784     pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
785
786     /* "unmarshaling" phase */
787
788     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
789     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
790     ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
791
792     NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
793
794     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
795     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
796     ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
797
798     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
799     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
800     ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
801
802     /* "marshaling" phase */
803
804     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
805     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
806     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
807
808     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
809     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
810     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
811
812     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
813     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
814     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
815
816     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
817     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
818     ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
819
820     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
821     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
822     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
823
824     /* "freeing" phase */
825
826     ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
827     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
828
829     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
830     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
831     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
832
833     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
834     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
835     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
836
837     ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
838     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
839
840     ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
841     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
842
843     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
844     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
845     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
846
847     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
848     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
849     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
850
851     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
852     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
853     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
854
855     ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
856     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
857
858     NdrFullPointerXlatFree(pXlatTables);
859 }
860
861 static void test_client_init(void)
862 {
863     MIDL_STUB_MESSAGE stubMsg;
864     RPC_MESSAGE rpcMsg;
865
866     memset(&stubMsg, 0xcc, sizeof(stubMsg));
867
868     NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
869
870 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have be set to zero instead of " fmt "\n", stubMsg.field)
871 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have be unset instead of %p\n", stubMsg.field)
872 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have be unset instead of 0x%x\n", stubMsg.field)
873 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have be unset instead of 0x%lx\n", stubMsg.field)
874
875     ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
876     TEST_POINTER_UNSET(Buffer);
877     TEST_ZERO(BufferStart, "%p");
878     TEST_ZERO(BufferEnd, "%p");
879     TEST_POINTER_UNSET(BufferMark);
880     TEST_ZERO(BufferLength, "%d");
881     TEST_ULONG_UNSET(MemorySize);
882     TEST_POINTER_UNSET(Memory);
883     ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
884     TEST_ZERO(ReuseBuffer, "%d");
885     TEST_ZERO(pAllocAllNodesContext, "%p");
886     TEST_ZERO(pPointerQueueState, "%p");
887     TEST_ZERO(IgnoreEmbeddedPointers, "%d");
888     TEST_ZERO(PointerBufferMark, "%p");
889     TEST_ZERO(fBufferValid, "%d");
890     TEST_ZERO(uFlags, "%d");
891     /* FIXME: UniquePtrCount */
892     TEST_ULONG_PTR_UNSET(MaxCount);
893     TEST_ULONG_UNSET(Offset);
894     TEST_ULONG_UNSET(ActualCount);
895     ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
896     ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
897     TEST_ZERO(StackTop, "%p");
898     TEST_POINTER_UNSET(pPresentedType);
899     TEST_POINTER_UNSET(pTransmitType);
900     TEST_POINTER_UNSET(SavedHandle);
901     ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
902     TEST_POINTER_UNSET(FullPtrXlatTables);
903     TEST_ZERO(FullPtrRefId, "%d");
904     TEST_ZERO(PointerLength, "%d");
905     TEST_ZERO(fInDontFree, "%d");
906     TEST_ZERO(fDontCallFreeInst, "%d");
907     TEST_ZERO(fInOnlyParam, "%d");
908     TEST_ZERO(fHasReturn, "%d");
909     TEST_ZERO(fHasExtensions, "%d");
910     TEST_ZERO(fHasNewCorrDesc, "%d");
911     TEST_ZERO(fUnused, "%d");
912     ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xcccc instead of 0x%x\n", stubMsg.fUnused2);
913     ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
914     TEST_ZERO(pvDestContext, "%p");
915     TEST_POINTER_UNSET(SavedContextHandles);
916     TEST_ULONG_UNSET(ParamNumber);
917     TEST_ZERO(pRpcChannelBuffer, "%p");
918     TEST_ZERO(pArrayInfo, "%p");
919     TEST_POINTER_UNSET(SizePtrCountArray);
920     TEST_POINTER_UNSET(SizePtrOffsetArray);
921     TEST_POINTER_UNSET(SizePtrLengthArray);
922     TEST_POINTER_UNSET(pArgQueue);
923     TEST_ZERO(dwStubPhase, "%d");
924     /* FIXME: where does this value come from? */
925     trace("LowStackMark is %p\n", stubMsg.LowStackMark);
926     TEST_ZERO(pAsyncMsg, "%p");
927     TEST_ZERO(pCorrInfo, "%p");
928     TEST_ZERO(pCorrMemory, "%p");
929     TEST_ZERO(pMemoryList, "%p");
930     TEST_POINTER_UNSET(pCSInfo);
931     TEST_POINTER_UNSET(ConformanceMark);
932     TEST_POINTER_UNSET(VarianceMark);
933     ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
934     TEST_POINTER_UNSET(pContext);
935     TEST_POINTER_UNSET(ContextHandleHash);
936     TEST_POINTER_UNSET(pUserMarshalList);
937     TEST_ULONG_PTR_UNSET(Reserved51_3);
938     TEST_ULONG_PTR_UNSET(Reserved51_4);
939     TEST_ULONG_PTR_UNSET(Reserved51_5);
940 #undef TEST_ULONG_UNSET
941 #undef TEST_POINTER_UNSET
942 #undef TEST_ZERO
943
944 }
945
946 static void test_ndr_allocate(void)
947 {
948     RPC_MESSAGE RpcMessage;
949     MIDL_STUB_MESSAGE StubMsg;
950     MIDL_STUB_DESC StubDesc;
951     void *p1, *p2;
952     struct tag_mem_list_t
953     {
954         DWORD magic;
955         void *ptr;
956         struct tag_mem_list_t *next;
957     } *mem_list;
958     const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
959
960     StubDesc = Object_StubDesc;
961     NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
962
963     ok(StubMsg.pMemoryList == NULL, "memlist %p\n", StubMsg.pMemoryList);
964     my_alloc_called = my_free_called = 0;
965     p1 = NdrAllocate(&StubMsg, 10);
966     p2 = NdrAllocate(&StubMsg, 20);
967     ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
968     mem_list = StubMsg.pMemoryList;
969 todo_wine {
970     ok(mem_list != NULL, "mem_list NULL\n");
971  }
972     if(mem_list)
973     {
974         ok(mem_list->magic == magic_MEML, "magic %08x\n", mem_list->magic);
975         ok(mem_list->ptr == p2, "ptr != p2\n");
976         ok(mem_list->next != NULL, "next NULL\n");
977         mem_list = mem_list->next;
978         if(mem_list)
979         {
980             ok(mem_list->magic == magic_MEML, "magic %08x\n", mem_list->magic);
981             ok(mem_list->ptr == p1, "ptr != p2\n");
982             ok(mem_list->next == NULL, "next %p\n", mem_list->next);
983         }
984     }
985     /* NdrFree isn't exported so we can't test free'ing */
986 }
987
988 static void test_conformant_array(void)
989 {
990     RPC_MESSAGE RpcMessage;
991     MIDL_STUB_MESSAGE StubMsg;
992     MIDL_STUB_DESC StubDesc;
993     void *ptr;
994     unsigned char *mem, *mem_orig;
995     unsigned char memsrc[20];
996
997     static const unsigned char fmtstr_conf_array[] =
998     {
999         0x1b,              /* FC_CARRAY */
1000         0x0,               /* align */
1001         NdrFcShort( 0x1 ), /* elem size */
1002         0x40,              /* Corr desc:  const */
1003         0x0,
1004         NdrFcShort(0x10),  /* const = 0x10 */
1005         0x1,               /* FC_BYTE */
1006         0x5b               /* FC_END */
1007     };
1008
1009     StubDesc = Object_StubDesc;
1010     StubDesc.pFormatTypes = fmtstr_conf_array;
1011
1012     NdrClientInitializeNew(
1013                            &RpcMessage,
1014                            &StubMsg,
1015                            &StubDesc,
1016                            0);
1017
1018     StubMsg.BufferLength = 0;
1019     NdrConformantArrayBufferSize( &StubMsg,
1020                           memsrc,
1021                           fmtstr_conf_array );
1022     ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1023
1024     /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1025     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1026     StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1027
1028     ptr = NdrConformantArrayMarshall( &StubMsg,  memsrc, fmtstr_conf_array );
1029     ok(ptr == NULL, "ret %p\n", ptr);
1030     ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1031     ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1032
1033     StubMsg.Buffer = StubMsg.BufferStart;
1034     StubMsg.MemorySize = 0;
1035     mem = NULL;
1036
1037     /* Client */
1038     my_alloc_called = 0;
1039     /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1040     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1041     ok(mem != NULL, "mem not alloced\n");
1042     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1043     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1044
1045     my_alloc_called = 0;
1046     StubMsg.Buffer = StubMsg.BufferStart;
1047     mem_orig = mem;
1048     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1049     ok(mem == mem_orig, "mem alloced\n");
1050     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1051     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1052
1053     my_alloc_called = 0;
1054     StubMsg.Buffer = StubMsg.BufferStart;
1055     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1056     ok(mem != mem_orig, "mem not alloced\n");
1057     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1058     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1059
1060     my_free_called = 0;
1061     StubMsg.Buffer = StubMsg.BufferStart;
1062     NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1063     ok(my_free_called == 0, "free called %d\n", my_free_called);
1064     StubMsg.pfnFree(mem);
1065
1066     /* Server */
1067     my_alloc_called = 0;
1068     StubMsg.IsClient = 0;
1069     mem = NULL;
1070     StubMsg.Buffer = StubMsg.BufferStart;
1071     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1072 todo_wine {
1073     ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1074     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1075 }
1076     my_alloc_called = 0;
1077     mem = NULL;
1078     StubMsg.Buffer = StubMsg.BufferStart;
1079     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1080     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1081     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1082     StubMsg.pfnFree(mem);
1083
1084     my_alloc_called = 0;
1085     mem = mem_orig;
1086     StubMsg.Buffer = StubMsg.BufferStart;
1087     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1088     ok(mem == mem_orig, "mem alloced\n");
1089     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1090
1091     my_alloc_called = 0;
1092     mem = mem_orig;
1093     StubMsg.Buffer = StubMsg.BufferStart;
1094     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1095     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1096     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1097     StubMsg.pfnFree(mem);
1098     StubMsg.pfnFree(mem_orig);
1099
1100     HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1101 }
1102
1103 START_TEST( ndr_marshall )
1104 {
1105     test_ndr_simple_type();
1106     test_simple_types();
1107     test_simple_struct();
1108     test_fullpointer_xlat();
1109     test_client_init();
1110     test_ndr_allocate();
1111     test_conformant_array();
1112 }