rpcrt4: Add tests for low-level context handle functions.
[wine/dcerpc.git] / dlls / rpcrt4 / tests / server.c
blobc8acab966054e6694856f94be77ee1529a16f4ae
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 void __RPC_FAR *__RPC_USER
40 midl_user_allocate(size_t n)
42 return malloc(n);
45 void __RPC_USER
46 midl_user_free(void __RPC_FAR *p)
48 free(p);
51 static char *
52 xstrdup(const char *s)
54 char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
55 strcpy(d, s);
56 return d;
59 int
60 s_int_return(void)
62 return INT_CODE;
65 int
66 s_square(int x)
68 return x * x;
71 int
72 s_sum(int x, int y)
74 return x + y;
77 void
78 s_square_out(int x, int *y)
80 *y = s_square(x);
83 void
84 s_square_ref(int *x)
86 *x = s_square(*x);
89 int
90 s_str_length(const char *s)
92 return strlen(s);
95 int
96 s_cstr_length(const char *s, int n)
98 int len = 0;
99 while (0 < n-- && *s++)
100 ++len;
101 return len;
105 s_dot_self(vector_t *v)
107 return s_square(v->x) + s_square(v->y) + s_square(v->z);
110 double
111 s_square_half(double x, double *y)
113 *y = x / 2.0;
114 return x * x;
117 float
118 s_square_half_float(float x, float *y)
120 *y = x / 2.0f;
121 return x * x;
124 long
125 s_square_half_long(long x, long *y)
127 *y = x / 2;
128 return x * x;
132 s_sum_fixed_array(int a[5])
134 return a[0] + a[1] + a[2] + a[3] + a[4];
138 s_pints_sum(pints_t *pints)
140 return *pints->pi + **pints->ppi + ***pints->pppi;
143 double
144 s_ptypes_sum(ptypes_t *pt)
146 return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
150 s_dot_pvectors(pvectors_t *p)
152 return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
156 s_sum_sp(sp_t *sp)
158 return sp->x + sp->s->x;
161 double
162 s_square_sun(sun_t *su)
164 switch (su->s)
166 case SUN_I: return su->u.i * su->u.i;
167 case SUN_F1:
168 case SUN_F2: return su->u.f * su->u.f;
169 case SUN_PI: return (*su->u.pi) * (*su->u.pi);
170 default:
171 return 0.0;
176 s_test_list_length(test_list_t *list)
178 return (list->t == TL_LIST
179 ? 1 + s_test_list_length(list->u.tail)
180 : 0);
184 s_sum_fixed_int_3d(int m[2][3][4])
186 int i, j, k;
187 int sum = 0;
189 for (i = 0; i < 2; ++i)
190 for (j = 0; j < 3; ++j)
191 for (k = 0; k < 4; ++k)
192 sum += m[i][j][k];
194 return sum;
198 s_sum_conf_array(int x[], int n)
200 int *p = x, *end = p + n;
201 int sum = 0;
203 while (p < end)
204 sum += *p++;
206 return sum;
210 s_sum_unique_conf_array(int x[], int n)
212 return s_sum_conf_array(x, n);
216 s_sum_unique_conf_ptr(int *x, int n)
218 return x ? s_sum_conf_array(x, n) : 0;
222 s_sum_var_array(int x[20], int n)
224 ok(0 <= n, "RPC sum_var_array\n");
225 ok(n <= 20, "RPC sum_var_array\n");
227 return s_sum_conf_array(x, n);
231 s_dot_two_vectors(vector_t vs[2])
233 return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
237 s_sum_cs(cs_t *cs)
239 return s_sum_conf_array(cs->ca, cs->n);
243 s_sum_cps(cps_t *cps)
245 int sum = 0;
246 int i;
248 for (i = 0; i < *cps->pn; ++i)
249 sum += cps->ca1[i];
251 for (i = 0; i < 2 * cps->n; ++i)
252 sum += cps->ca2[i];
254 return sum;
258 s_sum_cpsc(cpsc_t *cpsc)
260 int sum = 0;
261 int i;
262 for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
263 sum += cpsc->ca[i];
264 return sum;
268 s_square_puint(puint_t p)
270 int n = atoi(p);
271 return n * n;
275 s_sum_puints(puints_t *p)
277 int sum = 0;
278 int i;
279 for (i = 0; i < p->n; ++i)
280 sum += atoi(p->ps[i]);
281 return sum;
285 s_sum_cpuints(cpuints_t *p)
287 int sum = 0;
288 int i;
289 for (i = 0; i < p->n; ++i)
290 sum += atoi(p->ps[i]);
291 return sum;
295 s_dot_copy_vectors(vector_t u, vector_t v)
297 return u.x * v.x + u.y * v.y + u.z * v.z;
301 s_square_test_us(test_us_t *tus)
303 int n = atoi(tus->us.x);
304 return n * n;
307 double
308 s_square_encu(encu_t *eu)
310 switch (eu->t)
312 case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
313 case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
314 default:
315 return 0.0;
320 s_sum_parr(int *a[3])
322 return s_sum_pcarr(a, 3);
326 s_sum_pcarr(int *a[], int n)
328 int i, s = 0;
329 for (i = 0; i < n; ++i)
330 s += *a[i];
331 return s;
335 s_enum_ord(e_t e)
337 switch (e)
339 case E1: return 1;
340 case E2: return 2;
341 case E3: return 3;
342 case E4: return 4;
343 default:
344 return 0;
348 double
349 s_square_encue(encue_t *eue)
351 switch (eue->t)
353 case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
354 case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
355 default:
356 return 0.0;
361 s_sum_toplev_conf_2n(int *x, int n)
363 int sum = 0;
364 int i;
365 for (i = 0; i < 2 * n; ++i)
366 sum += x[i];
367 return sum;
371 s_sum_toplev_conf_cond(int *x, int a, int b, int c)
373 int sum = 0;
374 int n = c ? a : b;
375 int i;
376 for (i = 0; i < n; ++i)
377 sum += x[i];
378 return sum;
381 double
382 s_sum_aligns(aligns_t *a)
384 return a->c + a->i + a->s + a->d;
388 s_sum_padded(padded_t *p)
390 return p->i + p->c;
394 s_sum_padded2(padded_t ps[2])
396 return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
400 s_sum_padded_conf(padded_t *ps, int n)
402 int sum = 0;
403 int i;
404 for (i = 0; i < n; ++i)
405 sum += s_sum_padded(&ps[i]);
406 return sum;
410 s_sum_bogus(bogus_t *b)
412 return *b->h.p1 + *b->p2 + *b->p3 + b->c;
415 void
416 s_check_null(int *null)
418 ok(!null, "RPC check_null\n");
422 s_str_struct_len(str_struct_t *s)
424 return lstrlenA(s->s);
428 s_wstr_struct_len(wstr_struct_t *s)
430 return lstrlenW(s->s);
434 s_sum_doub_carr(doub_carr_t *dc)
436 int i, j;
437 int sum = 0;
438 for (i = 0; i < dc->n; ++i)
439 for (j = 0; j < dc->a[i]->n; ++j)
440 sum += dc->a[i]->a[j];
441 return sum;
444 void
445 s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
447 doub_carr_t *t;
448 int i, j;
449 t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
450 t->n = n;
451 for (i = 0; i < n; ++i)
453 int v = i + 1;
454 t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
455 t->a[i]->n = v;
456 for (j = 0; j < v; ++j)
457 t->a[i]->a[j] = j + 1;
459 *dc = t;
462 unsigned
463 s_hash_bstr(bstr_t b)
465 short n = b[-1];
466 short *s = b;
467 unsigned hash = 0;
468 short i;
469 for (i = 0; i < n; ++i)
470 hash = 5 * hash + (unsigned) s[i];
471 return hash;
474 void
475 s_get_name(name_t *name)
477 const char bossman[] = "Jeremy White";
478 memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
479 /* ensure nul-termination */
480 if (name->size < sizeof(bossman))
481 name->name[name->size - 1] = 0;
485 s_sum_pcarr2(int n, int **pa)
487 return s_sum_conf_array(*pa, n);
491 s_sum_L1_norms(int n, vector_t *vs)
493 int i;
494 int sum = 0;
495 for (i = 0; i < n; ++i)
496 sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
497 return sum;
500 str_t
501 s_get_filename(void)
503 return (char *)__FILE__;
506 void
507 s_context_handle_test(void)
509 NDR_SCONTEXT h;
510 RPC_BINDING_HANDLE binding;
511 RPC_STATUS status;
512 unsigned char buf[20];
513 static RPC_SERVER_INTERFACE server_if =
515 sizeof(RPC_SERVER_INTERFACE),
516 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
517 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
518 NULL,
526 binding = I_RpcGetCurrentCallHandle();
527 ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
529 h = NDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
530 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
532 /* marshal a context handle with NULL userContext */
533 memset(buf, 0xcc, sizeof(buf));
534 NDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
535 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
536 ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");
538 h = NDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
539 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
541 /* marshal a context handle with non-NULL userContext */
542 memset(buf, 0xcc, sizeof(buf));
543 h->userContext = (void *)0xdeadbeef;
544 NDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
545 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
546 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
548 h = NDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
549 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
550 ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
552 /* marshal a context handle with an interface specified */
553 h = NDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
554 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
556 memset(buf, 0xcc, sizeof(buf));
557 h->userContext = (void *)0xcafebabe;
558 NDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
559 ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
560 ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
562 h = NDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
563 ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
564 ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
566 /* test same interface data, but different pointer */
567 /* raises ERROR_INVALID_HANDLE exception */
568 if (0)
570 RPC_SERVER_INTERFACE server_if_clone = server_if;
572 NDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
575 /* test different interface data, but different pointer */
576 /* raises ERROR_INVALID_HANDLE exception */
577 if (0)
579 static RPC_SERVER_INTERFACE server_if2 =
581 sizeof(RPC_SERVER_INTERFACE),
582 {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
583 {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
584 NULL,
591 NDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
593 NDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
597 void
598 s_stop(void)
600 ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
601 ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
602 ok(SetEvent(stop_event), "SetEvent\n");
605 static void
606 make_cmdline(char buffer[MAX_PATH], const char *test)
608 sprintf(buffer, "%s server %s", progname, test);
611 static int
612 run_client(const char *test)
614 char cmdline[MAX_PATH];
615 PROCESS_INFORMATION info;
616 STARTUPINFOA startup;
617 DWORD exitcode;
619 memset(&startup, 0, sizeof startup);
620 startup.cb = sizeof startup;
622 make_cmdline(cmdline, test);
623 ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
624 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
625 ok(GetExitCodeProcess(info.hProcess, &exitcode), "GetExitCodeProcess\n");
626 ok(CloseHandle(info.hProcess), "CloseHandle\n");
627 ok(CloseHandle(info.hThread), "CloseHandle\n");
629 return exitcode == 0;
632 static void
633 basic_tests(void)
635 char string[] = "I am a string";
636 WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
637 int f[5] = {1, 3, 0, -2, -4};
638 vector_t a = {1, 3, 7};
639 vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
640 pvectors_t pvecs = {&vec1, &pvec2};
641 sp_inner_t spi = {42};
642 sp_t sp = {-13, &spi};
643 aligns_t aligns;
644 pints_t pints;
645 ptypes_t ptypes;
646 padded_t padded;
647 padded_t padded2[2];
648 bogus_t bogus;
649 int i1, i2, i3, *pi2, *pi3, **ppi3;
650 double u, v;
651 float s, t;
652 long q, r;
653 short h;
654 char c;
655 int x;
656 str_struct_t ss = {string};
657 wstr_struct_t ws = {wstring};
658 str_t str;
660 ok(int_return() == INT_CODE, "RPC int_return\n");
662 ok(square(7) == 49, "RPC square\n");
663 ok(sum(23, -4) == 19, "RPC sum\n");
665 x = 0;
666 square_out(11, &x);
667 ok(x == 121, "RPC square_out\n");
669 x = 5;
670 square_ref(&x);
671 ok(x == 25, "RPC square_ref\n");
673 ok(str_length(string) == strlen(string), "RPC str_length\n");
674 ok(dot_self(&a) == 59, "RPC dot_self\n");
676 ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
677 ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
679 v = 0.0;
680 u = square_half(3.0, &v);
681 ok(u == 9.0, "RPC square_half\n");
682 ok(v == 1.5, "RPC square_half\n");
684 t = 0.0f;
685 s = square_half_float(3.0f, &t);
686 ok(s == 9.0f, "RPC square_half_float\n");
687 ok(t == 1.5f, "RPC square_half_float\n");
689 r = 0;
690 q = square_half_long(3, &r);
691 ok(q == 9, "RPC square_half_long\n");
692 ok(r == 1, "RPC square_half_long\n");
694 i1 = 19;
695 i2 = -3;
696 i3 = -29;
697 pi2 = &i2;
698 pi3 = &i3;
699 ppi3 = &pi3;
700 pints.pi = &i1;
701 pints.ppi = &pi2;
702 pints.pppi = &ppi3;
703 ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
705 c = 10;
706 h = 3;
707 q = 14;
708 s = -5.0f;
709 u = 11.0;
710 ptypes.pc = &c;
711 ptypes.ps = &h;
712 ptypes.pl = &q;
713 ptypes.pf = &s;
714 ptypes.pd = &u;
715 ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
717 ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
718 ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
719 ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
720 ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
722 ok(enum_ord(E1) == 1, "RPC enum_ord\n");
723 ok(enum_ord(E2) == 2, "RPC enum_ord\n");
724 ok(enum_ord(E3) == 3, "RPC enum_ord\n");
725 ok(enum_ord(E4) == 4, "RPC enum_ord\n");
727 memset(&aligns, 0, sizeof(aligns));
728 aligns.c = 3;
729 aligns.i = 4;
730 aligns.s = 5;
731 aligns.d = 6.0;
732 ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
734 padded.i = -3;
735 padded.c = 8;
736 ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
737 padded2[0].i = -5;
738 padded2[0].c = 1;
739 padded2[1].i = 3;
740 padded2[1].c = 7;
741 ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
742 padded2[0].i = -5;
743 padded2[0].c = 1;
744 padded2[1].i = 3;
745 padded2[1].c = 7;
746 ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
748 i1 = 14;
749 i2 = -7;
750 i3 = -4;
751 bogus.h.p1 = &i1;
752 bogus.p2 = &i2;
753 bogus.p3 = &i3;
754 bogus.c = 9;
755 ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
757 check_null(NULL);
759 str = get_filename();
760 ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
761 midl_user_free(str);
764 static void
765 union_tests(void)
767 encue_t eue;
768 encu_t eu;
769 sun_t su;
770 int i;
772 su.s = SUN_I;
773 su.u.i = 9;
774 ok(square_sun(&su) == 81.0, "RPC square_sun\n");
776 su.s = SUN_F1;
777 su.u.f = 5.0;
778 ok(square_sun(&su) == 25.0, "RPC square_sun\n");
780 su.s = SUN_F2;
781 su.u.f = -2.0;
782 ok(square_sun(&su) == 4.0, "RPC square_sun\n");
784 su.s = SUN_PI;
785 su.u.pi = &i;
786 i = 11;
787 ok(square_sun(&su) == 121.0, "RPC square_sun\n");
789 eu.t = ENCU_I;
790 eu.tagged_union.i = 7;
791 ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
793 eu.t = ENCU_F;
794 eu.tagged_union.f = 3.0;
795 ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
797 eue.t = E1;
798 eue.tagged_union.i1 = 8;
799 ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
801 eue.t = E2;
802 eue.tagged_union.f2 = 10.0;
803 ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
806 static test_list_t *
807 null_list(void)
809 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
810 n->t = TL_NULL;
811 n->u.x = 0;
812 return n;
815 static test_list_t *
816 make_list(test_list_t *tail)
818 test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
819 n->t = TL_LIST;
820 n->u.tail = tail;
821 return n;
824 static void
825 free_list(test_list_t *list)
827 if (list->t == TL_LIST)
828 free_list(list->u.tail);
829 HeapFree(GetProcessHeap(), 0, list);
832 ULONG __RPC_USER
833 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
835 return start + sizeof(int);
838 unsigned char * __RPC_USER
839 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
841 int n = atoi(*p);
842 memcpy(buffer, &n, sizeof n);
843 return buffer + sizeof n;
846 unsigned char * __RPC_USER
847 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
849 int n;
850 memcpy(&n, buffer, sizeof n);
851 *p = HeapAlloc(GetProcessHeap(), 0, 10);
852 sprintf(*p, "%d", n);
853 return buffer + sizeof n;
856 void __RPC_USER
857 puint_t_UserFree(ULONG *flags, puint_t *p)
859 HeapFree(GetProcessHeap(), 0, *p);
862 ULONG __RPC_USER
863 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
865 return start + sizeof(struct wire_us);
868 unsigned char * __RPC_USER
869 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
871 struct wire_us wus;
872 wus.x = atoi(pus->x);
873 memcpy(buffer, &wus, sizeof wus);
874 return buffer + sizeof wus;
877 unsigned char * __RPC_USER
878 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
880 struct wire_us wus;
881 memcpy(&wus, buffer, sizeof wus);
882 pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
883 sprintf(pus->x, "%d", wus.x);
884 return buffer + sizeof wus;
887 void __RPC_USER
888 us_t_UserFree(ULONG *flags, us_t *pus)
890 HeapFree(GetProcessHeap(), 0, pus->x);
893 ULONG __RPC_USER
894 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
896 return start + FIELD_OFFSET(wire_bstr_t, data[(*b)[-1]]);
899 unsigned char * __RPC_USER
900 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
902 wire_bstr_t *wb = (wire_bstr_t *) buffer;
903 wb->n = (*b)[-1];
904 memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
905 return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
908 unsigned char * __RPC_USER
909 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
911 wire_bstr_t *wb = (wire_bstr_t *) buffer;
912 short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
913 data[0] = wb->n;
914 memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
915 *b = &data[1];
916 return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
919 void __RPC_USER
920 bstr_t_UserFree(ULONG *flags, bstr_t *b)
922 HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
925 static void
926 pointer_tests(void)
928 int a[] = {1, 2, 3, 4};
929 char p1[] = "11";
930 test_list_t *list = make_list(make_list(make_list(null_list())));
931 test_us_t tus = {{p1}};
932 int *pa[4];
933 puints_t pus;
934 cpuints_t cpus;
935 short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
936 bstr_t bstr = &bstr_data[1];
937 name_t name;
938 void *buffer;
939 int *pa2;
941 ok(test_list_length(list) == 3, "RPC test_list_length\n");
942 ok(square_puint(p1) == 121, "RPC square_puint\n");
943 pus.n = 4;
944 pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
945 pus.ps[0] = xstrdup("5");
946 pus.ps[1] = xstrdup("6");
947 pus.ps[2] = xstrdup("7");
948 pus.ps[3] = xstrdup("8");
949 ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
950 HeapFree(GetProcessHeap(), 0, pus.ps[0]);
951 HeapFree(GetProcessHeap(), 0, pus.ps[1]);
952 HeapFree(GetProcessHeap(), 0, pus.ps[2]);
953 HeapFree(GetProcessHeap(), 0, pus.ps[3]);
954 HeapFree(GetProcessHeap(), 0, pus.ps);
955 cpus.n = 4;
956 cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
957 cpus.ps[0] = xstrdup("5");
958 cpus.ps[1] = xstrdup("6");
959 cpus.ps[2] = xstrdup("7");
960 cpus.ps[3] = xstrdup("8");
961 ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
962 HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
963 HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
964 HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
965 HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
966 HeapFree(GetProcessHeap(), 0, cpus.ps);
967 ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
969 pa[0] = &a[0];
970 pa[1] = &a[1];
971 pa[2] = &a[2];
972 ok(sum_parr(pa) == 6, "RPC sum_parr\n");
974 pa[0] = &a[0];
975 pa[1] = &a[1];
976 pa[2] = &a[2];
977 pa[3] = &a[3];
978 ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
980 ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
982 free_list(list);
984 name.size = 10;
985 name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
986 get_name(&name);
987 ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
988 ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
989 HeapFree(GetProcessHeap(), 0, name.name);
991 pa2 = a;
992 ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
995 static int
996 check_pyramid_doub_carr(doub_carr_t *dc)
998 int i, j;
999 for (i = 0; i < dc->n; ++i)
1000 for (j = 0; j < dc->a[i]->n; ++j)
1001 if (dc->a[i]->a[j] != j + 1)
1002 return FALSE;
1003 return TRUE;
1006 static void
1007 free_pyramid_doub_carr(doub_carr_t *dc)
1009 int i;
1010 for (i = 0; i < dc->n; ++i)
1011 MIDL_user_free(dc->a[i]);
1012 MIDL_user_free(dc);
1015 static void
1016 array_tests(void)
1018 const char str1[25] = "Hello";
1019 int m[2][3][4] =
1021 {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
1022 {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
1024 int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1025 vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1026 cps_t cps;
1027 cpsc_t cpsc;
1028 cs_t *cs;
1029 int n;
1030 int ca[5] = {1, -2, 3, -4, 5};
1031 doub_carr_t *dc;
1033 ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
1035 ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
1037 ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
1038 ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1039 ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1040 ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1042 ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
1043 ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
1044 ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
1046 ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
1047 ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1048 ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1049 ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1051 ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1052 cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1053 cs->n = 5;
1054 cs->ca[0] = 3;
1055 cs->ca[1] = 5;
1056 cs->ca[2] = -2;
1057 cs->ca[3] = -1;
1058 cs->ca[4] = -4;
1059 ok(sum_cs(cs) == 1, "RPC sum_cs\n");
1060 HeapFree(GetProcessHeap(), 0, cs);
1062 n = 5;
1063 cps.pn = &n;
1064 cps.ca1 = &c[2];
1065 cps.n = 3;
1066 cps.ca2 = &c[3];
1067 ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1069 cpsc.a = 4;
1070 cpsc.b = 5;
1071 cpsc.c = 1;
1072 cpsc.ca = c;
1073 ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1074 cpsc.a = 4;
1075 cpsc.b = 5;
1076 cpsc.c = 0;
1077 cpsc.ca = c;
1078 ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1080 ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
1081 ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
1082 ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1084 dc = malloc(FIELD_OFFSET(doub_carr_t, a[2]));
1085 dc->n = 2;
1086 dc->a[0] = malloc(FIELD_OFFSET(doub_carr_1_t, a[3]));
1087 dc->a[0]->n = 3;
1088 dc->a[0]->a[0] = 5;
1089 dc->a[0]->a[1] = 1;
1090 dc->a[0]->a[2] = 8;
1091 dc->a[1] = malloc(FIELD_OFFSET(doub_carr_1_t, a[2]));
1092 dc->a[1]->n = 2;
1093 dc->a[1]->a[0] = 2;
1094 dc->a[1]->a[1] = 3;
1095 ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1096 free(dc->a[0]);
1097 free(dc->a[1]);
1098 free(dc);
1100 dc = NULL;
1101 make_pyramid_doub_carr(4, &dc);
1102 ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
1103 free_pyramid_doub_carr(dc);
1105 ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1108 static void
1109 run_tests(void)
1111 basic_tests();
1112 union_tests();
1113 pointer_tests();
1114 array_tests();
1115 context_handle_test();
1118 static void
1119 client(const char *test)
1121 if (strcmp(test, "tcp_basic") == 0)
1123 static unsigned char iptcp[] = "ncacn_ip_tcp";
1124 static unsigned char address[] = "127.0.0.1";
1125 static unsigned char port[] = PORT;
1126 unsigned char *binding;
1128 ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1129 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1131 run_tests();
1133 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1134 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1136 else if (strcmp(test, "np_basic") == 0)
1138 static unsigned char np[] = "ncacn_np";
1139 static unsigned char address[] = "\\\\.";
1140 static unsigned char pipe[] = PIPE;
1141 unsigned char *binding;
1143 ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1144 ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1146 run_tests();
1147 stop();
1149 ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1150 ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1154 static void
1155 server(void)
1157 static unsigned char iptcp[] = "ncacn_ip_tcp";
1158 static unsigned char port[] = PORT;
1159 static unsigned char np[] = "ncacn_np";
1160 static unsigned char pipe[] = PIPE;
1162 ok(RPC_S_OK == RpcServerUseProtseqEp(iptcp, 20, port, NULL), "RpcServerUseProtseqEp\n");
1163 ok(RPC_S_OK == RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL), "RpcServerRegisterIf\n");
1164 ok(RPC_S_OK == RpcServerListen(1, 20, TRUE), "RpcServerListen\n");
1166 stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1167 ok(stop_event != NULL, "CreateEvent failed\n");
1169 ok(run_client("tcp_basic"), "tcp_basic client test failed\n");
1171 ok(RPC_S_OK == RpcServerUseProtseqEp(np, 0, pipe, NULL), "RpcServerUseProtseqEp\n");
1172 ok(run_client("np_basic"), "np_basic client test failed\n");
1174 ok(WAIT_OBJECT_0 == WaitForSingleObject(stop_event, 60000), "WaitForSingleObject\n");
1175 todo_wine {
1176 ok(RPC_S_OK == RpcMgmtWaitServerListen(), "RpcMgmtWaitServerListening\n");
1180 START_TEST(server)
1182 int argc;
1183 char **argv;
1185 argc = winetest_get_mainargs(&argv);
1186 progname = argv[0];
1188 if (argc == 3)
1190 RpcTryExcept
1192 client(argv[2]);
1194 RpcExcept(TRUE)
1196 trace("Exception %d\n", RpcExceptionCode());
1198 RpcEndExcept
1200 else
1201 server();