rpcrt4: Reduce the timeout of waiting on the stop event in the server test to one...
[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 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
42 static void InitFunctionPointers(void)
43 {
44     HMODULE hrpcrt4 = GetModuleHandleA("rpcrt4.dll");
45
46     pNDRSContextMarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextMarshall2");
47     pNDRSContextUnmarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextUnmarshall2");
48 }
49
50 void __RPC_FAR *__RPC_USER
51 midl_user_allocate(size_t n)
52 {
53   return malloc(n);
54 }
55
56 void __RPC_USER
57 midl_user_free(void __RPC_FAR *p)
58 {
59   free(p);
60 }
61
62 static char *
63 xstrdup(const char *s)
64 {
65   char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
66   strcpy(d, s);
67   return d;
68 }
69
70 int
71 s_int_return(void)
72 {
73   return INT_CODE;
74 }
75
76 int
77 s_square(int x)
78 {
79   return x * x;
80 }
81
82 int
83 s_sum(int x, int y)
84 {
85   return x + y;
86 }
87
88 void
89 s_square_out(int x, int *y)
90 {
91   *y = s_square(x);
92 }
93
94 void
95 s_square_ref(int *x)
96 {
97   *x = s_square(*x);
98 }
99
100 int
101 s_str_length(const char *s)
102 {
103   return strlen(s);
104 }
105
106 int
107 s_str_t_length(str_t s)
108 {
109   return strlen(s);
110 }
111
112 int
113 s_cstr_length(const char *s, int n)
114 {
115   int len = 0;
116   while (0 < n-- && *s++)
117     ++len;
118   return len;
119 }
120
121 int
122 s_dot_self(vector_t *v)
123 {
124   return s_square(v->x) + s_square(v->y) + s_square(v->z);
125 }
126
127 double
128 s_square_half(double x, double *y)
129 {
130   *y = x / 2.0;
131   return x * x;
132 }
133
134 float
135 s_square_half_float(float x, float *y)
136 {
137   *y = x / 2.0f;
138   return x * x;
139 }
140
141 long
142 s_square_half_long(long x, long *y)
143 {
144   *y = x / 2;
145   return x * x;
146 }
147
148 int
149 s_sum_fixed_array(int a[5])
150 {
151   return a[0] + a[1] + a[2] + a[3] + a[4];
152 }
153
154 int
155 s_pints_sum(pints_t *pints)
156 {
157   return *pints->pi + **pints->ppi + ***pints->pppi;
158 }
159
160 double
161 s_ptypes_sum(ptypes_t *pt)
162 {
163   return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
164 }
165
166 int
167 s_dot_pvectors(pvectors_t *p)
168 {
169   return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
170 }
171
172 int
173 s_sum_sp(sp_t *sp)
174 {
175   return sp->x + sp->s->x;
176 }
177
178 double
179 s_square_sun(sun_t *su)
180 {
181   switch (su->s)
182   {
183   case SUN_I: return su->u.i * su->u.i;
184   case SUN_F1:
185   case SUN_F2: return su->u.f * su->u.f;
186   case SUN_PI: return (*su->u.pi) * (*su->u.pi);
187   default:
188     return 0.0;
189   }
190 }
191
192 int
193 s_test_list_length(test_list_t *list)
194 {
195   return (list->t == TL_LIST
196           ? 1 + s_test_list_length(list->u.tail)
197           : 0);
198 }
199
200 int
201 s_sum_fixed_int_3d(int m[2][3][4])
202 {
203   int i, j, k;
204   int sum = 0;
205
206   for (i = 0; i < 2; ++i)
207     for (j = 0; j < 3; ++j)
208       for (k = 0; k < 4; ++k)
209         sum += m[i][j][k];
210
211   return sum;
212 }
213
214 int
215 s_sum_conf_array(int x[], int n)
216 {
217   int *p = x, *end = p + n;
218   int sum = 0;
219
220   while (p < end)
221     sum += *p++;
222
223   return sum;
224 }
225
226 int
227 s_sum_unique_conf_array(int x[], int n)
228 {
229   return s_sum_conf_array(x, n);
230 }
231
232 int
233 s_sum_unique_conf_ptr(int *x, int n)
234 {
235   return x ? s_sum_conf_array(x, n) : 0;
236 }
237
238 int
239 s_sum_var_array(int x[20], int n)
240 {
241   ok(0 <= n, "RPC sum_var_array\n");
242   ok(n <= 20, "RPC sum_var_array\n");
243
244   return s_sum_conf_array(x, n);
245 }
246
247 int
248 s_dot_two_vectors(vector_t vs[2])
249 {
250   return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
251 }
252
253 int
254 s_sum_cs(cs_t *cs)
255 {
256   return s_sum_conf_array(cs->ca, cs->n);
257 }
258
259 int
260 s_sum_cps(cps_t *cps)
261 {
262   int sum = 0;
263   int i;
264
265   for (i = 0; i < *cps->pn; ++i)
266     sum += cps->ca1[i];
267
268   for (i = 0; i < 2 * cps->n; ++i)
269     sum += cps->ca2[i];
270
271   return sum;
272 }
273
274 int
275 s_sum_cpsc(cpsc_t *cpsc)
276 {
277   int sum = 0;
278   int i;
279   for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
280     sum += cpsc->ca[i];
281   return sum;
282 }
283
284 int
285 s_square_puint(puint_t p)
286 {
287   int n = atoi(p);
288   return n * n;
289 }
290
291 int
292 s_sum_puints(puints_t *p)
293 {
294   int sum = 0;
295   int i;
296   for (i = 0; i < p->n; ++i)
297     sum += atoi(p->ps[i]);
298   return sum;
299 }
300
301 int
302 s_sum_cpuints(cpuints_t *p)
303 {
304   int sum = 0;
305   int i;
306   for (i = 0; i < p->n; ++i)
307     sum += atoi(p->ps[i]);
308   return sum;
309 }
310
311 int
312 s_dot_copy_vectors(vector_t u, vector_t v)
313 {
314   return u.x * v.x + u.y * v.y + u.z * v.z;
315 }
316
317 int
318 s_square_test_us(test_us_t *tus)
319 {
320   int n = atoi(tus->us.x);
321   return n * n;
322 }
323
324 double
325 s_square_encu(encu_t *eu)
326 {
327   switch (eu->t)
328   {
329   case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
330   case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
331   default:
332     return 0.0;
333   }
334 }
335
336 void
337 s_check_se2(se_t *s)
338 {
339   ok(s->f == E2, "check_se2\n");
340 }
341
342 int
343 s_sum_parr(int *a[3])
344 {
345   return s_sum_pcarr(a, 3);
346 }
347
348 int
349 s_sum_pcarr(int *a[], int n)
350 {
351   int i, s = 0;
352   for (i = 0; i < n; ++i)
353     s += *a[i];
354   return s;
355 }
356
357 int
358 s_enum_ord(e_t e)
359 {
360   switch (e)
361   {
362   case E1: return 1;
363   case E2: return 2;
364   case E3: return 3;
365   case E4: return 4;
366   default:
367     return 0;
368   }
369 }
370
371 double
372 s_square_encue(encue_t *eue)
373 {
374   switch (eue->t)
375   {
376   case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
377   case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
378   default:
379     return 0.0;
380   }
381 }
382
383 int
384 s_sum_toplev_conf_2n(int *x, int n)
385 {
386   int sum = 0;
387   int i;
388   for (i = 0; i < 2 * n; ++i)
389     sum += x[i];
390   return sum;
391 }
392
393 int
394 s_sum_toplev_conf_cond(int *x, int a, int b, int c)
395 {
396   int sum = 0;
397   int n = c ? a : b;
398   int i;
399   for (i = 0; i < n; ++i)
400     sum += x[i];
401   return sum;
402 }
403
404 double
405 s_sum_aligns(aligns_t *a)
406 {
407   return a->c + a->i + a->s + a->d;
408 }
409
410 int
411 s_sum_padded(padded_t *p)
412 {
413   return p->i + p->c;
414 }
415
416 int
417 s_sum_padded2(padded_t ps[2])
418 {
419   return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
420 }
421
422 int
423 s_sum_padded_conf(padded_t *ps, int n)
424 {
425   int sum = 0;
426   int i;
427   for (i = 0; i < n; ++i)
428     sum += s_sum_padded(&ps[i]);
429   return sum;
430 }
431
432 int
433 s_sum_bogus(bogus_t *b)
434 {
435   return *b->h.p1 + *b->p2 + *b->p3 + b->c;
436 }
437
438 void
439 s_check_null(int *null)
440 {
441   ok(!null, "RPC check_null\n");
442 }
443
444 int
445 s_str_struct_len(str_struct_t *s)
446 {
447   return lstrlenA(s->s);
448 }
449
450 int
451 s_wstr_struct_len(wstr_struct_t *s)
452 {
453   return lstrlenW(s->s);
454 }
455
456 int
457 s_sum_doub_carr(doub_carr_t *dc)
458 {
459   int i, j;
460   int sum = 0;
461   for (i = 0; i < dc->n; ++i)
462     for (j = 0; j < dc->a[i]->n; ++j)
463       sum += dc->a[i]->a[j];
464   return sum;
465 }
466
467 void
468 s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
469 {
470   doub_carr_t *t;
471   int i, j;
472   t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
473   t->n = n;
474   for (i = 0; i < n; ++i)
475   {
476     int v = i + 1;
477     t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
478     t->a[i]->n = v;
479     for (j = 0; j < v; ++j)
480       t->a[i]->a[j] = j + 1;
481   }
482   *dc = t;
483 }
484
485 unsigned
486 s_hash_bstr(bstr_t b)
487 {
488   short n = b[-1];
489   short *s = b;
490   unsigned hash = 0;
491   short i;
492   for (i = 0; i < n; ++i)
493     hash = 5 * hash + (unsigned) s[i];
494   return hash;
495 }
496
497 void
498 s_get_name(name_t *name)
499 {
500   const char bossman[] = "Jeremy White";
501   memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
502   /* ensure nul-termination */
503   if (name->size < sizeof(bossman))
504     name->name[name->size - 1] = 0;
505 }
506
507 int
508 s_sum_pcarr2(int n, int **pa)
509 {
510   return s_sum_conf_array(*pa, n);
511 }
512
513 int
514 s_sum_L1_norms(int n, vector_t *vs)
515 {
516   int i;
517   int sum = 0;
518   for (i = 0; i < n; ++i)
519     sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
520   return sum;
521 }
522
523 s123_t *
524 s_get_s123(void)
525 {
526   s123_t *s = MIDL_user_allocate(sizeof *s);
527   s->f1 = 1;
528   s->f2 = 2;
529   s->f3 = 3;
530   return s;
531 }
532
533 str_t
534 s_get_filename(void)
535 {
536     return (char *)__FILE__;
537 }
538
539 void
540 s_context_handle_test(void)
541 {
542     NDR_SCONTEXT h;
543     RPC_BINDING_HANDLE binding;
544     RPC_STATUS status;
545     unsigned char buf[20];
546     static RPC_SERVER_INTERFACE server_if =
547     {
548         sizeof(RPC_SERVER_INTERFACE),
549         {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
550         {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
551         NULL,
552         0,
553         0,
554         0,
555         0,
556         0,
557     };
558
559     binding = I_RpcGetCurrentCallHandle();
560     ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
561
562     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
563     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
564
565     /* marshal a context handle with NULL userContext */
566     memset(buf, 0xcc, sizeof(buf));
567     pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
568     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
569     ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");
570
571     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
572     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
573
574     /* marshal a context handle with non-NULL userContext */
575     memset(buf, 0xcc, sizeof(buf));
576     h->userContext = (void *)0xdeadbeef;
577     pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
578     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
579     ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
580
581     h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
582     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
583     ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
584
585     /* marshal a context handle with an interface specified */
586     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
587     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
588
589     memset(buf, 0xcc, sizeof(buf));
590     h->userContext = (void *)0xcafebabe;
591     pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
592     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
593     ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
594
595     h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
596     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
597     ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
598
599     /* test same interface data, but different pointer */
600     /* raises ERROR_INVALID_HANDLE exception */
601     if (0)
602     {
603         RPC_SERVER_INTERFACE server_if_clone = server_if;
604
605         pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
606     }
607
608     /* test different interface data, but different pointer */
609     /* raises ERROR_INVALID_HANDLE exception */
610     if (0)
611     {
612         static RPC_SERVER_INTERFACE server_if2 =
613         {
614             sizeof(RPC_SERVER_INTERFACE),
615             {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
616             {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
617             NULL,
618             0,
619             0,
620             0,
621             0,
622             0,
623         };
624         pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
625
626         pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
627     }
628 }
629
630 void
631 s_get_5numbers(int count, pints_t n[5])
632 {
633     int i;
634     for (i = 0; i < count; i++)
635     {
636         n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
637         *n[i].pi = i;
638         n[i].ppi = NULL;
639         n[i].pppi = NULL;
640     }
641 }
642
643 void
644 s_get_numbers(int length, int size, pints_t n[])
645 {
646     int i;
647     for (i = 0; i < length; i++)
648     {
649         n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
650         *n[i].pi = i;
651         n[i].ppi = NULL;
652         n[i].pppi = NULL;
653     }
654 }
655
656 void
657 s_stop(void)
658 {
659   ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
660   ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
661   ok(SetEvent(stop_event), "SetEvent\n");
662 }
663
664 static void
665 make_cmdline(char buffer[MAX_PATH], const char *test)
666 {
667   sprintf(buffer, "%s server %s", progname, test);
668 }
669
670 static void
671 run_client(const char *test)
672 {
673   char cmdline[MAX_PATH];
674   PROCESS_INFORMATION info;
675   STARTUPINFOA startup;
676
677   memset(&startup, 0, sizeof startup);
678   startup.cb = sizeof startup;
679
680   make_cmdline(cmdline, test);
681   ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
682   winetest_wait_child_process( info.hProcess );
683   ok(CloseHandle(info.hProcess), "CloseHandle\n");
684   ok(CloseHandle(info.hThread), "CloseHandle\n");
685 }
686
687 static void
688 basic_tests(void)
689 {
690   char string[] = "I am a string";
691   WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
692   int f[5] = {1, 3, 0, -2, -4};
693   vector_t a = {1, 3, 7};
694   vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
695   pvectors_t pvecs = {&vec1, &pvec2};
696   sp_inner_t spi = {42};
697   sp_t sp = {-13, &spi};
698   aligns_t aligns;
699   pints_t pints;
700   ptypes_t ptypes;
701   padded_t padded;
702   padded_t padded2[2];
703   bogus_t bogus;
704   int i1, i2, i3, *pi2, *pi3, **ppi3;
705   double u, v;
706   float s, t;
707   long q, r;
708   short h;
709   char c;
710   int x;
711   str_struct_t ss = {string};
712   wstr_struct_t ws = {wstring};
713   str_t str;
714   se_t se;
715
716   ok(int_return() == INT_CODE, "RPC int_return\n");
717
718   ok(square(7) == 49, "RPC square\n");
719   ok(sum(23, -4) == 19, "RPC sum\n");
720
721   x = 0;
722   square_out(11, &x);
723   ok(x == 121, "RPC square_out\n");
724
725   x = 5;
726   square_ref(&x);
727   ok(x == 25, "RPC square_ref\n");
728
729   ok(str_length(string) == strlen(string), "RPC str_length\n");
730   ok(str_t_length(string) == strlen(string), "RPC str_length\n");
731   ok(dot_self(&a) == 59, "RPC dot_self\n");
732
733   ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
734   ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
735
736   v = 0.0;
737   u = square_half(3.0, &v);
738   ok(u == 9.0, "RPC square_half\n");
739   ok(v == 1.5, "RPC square_half\n");
740
741   t = 0.0f;
742   s = square_half_float(3.0f, &t);
743   ok(s == 9.0f, "RPC square_half_float\n");
744   ok(t == 1.5f, "RPC square_half_float\n");
745
746   r = 0;
747   q = square_half_long(3, &r);
748   ok(q == 9, "RPC square_half_long\n");
749   ok(r == 1, "RPC square_half_long\n");
750
751   i1 = 19;
752   i2 = -3;
753   i3 = -29;
754   pi2 = &i2;
755   pi3 = &i3;
756   ppi3 = &pi3;
757   pints.pi = &i1;
758   pints.ppi = &pi2;
759   pints.pppi = &ppi3;
760   ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
761
762   c = 10;
763   h = 3;
764   q = 14;
765   s = -5.0f;
766   u = 11.0;
767   ptypes.pc = &c;
768   ptypes.ps = &h;
769   ptypes.pl = &q;
770   ptypes.pf = &s;
771   ptypes.pd = &u;
772   ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
773
774   ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
775   ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
776   ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
777   ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
778
779   ok(enum_ord(E1) == 1, "RPC enum_ord\n");
780   ok(enum_ord(E2) == 2, "RPC enum_ord\n");
781   ok(enum_ord(E3) == 3, "RPC enum_ord\n");
782   ok(enum_ord(E4) == 4, "RPC enum_ord\n");
783
784   se.f = E2;
785   check_se2(&se);
786
787   memset(&aligns, 0, sizeof(aligns));
788   aligns.c = 3;
789   aligns.i = 4;
790   aligns.s = 5;
791   aligns.d = 6.0;
792   ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
793
794   padded.i = -3;
795   padded.c = 8;
796   ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
797   padded2[0].i = -5;
798   padded2[0].c = 1;
799   padded2[1].i = 3;
800   padded2[1].c = 7;
801   ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
802   padded2[0].i = -5;
803   padded2[0].c = 1;
804   padded2[1].i = 3;
805   padded2[1].c = 7;
806   ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
807
808   i1 = 14;
809   i2 = -7;
810   i3 = -4;
811   bogus.h.p1 = &i1;
812   bogus.p2 = &i2;
813   bogus.p3 = &i3;
814   bogus.c = 9;
815   ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
816
817   check_null(NULL);
818
819   str = get_filename();
820   ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
821   midl_user_free(str);
822 }
823
824 static void
825 union_tests(void)
826 {
827   encue_t eue;
828   encu_t eu;
829   sun_t su;
830   int i;
831
832   su.s = SUN_I;
833   su.u.i = 9;
834   ok(square_sun(&su) == 81.0, "RPC square_sun\n");
835
836   su.s = SUN_F1;
837   su.u.f = 5.0;
838   ok(square_sun(&su) == 25.0, "RPC square_sun\n");
839
840   su.s = SUN_F2;
841   su.u.f = -2.0;
842   ok(square_sun(&su) == 4.0, "RPC square_sun\n");
843
844   su.s = SUN_PI;
845   su.u.pi = &i;
846   i = 11;
847   ok(square_sun(&su) == 121.0, "RPC square_sun\n");
848
849   eu.t = ENCU_I;
850   eu.tagged_union.i = 7;
851   ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
852
853   eu.t = ENCU_F;
854   eu.tagged_union.f = 3.0;
855   ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
856
857   eue.t = E1;
858   eue.tagged_union.i1 = 8;
859   ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
860
861   eue.t = E2;
862   eue.tagged_union.f2 = 10.0;
863   ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
864 }
865
866 static test_list_t *
867 null_list(void)
868 {
869   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
870   n->t = TL_NULL;
871   n->u.x = 0;
872   return n;
873 }
874
875 static test_list_t *
876 make_list(test_list_t *tail)
877 {
878   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
879   n->t = TL_LIST;
880   n->u.tail = tail;
881   return n;
882 }
883
884 static void
885 free_list(test_list_t *list)
886 {
887   if (list->t == TL_LIST)
888     free_list(list->u.tail);
889   HeapFree(GetProcessHeap(), 0, list);
890 }
891
892 ULONG __RPC_USER
893 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
894 {
895   return start + sizeof(int);
896 }
897
898 unsigned char * __RPC_USER
899 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
900 {
901   int n = atoi(*p);
902   memcpy(buffer, &n, sizeof n);
903   return buffer + sizeof n;
904 }
905
906 unsigned char * __RPC_USER
907 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
908 {
909   int n;
910   memcpy(&n, buffer, sizeof n);
911   *p = HeapAlloc(GetProcessHeap(), 0, 10);
912   sprintf(*p, "%d", n);
913   return buffer + sizeof n;
914 }
915
916 void __RPC_USER
917 puint_t_UserFree(ULONG *flags, puint_t *p)
918 {
919   HeapFree(GetProcessHeap(), 0, *p);
920 }
921
922 ULONG __RPC_USER
923 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
924 {
925   return start + sizeof(struct wire_us);
926 }
927
928 unsigned char * __RPC_USER
929 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
930 {
931   struct wire_us wus;
932   wus.x = atoi(pus->x);
933   memcpy(buffer, &wus, sizeof wus);
934   return buffer + sizeof wus;
935 }
936
937 unsigned char * __RPC_USER
938 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
939 {
940   struct wire_us wus;
941   memcpy(&wus, buffer, sizeof wus);
942   pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
943   sprintf(pus->x, "%d", wus.x);
944   return buffer + sizeof wus;
945 }
946
947 void __RPC_USER
948 us_t_UserFree(ULONG *flags, us_t *pus)
949 {
950   HeapFree(GetProcessHeap(), 0, pus->x);
951 }
952
953 ULONG __RPC_USER
954 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
955 {
956   return start + FIELD_OFFSET(wire_bstr_t, data[(*b)[-1]]);
957 }
958
959 unsigned char * __RPC_USER
960 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
961 {
962   wire_bstr_t *wb = (wire_bstr_t *) buffer;
963   wb->n = (*b)[-1];
964   memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
965   return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
966 }
967
968 unsigned char * __RPC_USER
969 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
970 {
971   wire_bstr_t *wb = (wire_bstr_t *) buffer;
972   short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
973   data[0] = wb->n;
974   memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
975   *b = &data[1];
976   return buffer + FIELD_OFFSET(wire_bstr_t, data[wb->n]);
977 }
978
979 void __RPC_USER
980 bstr_t_UserFree(ULONG *flags, bstr_t *b)
981 {
982   HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
983 }
984
985 static void
986 pointer_tests(void)
987 {
988   int a[] = {1, 2, 3, 4};
989   char p1[] = "11";
990   test_list_t *list = make_list(make_list(make_list(null_list())));
991   test_us_t tus = {{p1}};
992   int *pa[4];
993   puints_t pus;
994   cpuints_t cpus;
995   short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
996   bstr_t bstr = &bstr_data[1];
997   name_t name;
998   void *buffer;
999   int *pa2;
1000   s123_t *s123;
1001
1002   ok(test_list_length(list) == 3, "RPC test_list_length\n");
1003   ok(square_puint(p1) == 121, "RPC square_puint\n");
1004   pus.n = 4;
1005   pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
1006   pus.ps[0] = xstrdup("5");
1007   pus.ps[1] = xstrdup("6");
1008   pus.ps[2] = xstrdup("7");
1009   pus.ps[3] = xstrdup("8");
1010   ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
1011   HeapFree(GetProcessHeap(), 0, pus.ps[0]);
1012   HeapFree(GetProcessHeap(), 0, pus.ps[1]);
1013   HeapFree(GetProcessHeap(), 0, pus.ps[2]);
1014   HeapFree(GetProcessHeap(), 0, pus.ps[3]);
1015   HeapFree(GetProcessHeap(), 0, pus.ps);
1016   cpus.n = 4;
1017   cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
1018   cpus.ps[0] = xstrdup("5");
1019   cpus.ps[1] = xstrdup("6");
1020   cpus.ps[2] = xstrdup("7");
1021   cpus.ps[3] = xstrdup("8");
1022   ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
1023   HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
1024   HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
1025   HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
1026   HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
1027   HeapFree(GetProcessHeap(), 0, cpus.ps);
1028   ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
1029
1030   pa[0] = &a[0];
1031   pa[1] = &a[1];
1032   pa[2] = &a[2];
1033   ok(sum_parr(pa) == 6, "RPC sum_parr\n");
1034
1035   pa[0] = &a[0];
1036   pa[1] = &a[1];
1037   pa[2] = &a[2];
1038   pa[3] = &a[3];
1039   ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
1040
1041   ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
1042
1043   free_list(list);
1044
1045   name.size = 10;
1046   name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
1047   get_name(&name);
1048   ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
1049   ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
1050   HeapFree(GetProcessHeap(), 0, name.name);
1051
1052   pa2 = a;
1053   ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
1054
1055   s123 = get_s123();
1056   ok(s123->f1 == 1 && s123->f2 == 2 && s123->f3 == 3, "RPC get_s123\n");
1057   MIDL_user_free(s123);
1058 }
1059
1060 static int
1061 check_pyramid_doub_carr(doub_carr_t *dc)
1062 {
1063   int i, j;
1064   for (i = 0; i < dc->n; ++i)
1065     for (j = 0; j < dc->a[i]->n; ++j)
1066       if (dc->a[i]->a[j] != j + 1)
1067         return FALSE;
1068   return TRUE;
1069 }
1070
1071 static void
1072 free_pyramid_doub_carr(doub_carr_t *dc)
1073 {
1074   int i;
1075   for (i = 0; i < dc->n; ++i)
1076     MIDL_user_free(dc->a[i]);
1077   MIDL_user_free(dc);
1078 }
1079
1080 static void
1081 array_tests(void)
1082 {
1083   const char str1[25] = "Hello";
1084   int m[2][3][4] =
1085   {
1086     {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
1087     {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
1088   };
1089   int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1090   vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1091   cps_t cps;
1092   cpsc_t cpsc;
1093   cs_t *cs;
1094   int n;
1095   int ca[5] = {1, -2, 3, -4, 5};
1096   doub_carr_t *dc;
1097   int *pi;
1098   pints_t api[5];
1099
1100   ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
1101
1102   ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
1103
1104   ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
1105   ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1106   ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1107   ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1108
1109   ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
1110   ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
1111   ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
1112
1113   ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
1114   ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1115   ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1116   ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1117
1118   ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1119   cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1120   cs->n = 5;
1121   cs->ca[0] = 3;
1122   cs->ca[1] = 5;
1123   cs->ca[2] = -2;
1124   cs->ca[3] = -1;
1125   cs->ca[4] = -4;
1126   ok(sum_cs(cs) == 1, "RPC sum_cs\n");
1127   HeapFree(GetProcessHeap(), 0, cs);
1128
1129   n = 5;
1130   cps.pn = &n;
1131   cps.ca1 = &c[2];
1132   cps.n = 3;
1133   cps.ca2 = &c[3];
1134   ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1135
1136   cpsc.a = 4;
1137   cpsc.b = 5;
1138   cpsc.c = 1;
1139   cpsc.ca = c;
1140   ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1141   cpsc.a = 4;
1142   cpsc.b = 5;
1143   cpsc.c = 0;
1144   cpsc.ca = c;
1145   ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1146
1147   ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
1148   ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
1149   ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1150
1151   dc = malloc(FIELD_OFFSET(doub_carr_t, a[2]));
1152   dc->n = 2;
1153   dc->a[0] = malloc(FIELD_OFFSET(doub_carr_1_t, a[3]));
1154   dc->a[0]->n = 3;
1155   dc->a[0]->a[0] = 5;
1156   dc->a[0]->a[1] = 1;
1157   dc->a[0]->a[2] = 8;
1158   dc->a[1] = malloc(FIELD_OFFSET(doub_carr_1_t, a[2]));
1159   dc->a[1]->n = 2;
1160   dc->a[1]->a[0] = 2;
1161   dc->a[1]->a[1] = 3;
1162   ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1163   free(dc->a[0]);
1164   free(dc->a[1]);
1165   free(dc);
1166
1167   dc = NULL;
1168   make_pyramid_doub_carr(4, &dc);
1169   ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
1170   free_pyramid_doub_carr(dc);
1171
1172   ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1173
1174   memset(api, 0, sizeof(api));
1175   pi = HeapAlloc(GetProcessHeap(), 0, sizeof(*pi));
1176   *pi = -1;
1177   api[0].pi = pi;
1178   get_5numbers(1, api);
1179   ok(api[0].pi == pi, "RPC varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1180   ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *pi);
1181
1182   api[0].pi = pi;
1183   get_numbers(1, 1, api);
1184   ok(api[0].pi == pi, "RPC conformant varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1185   ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *pi);
1186   HeapFree(GetProcessHeap(), 0, pi);
1187 }
1188
1189 static void
1190 run_tests(void)
1191 {
1192   basic_tests();
1193   union_tests();
1194   pointer_tests();
1195   array_tests();
1196   context_handle_test();
1197 }
1198
1199 static void
1200 client(const char *test)
1201 {
1202   if (strcmp(test, "tcp_basic") == 0)
1203   {
1204     static unsigned char iptcp[] = "ncacn_ip_tcp";
1205     static unsigned char address[] = "127.0.0.1";
1206     static unsigned char port[] = PORT;
1207     unsigned char *binding;
1208
1209     ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1210     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1211
1212     run_tests();
1213
1214     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1215     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1216   }
1217   else if (strcmp(test, "np_basic") == 0)
1218   {
1219     static unsigned char np[] = "ncacn_np";
1220     static unsigned char address[] = "\\\\.";
1221     static unsigned char pipe[] = PIPE;
1222     unsigned char *binding;
1223
1224     ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1225     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1226
1227     run_tests();
1228     stop();
1229
1230     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1231     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1232   }
1233 }
1234
1235 static void
1236 server(void)
1237 {
1238   static unsigned char iptcp[] = "ncacn_ip_tcp";
1239   static unsigned char port[] = PORT;
1240   static unsigned char np[] = "ncacn_np";
1241   static unsigned char pipe[] = PIPE;
1242   RPC_STATUS status, iptcp_status, np_status;
1243   RPC_STATUS (RPC_ENTRY *pRpcServerRegisterIfEx)(RPC_IF_HANDLE,UUID*,
1244     RPC_MGR_EPV*, unsigned int,unsigned int,RPC_IF_CALLBACK_FN*);
1245   DWORD ret;
1246
1247   iptcp_status = RpcServerUseProtseqEp(iptcp, 20, port, NULL);
1248   ok(iptcp_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_ip_tcp) failed with status %ld\n", iptcp_status);
1249   np_status = RpcServerUseProtseqEp(np, 0, pipe, NULL);
1250   ok(np_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %ld\n", np_status);
1251
1252   pRpcServerRegisterIfEx = (void *)GetProcAddress(GetModuleHandle("rpcrt4.dll"), "RpcServerRegisterIfEx");
1253   if (pRpcServerRegisterIfEx)
1254   {
1255     trace("Using RpcServerRegisterIfEx\n");
1256     status = pRpcServerRegisterIfEx(s_IServer_v0_0_s_ifspec, NULL, NULL,
1257                                     RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
1258                                     RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
1259   }
1260   else
1261     status = RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL);
1262   ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %ld\n", status);
1263   status = RpcServerListen(1, 20, TRUE);
1264   ok(status == RPC_S_OK, "RpcServerListen failed with status %ld\n", status);
1265   stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1266   ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
1267
1268   if (iptcp_status == RPC_S_OK)
1269     run_client("tcp_basic");
1270   else
1271     skip("tcp_basic tests skipped due to earlier failure\n");
1272
1273   if (np_status == RPC_S_OK)
1274     run_client("np_basic");
1275   else
1276   {
1277     skip("np_basic tests skipped due to earlier failure\n");
1278     /* np client is what signals stop_event, so bail out if we didn't run do it */
1279     return;
1280   }
1281
1282   ret = WaitForSingleObject(stop_event, 1000);
1283   ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
1284   /* if the stop event didn't fire then RpcMgmtWaitServerListen will wait
1285    * forever, so don't bother calling it in this case */
1286   if (ret == WAIT_OBJECT_0)
1287   {
1288     status = RpcMgmtWaitServerListen();
1289     todo_wine {
1290       ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %ld\n", status);
1291     }
1292   }
1293 }
1294
1295 START_TEST(server)
1296 {
1297   int argc;
1298   char **argv;
1299
1300   InitFunctionPointers();
1301
1302   argc = winetest_get_mainargs(&argv);
1303   progname = argv[0];
1304
1305   if (argc == 3)
1306   {
1307     RpcTryExcept
1308     {
1309       client(argv[2]);
1310     }
1311     RpcExcept(TRUE)
1312     {
1313       trace("Exception %d\n", RpcExceptionCode());
1314     }
1315     RpcEndExcept
1316   }
1317   else
1318     server();
1319 }