rpcrt4: Add tests for CreateStub and fix up CStdStubBuffer_Construct to match.
[wine] / dlls / rpcrt4 / tests / cstub.c
1 /*
2  * Unit test suite for cstubs
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 PROXY_DELEGATION
24 #define COBJMACROS
25
26 #include "wine/test.h"
27 #include <windef.h>
28 #include <winbase.h>
29 #include <winnt.h>
30 #include <winerror.h>
31
32
33 #include "rpc.h"
34 #include "rpcdce.h"
35 #include "rpcproxy.h"
36
37 static CStdPSFactoryBuffer PSFactoryBuffer;
38
39 CSTDSTUBBUFFERRELEASE(&PSFactoryBuffer)
40 CSTDSTUBBUFFER2RELEASE(&PSFactoryBuffer)
41
42 static GUID IID_if1 = {0x12345678, 1234, 5678, {12,34,56,78,90,0xab,0xcd,0xef}};
43 static GUID IID_if2 = {0x12345679, 1234, 5678, {12,34,56,78,90,0xab,0xcd,0xef}};
44 static GUID IID_if3 = {0x1234567a, 1234, 5678, {12,34,56,78,90,0xab,0xcd,0xef}};
45 static GUID IID_if4 = {0x1234567b, 1234, 5678, {12,34,56,78,90,0xab,0xcd,0xef}};
46
47 static int my_alloc_called;
48 static int my_free_called;
49
50 static void * CALLBACK my_alloc(size_t size)
51 {
52     my_alloc_called++;
53     return NdrOleAllocate(size);
54 }
55
56 static void CALLBACK my_free(void *ptr)
57 {
58     my_free_called++;
59     return NdrOleFree(ptr);
60 }
61
62 typedef struct _MIDL_PROC_FORMAT_STRING
63 {
64     short          Pad;
65     unsigned char  Format[ 2 ];
66 } MIDL_PROC_FORMAT_STRING;
67
68 typedef struct _MIDL_TYPE_FORMAT_STRING
69 {
70     short          Pad;
71     unsigned char  Format[ 2 ];
72 } MIDL_TYPE_FORMAT_STRING;
73
74
75 static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =
76 {
77     0,
78     {
79         0, 0
80     }
81 };
82
83 static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =
84 {
85     0,
86     {
87         0, 0
88     }
89 };
90
91 static const MIDL_STUB_DESC Object_StubDesc =
92     {
93     NULL,
94     my_alloc,
95     my_free,
96     { 0 },
97     0,
98     0,
99     0,
100     0,
101     __MIDL_TypeFormatString.Format,
102     1, /* -error bounds_check flag */
103     0x20000, /* Ndr library version */
104     0,
105     0x50100a4, /* MIDL Version 5.1.164 */
106     0,
107     NULL,
108     0,  /* notify & notify_flag routine table */
109     1,  /* Flags */
110     0,  /* Reserved3 */
111     0,  /* Reserved4 */
112     0   /* Reserved5 */
113     };
114
115 HRESULT WINAPI if1_fn1_Proxy(void *This)
116 {
117     return S_OK;
118 }
119
120 void __RPC_STUB if1_fn1_Stub(
121     IRpcStubBuffer *This,
122     IRpcChannelBuffer *_pRpcChannelBuffer,
123     PRPC_MESSAGE _pRpcMessage,
124     DWORD *_pdwStubPhase)
125 {
126     trace("fn1 stub\n");
127 }
128
129 HRESULT WINAPI if1_fn2_Proxy(void *This)
130 {
131     return S_OK;
132 }
133
134 void __RPC_STUB if1_fn2_Stub(
135     IRpcStubBuffer *This,
136     IRpcChannelBuffer *_pRpcChannelBuffer,
137     PRPC_MESSAGE _pRpcMessage,
138     DWORD *_pdwStubPhase)
139 {
140     trace("fn2 stub\n");
141 }
142
143 CINTERFACE_PROXY_VTABLE(5) if1_proxy_vtbl =
144 {
145     { &IID_if1 },
146     {   IUnknown_QueryInterface_Proxy,
147         IUnknown_AddRef_Proxy,
148         IUnknown_Release_Proxy ,
149         if1_fn1_Proxy,
150         if1_fn2_Proxy
151     }
152 };
153
154
155 static const unsigned short if1_FormatStringOffsetTable[] =
156     {
157     0,
158     0
159     };
160
161 static const MIDL_SERVER_INFO if1_server_info =
162     {
163     &Object_StubDesc,
164     0,
165     __MIDL_ProcFormatString.Format,
166     &if1_FormatStringOffsetTable[-3],
167     0,
168     0,
169     0,
170     0};
171
172
173 static const PRPC_STUB_FUNCTION if1_table[] =
174 {
175     if1_fn1_Stub,
176     if1_fn2_Stub
177 };
178
179 CInterfaceStubVtbl if1_stub_vtbl =
180 {
181     {
182         &IID_if1,
183         &if1_server_info,
184         5,
185         &if1_table[-3]
186     },
187     { CStdStubBuffer_METHODS }
188 };
189
190 CINTERFACE_PROXY_VTABLE(13) if2_proxy_vtbl =
191 {
192     { &IID_if2 },
193     {   IUnknown_QueryInterface_Proxy,
194         IUnknown_AddRef_Proxy,
195         IUnknown_Release_Proxy ,
196         0,
197         0,
198         0,
199         0,
200         0,
201         0,
202         0,
203         0,
204         0,
205         0
206     }
207 };
208
209 static const unsigned short if2_FormatStringOffsetTable[] =
210     {
211     (unsigned short) -1,
212     (unsigned short) -1,
213     (unsigned short) -1,
214     (unsigned short) -1,
215     (unsigned short) -1,
216     (unsigned short) -1,
217     (unsigned short) -1,
218     (unsigned short) -1,
219     (unsigned short) -1,
220     (unsigned short) -1,
221     0
222     };
223
224 static const MIDL_SERVER_INFO if2_server_info =
225     {
226     &Object_StubDesc,
227     0,
228     __MIDL_ProcFormatString.Format,
229     &if2_FormatStringOffsetTable[-3],
230     0,
231     0,
232     0,
233     0};
234
235
236 static const PRPC_STUB_FUNCTION if2_table[] =
237 {
238     STUB_FORWARDING_FUNCTION,
239     STUB_FORWARDING_FUNCTION,
240     STUB_FORWARDING_FUNCTION,
241     STUB_FORWARDING_FUNCTION,
242     STUB_FORWARDING_FUNCTION,
243     STUB_FORWARDING_FUNCTION,
244     STUB_FORWARDING_FUNCTION,
245     STUB_FORWARDING_FUNCTION,
246     STUB_FORWARDING_FUNCTION,
247     STUB_FORWARDING_FUNCTION
248 };
249
250 CInterfaceStubVtbl if2_stub_vtbl =
251 {
252     {
253         &IID_if2,
254         &if2_server_info,
255         13,
256         &if2_table[-3]
257     },
258     { CStdStubBuffer_DELEGATING_METHODS }
259 };
260
261 CINTERFACE_PROXY_VTABLE(4) if3_proxy_vtbl =
262 {
263     { &IID_if3 },
264     {   IUnknown_QueryInterface_Proxy,
265         IUnknown_AddRef_Proxy,
266         IUnknown_Release_Proxy ,
267         if1_fn1_Proxy
268     }
269 };
270
271
272 static const unsigned short if3_FormatStringOffsetTable[] =
273     {
274     0,
275     0
276     };
277
278 static const MIDL_SERVER_INFO if3_server_info =
279     {
280     &Object_StubDesc,
281     0,
282     __MIDL_ProcFormatString.Format,
283     &if3_FormatStringOffsetTable[-3],
284     0,
285     0,
286     0,
287     0};
288
289
290 static const PRPC_STUB_FUNCTION if3_table[] =
291 {
292     if1_fn1_Stub
293 };
294
295 CInterfaceStubVtbl if3_stub_vtbl =
296 {
297     {
298         &IID_if3,
299         &if3_server_info,
300         4,
301         &if1_table[-3]
302     },
303     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
304 };
305
306 CINTERFACE_PROXY_VTABLE(7) if4_proxy_vtbl =
307 {
308     { &IID_if4 },
309     {   IUnknown_QueryInterface_Proxy,
310         IUnknown_AddRef_Proxy,
311         IUnknown_Release_Proxy ,
312         0,
313         0,
314         0,
315         0
316     }
317 };
318
319 static const unsigned short if4_FormatStringOffsetTable[] =
320     {
321     (unsigned short) -1,
322     (unsigned short) -1,
323     (unsigned short) -1,
324     (unsigned short) -1,
325     0
326     };
327
328 static const MIDL_SERVER_INFO if4_server_info =
329     {
330     &Object_StubDesc,
331     0,
332     __MIDL_ProcFormatString.Format,
333     &if4_FormatStringOffsetTable[-3],
334     0,
335     0,
336     0,
337     0};
338
339
340 static const PRPC_STUB_FUNCTION if4_table[] =
341 {
342     STUB_FORWARDING_FUNCTION,
343     STUB_FORWARDING_FUNCTION,
344     STUB_FORWARDING_FUNCTION,
345     STUB_FORWARDING_FUNCTION,
346 };
347
348 CInterfaceStubVtbl if4_stub_vtbl =
349 {
350     {
351         &IID_if4,
352         &if4_server_info,
353         7,
354         &if2_table[-3]
355     },
356     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
357 };
358
359 const CInterfaceProxyVtbl *cstub_ProxyVtblList[] =
360 {
361     (const CInterfaceProxyVtbl *) &if1_proxy_vtbl,
362     (const CInterfaceProxyVtbl *) &if2_proxy_vtbl,
363     (const CInterfaceProxyVtbl *) &if3_proxy_vtbl,
364     (const CInterfaceProxyVtbl *) &if4_proxy_vtbl,
365     NULL
366 };
367
368 const CInterfaceStubVtbl *cstub_StubVtblList[] =
369 {
370     (const CInterfaceStubVtbl *) &if1_stub_vtbl,
371     (const CInterfaceStubVtbl *) &if2_stub_vtbl,
372     (const CInterfaceStubVtbl *) &if3_stub_vtbl,
373     (const CInterfaceStubVtbl *) &if4_stub_vtbl,
374     NULL
375 };
376
377 PCInterfaceName const if_name_list[] =
378 {
379     "if1",
380     "if2",
381     "if3",
382     "if4",
383     NULL
384 };
385
386 const IID *base_iid_list[] = 
387 {
388     NULL,
389     &IID_ITypeLib,
390     NULL,
391     &IID_IDispatch,
392     NULL
393 };
394
395 #define cstub_CHECK_IID(n)     IID_GENERIC_CHECK_IID( cstub, pIID, n)
396
397 int __stdcall iid_lookup( const IID * pIID, int * pIndex )
398 {
399     IID_BS_LOOKUP_SETUP
400
401     IID_BS_LOOKUP_INITIAL_TEST( cstub, 4, 4 )
402     IID_BS_LOOKUP_NEXT_TEST( cstub, 2 )
403     IID_BS_LOOKUP_NEXT_TEST( cstub, 1 )
404     IID_BS_LOOKUP_RETURN_RESULT( cstub, 4, *pIndex )
405
406 }
407
408
409 const ExtendedProxyFileInfo my_proxy_file_info =
410 {
411     (const PCInterfaceProxyVtblList *) &cstub_ProxyVtblList,
412     (const PCInterfaceStubVtblList *) &cstub_StubVtblList,
413     (const PCInterfaceName *) &if_name_list,
414     (const IID **) &base_iid_list,
415     &iid_lookup,
416     4,
417     1,
418     NULL,
419     0,
420     0,
421     0
422 };
423
424 const ProxyFileInfo *proxy_file_list[] = {
425     &my_proxy_file_info,
426     NULL
427 };
428
429
430 static IPSFactoryBuffer *test_NdrDllGetClassObject(void)
431 {
432     IPSFactoryBuffer *ppsf = NULL;
433     const CLSID PSDispatch = {0x20420, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}};
434     HRESULT r;
435     HMODULE hmod = LoadLibraryA("rpcrt4.dll");
436     void *CStd_QueryInterface = GetProcAddress(hmod, "CStdStubBuffer_QueryInterface");
437     void *CStd_AddRef = GetProcAddress(hmod, "CStdStubBuffer_AddRef");
438     void *CStd_Release = GetProcAddress(hmod, "NdrCStdStubBuffer_Release");
439     void *CStd_Connect = GetProcAddress(hmod, "CStdStubBuffer_Connect");
440     void *CStd_Disconnect = GetProcAddress(hmod, "CStdStubBuffer_Disconnect");
441     void *CStd_Invoke = GetProcAddress(hmod, "CStdStubBuffer_Invoke");
442     void *CStd_IsIIDSupported = GetProcAddress(hmod, "CStdStubBuffer_IsIIDSupported");
443     void *CStd_CountRefs = GetProcAddress(hmod, "CStdStubBuffer_CountRefs");
444     void *CStd_DebugServerQueryInterface = GetProcAddress(hmod, "CStdStubBuffer_DebugServerQueryInterface");
445     void *CStd_DebugServerRelease = GetProcAddress(hmod, "CStdStubBuffer_DebugServerRelease");
446
447     r = NdrDllGetClassObject(&PSDispatch, &IID_IPSFactoryBuffer, (void**)&ppsf, proxy_file_list,
448                          &PSDispatch, &PSFactoryBuffer);
449
450     ok(r == S_OK, "ret %08lx\n", r);
451     ok(ppsf != NULL, "ppsf == NULL\n");
452
453     ok(PSFactoryBuffer.pProxyFileList == proxy_file_list, "pfl not the same\n");
454     ok(PSFactoryBuffer.pProxyFileList[0]->pStubVtblList == (PCInterfaceStubVtblList *) &cstub_StubVtblList, "stub vtbllist not the same\n");
455
456     /* if1 is non-delegating, if2 is delegating, if3 is non-delegating
457        but I've zero'ed the vtbl entries, similarly if4 is delegating
458        with zero'ed vtbl entries */
459
460 #define VTBL_TEST_NOT_CHANGE_TO(name, i)                                  \
461     ok(PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name != CStd_##name, #name "vtbl %d updated %p %p\n", \
462        i, PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name, CStd_##name );
463 #define VTBL_TEST_CHANGE_TO(name, i)                                  \
464     ok(PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name == CStd_##name, #name "vtbl %d not updated %p %p\n", \
465        i, PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name, CStd_##name );
466 #define VTBL_TEST_ZERO(name, i)                                  \
467     ok(PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name == NULL, #name "vtbl %d not null %p\n", \
468        i, PSFactoryBuffer.pProxyFileList[0]->pStubVtblList[i]->Vtbl.name );
469     VTBL_TEST_NOT_CHANGE_TO(QueryInterface, 0);
470     VTBL_TEST_NOT_CHANGE_TO(AddRef, 0);
471     VTBL_TEST_NOT_CHANGE_TO(Release, 0);
472     VTBL_TEST_NOT_CHANGE_TO(Connect, 0);
473     VTBL_TEST_NOT_CHANGE_TO(Disconnect, 0);
474     VTBL_TEST_NOT_CHANGE_TO(Invoke, 0);
475     VTBL_TEST_NOT_CHANGE_TO(IsIIDSupported, 0);
476     VTBL_TEST_NOT_CHANGE_TO(CountRefs, 0);
477     VTBL_TEST_NOT_CHANGE_TO(DebugServerQueryInterface, 0);
478     VTBL_TEST_NOT_CHANGE_TO(DebugServerRelease, 0);
479
480     VTBL_TEST_CHANGE_TO(QueryInterface, 1);
481     VTBL_TEST_CHANGE_TO(AddRef, 1);
482     VTBL_TEST_NOT_CHANGE_TO(Release, 1);
483 todo_wine {
484     VTBL_TEST_NOT_CHANGE_TO(Connect, 1);
485     VTBL_TEST_NOT_CHANGE_TO(Disconnect, 1);
486 }
487     VTBL_TEST_CHANGE_TO(Invoke, 1);
488     VTBL_TEST_CHANGE_TO(IsIIDSupported, 1);
489 todo_wine {
490     VTBL_TEST_NOT_CHANGE_TO(CountRefs, 1);
491 }
492     VTBL_TEST_CHANGE_TO(DebugServerQueryInterface, 1);
493     VTBL_TEST_CHANGE_TO(DebugServerRelease, 1);
494
495     VTBL_TEST_CHANGE_TO(QueryInterface, 2);
496     VTBL_TEST_CHANGE_TO(AddRef, 2);
497     VTBL_TEST_ZERO(Release, 2);
498     VTBL_TEST_CHANGE_TO(Connect, 2);
499     VTBL_TEST_CHANGE_TO(Disconnect, 2);
500     VTBL_TEST_CHANGE_TO(Invoke, 2);
501     VTBL_TEST_CHANGE_TO(IsIIDSupported, 2);
502     VTBL_TEST_CHANGE_TO(CountRefs, 2);
503     VTBL_TEST_CHANGE_TO(DebugServerQueryInterface, 2);
504     VTBL_TEST_CHANGE_TO(DebugServerRelease, 2);
505
506     VTBL_TEST_CHANGE_TO(QueryInterface, 3);
507     VTBL_TEST_CHANGE_TO(AddRef, 3);
508     VTBL_TEST_ZERO(Release, 3);
509 todo_wine {
510     VTBL_TEST_NOT_CHANGE_TO(Connect, 3);
511     VTBL_TEST_NOT_CHANGE_TO(Disconnect, 3);
512 }
513     VTBL_TEST_CHANGE_TO(Invoke, 3);
514     VTBL_TEST_CHANGE_TO(IsIIDSupported, 3);
515 todo_wine {
516     VTBL_TEST_NOT_CHANGE_TO(CountRefs, 3);
517 }
518     VTBL_TEST_CHANGE_TO(DebugServerQueryInterface, 3);
519     VTBL_TEST_CHANGE_TO(DebugServerRelease, 3);
520
521
522
523 #undef VTBL_TEST_NOT_CHANGE_TO
524 #undef VTBL_TEST_CHANGE_TO
525 #undef VTBL_TEST_ZERO
526
527     ok(PSFactoryBuffer.RefCount == 1, "ref count %ld\n", PSFactoryBuffer.RefCount);
528     return ppsf;
529 }
530
531 static int base_buffer_invoke_called;
532 static HRESULT WINAPI base_buffer_Invoke(IRpcStubBuffer *This, RPCOLEMESSAGE *msg, IRpcChannelBuffer *channel)
533 {
534     base_buffer_invoke_called++;
535     ok(msg == (RPCOLEMESSAGE*)0xcafebabe, "msg ptr changed\n");
536     ok(channel == (IRpcChannelBuffer*)0xdeadbeef, "channel ptr changed\n");
537     return S_OK; /* returning any failure here results in an exception */
538 }
539
540 static IRpcStubBufferVtbl base_buffer_vtbl = {
541     (void*)0xcafebab0,
542     (void*)0xcafebab1,
543     (void*)0xcafebab2,
544     (void*)0xcafebab3,
545     (void*)0xcafebab4,
546     base_buffer_Invoke,
547     (void*)0xcafebab6,
548     (void*)0xcafebab7,
549     (void*)0xcafebab8,
550     (void*)0xcafebab9
551 };
552
553 static void test_NdrStubForwardingFunction(void)
554 {
555     void *This[5];
556     void *real_this;
557     IRpcChannelBuffer *channel = (IRpcChannelBuffer*)0xdeadbeef;
558     RPC_MESSAGE *msg = (RPC_MESSAGE*)0xcafebabe;
559     DWORD *phase = (DWORD*)0x12345678;
560     IRpcStubBufferVtbl *base_buffer_vtbl_ptr = &base_buffer_vtbl;
561     IRpcStubBuffer *base_stub_buffer = (IRpcStubBuffer*)&base_buffer_vtbl_ptr;
562
563     memset(This, 0xcc, sizeof(This));
564     This[0] = base_stub_buffer;
565     real_this = &This[1];
566
567     NdrStubForwardingFunction( real_this, channel, msg, phase );
568
569 todo_wine {
570     ok(base_buffer_invoke_called == 1, "base_buffer_invoke called %d times\n", base_buffer_invoke_called);
571 }
572
573 }
574
575 static IRpcStubBuffer *create_stub(IPSFactoryBuffer *ppsf, REFIID iid, IUnknown *obj, HRESULT expected_result)
576 {
577     IRpcStubBuffer *pstub = NULL;
578     HRESULT r;
579
580     r = IPSFactoryBuffer_CreateStub(ppsf, iid, obj, &pstub);
581     ok(r == expected_result, "CreateStub returned %08lx expected %08lx\n", r, expected_result);
582     return pstub;
583 }
584
585 static HRESULT WINAPI create_stub_test_QI(IUnknown *This, REFIID iid, void **ppv)
586 {
587     ok(IsEqualIID(iid, &IID_if1), "incorrect iid\n");
588     *ppv = (void*)0xdeadbeef;
589     return S_OK;
590 }
591
592 static IUnknownVtbl create_stub_test_vtbl =
593 {
594     create_stub_test_QI,
595     NULL,
596     NULL
597 };
598
599 static HRESULT WINAPI create_stub_test_fail_QI(IUnknown *This, REFIID iid, void **ppv)
600 {
601     ok(IsEqualIID(iid, &IID_if1), "incorrect iid\n");
602     *ppv = NULL;
603     return E_NOINTERFACE;
604 }
605
606 static IUnknownVtbl create_stub_test_fail_vtbl =
607 {
608     create_stub_test_fail_QI,
609     NULL,
610     NULL
611 };
612
613 static void test_CreateStub(IPSFactoryBuffer *ppsf)
614 {
615     IUnknownVtbl *vtbl = &create_stub_test_vtbl;
616     IUnknown *obj = (IUnknown*)&vtbl;
617     IRpcStubBuffer *pstub = create_stub(ppsf, &IID_if1, obj, S_OK);
618     CStdStubBuffer *cstd_stub = (CStdStubBuffer*)pstub;
619     CInterfaceStubHeader *header = ((CInterfaceStubHeader *)cstd_stub->lpVtbl) - 1;
620
621     ok(IsEqualIID(header->piid, &IID_if1), "header iid differs\n");
622     ok(cstd_stub->RefCount == 1, "ref count %ld\n", cstd_stub->RefCount);
623     /* 0xdeadbeef returned from create_stub_test_QI */
624     ok(cstd_stub->pvServerObject == (void*)0xdeadbeef, "pvServerObject %p", cstd_stub->pvServerObject);
625     ok(cstd_stub->pPSFactory == ppsf, "pPSFactory %p\n", cstd_stub->pPSFactory);
626
627     vtbl = &create_stub_test_fail_vtbl;
628     pstub = create_stub(ppsf, &IID_if1, obj, E_NOINTERFACE);
629
630 }
631     
632 START_TEST( cstub )
633 {
634     IPSFactoryBuffer *ppsf;
635
636     OleInitialize(NULL);
637
638     ppsf = test_NdrDllGetClassObject();
639     test_NdrStubForwardingFunction();
640     test_CreateStub(ppsf);
641
642     OleUninitialize();
643 }