rpcrt4: Fix the tests for up_enum16.
[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         if (formattypes[2] == 0xd /* FC_ENUM16 */)
251             ok(mem != StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem points to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
252         else
253 todo_wine {
254             ok(mem == StubMsg.BufferStart + wiredatalen - srcsize, "%s: mem doesn't point to buffer %p %p\n", msgpfx, mem, StubMsg.BufferStart);
255  }
256         ok(!cmp(mem, memsrc, size), "%s: incorrecly unmarshaled\n", msgpfx);
257         ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p len %d\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart, wiredatalen);
258         ok(StubMsg.MemorySize == 0, "%s: memorysize %d\n", msgpfx, StubMsg.MemorySize);
259         if (formattypes[2] != 0xd /* FC_ENUM16 */) {
260 todo_wine {
261             ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
262             my_alloc_called = 0;
263  }
264         }
265     }
266     HeapFree(GetProcessHeap(), 0, mem_orig);
267     HeapFree(GetProcessHeap(), 0, StubMsg.BufferStart);
268 }
269
270 static int deref_cmp(const void *s1, const void *s2, size_t num)
271 {
272     return memcmp(*(const void *const *)s1, *(const void *const *)s2, num);
273 }
274
275
276 static void test_simple_types(void)
277 {
278     unsigned char wiredata[16];
279     unsigned char ch;
280     unsigned char *ch_ptr;
281     unsigned short s;
282     unsigned int i;
283     unsigned long l;
284     ULONGLONG ll;
285     float f;
286     double d;
287
288     static const unsigned char fmtstr_up_char[] =
289     {
290         0x12, 0x8,      /* FC_UP [simple_pointer] */
291         0x2,            /* FC_CHAR */
292         0x5c,           /* FC_PAD */
293     };
294     static const unsigned char fmtstr_up_byte[] =
295     {
296         0x12, 0x8,      /* FC_UP [simple_pointer] */
297         0x1,            /* FC_BYTE */
298         0x5c,           /* FC_PAD */
299     };
300     static const unsigned char fmtstr_up_small[] =
301     {
302         0x12, 0x8,      /* FC_UP [simple_pointer] */
303         0x3,            /* FC_SMALL */
304         0x5c,           /* FC_PAD */
305     };
306     static const unsigned char fmtstr_up_usmall[] =
307     {
308         0x12, 0x8,      /* FC_UP [simple_pointer] */
309         0x4,            /* FC_USMALL */
310         0x5c,           /* FC_PAD */
311     };  
312     static const unsigned char fmtstr_rp_char[] =
313     {
314         0x11, 0x8,      /* FC_RP [simple_pointer] */
315         0x2,            /* FC_CHAR */
316         0x5c,           /* FC_PAD */
317     };
318     static const unsigned char fmtstr_rpup_char[] =
319     {
320         0x11, 0x14,     /* FC_RP [alloced_on_stack] */
321         NdrFcShort( 0x2 ),      /* Offset= 2 (4) */
322         0x12, 0x8,      /* FC_UP [simple_pointer] */
323         0x2,            /* FC_CHAR */
324         0x5c,           /* FC_PAD */
325     };
326     static const unsigned char fmtstr_rpup_char2[] =
327     {
328         0x11, 0x04,     /* FC_RP [alloced_on_stack] */
329         NdrFcShort( 0x2 ),      /* Offset= 2 (4) */
330         0x12, 0x8,      /* FC_UP [simple_pointer] */
331         0x2,            /* FC_CHAR */
332         0x5c,           /* FC_PAD */
333     };
334
335     static const unsigned char fmtstr_up_wchar[] =
336     {
337         0x12, 0x8,      /* FC_UP [simple_pointer] */
338         0x5,            /* FC_WCHAR */
339         0x5c,           /* FC_PAD */
340     };
341     static const unsigned char fmtstr_up_short[] =
342     {
343         0x12, 0x8,      /* FC_UP [simple_pointer] */
344         0x6,            /* FC_SHORT */
345         0x5c,           /* FC_PAD */
346     };
347     static const unsigned char fmtstr_up_ushort[] =
348     {
349         0x12, 0x8,      /* FC_UP [simple_pointer] */
350         0x7,            /* FC_USHORT */
351         0x5c,           /* FC_PAD */
352     };
353     static const unsigned char fmtstr_up_enum16[] =
354     {
355         0x12, 0x8,      /* FC_UP [simple_pointer] */
356         0xd,            /* FC_ENUM16 */
357         0x5c,           /* FC_PAD */
358     };
359     static const unsigned char fmtstr_up_long[] =
360     {
361         0x12, 0x8,      /* FC_UP [simple_pointer] */
362         0x8,            /* FC_LONG */
363         0x5c,           /* FC_PAD */
364     };
365     static const unsigned char fmtstr_up_ulong[] =
366     {
367         0x12, 0x8,      /* FC_UP [simple_pointer] */
368         0x9,            /* FC_ULONG */
369         0x5c,           /* FC_PAD */
370     };
371     static const unsigned char fmtstr_up_enum32[] =
372     {
373         0x12, 0x8,      /* FC_UP [simple_pointer] */
374         0xe,            /* FC_ENUM32 */
375         0x5c,           /* FC_PAD */
376     };
377     static const unsigned char fmtstr_up_errorstatus[] =
378     {
379         0x12, 0x8,      /* FC_UP [simple_pointer] */
380         0x10,           /* FC_ERROR_STATUS_T */
381         0x5c,           /* FC_PAD */
382     };
383
384     static const unsigned char fmtstr_up_longlong[] =
385     {
386         0x12, 0x8,      /* FC_UP [simple_pointer] */
387         0xb,            /* FC_HYPER */
388         0x5c,           /* FC_PAD */
389     };
390     static const unsigned char fmtstr_up_float[] =
391     {
392         0x12, 0x8,      /* FC_UP [simple_pointer] */
393         0xa,            /* FC_FLOAT */
394         0x5c,           /* FC_PAD */
395     };
396     static const unsigned char fmtstr_up_double[] =
397     {
398         0x12, 0x8,      /* FC_UP [simple_pointer] */
399         0xc,            /* FC_DOUBLE */
400         0x5c,           /* FC_PAD */
401     };
402
403     ch = 0xa5;
404     ch_ptr = &ch;
405     *(void**)wiredata = ch_ptr;
406     wiredata[sizeof(void*)] = ch;
407  
408     test_pointer_marshal(fmtstr_up_char, ch_ptr, 1, wiredata, 5, NULL, 0, "up_char");
409     test_pointer_marshal(fmtstr_up_byte, ch_ptr, 1, wiredata, 5, NULL, 0, "up_byte");
410     test_pointer_marshal(fmtstr_up_small, ch_ptr, 1, wiredata, 5, NULL, 0,  "up_small");
411     test_pointer_marshal(fmtstr_up_usmall, ch_ptr, 1, wiredata, 5, NULL, 0, "up_usmall");
412
413     test_pointer_marshal(fmtstr_rp_char, ch_ptr, 1, &ch, 1, NULL, 0, "rp_char");
414
415     test_pointer_marshal(fmtstr_rpup_char, &ch_ptr, 1, wiredata, 5, deref_cmp, 1, "rpup_char");
416     test_pointer_marshal(fmtstr_rpup_char2, ch_ptr, 1, wiredata, 5, NULL, 0, "rpup_char2");
417
418     s = 0xa597;
419     *(void**)wiredata = &s;
420     *(unsigned short*)(wiredata + sizeof(void*)) = s;
421
422     test_pointer_marshal(fmtstr_up_wchar, &s, 2, wiredata, 6, NULL, 0, "up_wchar");
423     test_pointer_marshal(fmtstr_up_short, &s, 2, wiredata, 6, NULL, 0, "up_short");
424     test_pointer_marshal(fmtstr_up_ushort, &s, 2, wiredata, 6, NULL, 0, "up_ushort");
425
426     i = 0x7fff;
427     *(void**)wiredata = &i;
428     *(unsigned short*)(wiredata + sizeof(void*)) = i;
429     test_pointer_marshal(fmtstr_up_enum16, &i, 2, wiredata, 6, NULL, 0, "up_enum16");
430
431     l = 0xcafebabe;
432     *(void**)wiredata = &l;
433     *(unsigned long*)(wiredata + sizeof(void*)) = l;
434
435     test_pointer_marshal(fmtstr_up_long, &l, 4, wiredata, 8, NULL, 0, "up_long");
436     test_pointer_marshal(fmtstr_up_ulong, &l, 4, wiredata, 8, NULL, 0,  "up_ulong");
437     test_pointer_marshal(fmtstr_up_enum32, &l, 4, wiredata, 8, NULL, 0,  "up_emun32");
438     test_pointer_marshal(fmtstr_up_errorstatus, &l, 4, wiredata, 8, NULL, 0,  "up_errorstatus");
439
440     ll = ((ULONGLONG)0xcafebabe) << 32 | 0xdeadbeef;
441     *(void**)wiredata = &ll;
442     *(void**)(wiredata + sizeof(void*)) = NULL;
443     *(ULONGLONG*)(wiredata + 2 * sizeof(void*)) = ll;
444     test_pointer_marshal(fmtstr_up_longlong, &ll, 8, wiredata, 16, NULL, 0, "up_longlong");
445
446     f = 3.1415f;
447     *(void**)wiredata = &f;
448     *(float*)(wiredata + sizeof(void*)) = f;
449     test_pointer_marshal(fmtstr_up_float, &f, 4, wiredata, 8, NULL, 0, "up_float");
450
451     d = 3.1415;
452     *(void**)wiredata = &d;
453     *(void**)(wiredata + sizeof(void*)) = NULL;
454     *(double*)(wiredata + 2 * sizeof(void*)) = d;
455     test_pointer_marshal(fmtstr_up_double, &d, 8, wiredata, 16, NULL, 0,  "up_double");
456
457 }
458
459 static void test_simple_struct_marshal(const unsigned char *formattypes,
460                                        void *memsrc,
461                                        long srcsize,
462                                        const void *wiredata,
463                                        ULONG wiredatalen,
464                                        int(*cmp)(const void*,const void*,size_t),
465                                        long num_additional_allocs,
466                                        const char *msgpfx)
467 {
468     RPC_MESSAGE RpcMessage;
469     MIDL_STUB_MESSAGE StubMsg;
470     MIDL_STUB_DESC StubDesc;
471     DWORD size;
472     void *ptr;
473     unsigned char *mem, *mem_orig;
474
475     my_alloc_called = my_free_called = 0;
476     if(!cmp)
477         cmp = memcmp;
478
479     StubDesc = Object_StubDesc;
480     StubDesc.pFormatTypes = formattypes;
481
482     NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
483
484     StubMsg.BufferLength = 0;
485     NdrSimpleStructBufferSize( &StubMsg, (unsigned char *)memsrc, formattypes );
486     ok(StubMsg.BufferLength >= wiredatalen, "%s: length %d\n", msgpfx, StubMsg.BufferLength);
487     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
488     StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
489     ptr = NdrSimpleStructMarshall( &StubMsg,  (unsigned char*)memsrc, formattypes );
490     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
491     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
492     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));
493
494     if (0)
495     {
496     /* FIXME: Causes Wine to crash */
497     StubMsg.Buffer = StubMsg.BufferStart;
498     StubMsg.MemorySize = 0;
499     size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
500     ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
501     ok(size == srcsize, "%s: mem size %u\n", msgpfx, size);
502     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
503
504     StubMsg.Buffer = StubMsg.BufferStart;
505     size = NdrSimpleStructMemorySize( &StubMsg, formattypes );
506 todo_wine {
507     ok(size == StubMsg.MemorySize, "%s: size != MemorySize\n", msgpfx);
508 }
509     ok(StubMsg.MemorySize == ((srcsize + 3) & ~3) + srcsize, "%s: mem size %u\n", msgpfx, size);
510     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
511     }
512     size = srcsize;
513     /*** Unmarshalling first with must_alloc false ***/
514
515     StubMsg.Buffer = StubMsg.BufferStart;
516     StubMsg.MemorySize = 0;
517     mem_orig = mem = HeapAlloc(GetProcessHeap(), 0, srcsize);
518     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
519     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
520     ok(StubMsg.Buffer - StubMsg.BufferStart == wiredatalen, "%s: Buffer %p Start %p\n", msgpfx, StubMsg.Buffer, StubMsg.BufferStart);
521     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
522     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
523     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called); 
524     my_alloc_called = 0;
525     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
526
527     /* if we're a server we still use the suppiled memory */
528     StubMsg.Buffer = StubMsg.BufferStart;
529     StubMsg.IsClient = 0;
530     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
531     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
532     ok(mem == mem_orig, "%s: mem has changed %p %p\n", msgpfx, mem, mem_orig);
533     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx); 
534     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
535     my_alloc_called = 0;
536     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
537
538     /* ...unless we pass a NULL ptr, then the buffer is used. 
539        Passing a NULL ptr while we're a client && !must_alloc
540        crashes on Windows, so we won't do that. */
541
542     mem = NULL;
543     StubMsg.IsClient = 0;
544     StubMsg.Buffer = StubMsg.BufferStart;
545     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 0 );
546     ok(ptr == NULL, "%s: ret %p\n", msgpfx, ptr);
547     ok(mem == StubMsg.BufferStart, "%s: mem not equal buffer\n", msgpfx);
548     ok(!cmp(mem, memsrc, srcsize), "%s: incorrectly unmarshaled\n", msgpfx);
549     ok(my_alloc_called == num_additional_allocs, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
550     my_alloc_called = 0;
551     ok(StubMsg.MemorySize == 0, "%s: memorysize touched in unmarshal\n", msgpfx);
552
553     /*** now must_alloc is true ***/
554
555     /* with must_alloc set we always allocate new memory whether or not we're
556        a server and also when passing NULL */
557     mem = mem_orig;
558     StubMsg.IsClient = 1;
559     StubMsg.Buffer = StubMsg.BufferStart;
560     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
561     ok(ptr == NULL, "ret %p\n", ptr);
562     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
563     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
564     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
565     my_alloc_called = 0;
566     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
567
568     mem = NULL;
569     StubMsg.Buffer = StubMsg.BufferStart;
570     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
571     ok(ptr == NULL, "ret %p\n", ptr);
572     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
573     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
574     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
575     my_alloc_called = 0; 
576     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
577
578     mem = mem_orig;
579     StubMsg.Buffer = StubMsg.BufferStart;
580     StubMsg.IsClient = 0;
581     StubMsg.ReuseBuffer = 1;
582     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
583     ok(ptr == NULL, "ret %p\n", ptr);
584     ok(mem != mem_orig, "mem not changed %p %p\n", mem, mem_orig);
585     ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
586     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n");
587     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
588     my_alloc_called = 0;
589     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
590
591     mem = NULL;
592     StubMsg.Buffer = StubMsg.BufferStart;
593     StubMsg.IsClient = 0;
594     StubMsg.ReuseBuffer = 1;
595     ptr = NdrSimpleStructUnmarshall( &StubMsg, &mem, formattypes, 1 );
596     ok(ptr == NULL, "ret %p\n", ptr);
597     ok(mem != StubMsg.BufferStart, "mem is buffer mem\n");
598     ok(!cmp(mem, memsrc, srcsize), "incorrectly unmarshaled\n"); 
599     ok(my_alloc_called == num_additional_allocs + 1, "%s: my_alloc got called %d times\n", msgpfx, my_alloc_called);
600     my_alloc_called = 0;
601     ok(StubMsg.MemorySize == 0, "memorysize touched in unmarshal\n");
602
603 }
604
605 typedef struct
606 {
607     long l1;
608     long *pl1;
609     char *pc1;
610 } ps1_t;
611
612 static int ps1_cmp(const void *s1, const void *s2, size_t num)
613 {
614     const ps1_t *p1, *p2;
615
616     p1 = s1;
617     p2 = s2;
618
619     if(p1->l1 != p2->l1)
620         return 1;
621
622     if(p1->pl1 && p2->pl1)
623     {
624         if(*p1->pl1 != *p2->pl1)
625             return 1;
626     }
627     else if(p1->pl1 || p1->pl1)
628         return 1;
629
630     if(p1->pc1 && p2->pc1)
631     {
632         if(*p1->pc1 != *p2->pc1)
633             return 1;
634     }
635     else if(p1->pc1 || p1->pc1)
636         return 1;
637
638     return 0;
639 }
640
641 static void test_simple_struct(void)
642 {
643     unsigned char wiredata[28];
644     unsigned long wiredatalen;
645     long l;
646     char c;
647     ps1_t ps1;
648
649     static const unsigned char fmtstr_simple_struct[] =
650     {
651         0x12, 0x0,      /* FC_UP */
652         NdrFcShort( 0x2 ), /* Offset=2 */
653         0x15, 0x3,      /* FC_STRUCT [align 4] */
654         NdrFcShort( 0x18 ),      /* [size 24] */
655         0x6,            /* FC_SHORT */
656         0x2,            /* FC_CHAR */ 
657         0x38,           /* FC_ALIGNM4 */
658         0x8,            /* FC_LONG */
659         0x8,            /* FC_LONG */
660         0x39,           /* FC_ALIGNM8 */
661         0xb,            /* FC_HYPER */ 
662         0x5b,           /* FC_END */
663     };
664     struct {
665         short s;
666         char c;
667         long l1, l2;
668         LONGLONG ll;
669     } s1;
670
671     static const unsigned char fmtstr_pointer_struct[] =
672     { 
673         0x12, 0x0,      /* FC_UP */
674         NdrFcShort( 0x2 ), /* Offset=2 */
675         0x16, 0x3,      /* FC_PSTRUCT [align 4] */
676         NdrFcShort( 0xc ),      /* [size 12] */
677         0x4b,           /* FC_PP */
678         0x5c,           /* FC_PAD */
679         0x46,           /* FC_NO_REPEAT */
680         0x5c,           /* FC_PAD */
681         NdrFcShort( 0x4 ),      /* 4 */
682         NdrFcShort( 0x4 ),      /* 4 */
683         0x13, 0x8,      /* FC_OP [simple_pointer] */
684         0x8,            /* FC_LONG */
685         0x5c,           /* FC_PAD */
686         0x46,           /* FC_NO_REPEAT */
687         0x5c,           /* FC_PAD */
688         NdrFcShort( 0x8 ),      /* 8 */
689         NdrFcShort( 0x8 ),      /* 8 */
690         0x13, 0x8,      /* FC_OP [simple_pointer] */
691         0x2,            /* FC_CHAR */
692         0x5c,           /* FC_PAD */
693         0x5b,           /* FC_END */
694         0x8,            /* FC_LONG */
695         0x8,            /* FC_LONG */
696         0x8,            /* FC_LONG */
697         0x5c,           /* FC_PAD */
698         0x5b,           /* FC_END */
699
700     };
701
702     /* FC_STRUCT */
703     s1.s = 0x1234;
704     s1.c = 0xa5;
705     s1.l1 = 0xdeadbeef;
706     s1.l2 = 0xcafebabe;
707     s1.ll = ((LONGLONG) 0xbadefeed << 32) | 0x2468ace0;
708
709     wiredatalen = 24;
710     memcpy(wiredata, &s1, wiredatalen); 
711     test_simple_struct_marshal(fmtstr_simple_struct + 4, &s1, 24, wiredata, 24, NULL, 0, "struct");
712
713     *(void**)wiredata = &s1;
714     memcpy(wiredata + 4, &s1, wiredatalen);
715     if (0)
716     {
717     /* one of the unmarshallings crashes Wine */
718     test_pointer_marshal(fmtstr_simple_struct, &s1, 24, wiredata, 28, NULL, 0, "struct");
719     }
720
721     /* FC_PSTRUCT */
722     ps1.l1 = 0xdeadbeef;
723     l = 0xcafebabe;
724     ps1.pl1 = &l;
725     c = 'a';
726     ps1.pc1 = &c;
727     memcpy(wiredata + 4, &ps1, 12);
728     memcpy(wiredata + 16, &l, 4);
729     memcpy(wiredata + 20, &c, 1);
730
731     test_simple_struct_marshal(fmtstr_pointer_struct + 4, &ps1, 17, wiredata + 4, 17, ps1_cmp, 2, "pointer_struct");
732     *(void**)wiredata = &ps1;
733     if (0)
734     {
735     /* one of the unmarshallings crashes Wine */
736     test_pointer_marshal(fmtstr_pointer_struct, &ps1, 17, wiredata, 21, ps1_cmp, 2, "pointer_struct");
737     }
738 }
739
740 static void test_fullpointer_xlat(void)
741 {
742     PFULL_PTR_XLAT_TABLES pXlatTables;
743     ULONG RefId;
744     int ret;
745     void *Pointer;
746
747     pXlatTables = NdrFullPointerXlatInit(2, XLAT_CLIENT);
748
749     /* "marshaling" phase */
750
751     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
752     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
753     ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
754
755     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
756     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
757     ok(RefId == 0x1, "RefId should be 0x1 instead of 0x%x\n", RefId);
758
759     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
760     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
761     ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
762
763     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
764     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
765     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
766
767     ret = NdrFullPointerQueryPointer(pXlatTables, NULL, 0, &RefId);
768     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
769     ok(RefId == 0, "RefId should be 0 instead of 0x%x\n", RefId);
770
771     /* "unmarshaling" phase */
772
773     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
774     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
775     ok(Pointer == (void *)0xcafebabe, "Pointer should be 0xcafebabe instead of %p\n", Pointer);
776
777     ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 0, &Pointer);
778     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
779     ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
780
781     NdrFullPointerInsertRefId(pXlatTables, 0x4, (void *)0xdeadbabe);
782
783     ret = NdrFullPointerQueryRefId(pXlatTables, 0x4, 1, &Pointer);
784     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
785     ok(Pointer == (void *)0xdeadbabe, "Pointer should be (void *)0xdeadbabe instead of %p\n", Pointer);
786
787     NdrFullPointerXlatFree(pXlatTables);
788
789     pXlatTables = NdrFullPointerXlatInit(2, XLAT_SERVER);
790
791     /* "unmarshaling" phase */
792
793     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
794     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
795     ok(Pointer == NULL, "Pointer should be NULL instead of %p\n", Pointer);
796
797     NdrFullPointerInsertRefId(pXlatTables, 0x2, (void *)0xcafebabe);
798
799     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 0, &Pointer);
800     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
801     ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
802
803     ret = NdrFullPointerQueryRefId(pXlatTables, 0x2, 1, &Pointer);
804     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
805     ok(Pointer == (void *)0xcafebabe, "Pointer should be (void *)0xcafebabe instead of %p\n", Pointer);
806
807     /* "marshaling" phase */
808
809     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
810     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
811     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
812
813     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
814     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
815     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
816
817     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0, &RefId);
818     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
819     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
820
821     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebabe, 0, &RefId);
822     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
823     ok(RefId == 0x2, "RefId should be 0x2 instead of 0x%x\n", RefId);
824
825     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0, &RefId);
826     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
827     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
828
829     /* "freeing" phase */
830
831     ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebeef);
832     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
833
834     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 0x20, &RefId);
835     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
836     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
837
838     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xcafebeef, 1, &RefId);
839     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
840     ok(RefId == 0x3, "RefId should be 0x3 instead of 0x%x\n", RefId);
841
842     ret = NdrFullPointerFree(pXlatTables, (void *)0xcafebabe);
843     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
844
845     ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
846     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
847
848     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 0x20, &RefId);
849     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
850     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
851
852     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
853     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
854     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
855
856     ret = NdrFullPointerQueryPointer(pXlatTables, (void *)0xdeadbeef, 1, &RefId);
857     ok(ret == 1, "ret should be 1 instead of 0x%x\n", ret);
858     ok(RefId == 0x4, "RefId should be 0x4 instead of 0x%x\n", RefId);
859
860     ret = NdrFullPointerFree(pXlatTables, (void *)0xdeadbeef);
861     ok(ret == 0, "ret should be 0 instead of 0x%x\n", ret);
862
863     NdrFullPointerXlatFree(pXlatTables);
864 }
865
866 static void test_client_init(void)
867 {
868     MIDL_STUB_MESSAGE stubMsg;
869     RPC_MESSAGE rpcMsg;
870
871     memset(&stubMsg, 0xcc, sizeof(stubMsg));
872
873     NdrClientInitializeNew(&rpcMsg, &stubMsg, &Object_StubDesc, 1);
874
875 #define TEST_ZERO(field, fmt) ok(stubMsg.field == 0, #field " should have be set to zero instead of " fmt "\n", stubMsg.field)
876 #define TEST_POINTER_UNSET(field) ok(stubMsg.field == (void *)0xcccccccc, #field " should have be unset instead of %p\n", stubMsg.field)
877 #define TEST_ULONG_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have be unset instead of 0x%x\n", stubMsg.field)
878 #define TEST_ULONG_PTR_UNSET(field) ok(stubMsg.field == 0xcccccccc, #field " should have be unset instead of 0x%lx\n", stubMsg.field)
879
880     ok(stubMsg.RpcMsg == &rpcMsg, "stubMsg.RpcMsg should have been %p instead of %p\n", &rpcMsg, stubMsg.RpcMsg);
881     TEST_POINTER_UNSET(Buffer);
882     TEST_ZERO(BufferStart, "%p");
883     TEST_ZERO(BufferEnd, "%p");
884     TEST_POINTER_UNSET(BufferMark);
885     TEST_ZERO(BufferLength, "%d");
886     TEST_ULONG_UNSET(MemorySize);
887     TEST_POINTER_UNSET(Memory);
888     ok(stubMsg.IsClient == 1, "stubMsg.IsClient should have been 1 instead of %u\n", stubMsg.IsClient);
889     TEST_ZERO(ReuseBuffer, "%d");
890     TEST_ZERO(pAllocAllNodesContext, "%p");
891     TEST_ZERO(pPointerQueueState, "%p");
892     TEST_ZERO(IgnoreEmbeddedPointers, "%d");
893     TEST_ZERO(PointerBufferMark, "%p");
894     TEST_ZERO(fBufferValid, "%d");
895     TEST_ZERO(uFlags, "%d");
896     /* FIXME: UniquePtrCount */
897     TEST_ULONG_PTR_UNSET(MaxCount);
898     TEST_ULONG_UNSET(Offset);
899     TEST_ULONG_UNSET(ActualCount);
900     ok(stubMsg.pfnAllocate == my_alloc, "stubMsg.pfnAllocate should have been %p instead of %p\n", my_alloc, stubMsg.pfnAllocate);
901     ok(stubMsg.pfnFree == my_free, "stubMsg.pfnFree should have been %p instead of %p\n", my_free, stubMsg.pfnFree);
902     TEST_ZERO(StackTop, "%p");
903     TEST_POINTER_UNSET(pPresentedType);
904     TEST_POINTER_UNSET(pTransmitType);
905     TEST_POINTER_UNSET(SavedHandle);
906     ok(stubMsg.StubDesc == &Object_StubDesc, "stubMsg.StubDesc should have been %p instead of %p\n", &Object_StubDesc, stubMsg.StubDesc);
907     TEST_POINTER_UNSET(FullPtrXlatTables);
908     TEST_ZERO(FullPtrRefId, "%d");
909     TEST_ZERO(PointerLength, "%d");
910     TEST_ZERO(fInDontFree, "%d");
911     TEST_ZERO(fDontCallFreeInst, "%d");
912     TEST_ZERO(fInOnlyParam, "%d");
913     TEST_ZERO(fHasReturn, "%d");
914     TEST_ZERO(fHasExtensions, "%d");
915     TEST_ZERO(fHasNewCorrDesc, "%d");
916     TEST_ZERO(fUnused, "%d");
917     ok(stubMsg.fUnused2 == 0xffffcccc, "stubMsg.fUnused2 should have been 0xcccc instead of 0x%x\n", stubMsg.fUnused2);
918     ok(stubMsg.dwDestContext == MSHCTX_DIFFERENTMACHINE, "stubMsg.dwDestContext should have been MSHCTX_DIFFERENTMACHINE instead of %d\n", stubMsg.dwDestContext);
919     TEST_ZERO(pvDestContext, "%p");
920     TEST_POINTER_UNSET(SavedContextHandles);
921     TEST_ULONG_UNSET(ParamNumber);
922     TEST_ZERO(pRpcChannelBuffer, "%p");
923     TEST_ZERO(pArrayInfo, "%p");
924     TEST_POINTER_UNSET(SizePtrCountArray);
925     TEST_POINTER_UNSET(SizePtrOffsetArray);
926     TEST_POINTER_UNSET(SizePtrLengthArray);
927     TEST_POINTER_UNSET(pArgQueue);
928     TEST_ZERO(dwStubPhase, "%d");
929     /* FIXME: where does this value come from? */
930     trace("LowStackMark is %p\n", stubMsg.LowStackMark);
931     TEST_ZERO(pAsyncMsg, "%p");
932     TEST_ZERO(pCorrInfo, "%p");
933     TEST_ZERO(pCorrMemory, "%p");
934     TEST_ZERO(pMemoryList, "%p");
935     TEST_POINTER_UNSET(pCSInfo);
936     TEST_POINTER_UNSET(ConformanceMark);
937     TEST_POINTER_UNSET(VarianceMark);
938     ok(stubMsg.Unused == 0xcccccccc, "Unused should have be unset instead of 0x%lx\n", stubMsg.Unused);
939     TEST_POINTER_UNSET(pContext);
940     TEST_POINTER_UNSET(ContextHandleHash);
941     TEST_POINTER_UNSET(pUserMarshalList);
942     TEST_ULONG_PTR_UNSET(Reserved51_3);
943     TEST_ULONG_PTR_UNSET(Reserved51_4);
944     TEST_ULONG_PTR_UNSET(Reserved51_5);
945 #undef TEST_ULONG_UNSET
946 #undef TEST_POINTER_UNSET
947 #undef TEST_ZERO
948
949 }
950
951 static void test_ndr_allocate(void)
952 {
953     RPC_MESSAGE RpcMessage;
954     MIDL_STUB_MESSAGE StubMsg;
955     MIDL_STUB_DESC StubDesc;
956     void *p1, *p2;
957     struct tag_mem_list_t
958     {
959         DWORD magic;
960         void *ptr;
961         struct tag_mem_list_t *next;
962     } *mem_list;
963     const DWORD magic_MEML = 'M' << 24 | 'E' << 16 | 'M' << 8 | 'L';
964
965     StubDesc = Object_StubDesc;
966     NdrClientInitializeNew(&RpcMessage, &StubMsg, &StubDesc, 0);
967
968     ok(StubMsg.pMemoryList == NULL, "memlist %p\n", StubMsg.pMemoryList);
969     my_alloc_called = my_free_called = 0;
970     p1 = NdrAllocate(&StubMsg, 10);
971     p2 = NdrAllocate(&StubMsg, 20);
972     ok(my_alloc_called == 2, "alloc called %d\n", my_alloc_called);
973     mem_list = StubMsg.pMemoryList;
974 todo_wine {
975     ok(mem_list != NULL, "mem_list NULL\n");
976  }
977     if(mem_list)
978     {
979         ok(mem_list->magic == magic_MEML, "magic %08x\n", mem_list->magic);
980         ok(mem_list->ptr == p2, "ptr != p2\n");
981         ok(mem_list->next != NULL, "next NULL\n");
982         mem_list = mem_list->next;
983         if(mem_list)
984         {
985             ok(mem_list->magic == magic_MEML, "magic %08x\n", mem_list->magic);
986             ok(mem_list->ptr == p1, "ptr != p2\n");
987             ok(mem_list->next == NULL, "next %p\n", mem_list->next);
988         }
989     }
990     /* NdrFree isn't exported so we can't test free'ing */
991 }
992
993 static void test_conformant_array(void)
994 {
995     RPC_MESSAGE RpcMessage;
996     MIDL_STUB_MESSAGE StubMsg;
997     MIDL_STUB_DESC StubDesc;
998     void *ptr;
999     unsigned char *mem, *mem_orig;
1000     unsigned char memsrc[20];
1001
1002     static const unsigned char fmtstr_conf_array[] =
1003     {
1004         0x1b,              /* FC_CARRAY */
1005         0x0,               /* align */
1006         NdrFcShort( 0x1 ), /* elem size */
1007         0x40,              /* Corr desc:  const */
1008         0x0,
1009         NdrFcShort(0x10),  /* const = 0x10 */
1010         0x1,               /* FC_BYTE */
1011         0x5b               /* FC_END */
1012     };
1013
1014     StubDesc = Object_StubDesc;
1015     StubDesc.pFormatTypes = fmtstr_conf_array;
1016
1017     NdrClientInitializeNew(
1018                            &RpcMessage,
1019                            &StubMsg,
1020                            &StubDesc,
1021                            0);
1022
1023     StubMsg.BufferLength = 0;
1024     NdrConformantArrayBufferSize( &StubMsg,
1025                           memsrc,
1026                           fmtstr_conf_array );
1027     ok(StubMsg.BufferLength >= 20, "length %d\n", StubMsg.BufferLength);
1028
1029     /*NdrGetBuffer(&_StubMsg, _StubMsg.BufferLength, NULL);*/
1030     StubMsg.RpcMsg->Buffer = StubMsg.BufferStart = StubMsg.Buffer = HeapAlloc(GetProcessHeap(), 0, StubMsg.BufferLength);
1031     StubMsg.BufferEnd = StubMsg.BufferStart + StubMsg.BufferLength;
1032
1033     ptr = NdrConformantArrayMarshall( &StubMsg,  memsrc, fmtstr_conf_array );
1034     ok(ptr == NULL, "ret %p\n", ptr);
1035     ok(StubMsg.Buffer - StubMsg.BufferStart == 20, "Buffer %p Start %p len %d\n", StubMsg.Buffer, StubMsg.BufferStart, 20);
1036     ok(!memcmp(StubMsg.BufferStart + 4, memsrc, 16), "incorrectly marshaled\n");
1037
1038     StubMsg.Buffer = StubMsg.BufferStart;
1039     StubMsg.MemorySize = 0;
1040     mem = NULL;
1041
1042     /* Client */
1043     my_alloc_called = 0;
1044     /* passing mem == NULL with must_alloc == 0 crashes under Windows */
1045     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1046     ok(mem != NULL, "mem not alloced\n");
1047     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1048     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1049
1050     my_alloc_called = 0;
1051     StubMsg.Buffer = StubMsg.BufferStart;
1052     mem_orig = mem;
1053     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1054     ok(mem == mem_orig, "mem alloced\n");
1055     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1056     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1057
1058     my_alloc_called = 0;
1059     StubMsg.Buffer = StubMsg.BufferStart;
1060     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1061     ok(mem != mem_orig, "mem not alloced\n");
1062     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1063     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1064
1065     my_free_called = 0;
1066     StubMsg.Buffer = StubMsg.BufferStart;
1067     NdrConformantArrayFree( &StubMsg, mem, fmtstr_conf_array );
1068     ok(my_free_called == 0, "free called %d\n", my_free_called);
1069     StubMsg.pfnFree(mem);
1070
1071     /* Server */
1072     my_alloc_called = 0;
1073     StubMsg.IsClient = 0;
1074     mem = NULL;
1075     StubMsg.Buffer = StubMsg.BufferStart;
1076     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1077 todo_wine {
1078     ok(mem == StubMsg.BufferStart + 4, "mem not pointing at buffer\n");
1079     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1080 }
1081     my_alloc_called = 0;
1082     mem = NULL;
1083     StubMsg.Buffer = StubMsg.BufferStart;
1084     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1085     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1086     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1087     StubMsg.pfnFree(mem);
1088
1089     my_alloc_called = 0;
1090     mem = mem_orig;
1091     StubMsg.Buffer = StubMsg.BufferStart;
1092     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 0);
1093     ok(mem == mem_orig, "mem alloced\n");
1094     ok(my_alloc_called == 0, "alloc called %d\n", my_alloc_called);
1095
1096     my_alloc_called = 0;
1097     mem = mem_orig;
1098     StubMsg.Buffer = StubMsg.BufferStart;
1099     NdrConformantArrayUnmarshall( &StubMsg, &mem, fmtstr_conf_array, 1);
1100     ok(mem != StubMsg.BufferStart + 4, "mem pointing at buffer\n");
1101     ok(my_alloc_called == 1, "alloc called %d\n", my_alloc_called);
1102     StubMsg.pfnFree(mem);
1103     StubMsg.pfnFree(mem_orig);
1104
1105     HeapFree(GetProcessHeap(), 0, StubMsg.RpcMsg->Buffer);
1106 }
1107
1108 START_TEST( ndr_marshall )
1109 {
1110     test_ndr_simple_type();
1111     test_simple_types();
1112     test_simple_struct();
1113     test_fullpointer_xlat();
1114     test_client_init();
1115     test_ndr_allocate();
1116     test_conformant_array();
1117 }