rpcrt4: Set the destination pointer in PointerUnmarshall before calling the reference...
[wine] / dlls / rpcrt4 / tests / server.c
1 /*
2  * Tests for WIDL and RPC server/clients.
3  *
4  * Copyright (C) Google 2007 (Dan Hipschman)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <windows.h>
22 #include "wine/test.h"
23 #include "server.h"
24 #include "server_defines.h"
25
26 #include <stddef.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #define PORT "4114"
31 #define PIPE "\\pipe\\wine_rpcrt4_test"
32
33 #define INT_CODE 4198
34
35 static const char *progname;
36
37 static HANDLE stop_event;
38
39 void __RPC_FAR *__RPC_USER
40 midl_user_allocate(size_t n)
41 {
42   return malloc(n);
43 }
44
45 void __RPC_USER
46 midl_user_free(void __RPC_FAR *p)
47 {
48   free(p);
49 }
50
51 static char *
52 xstrdup(const char *s)
53 {
54   char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
55   strcpy(d, s);
56   return d;
57 }
58
59 int
60 s_int_return(void)
61 {
62   return INT_CODE;
63 }
64
65 int
66 s_square(int x)
67 {
68   return x * x;
69 }
70
71 int
72 s_sum(int x, int y)
73 {
74   return x + y;
75 }
76
77 void
78 s_square_out(int x, int *y)
79 {
80   *y = s_square(x);
81 }
82
83 void
84 s_square_ref(int *x)
85 {
86   *x = s_square(*x);
87 }
88
89 int
90 s_str_length(const char *s)
91 {
92   return strlen(s);
93 }
94
95 int
96 s_cstr_length(const char *s, int n)
97 {
98   int len = 0;
99   while (0 < n-- && *s++)
100     ++len;
101   return len;
102 }
103
104 int
105 s_dot_self(vector_t *v)
106 {
107   return s_square(v->x) + s_square(v->y) + s_square(v->z);
108 }
109
110 double
111 s_square_half(double x, double *y)
112 {
113   *y = x / 2.0;
114   return x * x;
115 }
116
117 float
118 s_square_half_float(float x, float *y)
119 {
120   *y = x / 2.0f;
121   return x * x;
122 }
123
124 long
125 s_square_half_long(long x, long *y)
126 {
127   *y = x / 2;
128   return x * x;
129 }
130
131 int
132 s_sum_fixed_array(int a[5])
133 {
134   return a[0] + a[1] + a[2] + a[3] + a[4];
135 }
136
137 int
138 s_pints_sum(pints_t *pints)
139 {
140   return *pints->pi + **pints->ppi + ***pints->pppi;
141 }
142
143 double
144 s_ptypes_sum(ptypes_t *pt)
145 {
146   return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
147 }
148
149 int
150 s_dot_pvectors(pvectors_t *p)
151 {
152   return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
153 }
154
155 int
156 s_sum_sp(sp_t *sp)
157 {
158   return sp->x + sp->s->x;
159 }
160
161 double
162 s_square_sun(sun_t *su)
163 {
164   switch (su->s)
165   {
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;
172   }
173 }
174
175 int
176 s_test_list_length(test_list_t *list)
177 {
178   return (list->t == TL_LIST
179           ? 1 + s_test_list_length(list->u.tail)
180           : 0);
181 }
182
183 int
184 s_sum_fixed_int_3d(int m[2][3][4])
185 {
186   int i, j, k;
187   int sum = 0;
188
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];
193
194   return sum;
195 }
196
197 int
198 s_sum_conf_array(int x[], int n)
199 {
200   int *p = x, *end = p + n;
201   int sum = 0;
202
203   while (p < end)
204     sum += *p++;
205
206   return sum;
207 }
208
209 int
210 s_sum_unique_conf_array(int x[], int n)
211 {
212   return s_sum_conf_array(x, n);
213 }
214
215 int
216 s_sum_unique_conf_ptr(int *x, int n)
217 {
218   return x ? s_sum_conf_array(x, n) : 0;
219 }
220
221 int
222 s_sum_var_array(int x[20], int n)
223 {
224   ok(0 <= n, "RPC sum_var_array\n");
225   ok(n <= 20, "RPC sum_var_array\n");
226
227   return s_sum_conf_array(x, n);
228 }
229
230 int
231 s_dot_two_vectors(vector_t vs[2])
232 {
233   return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
234 }
235
236 int
237 s_sum_cs(cs_t *cs)
238 {
239   return s_sum_conf_array(cs->ca, cs->n);
240 }
241
242 int
243 s_sum_cps(cps_t *cps)
244 {
245   int sum = 0;
246   int i;
247
248   for (i = 0; i < *cps->pn; ++i)
249     sum += cps->ca1[i];
250
251   for (i = 0; i < 2 * cps->n; ++i)
252     sum += cps->ca2[i];
253
254   return sum;
255 }
256
257 int
258 s_sum_cpsc(cpsc_t *cpsc)
259 {
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;
265 }
266
267 int
268 s_square_puint(puint_t p)
269 {
270   int n = atoi(p);
271   return n * n;
272 }
273
274 int
275 s_sum_puints(puints_t *p)
276 {
277   int sum = 0;
278   int i;
279   for (i = 0; i < p->n; ++i)
280     sum += atoi(p->ps[i]);
281   return sum;
282 }
283
284 int
285 s_sum_cpuints(cpuints_t *p)
286 {
287   int sum = 0;
288   int i;
289   for (i = 0; i < p->n; ++i)
290     sum += atoi(p->ps[i]);
291   return sum;
292 }
293
294 int
295 s_dot_copy_vectors(vector_t u, vector_t v)
296 {
297   return u.x * v.x + u.y * v.y + u.z * v.z;
298 }
299
300 int
301 s_square_test_us(test_us_t *tus)
302 {
303   int n = atoi(tus->us.x);
304   return n * n;
305 }
306
307 double
308 s_square_encu(encu_t *eu)
309 {
310   switch (eu->t)
311   {
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;
316   }
317 }
318
319 int
320 s_sum_parr(int *a[3])
321 {
322   return s_sum_pcarr(a, 3);
323 }
324
325 int
326 s_sum_pcarr(int *a[], int n)
327 {
328   int i, s = 0;
329   for (i = 0; i < n; ++i)
330     s += *a[i];
331   return s;
332 }
333
334 int
335 s_enum_ord(e_t e)
336 {
337   switch (e)
338   {
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;
345   }
346 }
347
348 double
349 s_square_encue(encue_t *eue)
350 {
351   switch (eue->t)
352   {
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;
357   }
358 }
359
360 int
361 s_sum_toplev_conf_2n(int *x, int n)
362 {
363   int sum = 0;
364   int i;
365   for (i = 0; i < 2 * n; ++i)
366     sum += x[i];
367   return sum;
368 }
369
370 int
371 s_sum_toplev_conf_cond(int *x, int a, int b, int c)
372 {
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;
379 }
380
381 double
382 s_sum_aligns(aligns_t *a)
383 {
384   return a->c + a->i + a->s + a->d;
385 }
386
387 int
388 s_sum_padded(padded_t *p)
389 {
390   return p->i + p->c;
391 }
392
393 int
394 s_sum_padded2(padded_t ps[2])
395 {
396   return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
397 }
398
399 int
400 s_sum_padded_conf(padded_t *ps, int n)
401 {
402   int sum = 0;
403   int i;
404   for (i = 0; i < n; ++i)
405     sum += s_sum_padded(&ps[i]);
406   return sum;
407 }
408
409 int
410 s_sum_bogus(bogus_t *b)
411 {
412   return *b->h.p1 + *b->p2 + *b->p3 + b->c;
413 }
414
415 void
416 s_check_null(int *null)
417 {
418   ok(!null, "RPC check_null\n");
419 }
420
421 int
422 s_str_struct_len(str_struct_t *s)
423 {
424   return lstrlenA(s->s);
425 }
426
427 int
428 s_wstr_struct_len(wstr_struct_t *s)
429 {
430   return lstrlenW(s->s);
431 }
432
433 int
434 s_sum_doub_carr(doub_carr_t *dc)
435 {
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;
442 }
443
444 void
445 s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
446 {
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)
452   {
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;
458   }
459   *dc = t;
460 }
461
462 unsigned
463 s_hash_bstr(bstr_t b)
464 {
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;
472 }
473
474 void
475 s_get_name(name_t *name)
476 {
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;
482 }
483
484 int
485 s_sum_pcarr2(int n, int **pa)
486 {
487   return s_sum_conf_array(*pa, n);
488 }
489
490 int
491 s_sum_L1_norms(int n, vector_t *vs)
492 {
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;
498 }
499
500 void
501 s_stop(void)
502 {
503   ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
504   ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
505   ok(SetEvent(stop_event), "SetEvent\n");
506 }
507
508 static void
509 make_cmdline(char buffer[MAX_PATH], const char *test)
510 {
511   sprintf(buffer, "%s server %s", progname, test);
512 }
513
514 static int
515 run_client(const char *test)
516 {
517   char cmdline[MAX_PATH];
518   PROCESS_INFORMATION info;
519   STARTUPINFOA startup;
520   DWORD exitcode;
521
522   memset(&startup, 0, sizeof startup);
523   startup.cb = sizeof startup;
524
525   make_cmdline(cmdline, test);
526   ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
527   ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0, "Child process termination\n");
528   ok(GetExitCodeProcess(info.hProcess, &exitcode), "GetExitCodeProcess\n");
529   ok(CloseHandle(info.hProcess), "CloseHandle\n");
530   ok(CloseHandle(info.hThread), "CloseHandle\n");
531
532   return exitcode == 0;
533 }
534
535 static void
536 basic_tests(void)
537 {
538   char string[] = "I am a string";
539   WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
540   int f[5] = {1, 3, 0, -2, -4};
541   vector_t a = {1, 3, 7};
542   vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
543   pvectors_t pvecs = {&vec1, &pvec2};
544   sp_inner_t spi = {42};
545   sp_t sp = {-13, &spi};
546   aligns_t aligns = {3, 4, 5, 6.0};
547   pints_t pints;
548   ptypes_t ptypes;
549   padded_t padded;
550   padded_t padded2[2];
551   bogus_t bogus;
552   int i1, i2, i3, *pi2, *pi3, **ppi3;
553   double u, v;
554   float s, t;
555   long q, r;
556   short h;
557   char c;
558   int x;
559   str_struct_t ss = {string};
560   wstr_struct_t ws = {wstring};
561
562   ok(int_return() == INT_CODE, "RPC int_return\n");
563
564   ok(square(7) == 49, "RPC square\n");
565   ok(sum(23, -4) == 19, "RPC sum\n");
566
567   x = 0;
568   square_out(11, &x);
569   ok(x == 121, "RPC square_out\n");
570
571   x = 5;
572   square_ref(&x);
573   ok(x == 25, "RPC square_ref\n");
574
575   ok(str_length(string) == strlen(string), "RPC str_length\n");
576   ok(dot_self(&a) == 59, "RPC dot_self\n");
577
578   ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
579   ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
580
581   v = 0.0;
582   u = square_half(3.0, &v);
583   ok(u == 9.0, "RPC square_half\n");
584   ok(v == 1.5, "RPC square_half\n");
585
586   t = 0.0f;
587   s = square_half_float(3.0f, &t);
588   ok(s == 9.0f, "RPC square_half_float\n");
589   ok(t == 1.5f, "RPC square_half_float\n");
590
591   r = 0;
592   q = square_half_long(3, &r);
593   ok(q == 9, "RPC square_half_long\n");
594   ok(r == 1, "RPC square_half_long\n");
595
596   i1 = 19;
597   i2 = -3;
598   i3 = -29;
599   pi2 = &i2;
600   pi3 = &i3;
601   ppi3 = &pi3;
602   pints.pi = &i1;
603   pints.ppi = &pi2;
604   pints.pppi = &ppi3;
605   ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
606
607   c = 10;
608   h = 3;
609   q = 14;
610   s = -5.0f;
611   u = 11.0;
612   ptypes.pc = &c;
613   ptypes.ps = &h;
614   ptypes.pl = &q;
615   ptypes.pf = &s;
616   ptypes.pd = &u;
617   ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
618
619   ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
620   ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
621   ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
622   ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
623
624   ok(enum_ord(E1) == 1, "RPC enum_ord\n");
625   ok(enum_ord(E2) == 2, "RPC enum_ord\n");
626   ok(enum_ord(E3) == 3, "RPC enum_ord\n");
627   ok(enum_ord(E4) == 4, "RPC enum_ord\n");
628
629   ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
630
631   padded.i = -3;
632   padded.c = 8;
633   ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
634   padded2[0].i = -5;
635   padded2[0].c = 1;
636   padded2[1].i = 3;
637   padded2[1].c = 7;
638   ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
639   padded2[0].i = -5;
640   padded2[0].c = 1;
641   padded2[1].i = 3;
642   padded2[1].c = 7;
643   ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
644
645   i1 = 14;
646   i2 = -7;
647   i3 = -4;
648   bogus.h.p1 = &i1;
649   bogus.p2 = &i2;
650   bogus.p3 = &i3;
651   bogus.c = 9;
652   ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
653
654   check_null(NULL);
655 }
656
657 static void
658 union_tests(void)
659 {
660   encue_t eue;
661   encu_t eu;
662   sun_t su;
663   int i;
664
665   su.s = SUN_I;
666   su.u.i = 9;
667   ok(square_sun(&su) == 81.0, "RPC square_sun\n");
668
669   su.s = SUN_F1;
670   su.u.f = 5.0;
671   ok(square_sun(&su) == 25.0, "RPC square_sun\n");
672
673   su.s = SUN_F2;
674   su.u.f = -2.0;
675   ok(square_sun(&su) == 4.0, "RPC square_sun\n");
676
677   su.s = SUN_PI;
678   su.u.pi = &i;
679   i = 11;
680   ok(square_sun(&su) == 121.0, "RPC square_sun\n");
681
682   eu.t = ENCU_I;
683   eu.tagged_union.i = 7;
684   ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
685
686   eu.t = ENCU_F;
687   eu.tagged_union.f = 3.0;
688   ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
689
690   eue.t = E1;
691   eue.tagged_union.i1 = 8;
692   ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
693
694   eue.t = E2;
695   eue.tagged_union.f2 = 10.0;
696   ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
697 }
698
699 static test_list_t *
700 null_list(void)
701 {
702   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
703   n->t = TL_NULL;
704   return n;
705 }
706
707 static test_list_t *
708 make_list(test_list_t *tail)
709 {
710   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
711   n->t = TL_LIST;
712   n->u.tail = tail;
713   return n;
714 }
715
716 static void
717 free_list(test_list_t *list)
718 {
719   if (list->t == TL_LIST)
720     free_list(list->u.tail);
721   HeapFree(GetProcessHeap(), 0, list);
722 }
723
724 ULONG __RPC_USER
725 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
726 {
727   return start + sizeof(int);
728 }
729
730 unsigned char * __RPC_USER
731 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
732 {
733   int n = atoi(*p);
734   memcpy(buffer, &n, sizeof n);
735   return buffer + sizeof n;
736 }
737
738 unsigned char * __RPC_USER
739 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
740 {
741   int n;
742   memcpy(&n, buffer, sizeof n);
743   *p = HeapAlloc(GetProcessHeap(), 0, 10);
744   sprintf(*p, "%d", n);
745   return buffer + sizeof n;
746 }
747
748 void __RPC_USER
749 puint_t_UserFree(ULONG *flags, puint_t *p)
750 {
751   HeapFree(GetProcessHeap(), 0, *p);
752 }
753
754 ULONG __RPC_USER
755 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
756 {
757   return start + sizeof(struct wire_us);
758 }
759
760 unsigned char * __RPC_USER
761 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
762 {
763   struct wire_us wus;
764   wus.x = atoi(pus->x);
765   memcpy(buffer, &wus, sizeof wus);
766   return buffer + sizeof wus;
767 }
768
769 unsigned char * __RPC_USER
770 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
771 {
772   struct wire_us wus;
773   memcpy(&wus, buffer, sizeof wus);
774   pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
775   sprintf(pus->x, "%d", wus.x);
776   return buffer + sizeof wus;
777 }
778
779 void __RPC_USER
780 us_t_UserFree(ULONG *flags, us_t *pus)
781 {
782   HeapFree(GetProcessHeap(), 0, pus->x);
783 }
784
785 ULONG __RPC_USER
786 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
787 {
788   return start + FIELD_OFFSET(wire_bstr_t, data[(*b)[-1]]);
789 }
790
791 unsigned char * __RPC_USER
792 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
793 {
794   wire_bstr_t *wb = (wire_bstr_t *) buffer;
795   wb->n = (*b)[-1];
796   memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
797   return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
798 }
799
800 unsigned char * __RPC_USER
801 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
802 {
803   wire_bstr_t *wb = (wire_bstr_t *) buffer;
804   short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
805   data[0] = wb->n;
806   memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
807   *b = &data[1];
808   return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
809 }
810
811 void __RPC_USER
812 bstr_t_UserFree(ULONG *flags, bstr_t *b)
813 {
814   HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
815 }
816
817 static void
818 pointer_tests(void)
819 {
820   int a[] = {1, 2, 3, 4};
821   char p1[] = "11";
822   test_list_t *list = make_list(make_list(make_list(null_list())));
823   test_us_t tus = {{p1}};
824   int *pa[4];
825   puints_t pus;
826   cpuints_t cpus;
827   short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
828   bstr_t bstr = &bstr_data[1];
829   name_t name;
830   void *buffer;
831   int *pa2;
832
833   ok(test_list_length(list) == 3, "RPC test_list_length\n");
834   ok(square_puint(p1) == 121, "RPC square_puint\n");
835   pus.n = 4;
836   pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
837   pus.ps[0] = xstrdup("5");
838   pus.ps[1] = xstrdup("6");
839   pus.ps[2] = xstrdup("7");
840   pus.ps[3] = xstrdup("8");
841   ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
842   HeapFree(GetProcessHeap(), 0, pus.ps[0]);
843   HeapFree(GetProcessHeap(), 0, pus.ps[1]);
844   HeapFree(GetProcessHeap(), 0, pus.ps[2]);
845   HeapFree(GetProcessHeap(), 0, pus.ps[3]);
846   HeapFree(GetProcessHeap(), 0, pus.ps);
847   cpus.n = 4;
848   cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
849   cpus.ps[0] = xstrdup("5");
850   cpus.ps[1] = xstrdup("6");
851   cpus.ps[2] = xstrdup("7");
852   cpus.ps[3] = xstrdup("8");
853   ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
854   HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
855   HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
856   HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
857   HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
858   HeapFree(GetProcessHeap(), 0, cpus.ps);
859   ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
860
861   pa[0] = &a[0];
862   pa[1] = &a[1];
863   pa[2] = &a[2];
864   ok(sum_parr(pa) == 6, "RPC sum_parr\n");
865
866   pa[0] = &a[0];
867   pa[1] = &a[1];
868   pa[2] = &a[2];
869   pa[3] = &a[3];
870   ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
871
872   ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
873
874   free_list(list);
875
876   name.size = 10;
877   name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
878   get_name(&name);
879   ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
880   ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
881   HeapFree(GetProcessHeap(), 0, name.name);
882
883   pa2 = a;
884   ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
885 }
886
887 static int
888 check_pyramid_doub_carr(doub_carr_t *dc)
889 {
890   int i, j;
891   for (i = 0; i < dc->n; ++i)
892     for (j = 0; j < dc->a[i]->n; ++j)
893       if (dc->a[i]->a[j] != j + 1)
894         return FALSE;
895   return TRUE;
896 }
897
898 static void
899 free_pyramid_doub_carr(doub_carr_t *dc)
900 {
901   int i;
902   for (i = 0; i < dc->n; ++i)
903     MIDL_user_free(dc->a[i]);
904   MIDL_user_free(dc);
905 }
906
907 static void
908 array_tests(void)
909 {
910   const char str1[25] = "Hello";
911   int m[2][3][4] =
912   {
913     {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
914     {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
915   };
916   int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
917   vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
918   cps_t cps;
919   cpsc_t cpsc;
920   cs_t *cs;
921   int n;
922   int ca[5] = {1, -2, 3, -4, 5};
923   doub_carr_t *dc;
924
925   ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
926
927   ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
928
929   ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
930   ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
931   ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
932   ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
933
934   ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
935   ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
936   ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
937
938   ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
939   ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
940   ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
941   ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
942
943   ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
944   cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
945   cs->n = 5;
946   cs->ca[0] = 3;
947   cs->ca[1] = 5;
948   cs->ca[2] = -2;
949   cs->ca[3] = -1;
950   cs->ca[4] = -4;
951   ok(sum_cs(cs) == 1, "RPC sum_cs\n");
952   HeapFree(GetProcessHeap(), 0, cs);
953
954   n = 5;
955   cps.pn = &n;
956   cps.ca1 = &c[2];
957   cps.n = 3;
958   cps.ca2 = &c[3];
959   ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
960
961   cpsc.a = 4;
962   cpsc.b = 5;
963   cpsc.c = 1;
964   cpsc.ca = c;
965   ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
966   cpsc.a = 4;
967   cpsc.b = 5;
968   cpsc.c = 0;
969   cpsc.ca = c;
970   ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
971
972   ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
973   ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
974   ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
975
976   dc = malloc(FIELD_OFFSET(doub_carr_t, a[2]));
977   dc->n = 2;
978   dc->a[0] = malloc(FIELD_OFFSET(doub_carr_1_t, a[3]));
979   dc->a[0]->n = 3;
980   dc->a[0]->a[0] = 5;
981   dc->a[0]->a[1] = 1;
982   dc->a[0]->a[2] = 8;
983   dc->a[1] = malloc(FIELD_OFFSET(doub_carr_1_t, a[2]));
984   dc->a[1]->n = 2;
985   dc->a[1]->a[0] = 2;
986   dc->a[1]->a[1] = 3;
987   ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
988   free(dc->a[0]);
989   free(dc->a[1]);
990   free(dc);
991
992   dc = NULL;
993   make_pyramid_doub_carr(4, &dc);
994   ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
995   free_pyramid_doub_carr(dc);
996
997   ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
998 }
999
1000 static void
1001 run_tests(void)
1002 {
1003   basic_tests();
1004   union_tests();
1005   pointer_tests();
1006   array_tests();
1007 }
1008
1009 static void
1010 client(const char *test)
1011 {
1012   if (strcmp(test, "tcp_basic") == 0)
1013   {
1014     static unsigned char iptcp[] = "ncacn_ip_tcp";
1015     static unsigned char address[] = "127.0.0.1";
1016     static unsigned char port[] = PORT;
1017     unsigned char *binding;
1018
1019     ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1020     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1021
1022     run_tests();
1023
1024     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1025     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1026   }
1027   else if (strcmp(test, "np_basic") == 0)
1028   {
1029     static unsigned char np[] = "ncacn_np";
1030     static unsigned char address[] = "\\\\.";
1031     static unsigned char pipe[] = PIPE;
1032     unsigned char *binding;
1033
1034     ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1035     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1036
1037     run_tests();
1038     stop();
1039
1040     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1041     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1042   }
1043 }
1044
1045 static void
1046 server(void)
1047 {
1048   static unsigned char iptcp[] = "ncacn_ip_tcp";
1049   static unsigned char port[] = PORT;
1050   static unsigned char np[] = "ncacn_np";
1051   static unsigned char pipe[] = PIPE;
1052
1053   ok(RPC_S_OK == RpcServerUseProtseqEp(iptcp, 20, port, NULL), "RpcServerUseProtseqEp\n");
1054   ok(RPC_S_OK == RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL), "RpcServerRegisterIf\n");
1055   ok(RPC_S_OK == RpcServerListen(1, 20, TRUE), "RpcServerListen\n");
1056
1057   stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1058   ok(stop_event != NULL, "CreateEvent failed\n");
1059
1060   ok(run_client("tcp_basic"), "tcp_basic client test failed\n");
1061
1062   ok(RPC_S_OK == RpcServerUseProtseqEp(np, 0, pipe, NULL), "RpcServerUseProtseqEp\n");
1063   ok(run_client("np_basic"), "np_basic client test failed\n");
1064
1065   ok(WAIT_OBJECT_0 == WaitForSingleObject(stop_event, 60000), "WaitForSingleObject\n");
1066   todo_wine {
1067     ok(RPC_S_OK == RpcMgmtWaitServerListen(), "RpcMgmtWaitServerListening\n");
1068   }
1069 }
1070
1071 START_TEST(server)
1072 {
1073   int argc;
1074   char **argv;
1075
1076   argc = winetest_get_mainargs(&argv);
1077   progname = argv[0];
1078
1079   if (argc == 3)
1080   {
1081     RpcTryExcept
1082     {
1083       client(argv[2]);
1084     }
1085     RpcExcept(TRUE)
1086     {
1087       trace("Exception %d\n", RpcExceptionCode());
1088     }
1089     RpcEndExcept
1090   }
1091   else
1092     server();
1093 }