push 0a0aa53cd365a71ca6121b6df157ca635450378f
[wine/hacks.git] / dlls / rpcrt4 / tests / server.c
blob0d41a6278fc8ad4c7a0d2dbfa9c349d4395a3db9
1 /*
2 * Tests for WIDL and RPC server/clients.
4 * Copyright (C) Google 2007 (Dan Hipschman)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <windows.h>
22 #include "wine/test.h"
23 #include "server.h"
24 #include "server_defines.h"
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
30 #define PORT "4114"
31 #define PIPE "\\pipe\\wine_rpcrt4_test"
33 #define INT_CODE 4198
35 static const char *progname;
36 static BOOL old_windows_version;
38 static HANDLE stop_event;
40 static void (WINAPI *pNDRSContextMarshall2)(RPC_BINDING_HANDLE, NDR_SCONTEXT, void*, NDR_RUNDOWN, void*, ULONG);
41 static NDR_SCONTEXT (WINAPI *pNDRSContextUnmarshall2)(RPC_BINDING_HANDLE, void*, ULONG, void*, ULONG);
42 static RPC_STATUS (WINAPI *pRpcServerRegisterIfEx)(RPC_IF_HANDLE,UUID*, RPC_MGR_EPV*, unsigned int,
43 unsigned int,RPC_IF_CALLBACK_FN*);
45 static void InitFunctionPointers(void)
47 HMODULE hrpcrt4 = GetModuleHandleA("rpcrt4.dll");
49 pNDRSContextMarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextMarshall2");
50 pNDRSContextUnmarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextUnmarshall2");
51 pRpcServerRegisterIfEx = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterIfEx");
53 if (!pNDRSContextMarshall2) old_windows_version = TRUE;
56 void __RPC_FAR *__RPC_USER
57 midl_user_allocate(SIZE_T n)
59 return malloc(n);
62 void __RPC_USER
63 midl_user_free(void __RPC_FAR *p)
65 free(p);
68 static char *
69 xstrdup(const char *s)
71 char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
72 strcpy(d, s);
73 return d;
76 int
77 s_int_return(void)
79 return INT_CODE;
82 int
83 s_square(int x)
85 return x * x;
88 int
89 s_sum(int x, int y)
91 return x + y;
94 void
95 s_square_out(int x, int *y)
97 *y = s_square(x);
100 void
101 s_square_ref(int *x)
103 *x = s_square(*x);
107 s_str_length(const char *s)
109 return strlen(s);
113 s_str_t_length(str_t s)
115 return strlen(s);
119 s_cstr_length(const char *s, int n)
121 int len = 0;
122 while (0 < n-- && *s++)
123 ++len;
124 return len;
128 s_dot_self(vector_t *v)
130 return s_square(v->x) + s_square(v->y) + s_square(v->z);
133 double
134 s_square_half(double x, double *y)
136 *y = x / 2.0;
137 return x * x;
140 float
141 s_square_half_float(float x, float *y)
143 *y = x / 2.0f;
144 return x * x;
147 LONG
148 s_square_half_long(LONG x, LONG *y)
150 *y = x / 2;
151 return x * x;
155 s_sum_fixed_array(int a[5])
157 return a[0] + a[1] + a[2] + a[3] + a[4];
161 s_pints_sum(pints_t *pints)
163 return *pints->pi + **pints->ppi + ***pints->pppi;
166 double
167 s_ptypes_sum(ptypes_t *pt)
169 return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
173 s_dot_pvectors(pvectors_t *p)
175 return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
179 s_sum_sp(sp_t *sp)
181 return sp->x + sp->s->x;
184 double
185 s_square_sun(sun_t *su)
187 switch (su->s)
189 case SUN_I: return su->u.i * su->u.i;
190 case SUN_F1:
191 case SUN_F2: return su->u.f * su->u.f;
192 case SUN_PI: return (*su->u.pi) * (*su->u.pi);
193 default:
194 return 0.0;
199 s_test_list_length(test_list_t *list)
201 return (list->t == TL_LIST
202 ? 1 + s_test_list_length(list->u.tail)
203 : 0);
207 s_sum_fixed_int_3d(int m[2][3][4])
209 int i, j, k;
210 int sum = 0;
212 for (i = 0; i < 2; ++i)
213 for (j = 0; j < 3; ++j)
214 for (k = 0; k < 4; ++k)
215 sum += m[i][j][k];
217 return sum;
221 s_sum_conf_array(int x[], int n)
223 int *p = x, *end = p + n;
224 int sum = 0;
226 while (p < end)
227 sum += *p++;
229 return sum;
233 s_sum_conf_ptr_by_conf_ptr(int n1, int *n2_then_x1, int *x2)
235 int i;
236 int sum = 0;
237 if(n1 == 0)
238 return 0;
240 for(i = 1; i < n1; ++i)
241 sum += n2_then_x1[i];
243 for(i = 0; i < *n2_then_x1; ++i)
244 sum += x2[i];
246 return sum;
250 s_sum_unique_conf_array(int x[], int n)
252 return s_sum_conf_array(x, n);
256 s_sum_unique_conf_ptr(int *x, int n)
258 return x ? s_sum_conf_array(x, n) : 0;
262 s_sum_var_array(int x[20], int n)
264 ok(0 <= n, "RPC sum_var_array\n");
265 ok(n <= 20, "RPC sum_var_array\n");
267 return s_sum_conf_array(x, n);
271 s_dot_two_vectors(vector_t vs[2])
273 return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
277 s_sum_cs(cs_t *cs)
279 return s_sum_conf_array(cs->ca, cs->n);
283 s_sum_cps(cps_t *cps)
285 int sum = 0;
286 int i;
288 for (i = 0; i < *cps->pn; ++i)
289 sum += cps->ca1[i];
291 for (i = 0; i < 2 * cps->n; ++i)
292 sum += cps->ca2[i];
294 return sum;
298 s_sum_cpsc(cpsc_t *cpsc)
300 int sum = 0;
301 int i;
302 for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
303 sum += cpsc->ca[i];
304 return sum;
308 s_square_puint(puint_t p)
310 int n = atoi(p);
311 return n * n;
315 s_sum_puints(puints_t *p)
317 int sum = 0;
318 int i;
319 for (i = 0; i < p->n; ++i)
320 sum += atoi(p->ps[i]);
321 return sum;
325 s_sum_cpuints(cpuints_t *p)
327 int sum = 0;
328 int i;
329 for (i = 0; i < p->n; ++i)
330 sum += atoi(p->ps[i]);
331 return sum;
335 s_dot_copy_vectors(vector_t u, vector_t v)
337 return u.x * v.x + u.y * v.y + u.z * v.z;
341 s_square_test_us(test_us_t *tus)
343 int n = atoi(tus->us.x);
344 return n * n;
347 double
348 s_square_encu(encu_t *eu)
350 switch (eu->t)
352 case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
353 case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
354 default:
355 return 0.0;
359 double
360 s_square_unencu(int t, unencu_t *eu)
362 switch (t)
364 case ENCU_I: return eu->i * eu->i;
365 case ENCU_F: return eu->f * eu->f;
366 default:
367 return 0.0;
371 void
372 s_check_se2(se_t *s)
374 ok(s->f == E2, "check_se2\n");
378 s_sum_parr(int *a[3])
380 return s_sum_pcarr(a, 3);
384 s_sum_pcarr(int *a[], int n)
386 int i, s = 0;
387 for (i = 0; i < n; ++i)
388 s += *a[i];
389 return s;
393 s_enum_ord(e_t e)
395 switch (e)
397 case E1: return 1;
398 case E2: return 2;
399 case E3: return 3;
400 case E4: return 4;
401 default:
402 return 0;
406 double
407 s_square_encue(encue_t *eue)
409 switch (eue->t)
411 case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
412 case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
413 default:
414 return 0.0;
419 s_sum_toplev_conf_2n(int *x, int n)
421 int sum = 0;
422 int i;
423 for (i = 0; i < 2 * n; ++i)
424 sum += x[i];
425 return sum;
429 s_sum_toplev_conf_cond(int *x, int a, int b, int c)
431 int sum = 0;
432 int n = c ? a : b;
433 int i;
434 for (i = 0; i < n; ++i)
435 sum += x[i];
436 return sum;
439 double
440 s_sum_aligns(aligns_t *a)
442 return a->c + a->i + a->s + a->d;
446 s_sum_padded(padded_t *p)
448 return p->i + p->c;
452 s_sum_padded2(padded_t ps[2])
454 return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
458 s_sum_padded_conf(padded_t *ps, int n)
460 int sum = 0;
461 int i;
462 for (i = 0; i < n; ++i)
463 sum += s_sum_padded(&ps[i]);
464 return sum;
468 s_sum_bogus(bogus_t *b)
470 return *b->h.p1 + *b->p2 + *b->p3 + b->c;
473 void
474 s_check_null(int *null)
476 ok(!null, "RPC check_null\n");
480 s_str_struct_len(str_struct_t *s)
482 return lstrlenA(s->s);
486 s_wstr_struct_len(wstr_struct_t *s)
488 return lstrlenW(s->s);
492 s_sum_doub_carr(doub_carr_t *dc)
494 int i, j;
495 int sum = 0;
496 for (i = 0; i < dc->n; ++i)
497 for (j = 0; j < dc->a[i]->n; ++j)
498 sum += dc->a[i]->a[j];
499 return sum;
502 void
503 s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
505 doub_carr_t *t;
506 int i, j;
507 t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
508 t->n = n;
509 for (i = 0; i < n; ++i)
511 int v = i + 1;
512 t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
513 t->a[i]->n = v;
514 for (j = 0; j < v; ++j)
515 t->a[i]->a[j] = j + 1;
517 *dc = t;
520 unsigned
521 s_hash_bstr(bstr_t b)
523 short n = b[-1];
524 short *s = b;
525 unsigned hash = 0;
526 short i;
527 for (i = 0; i < n; ++i)
528 hash = 5 * hash + (unsigned) s[i];
529 return hash;
532 void
533 s_get_name(name_t *name)
535 const char bossman[] = "Jeremy White";
536 memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
537 /* ensure nul-termination */
538 if (name->size < sizeof(bossman))
539 name->name[name->size - 1] = 0;
543 s_sum_pcarr2(int n, int **pa)
545 return s_sum_conf_array(*pa, n);
549 s_sum_L1_norms(int n, vector_t *vs)
551 int i;
552 int sum = 0;
553 for (i = 0; i < n; ++i)
554 sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
555 return sum;
558 s123_t *
559 s_get_s123(void)
561 s123_t *s = MIDL_user_allocate(sizeof *s);
562 s->f1 = 1;
563 s->f2 = 2;
564 s->f3 = 3;
565 return s;
568 str_t
569 s_get_filename(void)
571 return (char *)__FILE__;
574 void
575 s_context_handle_test(void)
577 NDR_SCONTEXT h;
578 RPC_BINDING_HANDLE binding;
579 RPC_STATUS status;
580 unsigned char buf[20];
581 static RPC_SERVER_INTERFACE server_if =
583 sizeof(RPC_SERVER_INTERFACE),
584 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
585 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
586 NULL,
594 binding = I_RpcGetCurrentCallHandle();
595 ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
597 if (!pNDRSContextMarshall2 || !pNDRSContextUnmarshall2)
599 win_skip("NDRSContextMarshall2 or NDRSContextUnmarshall2 not exported from rpcrt4.dll\n");
600 return;
603 h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
604 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
606 /* marshal a context handle with NULL userContext */
607 memset(buf, 0xcc, sizeof(buf));
608 pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
609 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
610 ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");
612 h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
613 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
615 /* marshal a context handle with non-NULL userContext */
616 memset(buf, 0xcc, sizeof(buf));
617 h->userContext = (void *)0xdeadbeef;
618 pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
619 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
620 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
622 /* raises ERROR_INVALID_HANDLE exception on Vista upwards */
623 if (0)
625 h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
626 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
627 ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
629 /* marshal a context handle with an interface specified */
630 h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
631 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
633 memset(buf, 0xcc, sizeof(buf));
634 h->userContext = (void *)0xcafebabe;
635 pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
636 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
637 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
639 h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
640 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
641 ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
644 /* test same interface data, but different pointer */
645 /* raises ERROR_INVALID_HANDLE exception */
646 if (0)
648 RPC_SERVER_INTERFACE server_if_clone = server_if;
650 pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
653 /* test different interface data, but different pointer */
654 /* raises ERROR_INVALID_HANDLE exception */
655 if (0)
657 static RPC_SERVER_INTERFACE server_if2 =
659 sizeof(RPC_SERVER_INTERFACE),
660 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
661 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
662 NULL,
669 pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
671 pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
675 void
676 s_get_numbers(int length, int size, pints_t n[])
678 int i;
679 for (i = 0; i < length; i++)
681 n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
682 *n[i].pi = i;
683 n[i].ppi = NULL;
684 n[i].pppi = NULL;
688 void
689 s_get_numbers_struct(numbers_struct_t **ns)
691 int i;
692 *ns = midl_user_allocate(FIELD_OFFSET(numbers_struct_t, numbers[5]));
693 if (!*ns) return;
694 (*ns)->length = 5;
695 (*ns)->size = 5;
696 for (i = 0; i < (*ns)->length; i++)
698 (*ns)->numbers[i].pi = NULL;
699 (*ns)->numbers[i].ppi = NULL;
700 (*ns)->numbers[i].pppi = NULL;
702 (*ns)->numbers[0].pi = midl_user_allocate(sizeof(*(*ns)->numbers[i].pi));
703 *(*ns)->numbers[0].pi = 5;
706 void
707 s_stop(void)
709 ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
710 ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
711 ok(SetEvent(stop_event), "SetEvent\n");
714 static void
715 make_cmdline(char buffer[MAX_PATH], const char *test)
717 sprintf(buffer, "%s server %s", progname, test);
720 static void
721 run_client(const char *test)
723 char cmdline[MAX_PATH];
724 PROCESS_INFORMATION info;
725 STARTUPINFOA startup;
727 memset(&startup, 0, sizeof startup);
728 startup.cb = sizeof startup;
730 make_cmdline(cmdline, test);
731 ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
732 winetest_wait_child_process( info.hProcess );
733 ok(CloseHandle(info.hProcess), "CloseHandle\n");
734 ok(CloseHandle(info.hThread), "CloseHandle\n");
737 static void
738 basic_tests(void)
740 char string[] = "I am a string";
741 WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
742 int f[5] = {1, 3, 0, -2, -4};
743 vector_t a = {1, 3, 7};
744 vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
745 pvectors_t pvecs = {&vec1, &pvec2};
746 sp_inner_t spi = {42};
747 sp_t sp = {-13, &spi};
748 aligns_t aligns;
749 pints_t pints;
750 ptypes_t ptypes;
751 padded_t padded;
752 padded_t padded2[2];
753 bogus_t bogus;
754 int i1, i2, i3, *pi2, *pi3, **ppi3;
755 double u, v;
756 float s, t;
757 LONG q, r;
758 short h;
759 char c;
760 int x;
761 str_struct_t ss = {string};
762 wstr_struct_t ws = {wstring};
763 str_t str;
764 se_t se;
766 ok(int_return() == INT_CODE, "RPC int_return\n");
768 ok(square(7) == 49, "RPC square\n");
769 ok(sum(23, -4) == 19, "RPC sum\n");
771 x = 0;
772 square_out(11, &x);
773 ok(x == 121, "RPC square_out\n");
775 x = 5;
776 square_ref(&x);
777 ok(x == 25, "RPC square_ref\n");
779 ok(str_length(string) == strlen(string), "RPC str_length\n");
780 ok(str_t_length(string) == strlen(string), "RPC str_length\n");
781 ok(dot_self(&a) == 59, "RPC dot_self\n");
783 ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
784 ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
786 v = 0.0;
787 u = square_half(3.0, &v);
788 ok(u == 9.0, "RPC square_half\n");
789 ok(v == 1.5, "RPC square_half\n");
791 t = 0.0f;
792 s = square_half_float(3.0f, &t);
793 ok(s == 9.0f, "RPC square_half_float\n");
794 ok(t == 1.5f, "RPC square_half_float\n");
796 r = 0;
797 q = square_half_long(3, &r);
798 ok(q == 9, "RPC square_half_long\n");
799 ok(r == 1, "RPC square_half_long\n");
801 i1 = 19;
802 i2 = -3;
803 i3 = -29;
804 pi2 = &i2;
805 pi3 = &i3;
806 ppi3 = &pi3;
807 pints.pi = &i1;
808 pints.ppi = &pi2;
809 pints.pppi = &ppi3;
810 ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
812 c = 10;
813 h = 3;
814 q = 14;
815 s = -5.0f;
816 u = 11.0;
817 ptypes.pc = &c;
818 ptypes.ps = &h;
819 ptypes.pl = &q;
820 ptypes.pf = &s;
821 ptypes.pd = &u;
822 ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
824 ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
825 ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
826 ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
827 ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
829 ok(enum_ord(E1) == 1, "RPC enum_ord\n");
830 ok(enum_ord(E2) == 2, "RPC enum_ord\n");
831 ok(enum_ord(E3) == 3, "RPC enum_ord\n");
832 ok(enum_ord(E4) == 4, "RPC enum_ord\n");
834 se.f = E2;
835 check_se2(&se);
837 memset(&aligns, 0, sizeof(aligns));
838 aligns.c = 3;
839 aligns.i = 4;
840 aligns.s = 5;
841 aligns.d = 6.0;
842 ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
844 padded.i = -3;
845 padded.c = 8;
846 ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
847 padded2[0].i = -5;
848 padded2[0].c = 1;
849 padded2[1].i = 3;
850 padded2[1].c = 7;
851 ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
852 padded2[0].i = -5;
853 padded2[0].c = 1;
854 padded2[1].i = 3;
855 padded2[1].c = 7;
856 ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
858 i1 = 14;
859 i2 = -7;
860 i3 = -4;
861 bogus.h.p1 = &i1;
862 bogus.p2 = &i2;
863 bogus.p3 = &i3;
864 bogus.c = 9;
865 ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
867 check_null(NULL);
869 str = get_filename();
870 ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
871 midl_user_free(str);
874 static void
875 union_tests(void)
877 encue_t eue;
878 encu_t eu;
879 unencu_t uneu;
880 sun_t su;
881 int i;
883 su.s = SUN_I;
884 su.u.i = 9;
885 ok(square_sun(&su) == 81.0, "RPC square_sun\n");
887 su.s = SUN_F1;
888 su.u.f = 5.0;
889 ok(square_sun(&su) == 25.0, "RPC square_sun\n");
891 su.s = SUN_F2;
892 su.u.f = -2.0;
893 ok(square_sun(&su) == 4.0, "RPC square_sun\n");
895 su.s = SUN_PI;
896 su.u.pi = &i;
897 i = 11;
898 ok(square_sun(&su) == 121.0, "RPC square_sun\n");
900 eu.t = ENCU_I;
901 eu.tagged_union.i = 7;
902 ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
904 eu.t = ENCU_F;
905 eu.tagged_union.f = 3.0;
906 ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
908 uneu.i = 4;
909 ok(square_unencu(ENCU_I, &uneu) == 16.0, "RPC square_unencu\n");
911 uneu.f = 5.0;
912 ok(square_unencu(ENCU_F, &uneu) == 25.0, "RPC square_unencu\n");
914 eue.t = E1;
915 eue.tagged_union.i1 = 8;
916 ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
918 eue.t = E2;
919 eue.tagged_union.f2 = 10.0;
920 ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
923 static test_list_t *
924 null_list(void)
926 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
927 n->t = TL_NULL;
928 n->u.x = 0;
929 return n;
932 static test_list_t *
933 make_list(test_list_t *tail)
935 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
936 n->t = TL_LIST;
937 n->u.tail = tail;
938 return n;
941 static void
942 free_list(test_list_t *list)
944 if (list->t == TL_LIST)
945 free_list(list->u.tail);
946 HeapFree(GetProcessHeap(), 0, list);
949 ULONG __RPC_USER
950 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
952 return start + sizeof(int);
955 unsigned char * __RPC_USER
956 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
958 int n = atoi(*p);
959 memcpy(buffer, &n, sizeof n);
960 return buffer + sizeof n;
963 unsigned char * __RPC_USER
964 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
966 int n;
967 memcpy(&n, buffer, sizeof n);
968 *p = HeapAlloc(GetProcessHeap(), 0, 10);
969 sprintf(*p, "%d", n);
970 return buffer + sizeof n;
973 void __RPC_USER
974 puint_t_UserFree(ULONG *flags, puint_t *p)
976 HeapFree(GetProcessHeap(), 0, *p);
979 ULONG __RPC_USER
980 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
982 return start + sizeof(struct wire_us);
985 unsigned char * __RPC_USER
986 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
988 struct wire_us wus;
989 wus.x = atoi(pus->x);
990 memcpy(buffer, &wus, sizeof wus);
991 return buffer + sizeof wus;
994 unsigned char * __RPC_USER
995 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
997 struct wire_us wus;
998 memcpy(&wus, buffer, sizeof wus);
999 pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
1000 sprintf(pus->x, "%d", wus.x);
1001 return buffer + sizeof wus;
1004 void __RPC_USER
1005 us_t_UserFree(ULONG *flags, us_t *pus)
1007 HeapFree(GetProcessHeap(), 0, pus->x);
1010 ULONG __RPC_USER
1011 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
1013 return start + FIELD_OFFSET(user_bstr_t, data[(*b)[-1]]);
1016 unsigned char * __RPC_USER
1017 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1019 wire_bstr_t wb = (wire_bstr_t) buffer;
1020 wb->n = (*b)[-1];
1021 memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
1022 return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1025 unsigned char * __RPC_USER
1026 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1028 wire_bstr_t wb = (wire_bstr_t) buffer;
1029 short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
1030 data[0] = wb->n;
1031 memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
1032 *b = &data[1];
1033 return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1036 void __RPC_USER
1037 bstr_t_UserFree(ULONG *flags, bstr_t *b)
1039 HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
1042 static void
1043 pointer_tests(void)
1045 int a[] = {1, 2, 3, 4};
1046 char p1[] = "11";
1047 test_list_t *list = make_list(make_list(make_list(null_list())));
1048 test_us_t tus = {{p1}};
1049 int *pa[4];
1050 puints_t pus;
1051 cpuints_t cpus;
1052 short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
1053 bstr_t bstr = &bstr_data[1];
1054 name_t name;
1055 void *buffer;
1056 int *pa2;
1057 s123_t *s123;
1059 ok(test_list_length(list) == 3, "RPC test_list_length\n");
1060 ok(square_puint(p1) == 121, "RPC square_puint\n");
1061 pus.n = 4;
1062 pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
1063 pus.ps[0] = xstrdup("5");
1064 pus.ps[1] = xstrdup("6");
1065 pus.ps[2] = xstrdup("7");
1066 pus.ps[3] = xstrdup("8");
1067 ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
1068 HeapFree(GetProcessHeap(), 0, pus.ps[0]);
1069 HeapFree(GetProcessHeap(), 0, pus.ps[1]);
1070 HeapFree(GetProcessHeap(), 0, pus.ps[2]);
1071 HeapFree(GetProcessHeap(), 0, pus.ps[3]);
1072 HeapFree(GetProcessHeap(), 0, pus.ps);
1073 cpus.n = 4;
1074 cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
1075 cpus.ps[0] = xstrdup("5");
1076 cpus.ps[1] = xstrdup("6");
1077 cpus.ps[2] = xstrdup("7");
1078 cpus.ps[3] = xstrdup("8");
1079 ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
1080 HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
1081 HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
1082 HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
1083 HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
1084 HeapFree(GetProcessHeap(), 0, cpus.ps);
1085 ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
1087 pa[0] = &a[0];
1088 pa[1] = &a[1];
1089 pa[2] = &a[2];
1090 ok(sum_parr(pa) == 6, "RPC sum_parr\n");
1092 pa[0] = &a[0];
1093 pa[1] = &a[1];
1094 pa[2] = &a[2];
1095 pa[3] = &a[3];
1096 ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
1098 ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
1100 free_list(list);
1102 if (!old_windows_version)
1104 name.size = 10;
1105 name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
1106 get_name(&name);
1107 ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
1108 ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
1109 HeapFree(GetProcessHeap(), 0, name.name);
1112 pa2 = a;
1113 ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
1115 s123 = get_s123();
1116 ok(s123->f1 == 1 && s123->f2 == 2 && s123->f3 == 3, "RPC get_s123\n");
1117 MIDL_user_free(s123);
1120 static int
1121 check_pyramid_doub_carr(doub_carr_t *dc)
1123 int i, j;
1124 for (i = 0; i < dc->n; ++i)
1125 for (j = 0; j < dc->a[i]->n; ++j)
1126 if (dc->a[i]->a[j] != j + 1)
1127 return FALSE;
1128 return TRUE;
1131 static void
1132 free_pyramid_doub_carr(doub_carr_t *dc)
1134 int i;
1135 for (i = 0; i < dc->n; ++i)
1136 MIDL_user_free(dc->a[i]);
1137 MIDL_user_free(dc);
1140 static void
1141 array_tests(void)
1143 int m[2][3][4] =
1145 {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
1146 {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
1148 int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1149 int c2[] = {10, 100, 200};
1150 vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1151 cps_t cps;
1152 cpsc_t cpsc;
1153 cs_t *cs;
1154 int n;
1155 int ca[5] = {1, -2, 3, -4, 5};
1156 doub_carr_t *dc;
1157 int *pi;
1158 pints_t api[5];
1159 numbers_struct_t *ns;
1161 if (!old_windows_version)
1163 const char str1[25] = "Hello";
1164 ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
1167 ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
1169 ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
1170 ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1171 ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1172 ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1174 ok(sum_conf_ptr_by_conf_ptr(1, c2, c) == 45, "RPC sum_conf_ptr_by_conf_ptr\n");
1175 ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 345, "RPC sum_conf_ptr_by_conf_ptr\n");
1176 c2[0] = 0;
1177 ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 300, "RPC sum_conf_ptr_by_conf_ptr\n");
1179 ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
1180 ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
1181 ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
1183 ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
1184 ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1185 ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1186 ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1188 ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1189 cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1190 cs->n = 5;
1191 cs->ca[0] = 3;
1192 cs->ca[1] = 5;
1193 cs->ca[2] = -2;
1194 cs->ca[3] = -1;
1195 cs->ca[4] = -4;
1196 ok(sum_cs(cs) == 1, "RPC sum_cs\n");
1197 HeapFree(GetProcessHeap(), 0, cs);
1199 n = 5;
1200 cps.pn = &n;
1201 cps.ca1 = &c[2];
1202 cps.n = 3;
1203 cps.ca2 = &c[3];
1204 ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1206 cpsc.a = 4;
1207 cpsc.b = 5;
1208 cpsc.c = 1;
1209 cpsc.ca = c;
1210 ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1211 cpsc.a = 4;
1212 cpsc.b = 5;
1213 cpsc.c = 0;
1214 cpsc.ca = c;
1215 ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1217 ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
1218 ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
1219 ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1221 dc = malloc(FIELD_OFFSET(doub_carr_t, a[2]));
1222 dc->n = 2;
1223 dc->a[0] = malloc(FIELD_OFFSET(doub_carr_1_t, a[3]));
1224 dc->a[0]->n = 3;
1225 dc->a[0]->a[0] = 5;
1226 dc->a[0]->a[1] = 1;
1227 dc->a[0]->a[2] = 8;
1228 dc->a[1] = malloc(FIELD_OFFSET(doub_carr_1_t, a[2]));
1229 dc->a[1]->n = 2;
1230 dc->a[1]->a[0] = 2;
1231 dc->a[1]->a[1] = 3;
1232 ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1233 free(dc->a[0]);
1234 free(dc->a[1]);
1235 free(dc);
1237 dc = NULL;
1238 make_pyramid_doub_carr(4, &dc);
1239 ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
1240 free_pyramid_doub_carr(dc);
1242 ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1244 memset(api, 0, sizeof(api));
1245 pi = HeapAlloc(GetProcessHeap(), 0, sizeof(*pi));
1246 *pi = -1;
1247 api[0].pi = pi;
1248 get_numbers(1, 1, api);
1249 ok(api[0].pi == pi, "RPC conformant varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1250 ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *api[0].pi);
1252 if (!old_windows_version)
1254 ns = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(numbers_struct_t, numbers[5]));
1255 ns->length = 5;
1256 ns->size = 5;
1257 ns->numbers[0].pi = pi;
1258 get_numbers_struct(&ns);
1259 ok(ns->numbers[0].pi == pi, "RPC conformant varying struct embedded pointer changed from %p to %p\n", pi, ns->numbers[0].pi);
1260 ok(*ns->numbers[0].pi == 5, "pi unmarshalled incorrectly %d\n", *ns->numbers[0].pi);
1261 HeapFree(GetProcessHeap(), 0, ns);
1263 HeapFree(GetProcessHeap(), 0, pi);
1266 static void
1267 run_tests(void)
1269 basic_tests();
1270 union_tests();
1271 pointer_tests();
1272 array_tests();
1273 context_handle_test();
1276 static void
1277 client(const char *test)
1279 if (strcmp(test, "tcp_basic") == 0)
1281 static unsigned char iptcp[] = "ncacn_ip_tcp";
1282 static unsigned char address[] = "127.0.0.1";
1283 static unsigned char port[] = PORT;
1284 unsigned char *binding;
1286 ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1287 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1289 run_tests();
1291 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1292 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1294 else if (strcmp(test, "np_basic") == 0)
1296 static unsigned char np[] = "ncacn_np";
1297 static unsigned char address[] = "\\\\.";
1298 static unsigned char pipe[] = PIPE;
1299 unsigned char *binding;
1301 ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1302 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1304 run_tests();
1305 stop();
1307 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1308 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1312 static void
1313 server(void)
1315 static unsigned char iptcp[] = "ncacn_ip_tcp";
1316 static unsigned char port[] = PORT;
1317 static unsigned char np[] = "ncacn_np";
1318 static unsigned char pipe[] = PIPE;
1319 RPC_STATUS status, iptcp_status, np_status;
1320 DWORD ret;
1322 iptcp_status = RpcServerUseProtseqEp(iptcp, 20, port, NULL);
1323 ok(iptcp_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_ip_tcp) failed with status %d\n", iptcp_status);
1324 np_status = RpcServerUseProtseqEp(np, 0, pipe, NULL);
1325 if (np_status == RPC_S_PROTSEQ_NOT_SUPPORTED)
1326 skip("Protocol sequence ncacn_np is not supported\n");
1327 else
1328 ok(np_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", np_status);
1330 if (pRpcServerRegisterIfEx)
1332 trace("Using RpcServerRegisterIfEx\n");
1333 status = pRpcServerRegisterIfEx(s_IServer_v0_0_s_ifspec, NULL, NULL,
1334 RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
1335 RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
1337 else
1338 status = RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL);
1339 ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);
1340 status = RpcServerListen(1, 20, TRUE);
1341 ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
1342 stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1343 ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
1345 if (iptcp_status == RPC_S_OK)
1346 run_client("tcp_basic");
1347 else
1348 skip("tcp_basic tests skipped due to earlier failure\n");
1350 if (np_status == RPC_S_OK)
1351 run_client("np_basic");
1352 else
1354 skip("np_basic tests skipped due to earlier failure\n");
1355 /* np client is what signals stop_event, so bail out if we didn't run do it */
1356 return;
1359 ret = WaitForSingleObject(stop_event, 1000);
1360 ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
1361 /* if the stop event didn't fire then RpcMgmtWaitServerListen will wait
1362 * forever, so don't bother calling it in this case */
1363 if (ret == WAIT_OBJECT_0)
1365 status = RpcMgmtWaitServerListen();
1366 todo_wine {
1367 ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);
1372 START_TEST(server)
1374 int argc;
1375 char **argv;
1377 InitFunctionPointers();
1379 argc = winetest_get_mainargs(&argv);
1380 progname = argv[0];
1382 if (argc == 3)
1384 RpcTryExcept
1386 client(argv[2]);
1388 RpcExcept(TRUE)
1390 trace("Exception %d\n", RpcExceptionCode());
1392 RpcEndExcept
1394 else
1395 server();