push c6fcfc519a04d046be60ec60e33d075a2146cc03
[wine/hacks.git] / dlls / rpcrt4 / tests / server.c
blob9f39f01115da4ef339dc08ac069bf5b3bfea3832
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;
37 static HANDLE stop_event;
39 static void (WINAPI *pNDRSContextMarshall2)(RPC_BINDING_HANDLE, NDR_SCONTEXT, void*, NDR_RUNDOWN, void*, ULONG);
40 static NDR_SCONTEXT (WINAPI *pNDRSContextUnmarshall2)(RPC_BINDING_HANDLE, void*, ULONG, void*, ULONG);
41 static RPC_STATUS (WINAPI *pRpcServerRegisterIfEx)(RPC_IF_HANDLE,UUID*, RPC_MGR_EPV*, unsigned int,
42 unsigned int,RPC_IF_CALLBACK_FN*);
44 static void InitFunctionPointers(void)
46 HMODULE hrpcrt4 = GetModuleHandleA("rpcrt4.dll");
48 pNDRSContextMarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextMarshall2");
49 pNDRSContextUnmarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextUnmarshall2");
50 pRpcServerRegisterIfEx = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterIfEx");
53 void __RPC_FAR *__RPC_USER
54 midl_user_allocate(size_t n)
56 return malloc(n);
59 void __RPC_USER
60 midl_user_free(void __RPC_FAR *p)
62 free(p);
65 static char *
66 xstrdup(const char *s)
68 char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
69 strcpy(d, s);
70 return d;
73 int
74 s_int_return(void)
76 return INT_CODE;
79 int
80 s_square(int x)
82 return x * x;
85 int
86 s_sum(int x, int y)
88 return x + y;
91 void
92 s_square_out(int x, int *y)
94 *y = s_square(x);
97 void
98 s_square_ref(int *x)
100 *x = s_square(*x);
104 s_str_length(const char *s)
106 return strlen(s);
110 s_str_t_length(str_t s)
112 return strlen(s);
116 s_cstr_length(const char *s, int n)
118 int len = 0;
119 while (0 < n-- && *s++)
120 ++len;
121 return len;
125 s_dot_self(vector_t *v)
127 return s_square(v->x) + s_square(v->y) + s_square(v->z);
130 double
131 s_square_half(double x, double *y)
133 *y = x / 2.0;
134 return x * x;
137 float
138 s_square_half_float(float x, float *y)
140 *y = x / 2.0f;
141 return x * x;
144 long
145 s_square_half_long(long x, long *y)
147 *y = x / 2;
148 return x * x;
152 s_sum_fixed_array(int a[5])
154 return a[0] + a[1] + a[2] + a[3] + a[4];
158 s_pints_sum(pints_t *pints)
160 return *pints->pi + **pints->ppi + ***pints->pppi;
163 double
164 s_ptypes_sum(ptypes_t *pt)
166 return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
170 s_dot_pvectors(pvectors_t *p)
172 return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
176 s_sum_sp(sp_t *sp)
178 return sp->x + sp->s->x;
181 double
182 s_square_sun(sun_t *su)
184 switch (su->s)
186 case SUN_I: return su->u.i * su->u.i;
187 case SUN_F1:
188 case SUN_F2: return su->u.f * su->u.f;
189 case SUN_PI: return (*su->u.pi) * (*su->u.pi);
190 default:
191 return 0.0;
196 s_test_list_length(test_list_t *list)
198 return (list->t == TL_LIST
199 ? 1 + s_test_list_length(list->u.tail)
200 : 0);
204 s_sum_fixed_int_3d(int m[2][3][4])
206 int i, j, k;
207 int sum = 0;
209 for (i = 0; i < 2; ++i)
210 for (j = 0; j < 3; ++j)
211 for (k = 0; k < 4; ++k)
212 sum += m[i][j][k];
214 return sum;
218 s_sum_conf_array(int x[], int n)
220 int *p = x, *end = p + n;
221 int sum = 0;
223 while (p < end)
224 sum += *p++;
226 return sum;
230 s_sum_conf_ptr_by_conf_ptr(int n1, int *n2_then_x1, int *x2)
232 int i;
233 int sum = 0;
234 if(n1 == 0)
235 return 0;
237 for(i = 1; i < n1; ++i)
238 sum += n2_then_x1[i];
240 for(i = 0; i < *n2_then_x1; ++i)
241 sum += x2[i];
243 return sum;
247 s_sum_unique_conf_array(int x[], int n)
249 return s_sum_conf_array(x, n);
253 s_sum_unique_conf_ptr(int *x, int n)
255 return x ? s_sum_conf_array(x, n) : 0;
259 s_sum_var_array(int x[20], int n)
261 ok(0 <= n, "RPC sum_var_array\n");
262 ok(n <= 20, "RPC sum_var_array\n");
264 return s_sum_conf_array(x, n);
268 s_dot_two_vectors(vector_t vs[2])
270 return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
274 s_sum_cs(cs_t *cs)
276 return s_sum_conf_array(cs->ca, cs->n);
280 s_sum_cps(cps_t *cps)
282 int sum = 0;
283 int i;
285 for (i = 0; i < *cps->pn; ++i)
286 sum += cps->ca1[i];
288 for (i = 0; i < 2 * cps->n; ++i)
289 sum += cps->ca2[i];
291 return sum;
295 s_sum_cpsc(cpsc_t *cpsc)
297 int sum = 0;
298 int i;
299 for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
300 sum += cpsc->ca[i];
301 return sum;
305 s_square_puint(puint_t p)
307 int n = atoi(p);
308 return n * n;
312 s_sum_puints(puints_t *p)
314 int sum = 0;
315 int i;
316 for (i = 0; i < p->n; ++i)
317 sum += atoi(p->ps[i]);
318 return sum;
322 s_sum_cpuints(cpuints_t *p)
324 int sum = 0;
325 int i;
326 for (i = 0; i < p->n; ++i)
327 sum += atoi(p->ps[i]);
328 return sum;
332 s_dot_copy_vectors(vector_t u, vector_t v)
334 return u.x * v.x + u.y * v.y + u.z * v.z;
338 s_square_test_us(test_us_t *tus)
340 int n = atoi(tus->us.x);
341 return n * n;
344 double
345 s_square_encu(encu_t *eu)
347 switch (eu->t)
349 case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
350 case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
351 default:
352 return 0.0;
356 double
357 s_square_unencu(int t, unencu_t *eu)
359 switch (t)
361 case ENCU_I: return eu->i * eu->i;
362 case ENCU_F: return eu->f * eu->f;
363 default:
364 return 0.0;
368 void
369 s_check_se2(se_t *s)
371 ok(s->f == E2, "check_se2\n");
375 s_sum_parr(int *a[3])
377 return s_sum_pcarr(a, 3);
381 s_sum_pcarr(int *a[], int n)
383 int i, s = 0;
384 for (i = 0; i < n; ++i)
385 s += *a[i];
386 return s;
390 s_enum_ord(e_t e)
392 switch (e)
394 case E1: return 1;
395 case E2: return 2;
396 case E3: return 3;
397 case E4: return 4;
398 default:
399 return 0;
403 double
404 s_square_encue(encue_t *eue)
406 switch (eue->t)
408 case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
409 case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
410 default:
411 return 0.0;
416 s_sum_toplev_conf_2n(int *x, int n)
418 int sum = 0;
419 int i;
420 for (i = 0; i < 2 * n; ++i)
421 sum += x[i];
422 return sum;
426 s_sum_toplev_conf_cond(int *x, int a, int b, int c)
428 int sum = 0;
429 int n = c ? a : b;
430 int i;
431 for (i = 0; i < n; ++i)
432 sum += x[i];
433 return sum;
436 double
437 s_sum_aligns(aligns_t *a)
439 return a->c + a->i + a->s + a->d;
443 s_sum_padded(padded_t *p)
445 return p->i + p->c;
449 s_sum_padded2(padded_t ps[2])
451 return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
455 s_sum_padded_conf(padded_t *ps, int n)
457 int sum = 0;
458 int i;
459 for (i = 0; i < n; ++i)
460 sum += s_sum_padded(&ps[i]);
461 return sum;
465 s_sum_bogus(bogus_t *b)
467 return *b->h.p1 + *b->p2 + *b->p3 + b->c;
470 void
471 s_check_null(int *null)
473 ok(!null, "RPC check_null\n");
477 s_str_struct_len(str_struct_t *s)
479 return lstrlenA(s->s);
483 s_wstr_struct_len(wstr_struct_t *s)
485 return lstrlenW(s->s);
489 s_sum_doub_carr(doub_carr_t *dc)
491 int i, j;
492 int sum = 0;
493 for (i = 0; i < dc->n; ++i)
494 for (j = 0; j < dc->a[i]->n; ++j)
495 sum += dc->a[i]->a[j];
496 return sum;
499 void
500 s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
502 doub_carr_t *t;
503 int i, j;
504 t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
505 t->n = n;
506 for (i = 0; i < n; ++i)
508 int v = i + 1;
509 t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
510 t->a[i]->n = v;
511 for (j = 0; j < v; ++j)
512 t->a[i]->a[j] = j + 1;
514 *dc = t;
517 unsigned
518 s_hash_bstr(bstr_t b)
520 short n = b[-1];
521 short *s = b;
522 unsigned hash = 0;
523 short i;
524 for (i = 0; i < n; ++i)
525 hash = 5 * hash + (unsigned) s[i];
526 return hash;
529 void
530 s_get_name(name_t *name)
532 const char bossman[] = "Jeremy White";
533 memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
534 /* ensure nul-termination */
535 if (name->size < sizeof(bossman))
536 name->name[name->size - 1] = 0;
540 s_sum_pcarr2(int n, int **pa)
542 return s_sum_conf_array(*pa, n);
546 s_sum_L1_norms(int n, vector_t *vs)
548 int i;
549 int sum = 0;
550 for (i = 0; i < n; ++i)
551 sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
552 return sum;
555 s123_t *
556 s_get_s123(void)
558 s123_t *s = MIDL_user_allocate(sizeof *s);
559 s->f1 = 1;
560 s->f2 = 2;
561 s->f3 = 3;
562 return s;
565 str_t
566 s_get_filename(void)
568 return (char *)__FILE__;
571 void
572 s_context_handle_test(void)
574 NDR_SCONTEXT h;
575 RPC_BINDING_HANDLE binding;
576 RPC_STATUS status;
577 unsigned char buf[20];
578 static RPC_SERVER_INTERFACE server_if =
580 sizeof(RPC_SERVER_INTERFACE),
581 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
582 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
583 NULL,
591 binding = I_RpcGetCurrentCallHandle();
592 ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
594 h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
595 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
597 /* marshal a context handle with NULL userContext */
598 memset(buf, 0xcc, sizeof(buf));
599 pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
600 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
601 ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");
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 non-NULL userContext */
607 memset(buf, 0xcc, sizeof(buf));
608 h->userContext = (void *)0xdeadbeef;
609 pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
610 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
611 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
613 h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
614 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
615 ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
617 /* marshal a context handle with an interface specified */
618 h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
619 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
621 memset(buf, 0xcc, sizeof(buf));
622 h->userContext = (void *)0xcafebabe;
623 pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
624 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
625 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
627 h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
628 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
629 ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
631 /* test same interface data, but different pointer */
632 /* raises ERROR_INVALID_HANDLE exception */
633 if (0)
635 RPC_SERVER_INTERFACE server_if_clone = server_if;
637 pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
640 /* test different interface data, but different pointer */
641 /* raises ERROR_INVALID_HANDLE exception */
642 if (0)
644 static RPC_SERVER_INTERFACE server_if2 =
646 sizeof(RPC_SERVER_INTERFACE),
647 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
648 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
649 NULL,
656 pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
658 pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
662 void
663 s_get_numbers(int length, int size, pints_t n[])
665 int i;
666 for (i = 0; i < length; i++)
668 n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
669 *n[i].pi = i;
670 n[i].ppi = NULL;
671 n[i].pppi = NULL;
675 void
676 s_get_numbers_struct(numbers_struct_t **ns)
678 int i;
679 *ns = midl_user_allocate(FIELD_OFFSET(numbers_struct_t, numbers[5]));
680 if (!*ns) return;
681 (*ns)->length = 5;
682 (*ns)->size = 5;
683 for (i = 0; i < (*ns)->length; i++)
685 (*ns)->numbers[i].pi = NULL;
686 (*ns)->numbers[i].ppi = NULL;
687 (*ns)->numbers[i].pppi = NULL;
689 (*ns)->numbers[0].pi = midl_user_allocate(sizeof(*(*ns)->numbers[i].pi));
690 *(*ns)->numbers[0].pi = 5;
693 void
694 s_stop(void)
696 ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
697 ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
698 ok(SetEvent(stop_event), "SetEvent\n");
701 static void
702 make_cmdline(char buffer[MAX_PATH], const char *test)
704 sprintf(buffer, "%s server %s", progname, test);
707 static void
708 run_client(const char *test)
710 char cmdline[MAX_PATH];
711 PROCESS_INFORMATION info;
712 STARTUPINFOA startup;
714 memset(&startup, 0, sizeof startup);
715 startup.cb = sizeof startup;
717 make_cmdline(cmdline, test);
718 ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
719 winetest_wait_child_process( info.hProcess );
720 ok(CloseHandle(info.hProcess), "CloseHandle\n");
721 ok(CloseHandle(info.hThread), "CloseHandle\n");
724 static void
725 basic_tests(void)
727 char string[] = "I am a string";
728 WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
729 int f[5] = {1, 3, 0, -2, -4};
730 vector_t a = {1, 3, 7};
731 vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
732 pvectors_t pvecs = {&vec1, &pvec2};
733 sp_inner_t spi = {42};
734 sp_t sp = {-13, &spi};
735 aligns_t aligns;
736 pints_t pints;
737 ptypes_t ptypes;
738 padded_t padded;
739 padded_t padded2[2];
740 bogus_t bogus;
741 int i1, i2, i3, *pi2, *pi3, **ppi3;
742 double u, v;
743 float s, t;
744 long q, r;
745 short h;
746 char c;
747 int x;
748 str_struct_t ss = {string};
749 wstr_struct_t ws = {wstring};
750 str_t str;
751 se_t se;
753 ok(int_return() == INT_CODE, "RPC int_return\n");
755 ok(square(7) == 49, "RPC square\n");
756 ok(sum(23, -4) == 19, "RPC sum\n");
758 x = 0;
759 square_out(11, &x);
760 ok(x == 121, "RPC square_out\n");
762 x = 5;
763 square_ref(&x);
764 ok(x == 25, "RPC square_ref\n");
766 ok(str_length(string) == strlen(string), "RPC str_length\n");
767 ok(str_t_length(string) == strlen(string), "RPC str_length\n");
768 ok(dot_self(&a) == 59, "RPC dot_self\n");
770 ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
771 ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
773 v = 0.0;
774 u = square_half(3.0, &v);
775 ok(u == 9.0, "RPC square_half\n");
776 ok(v == 1.5, "RPC square_half\n");
778 t = 0.0f;
779 s = square_half_float(3.0f, &t);
780 ok(s == 9.0f, "RPC square_half_float\n");
781 ok(t == 1.5f, "RPC square_half_float\n");
783 r = 0;
784 q = square_half_long(3, &r);
785 ok(q == 9, "RPC square_half_long\n");
786 ok(r == 1, "RPC square_half_long\n");
788 i1 = 19;
789 i2 = -3;
790 i3 = -29;
791 pi2 = &i2;
792 pi3 = &i3;
793 ppi3 = &pi3;
794 pints.pi = &i1;
795 pints.ppi = &pi2;
796 pints.pppi = &ppi3;
797 ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
799 c = 10;
800 h = 3;
801 q = 14;
802 s = -5.0f;
803 u = 11.0;
804 ptypes.pc = &c;
805 ptypes.ps = &h;
806 ptypes.pl = &q;
807 ptypes.pf = &s;
808 ptypes.pd = &u;
809 ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
811 ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
812 ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
813 ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
814 ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
816 ok(enum_ord(E1) == 1, "RPC enum_ord\n");
817 ok(enum_ord(E2) == 2, "RPC enum_ord\n");
818 ok(enum_ord(E3) == 3, "RPC enum_ord\n");
819 ok(enum_ord(E4) == 4, "RPC enum_ord\n");
821 se.f = E2;
822 check_se2(&se);
824 memset(&aligns, 0, sizeof(aligns));
825 aligns.c = 3;
826 aligns.i = 4;
827 aligns.s = 5;
828 aligns.d = 6.0;
829 ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
831 padded.i = -3;
832 padded.c = 8;
833 ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
834 padded2[0].i = -5;
835 padded2[0].c = 1;
836 padded2[1].i = 3;
837 padded2[1].c = 7;
838 ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
839 padded2[0].i = -5;
840 padded2[0].c = 1;
841 padded2[1].i = 3;
842 padded2[1].c = 7;
843 ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
845 i1 = 14;
846 i2 = -7;
847 i3 = -4;
848 bogus.h.p1 = &i1;
849 bogus.p2 = &i2;
850 bogus.p3 = &i3;
851 bogus.c = 9;
852 ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
854 check_null(NULL);
856 str = get_filename();
857 ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
858 midl_user_free(str);
861 static void
862 union_tests(void)
864 encue_t eue;
865 encu_t eu;
866 unencu_t uneu;
867 sun_t su;
868 int i;
870 su.s = SUN_I;
871 su.u.i = 9;
872 ok(square_sun(&su) == 81.0, "RPC square_sun\n");
874 su.s = SUN_F1;
875 su.u.f = 5.0;
876 ok(square_sun(&su) == 25.0, "RPC square_sun\n");
878 su.s = SUN_F2;
879 su.u.f = -2.0;
880 ok(square_sun(&su) == 4.0, "RPC square_sun\n");
882 su.s = SUN_PI;
883 su.u.pi = &i;
884 i = 11;
885 ok(square_sun(&su) == 121.0, "RPC square_sun\n");
887 eu.t = ENCU_I;
888 eu.tagged_union.i = 7;
889 ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
891 eu.t = ENCU_F;
892 eu.tagged_union.f = 3.0;
893 ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
895 uneu.i = 4;
896 ok(square_unencu(ENCU_I, &uneu) == 16.0, "RPC square_unencu\n");
898 uneu.f = 5.0;
899 ok(square_unencu(ENCU_F, &uneu) == 25.0, "RPC square_unencu\n");
901 eue.t = E1;
902 eue.tagged_union.i1 = 8;
903 ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
905 eue.t = E2;
906 eue.tagged_union.f2 = 10.0;
907 ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
910 static test_list_t *
911 null_list(void)
913 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
914 n->t = TL_NULL;
915 n->u.x = 0;
916 return n;
919 static test_list_t *
920 make_list(test_list_t *tail)
922 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
923 n->t = TL_LIST;
924 n->u.tail = tail;
925 return n;
928 static void
929 free_list(test_list_t *list)
931 if (list->t == TL_LIST)
932 free_list(list->u.tail);
933 HeapFree(GetProcessHeap(), 0, list);
936 ULONG __RPC_USER
937 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
939 return start + sizeof(int);
942 unsigned char * __RPC_USER
943 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
945 int n = atoi(*p);
946 memcpy(buffer, &n, sizeof n);
947 return buffer + sizeof n;
950 unsigned char * __RPC_USER
951 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
953 int n;
954 memcpy(&n, buffer, sizeof n);
955 *p = HeapAlloc(GetProcessHeap(), 0, 10);
956 sprintf(*p, "%d", n);
957 return buffer + sizeof n;
960 void __RPC_USER
961 puint_t_UserFree(ULONG *flags, puint_t *p)
963 HeapFree(GetProcessHeap(), 0, *p);
966 ULONG __RPC_USER
967 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
969 return start + sizeof(struct wire_us);
972 unsigned char * __RPC_USER
973 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
975 struct wire_us wus;
976 wus.x = atoi(pus->x);
977 memcpy(buffer, &wus, sizeof wus);
978 return buffer + sizeof wus;
981 unsigned char * __RPC_USER
982 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
984 struct wire_us wus;
985 memcpy(&wus, buffer, sizeof wus);
986 pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
987 sprintf(pus->x, "%d", wus.x);
988 return buffer + sizeof wus;
991 void __RPC_USER
992 us_t_UserFree(ULONG *flags, us_t *pus)
994 HeapFree(GetProcessHeap(), 0, pus->x);
997 ULONG __RPC_USER
998 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
1000 return start + FIELD_OFFSET(user_bstr_t, data[(*b)[-1]]);
1003 unsigned char * __RPC_USER
1004 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1006 wire_bstr_t wb = (wire_bstr_t) buffer;
1007 wb->n = (*b)[-1];
1008 memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
1009 return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1012 unsigned char * __RPC_USER
1013 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1015 wire_bstr_t wb = (wire_bstr_t) buffer;
1016 short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
1017 data[0] = wb->n;
1018 memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
1019 *b = &data[1];
1020 return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1023 void __RPC_USER
1024 bstr_t_UserFree(ULONG *flags, bstr_t *b)
1026 HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
1029 static void
1030 pointer_tests(void)
1032 int a[] = {1, 2, 3, 4};
1033 char p1[] = "11";
1034 test_list_t *list = make_list(make_list(make_list(null_list())));
1035 test_us_t tus = {{p1}};
1036 int *pa[4];
1037 puints_t pus;
1038 cpuints_t cpus;
1039 short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
1040 bstr_t bstr = &bstr_data[1];
1041 name_t name;
1042 void *buffer;
1043 int *pa2;
1044 s123_t *s123;
1046 ok(test_list_length(list) == 3, "RPC test_list_length\n");
1047 ok(square_puint(p1) == 121, "RPC square_puint\n");
1048 pus.n = 4;
1049 pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
1050 pus.ps[0] = xstrdup("5");
1051 pus.ps[1] = xstrdup("6");
1052 pus.ps[2] = xstrdup("7");
1053 pus.ps[3] = xstrdup("8");
1054 ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
1055 HeapFree(GetProcessHeap(), 0, pus.ps[0]);
1056 HeapFree(GetProcessHeap(), 0, pus.ps[1]);
1057 HeapFree(GetProcessHeap(), 0, pus.ps[2]);
1058 HeapFree(GetProcessHeap(), 0, pus.ps[3]);
1059 HeapFree(GetProcessHeap(), 0, pus.ps);
1060 cpus.n = 4;
1061 cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
1062 cpus.ps[0] = xstrdup("5");
1063 cpus.ps[1] = xstrdup("6");
1064 cpus.ps[2] = xstrdup("7");
1065 cpus.ps[3] = xstrdup("8");
1066 ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
1067 HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
1068 HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
1069 HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
1070 HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
1071 HeapFree(GetProcessHeap(), 0, cpus.ps);
1072 ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
1074 pa[0] = &a[0];
1075 pa[1] = &a[1];
1076 pa[2] = &a[2];
1077 ok(sum_parr(pa) == 6, "RPC sum_parr\n");
1079 pa[0] = &a[0];
1080 pa[1] = &a[1];
1081 pa[2] = &a[2];
1082 pa[3] = &a[3];
1083 ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
1085 ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
1087 free_list(list);
1089 name.size = 10;
1090 name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
1091 get_name(&name);
1092 ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
1093 ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
1094 HeapFree(GetProcessHeap(), 0, name.name);
1096 pa2 = a;
1097 ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
1099 s123 = get_s123();
1100 ok(s123->f1 == 1 && s123->f2 == 2 && s123->f3 == 3, "RPC get_s123\n");
1101 MIDL_user_free(s123);
1104 static int
1105 check_pyramid_doub_carr(doub_carr_t *dc)
1107 int i, j;
1108 for (i = 0; i < dc->n; ++i)
1109 for (j = 0; j < dc->a[i]->n; ++j)
1110 if (dc->a[i]->a[j] != j + 1)
1111 return FALSE;
1112 return TRUE;
1115 static void
1116 free_pyramid_doub_carr(doub_carr_t *dc)
1118 int i;
1119 for (i = 0; i < dc->n; ++i)
1120 MIDL_user_free(dc->a[i]);
1121 MIDL_user_free(dc);
1124 static void
1125 array_tests(void)
1127 const char str1[25] = "Hello";
1128 int m[2][3][4] =
1130 {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
1131 {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
1133 int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1134 int c2[] = {10, 100, 200};
1135 vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1136 cps_t cps;
1137 cpsc_t cpsc;
1138 cs_t *cs;
1139 int n;
1140 int ca[5] = {1, -2, 3, -4, 5};
1141 doub_carr_t *dc;
1142 int *pi;
1143 pints_t api[5];
1144 numbers_struct_t *ns;
1146 ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
1148 ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
1150 ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
1151 ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1152 ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1153 ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1155 ok(sum_conf_ptr_by_conf_ptr(1, c2, c) == 45, "RPC sum_conf_ptr_by_conf_ptr\n");
1156 ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 345, "RPC sum_conf_ptr_by_conf_ptr\n");
1157 c2[0] = 0;
1158 ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 300, "RPC sum_conf_ptr_by_conf_ptr\n");
1160 ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
1161 ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
1162 ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
1164 ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
1165 ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1166 ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1167 ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1169 ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1170 cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1171 cs->n = 5;
1172 cs->ca[0] = 3;
1173 cs->ca[1] = 5;
1174 cs->ca[2] = -2;
1175 cs->ca[3] = -1;
1176 cs->ca[4] = -4;
1177 ok(sum_cs(cs) == 1, "RPC sum_cs\n");
1178 HeapFree(GetProcessHeap(), 0, cs);
1180 n = 5;
1181 cps.pn = &n;
1182 cps.ca1 = &c[2];
1183 cps.n = 3;
1184 cps.ca2 = &c[3];
1185 ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1187 cpsc.a = 4;
1188 cpsc.b = 5;
1189 cpsc.c = 1;
1190 cpsc.ca = c;
1191 ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1192 cpsc.a = 4;
1193 cpsc.b = 5;
1194 cpsc.c = 0;
1195 cpsc.ca = c;
1196 ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1198 ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
1199 ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
1200 ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1202 dc = malloc(FIELD_OFFSET(doub_carr_t, a[2]));
1203 dc->n = 2;
1204 dc->a[0] = malloc(FIELD_OFFSET(doub_carr_1_t, a[3]));
1205 dc->a[0]->n = 3;
1206 dc->a[0]->a[0] = 5;
1207 dc->a[0]->a[1] = 1;
1208 dc->a[0]->a[2] = 8;
1209 dc->a[1] = malloc(FIELD_OFFSET(doub_carr_1_t, a[2]));
1210 dc->a[1]->n = 2;
1211 dc->a[1]->a[0] = 2;
1212 dc->a[1]->a[1] = 3;
1213 ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1214 free(dc->a[0]);
1215 free(dc->a[1]);
1216 free(dc);
1218 dc = NULL;
1219 make_pyramid_doub_carr(4, &dc);
1220 ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
1221 free_pyramid_doub_carr(dc);
1223 ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1225 memset(api, 0, sizeof(api));
1226 pi = HeapAlloc(GetProcessHeap(), 0, sizeof(*pi));
1227 *pi = -1;
1228 api[0].pi = pi;
1229 get_numbers(1, 1, api);
1230 ok(api[0].pi == pi, "RPC conformant varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1231 ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *api[0].pi);
1233 ns = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(numbers_struct_t, numbers[5]));
1234 ns->length = 5;
1235 ns->size = 5;
1236 ns->numbers[0].pi = pi;
1237 get_numbers_struct(&ns);
1238 ok(ns->numbers[0].pi == pi, "RPC conformant varying struct embedded pointer changed from %p to %p\n", pi, ns->numbers[0].pi);
1239 ok(*ns->numbers[0].pi == 5, "pi unmarshalled incorrectly %d\n", *ns->numbers[0].pi);
1241 HeapFree(GetProcessHeap(), 0, ns);
1242 HeapFree(GetProcessHeap(), 0, pi);
1245 static void
1246 run_tests(void)
1248 basic_tests();
1249 union_tests();
1250 pointer_tests();
1251 array_tests();
1252 context_handle_test();
1255 static void
1256 client(const char *test)
1258 if (strcmp(test, "tcp_basic") == 0)
1260 static unsigned char iptcp[] = "ncacn_ip_tcp";
1261 static unsigned char address[] = "127.0.0.1";
1262 static unsigned char port[] = PORT;
1263 unsigned char *binding;
1265 ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1266 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1268 run_tests();
1270 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1271 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1273 else if (strcmp(test, "np_basic") == 0)
1275 static unsigned char np[] = "ncacn_np";
1276 static unsigned char address[] = "\\\\.";
1277 static unsigned char pipe[] = PIPE;
1278 unsigned char *binding;
1280 ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1281 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1283 run_tests();
1284 stop();
1286 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1287 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1291 static void
1292 server(void)
1294 static unsigned char iptcp[] = "ncacn_ip_tcp";
1295 static unsigned char port[] = PORT;
1296 static unsigned char np[] = "ncacn_np";
1297 static unsigned char pipe[] = PIPE;
1298 RPC_STATUS status, iptcp_status, np_status;
1299 DWORD ret;
1301 iptcp_status = RpcServerUseProtseqEp(iptcp, 20, port, NULL);
1302 ok(iptcp_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_ip_tcp) failed with status %d\n", iptcp_status);
1303 np_status = RpcServerUseProtseqEp(np, 0, pipe, NULL);
1304 if (np_status == RPC_S_PROTSEQ_NOT_SUPPORTED)
1305 skip("Protocol sequence ncacn_np is not supported\n");
1306 else
1307 ok(np_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", np_status);
1309 if (pRpcServerRegisterIfEx)
1311 trace("Using RpcServerRegisterIfEx\n");
1312 status = pRpcServerRegisterIfEx(s_IServer_v0_0_s_ifspec, NULL, NULL,
1313 RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
1314 RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
1316 else
1317 status = RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL);
1318 ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);
1319 status = RpcServerListen(1, 20, TRUE);
1320 ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
1321 stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1322 ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
1324 if (iptcp_status == RPC_S_OK)
1325 run_client("tcp_basic");
1326 else
1327 skip("tcp_basic tests skipped due to earlier failure\n");
1329 if (np_status == RPC_S_OK)
1330 run_client("np_basic");
1331 else
1333 skip("np_basic tests skipped due to earlier failure\n");
1334 /* np client is what signals stop_event, so bail out if we didn't run do it */
1335 return;
1338 ret = WaitForSingleObject(stop_event, 1000);
1339 ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
1340 /* if the stop event didn't fire then RpcMgmtWaitServerListen will wait
1341 * forever, so don't bother calling it in this case */
1342 if (ret == WAIT_OBJECT_0)
1344 status = RpcMgmtWaitServerListen();
1345 todo_wine {
1346 ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);
1351 START_TEST(server)
1353 int argc;
1354 char **argv;
1356 InitFunctionPointers();
1358 argc = winetest_get_mainargs(&argv);
1359 progname = argv[0];
1361 if (argc == 3)
1363 RpcTryExcept
1365 client(argv[2]);
1367 RpcExcept(TRUE)
1369 trace("Exception %d\n", RpcExceptionCode());
1371 RpcEndExcept
1373 else
1374 server();