Release 1.5.29.
[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 <secext.h>
23 #include <rpcdce.h>
24 #include "wine/test.h"
25 #include "server.h"
26 #include "server_defines.h"
27
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #define PORT "4114"
33 #define PIPE "\\pipe\\wine_rpcrt4_test"
34
35 #define INT_CODE 4198
36
37 static const char *progname;
38 static BOOL old_windows_version;
39
40 static HANDLE stop_event;
41
42 static void (WINAPI *pNDRSContextMarshall2)(RPC_BINDING_HANDLE, NDR_SCONTEXT, void*, NDR_RUNDOWN, void*, ULONG);
43 static NDR_SCONTEXT (WINAPI *pNDRSContextUnmarshall2)(RPC_BINDING_HANDLE, void*, ULONG, void*, ULONG);
44 static RPC_STATUS (WINAPI *pRpcServerRegisterIfEx)(RPC_IF_HANDLE,UUID*, RPC_MGR_EPV*, unsigned int,
45                    unsigned int,RPC_IF_CALLBACK_FN*);
46 static BOOLEAN (WINAPI *pGetUserNameExA)(EXTENDED_NAME_FORMAT, LPSTR, PULONG);
47 static RPC_STATUS (WINAPI *pRpcBindingSetAuthInfoExA)(RPC_BINDING_HANDLE, RPC_CSTR, ULONG, ULONG,
48                                                       RPC_AUTH_IDENTITY_HANDLE, ULONG, RPC_SECURITY_QOS *);
49 static RPC_STATUS (WINAPI *pRpcServerRegisterAuthInfoA)(RPC_CSTR, ULONG, RPC_AUTH_KEY_RETRIEVAL_FN, LPVOID);
50
51 static char *domain_and_user;
52
53 /* type check statements generated in header file */
54 fnprintf *p_printf = printf;
55
56 static void InitFunctionPointers(void)
57 {
58     HMODULE hrpcrt4 = GetModuleHandleA("rpcrt4.dll");
59     HMODULE hsecur32 = LoadLibraryA("secur32.dll");
60
61     pNDRSContextMarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextMarshall2");
62     pNDRSContextUnmarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextUnmarshall2");
63     pRpcServerRegisterIfEx = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterIfEx");
64     pRpcBindingSetAuthInfoExA = (void *)GetProcAddress(hrpcrt4, "RpcBindingSetAuthInfoExA");
65     pRpcServerRegisterAuthInfoA = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterAuthInfoA");
66     pGetUserNameExA = (void *)GetProcAddress(hsecur32, "GetUserNameExA");
67
68     if (!pNDRSContextMarshall2) old_windows_version = TRUE;
69 }
70
71 void __RPC_FAR *__RPC_USER
72 midl_user_allocate(SIZE_T n)
73 {
74   return HeapAlloc(GetProcessHeap(), 0, n);
75 }
76
77 void __RPC_USER
78 midl_user_free(void __RPC_FAR *p)
79 {
80   HeapFree(GetProcessHeap(), 0, p);
81 }
82
83 static char *
84 xstrdup(const char *s)
85 {
86   char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
87   strcpy(d, s);
88   return d;
89 }
90
91 int __cdecl s_int_return(void)
92 {
93   return INT_CODE;
94 }
95
96 int __cdecl s_square(int x)
97 {
98   return x * x;
99 }
100
101 int __cdecl s_sum(int x, int y)
102 {
103   return x + y;
104 }
105
106 signed char __cdecl s_sum_char(signed char x, signed char y)
107 {
108     return x + y;
109 }
110
111 short __cdecl s_sum_short(short x, short y)
112 {
113     return x + y;
114 }
115
116 int __cdecl s_sum_float(float x, float y)
117 {
118     return x + y;
119 }
120
121 int __cdecl s_sum_double_int(int x, double y)
122 {
123     return x + y;
124 }
125
126 hyper __cdecl s_sum_hyper(hyper x, hyper y)
127 {
128     return x + y;
129 }
130
131 int __cdecl s_sum_hyper_int(hyper x, hyper y)
132 {
133     return x + y;
134 }
135
136 int __cdecl s_sum_char_hyper(signed char x, hyper y)
137 {
138     return x + y;
139 }
140
141 void __cdecl s_square_out(int x, int *y)
142 {
143   *y = s_square(x);
144 }
145
146 void __cdecl s_square_ref(int *x)
147 {
148   *x = s_square(*x);
149 }
150
151 int __cdecl s_str_length(const char *s)
152 {
153   return strlen(s);
154 }
155
156 int __cdecl s_str_t_length(str_t s)
157 {
158   return strlen(s);
159 }
160
161 int __cdecl s_cstr_length(const char *s, int n)
162 {
163   int len = 0;
164   while (0 < n-- && *s++)
165     ++len;
166   return len;
167 }
168
169 int __cdecl s_dot_self(vector_t *v)
170 {
171   return s_square(v->x) + s_square(v->y) + s_square(v->z);
172 }
173
174 double __cdecl s_square_half(double x, double *y)
175 {
176   *y = x / 2.0;
177   return x * x;
178 }
179
180 float __cdecl s_square_half_float(float x, float *y)
181 {
182   *y = x / 2.0f;
183   return x * x;
184 }
185
186 LONG __cdecl s_square_half_long(LONG x, LONG *y)
187 {
188   *y = x / 2;
189   return x * x;
190 }
191
192 int __cdecl s_sum_fixed_array(int a[5])
193 {
194   return a[0] + a[1] + a[2] + a[3] + a[4];
195 }
196
197 int __cdecl s_pints_sum(pints_t *pints)
198 {
199   return *pints->pi + **pints->ppi + ***pints->pppi;
200 }
201
202 double __cdecl s_ptypes_sum(ptypes_t *pt)
203 {
204   return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
205 }
206
207 int __cdecl s_dot_pvectors(pvectors_t *p)
208 {
209   return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
210 }
211
212 int __cdecl s_sum_sp(sp_t *sp)
213 {
214   return sp->x + sp->s->x;
215 }
216
217 double __cdecl s_square_sun(sun_t *su)
218 {
219   switch (su->s)
220   {
221   case SUN_I: return su->u.i * su->u.i;
222   case SUN_F1:
223   case SUN_F2: return su->u.f * su->u.f;
224   case SUN_PI: return (*su->u.pi) * (*su->u.pi);
225   default:
226     return 0.0;
227   }
228 }
229
230 int __cdecl s_test_list_length(test_list_t *list)
231 {
232   return (list->t == TL_LIST
233           ? 1 + s_test_list_length(list->u.tail)
234           : 0);
235 }
236
237 int __cdecl s_sum_fixed_int_3d(int m[2][3][4])
238 {
239   int i, j, k;
240   int sum = 0;
241
242   for (i = 0; i < 2; ++i)
243     for (j = 0; j < 3; ++j)
244       for (k = 0; k < 4; ++k)
245         sum += m[i][j][k];
246
247   return sum;
248 }
249
250 int __cdecl s_sum_conf_array(int x[], int n)
251 {
252   int *p = x, *end = p + n;
253   int sum = 0;
254
255   while (p < end)
256     sum += *p++;
257
258   return sum;
259 }
260
261 int __cdecl s_sum_conf_ptr_by_conf_ptr(int n1, int *n2_then_x1, int *x2)
262 {
263   int i;
264   int sum = 0;
265   if(n1 == 0)
266     return 0;
267
268   for(i = 1; i < n1; ++i)
269     sum += n2_then_x1[i];
270
271   for(i = 0; i < *n2_then_x1; ++i)
272     sum += x2[i];
273
274   return sum;
275 }
276
277 int __cdecl s_sum_unique_conf_array(int x[], int n)
278 {
279   return s_sum_conf_array(x, n);
280 }
281
282 int __cdecl s_sum_unique_conf_ptr(int *x, int n)
283 {
284   return x ? s_sum_conf_array(x, n) : 0;
285 }
286
287 int __cdecl s_sum_var_array(int x[20], int n)
288 {
289   ok(0 <= n, "RPC sum_var_array\n");
290   ok(n <= 20, "RPC sum_var_array\n");
291
292   return s_sum_conf_array(x, n);
293 }
294
295 int __cdecl s_sum_complex_array(int n, refpint_t pi[])
296 {
297   int total = 0;
298   for (; n > 0; n--)
299     total += *pi[n - 1];
300   return total;
301 }
302
303 int __cdecl s_dot_two_vectors(vector_t vs[2])
304 {
305   return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
306 }
307
308 void __cdecl s_get_number_array(int x[20], int *n)
309 {
310   int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
311   memcpy(x, c, sizeof(c));
312   *n = sizeof(c)/sizeof(c[0]);
313 }
314
315 int __cdecl s_sum_cs(cs_t *cs)
316 {
317   return s_sum_conf_array(cs->ca, cs->n);
318 }
319
320 int __cdecl s_sum_cps(cps_t *cps)
321 {
322   int sum = 0;
323   int i;
324
325   for (i = 0; i < *cps->pn; ++i)
326     sum += cps->ca1[i];
327
328   for (i = 0; i < 2 * cps->n; ++i)
329     sum += cps->ca2[i];
330
331   return sum;
332 }
333
334 int __cdecl s_sum_cpsc(cpsc_t *cpsc)
335 {
336   int sum = 0;
337   int i;
338   for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
339     sum += cpsc->ca[i];
340   return sum;
341 }
342
343 int __cdecl s_get_cpsc(int n, cpsc_t *cpsc)
344 {
345   int i, ret;
346
347   cpsc->a = 2 * n;
348   cpsc->b = 2;
349   cpsc->c = 1;
350   cpsc->ca = MIDL_user_allocate( cpsc->a * sizeof(int) );
351   for (i = ret = 0; i < cpsc->a; i++) cpsc->ca[i] = i;
352   for (i = ret = 0; i < cpsc->a; i++) ret += cpsc->ca[i];
353   return ret;
354 }
355
356 int __cdecl s_square_puint(puint_t p)
357 {
358   int n = atoi(p);
359   return n * n;
360 }
361
362 int __cdecl s_sum_puints(puints_t *p)
363 {
364   int sum = 0;
365   int i;
366   for (i = 0; i < p->n; ++i)
367     sum += atoi(p->ps[i]);
368   return sum;
369 }
370
371 int __cdecl s_sum_cpuints(cpuints_t *p)
372 {
373   int sum = 0;
374   int i;
375   for (i = 0; i < p->n; ++i)
376     sum += atoi(p->ps[i]);
377   return sum;
378 }
379
380 int __cdecl s_dot_copy_vectors(vector_t u, vector_t v)
381 {
382   return u.x * v.x + u.y * v.y + u.z * v.z;
383 }
384
385 int __cdecl s_square_test_us(test_us_t *tus)
386 {
387   int n = atoi(tus->us.x);
388   return n * n;
389 }
390
391 double __cdecl s_square_encu(encu_t *eu)
392 {
393   switch (eu->t)
394   {
395   case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
396   case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
397   default:
398     return 0.0;
399   }
400 }
401
402 double __cdecl s_square_unencu(int t, unencu_t *eu)
403 {
404   switch (t)
405   {
406   case ENCU_I: return eu->i * eu->i;
407   case ENCU_F: return eu->f * eu->f;
408   default:
409     return 0.0;
410   }
411 }
412
413 void __cdecl s_check_se2(se_t *s)
414 {
415   ok(s->f == E2, "check_se2\n");
416 }
417
418 int __cdecl s_sum_parr(int *a[3])
419 {
420   return s_sum_pcarr(a, 3);
421 }
422
423 int __cdecl s_sum_pcarr(int *a[], int n)
424 {
425   int i, s = 0;
426   for (i = 0; i < n; ++i)
427     s += *a[i];
428   return s;
429 }
430
431 int __cdecl s_enum_ord(e_t e)
432 {
433   switch (e)
434   {
435   case E1: return 1;
436   case E2: return 2;
437   case E3: return 3;
438   case E4: return 4;
439   default:
440     return 0;
441   }
442 }
443
444 double __cdecl s_square_encue(encue_t *eue)
445 {
446   switch (eue->t)
447   {
448   case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
449   case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
450   default:
451     return 0.0;
452   }
453 }
454
455 int __cdecl s_sum_toplev_conf_2n(int *x, int n)
456 {
457   int sum = 0;
458   int i;
459   for (i = 0; i < 2 * n; ++i)
460     sum += x[i];
461   return sum;
462 }
463
464 int __cdecl s_sum_toplev_conf_cond(int *x, int a, int b, int c)
465 {
466   int sum = 0;
467   int n = c ? a : b;
468   int i;
469   for (i = 0; i < n; ++i)
470     sum += x[i];
471   return sum;
472 }
473
474 double __cdecl s_sum_aligns(aligns_t *a)
475 {
476   return a->c + a->i + a->s + a->d;
477 }
478
479 int __cdecl s_sum_padded(padded_t *p)
480 {
481   return p->i + p->c;
482 }
483
484 int __cdecl s_sum_padded2(padded_t ps[2])
485 {
486   return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
487 }
488
489 int __cdecl s_sum_padded_conf(padded_t *ps, int n)
490 {
491   int sum = 0;
492   int i;
493   for (i = 0; i < n; ++i)
494     sum += s_sum_padded(&ps[i]);
495   return sum;
496 }
497
498 int __cdecl s_sum_bogus(bogus_t *b)
499 {
500   return *b->h.p1 + *b->p2 + *b->p3 + b->c;
501 }
502
503 void __cdecl s_check_null(int *null)
504 {
505   ok(!null, "RPC check_null\n");
506 }
507
508 int __cdecl s_str_struct_len(str_struct_t *s)
509 {
510   return lstrlenA(s->s);
511 }
512
513 int __cdecl s_wstr_struct_len(wstr_struct_t *s)
514 {
515   return lstrlenW(s->s);
516 }
517
518 int __cdecl s_sum_doub_carr(doub_carr_t *dc)
519 {
520   int i, j;
521   int sum = 0;
522   for (i = 0; i < dc->n; ++i)
523     for (j = 0; j < dc->a[i]->n; ++j)
524       sum += dc->a[i]->a[j];
525   return sum;
526 }
527
528 void __cdecl s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
529 {
530   doub_carr_t *t;
531   int i, j;
532   t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
533   t->n = n;
534   for (i = 0; i < n; ++i)
535   {
536     int v = i + 1;
537     t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
538     t->a[i]->n = v;
539     for (j = 0; j < v; ++j)
540       t->a[i]->a[j] = j + 1;
541   }
542   *dc = t;
543 }
544
545 unsigned __cdecl s_hash_bstr(bstr_t b)
546 {
547   short n = b[-1];
548   short *s = b;
549   unsigned hash = 0;
550   short i;
551   for (i = 0; i < n; ++i)
552     hash = 5 * hash + (unsigned) s[i];
553   return hash;
554 }
555
556 void __cdecl s_get_a_bstr(bstr_t *b)
557 {
558   bstr_t bstr;
559   short str[] = {5, 'W', 'i', 'n', 'e', 0};
560   bstr = HeapAlloc(GetProcessHeap(), 0, sizeof(str));
561   memcpy(bstr, str, sizeof(str));
562   *b = bstr + 1;
563 }
564
565 void __cdecl s_get_name(name_t *name)
566 {
567   const char bossman[] = "Jeremy White";
568   memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
569   /* ensure nul-termination */
570   if (name->size < sizeof(bossman))
571     name->name[name->size - 1] = 0;
572 }
573
574 int __cdecl s_sum_pcarr2(int n, int **pa)
575 {
576   return s_sum_conf_array(*pa, n);
577 }
578
579 int __cdecl s_sum_L1_norms(int n, vector_t *vs)
580 {
581   int i;
582   int sum = 0;
583   for (i = 0; i < n; ++i)
584     sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
585   return sum;
586 }
587
588 s123_t * __cdecl s_get_s123(void)
589 {
590   s123_t *s = MIDL_user_allocate(sizeof *s);
591   s->f1 = 1;
592   s->f2 = 2;
593   s->f3 = 3;
594   return s;
595 }
596
597 str_t __cdecl s_get_filename(void)
598 {
599     return (char *)__FILE__;
600 }
601
602 int __cdecl s_echo_ranged_int(int i, int j, int k)
603 {
604     return min( 100, i + j + k );
605 }
606
607 int __cdecl s_echo_ranged_int2(int i)
608 {
609     return i;
610 }
611
612 void __cdecl s_get_ranged_enum(renum_t *re)
613 {
614     *re = RE3;
615 }
616
617 void __cdecl s_context_handle_test(void)
618 {
619     NDR_SCONTEXT h;
620     RPC_BINDING_HANDLE binding;
621     RPC_STATUS status;
622     unsigned char buf[20];
623     static RPC_SERVER_INTERFACE server_if =
624     {
625         sizeof(RPC_SERVER_INTERFACE),
626         {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
627         {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
628         NULL,
629         0,
630         0,
631         0,
632         0,
633         0,
634     };
635
636     binding = I_RpcGetCurrentCallHandle();
637     ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
638
639     if (!pNDRSContextMarshall2 || !pNDRSContextUnmarshall2)
640     {
641         win_skip("NDRSContextMarshall2 or NDRSContextUnmarshall2 not exported from rpcrt4.dll\n");
642         return;
643     }
644
645     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
646     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
647
648     /* marshal a context handle with NULL userContext */
649     memset(buf, 0xcc, sizeof(buf));
650     pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
651     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
652     ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");
653
654     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
655     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
656
657     /* marshal a context handle with non-NULL userContext */
658     memset(buf, 0xcc, sizeof(buf));
659     h->userContext = (void *)0xdeadbeef;
660     pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
661     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
662     ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
663
664     /* raises ERROR_INVALID_HANDLE exception on Vista upwards */
665     if (0)
666     {
667     h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
668     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
669     ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
670
671     /* marshal a context handle with an interface specified */
672     h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
673     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
674
675     memset(buf, 0xcc, sizeof(buf));
676     h->userContext = (void *)0xcafebabe;
677     pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
678     ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
679     ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");
680
681     h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
682     ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
683     ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
684     }
685
686     /* test same interface data, but different pointer */
687     /* raises ERROR_INVALID_HANDLE exception */
688     if (0)
689     {
690         RPC_SERVER_INTERFACE server_if_clone = server_if;
691
692         pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
693     }
694
695     /* test different interface data, but different pointer */
696     /* raises ERROR_INVALID_HANDLE exception */
697     if (0)
698     {
699         static RPC_SERVER_INTERFACE server_if2 =
700         {
701             sizeof(RPC_SERVER_INTERFACE),
702             {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
703             {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
704             NULL,
705             0,
706             0,
707             0,
708             0,
709             0,
710         };
711         pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
712
713         pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
714     }
715 }
716
717 void __cdecl s_get_numbers(int length, int size, pints_t n[])
718 {
719     int i;
720     for (i = 0; i < length; i++)
721     {
722         n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
723         *n[i].pi = i;
724         n[i].ppi = NULL;
725         n[i].pppi = NULL;
726     }
727 }
728
729 void __cdecl s_get_numbers_struct(numbers_struct_t **ns)
730 {
731     int i;
732     *ns = midl_user_allocate(FIELD_OFFSET(numbers_struct_t, numbers[5]));
733     if (!*ns) return;
734     (*ns)->length = 5;
735     (*ns)->size = 5;
736     for (i = 0; i < (*ns)->length; i++)
737     {
738         (*ns)->numbers[i].pi = NULL;
739         (*ns)->numbers[i].ppi = NULL;
740         (*ns)->numbers[i].pppi = NULL;
741     }
742     (*ns)->numbers[0].pi = midl_user_allocate(sizeof(*(*ns)->numbers[i].pi));
743     *(*ns)->numbers[0].pi = 5;
744 }
745
746 void __cdecl s_full_pointer_test(int *a, int *b)
747 {
748     ok(*a == 42, "Expected *a to be 42 instead of %d\n", *a);
749     ok(*b == 42, "Expected *b to be 42 instead of %d\n", *a);
750     ok(a == b, "Expected a (%p) to point to the same memory as b (%p)\n", a, b);
751 }
752
753 void __cdecl s_full_pointer_null_test(int *a, int *b)
754 {
755     ok(*a == 42, "Expected *a to be 42 instead of %d\n", *a);
756     ok(b == NULL, "Expected b to be NULL instead of %p\n", b);
757 }
758
759 void __cdecl s_stop(void)
760 {
761   ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
762   ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
763   ok(SetEvent(stop_event), "SetEvent\n");
764 }
765
766 static void
767 make_cmdline(char buffer[MAX_PATH], const char *test)
768 {
769   sprintf(buffer, "%s server %s", progname, test);
770 }
771
772 static void
773 run_client(const char *test)
774 {
775   char cmdline[MAX_PATH];
776   PROCESS_INFORMATION info;
777   STARTUPINFOA startup;
778
779   memset(&startup, 0, sizeof startup);
780   startup.cb = sizeof startup;
781
782   make_cmdline(cmdline, test);
783   ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
784   winetest_wait_child_process( info.hProcess );
785   ok(CloseHandle(info.hProcess), "CloseHandle\n");
786   ok(CloseHandle(info.hThread), "CloseHandle\n");
787 }
788
789 static void
790 basic_tests(void)
791 {
792   char string[] = "I am a string";
793   WCHAR wstring[] = {'I',' ','a','m',' ','a',' ','w','s','t','r','i','n','g', 0};
794   int f[5] = {1, 3, 0, -2, -4};
795   vector_t a = {1, 3, 7};
796   vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
797   pvectors_t pvecs = {&vec1, &pvec2};
798   sp_inner_t spi = {42};
799   sp_t sp = {-13, &spi};
800   aligns_t aligns;
801   pints_t pints;
802   ptypes_t ptypes;
803   padded_t padded;
804   padded_t padded2[2];
805   bogus_t bogus;
806   int i1, i2, i3, *pi2, *pi3, **ppi3;
807   double u, v;
808   float s, t;
809   LONG q, r;
810   short h;
811   char c;
812   int x;
813   hyper y;
814   str_struct_t ss = {string};
815   wstr_struct_t ws = {wstring};
816   str_t str;
817   se_t se;
818   renum_t re;
819
820   ok(int_return() == INT_CODE, "RPC int_return\n");
821
822   ok(square(7) == 49, "RPC square\n");
823   x = sum(23, -4);
824   ok(x == 19, "RPC sum got %d\n", x);
825   c = sum_char(-23, 50);
826   ok(c == 27, "RPC sum_char got %d\n", (int)c);
827   h = sum_short(1122, -344);
828   ok(h == 778, "RPC sum_short got %d\n", (int)h);
829   x = sum_float(123.45, -32.2);
830   ok(x == 91, "RPC sum_float got %d\n", x);
831   x = sum_double_int(-78, 148.46);
832   ok(x == 70, "RPC sum_double_int got %d\n", x);
833   y = sum_hyper((hyper)0x12345678 << 16, (hyper)0x33557799 << 16);
834   ok(y == (hyper)0x4589ce11 << 16, "RPC hyper got %x%08x\n", (DWORD)(y >> 32), (DWORD)y);
835   x = sum_hyper_int((hyper)0x24242424 << 16, -((hyper)0x24241212 << 16));
836   ok(x == 0x12120000, "RPC hyper_int got 0x%x\n", x);
837   x = sum_char_hyper( 12, ((hyper)0x42424242 << 32) | 0x33334444 );
838   ok(x == 0x33334450, "RPC char_hyper got 0x%x\n", x);
839
840   x = 0;
841   square_out(11, &x);
842   ok(x == 121, "RPC square_out\n");
843
844   x = 5;
845   square_ref(&x);
846   ok(x == 25, "RPC square_ref\n");
847
848   ok(str_length(string) == strlen(string), "RPC str_length\n");
849   ok(str_t_length(string) == strlen(string), "RPC str_length\n");
850   ok(dot_self(&a) == 59, "RPC dot_self\n");
851
852   ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
853   ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");
854
855   v = 0.0;
856   u = square_half(3.0, &v);
857   ok(u == 9.0, "RPC square_half\n");
858   ok(v == 1.5, "RPC square_half\n");
859
860   t = 0.0f;
861   s = square_half_float(3.0f, &t);
862   ok(s == 9.0f, "RPC square_half_float\n");
863   ok(t == 1.5f, "RPC square_half_float\n");
864
865   r = 0;
866   q = square_half_long(3, &r);
867   ok(q == 9, "RPC square_half_long\n");
868   ok(r == 1, "RPC square_half_long\n");
869
870   i1 = 19;
871   i2 = -3;
872   i3 = -29;
873   pi2 = &i2;
874   pi3 = &i3;
875   ppi3 = &pi3;
876   pints.pi = &i1;
877   pints.ppi = &pi2;
878   pints.pppi = &ppi3;
879   ok(pints_sum(&pints) == -13, "RPC pints_sum\n");
880
881   c = 10;
882   h = 3;
883   q = 14;
884   s = -5.0f;
885   u = 11.0;
886   ptypes.pc = &c;
887   ptypes.ps = &h;
888   ptypes.pl = &q;
889   ptypes.pf = &s;
890   ptypes.pd = &u;
891   ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");
892
893   ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
894   ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
895   ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
896   ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
897
898   ok(enum_ord(E1) == 1, "RPC enum_ord\n");
899   ok(enum_ord(E2) == 2, "RPC enum_ord\n");
900   ok(enum_ord(E3) == 3, "RPC enum_ord\n");
901   ok(enum_ord(E4) == 4, "RPC enum_ord\n");
902
903   se.f = E2;
904   check_se2(&se);
905
906   memset(&aligns, 0, sizeof(aligns));
907   aligns.c = 3;
908   aligns.i = 4;
909   aligns.s = 5;
910   aligns.d = 6.0;
911   ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
912
913   padded.i = -3;
914   padded.c = 8;
915   ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
916   padded2[0].i = -5;
917   padded2[0].c = 1;
918   padded2[1].i = 3;
919   padded2[1].c = 7;
920   ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
921   padded2[0].i = -5;
922   padded2[0].c = 1;
923   padded2[1].i = 3;
924   padded2[1].c = 7;
925   ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
926
927   i1 = 14;
928   i2 = -7;
929   i3 = -4;
930   bogus.h.p1 = &i1;
931   bogus.p2 = &i2;
932   bogus.p3 = &i3;
933   bogus.c = 9;
934   ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
935
936   check_null(NULL);
937
938   str = get_filename();
939   ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
940   midl_user_free(str);
941
942   x = echo_ranged_int(0,0,0);
943   ok(x == 0, "echo_ranged_int() returned %d instead of 0\n", x);
944   x = echo_ranged_int(10,20,100);
945   ok(x == 100, "echo_ranged_int() returned %d instead of 100\n", x);
946   x = echo_ranged_int2(40);
947   ok(x == 40, "echo_ranged_int() returned %d instead of 40\n", x);
948
949   if (!old_windows_version)
950   {
951       get_ranged_enum(&re);
952       ok(re == RE3, "get_ranged_enum() returned %d instead of RE3\n", re);
953   }
954 }
955
956 static void
957 union_tests(void)
958 {
959   encue_t eue;
960   encu_t eu;
961   unencu_t uneu;
962   sun_t su;
963   int i;
964
965   su.s = SUN_I;
966   su.u.i = 9;
967   ok(square_sun(&su) == 81.0, "RPC square_sun\n");
968
969   su.s = SUN_F1;
970   su.u.f = 5.0;
971   ok(square_sun(&su) == 25.0, "RPC square_sun\n");
972
973   su.s = SUN_F2;
974   su.u.f = -2.0;
975   ok(square_sun(&su) == 4.0, "RPC square_sun\n");
976
977   su.s = SUN_PI;
978   su.u.pi = &i;
979   i = 11;
980   ok(square_sun(&su) == 121.0, "RPC square_sun\n");
981
982   eu.t = ENCU_I;
983   eu.tagged_union.i = 7;
984   ok(square_encu(&eu) == 49.0, "RPC square_encu\n");
985
986   eu.t = ENCU_F;
987   eu.tagged_union.f = 3.0;
988   ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
989
990   uneu.i = 4;
991   ok(square_unencu(ENCU_I, &uneu) == 16.0, "RPC square_unencu\n");
992
993   uneu.f = 5.0;
994   ok(square_unencu(ENCU_F, &uneu) == 25.0, "RPC square_unencu\n");
995
996   eue.t = E1;
997   eue.tagged_union.i1 = 8;
998   ok(square_encue(&eue) == 64.0, "RPC square_encue\n");
999
1000   eue.t = E2;
1001   eue.tagged_union.f2 = 10.0;
1002   ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
1003 }
1004
1005 static test_list_t *
1006 null_list(void)
1007 {
1008   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
1009   n->t = TL_NULL;
1010   n->u.x = 0;
1011   return n;
1012 }
1013
1014 static test_list_t *
1015 make_list(test_list_t *tail)
1016 {
1017   test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
1018   n->t = TL_LIST;
1019   n->u.tail = tail;
1020   return n;
1021 }
1022
1023 static void
1024 free_list(test_list_t *list)
1025 {
1026   if (list->t == TL_LIST)
1027     free_list(list->u.tail);
1028   HeapFree(GetProcessHeap(), 0, list);
1029 }
1030
1031 ULONG __RPC_USER
1032 puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
1033 {
1034   return start + sizeof(int);
1035 }
1036
1037 unsigned char * __RPC_USER
1038 puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
1039 {
1040   int n = atoi(*p);
1041   memcpy(buffer, &n, sizeof n);
1042   return buffer + sizeof n;
1043 }
1044
1045 unsigned char * __RPC_USER
1046 puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
1047 {
1048   int n;
1049   memcpy(&n, buffer, sizeof n);
1050   *p = HeapAlloc(GetProcessHeap(), 0, 10);
1051   sprintf(*p, "%d", n);
1052   return buffer + sizeof n;
1053 }
1054
1055 void __RPC_USER
1056 puint_t_UserFree(ULONG *flags, puint_t *p)
1057 {
1058   HeapFree(GetProcessHeap(), 0, *p);
1059 }
1060
1061 ULONG __RPC_USER
1062 us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
1063 {
1064   return start + sizeof(struct wire_us);
1065 }
1066
1067 unsigned char * __RPC_USER
1068 us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
1069 {
1070   struct wire_us wus;
1071   wus.x = atoi(pus->x);
1072   memcpy(buffer, &wus, sizeof wus);
1073   return buffer + sizeof wus;
1074 }
1075
1076 unsigned char * __RPC_USER
1077 us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
1078 {
1079   struct wire_us wus;
1080   memcpy(&wus, buffer, sizeof wus);
1081   pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
1082   sprintf(pus->x, "%d", wus.x);
1083   return buffer + sizeof wus;
1084 }
1085
1086 void __RPC_USER
1087 us_t_UserFree(ULONG *flags, us_t *pus)
1088 {
1089   HeapFree(GetProcessHeap(), 0, pus->x);
1090 }
1091
1092 ULONG __RPC_USER
1093 bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
1094 {
1095   return start + FIELD_OFFSET(user_bstr_t, data[(*b)[-1]]);
1096 }
1097
1098 unsigned char * __RPC_USER
1099 bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1100 {
1101   wire_bstr_t wb = (wire_bstr_t) buffer;
1102   wb->n = (*b)[-1];
1103   memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
1104   return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1105 }
1106
1107 unsigned char * __RPC_USER
1108 bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
1109 {
1110   wire_bstr_t wb = (wire_bstr_t) buffer;
1111   short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
1112   data[0] = wb->n;
1113   memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
1114   *b = &data[1];
1115   return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1116 }
1117
1118 void __RPC_USER
1119 bstr_t_UserFree(ULONG *flags, bstr_t *b)
1120 {
1121   HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
1122 }
1123
1124 static void
1125 pointer_tests(void)
1126 {
1127   int a[] = {1, 2, 3, 4};
1128   char p1[] = "11";
1129   test_list_t *list = make_list(make_list(make_list(null_list())));
1130   test_us_t tus = {{p1}};
1131   int *pa[4];
1132   puints_t pus;
1133   cpuints_t cpus;
1134   short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
1135   bstr_t bstr = &bstr_data[1], bstr2;
1136   name_t name;
1137   void *buffer;
1138   int *pa2;
1139   s123_t *s123;
1140   int val = 42;
1141
1142   ok(test_list_length(list) == 3, "RPC test_list_length\n");
1143   ok(square_puint(p1) == 121, "RPC square_puint\n");
1144   pus.n = 4;
1145   pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
1146   pus.ps[0] = xstrdup("5");
1147   pus.ps[1] = xstrdup("6");
1148   pus.ps[2] = xstrdup("7");
1149   pus.ps[3] = xstrdup("8");
1150   ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
1151   HeapFree(GetProcessHeap(), 0, pus.ps[0]);
1152   HeapFree(GetProcessHeap(), 0, pus.ps[1]);
1153   HeapFree(GetProcessHeap(), 0, pus.ps[2]);
1154   HeapFree(GetProcessHeap(), 0, pus.ps[3]);
1155   HeapFree(GetProcessHeap(), 0, pus.ps);
1156   cpus.n = 4;
1157   cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
1158   cpus.ps[0] = xstrdup("5");
1159   cpus.ps[1] = xstrdup("6");
1160   cpus.ps[2] = xstrdup("7");
1161   cpus.ps[3] = xstrdup("8");
1162   ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
1163   HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
1164   HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
1165   HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
1166   HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
1167   HeapFree(GetProcessHeap(), 0, cpus.ps);
1168   ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
1169
1170   pa[0] = &a[0];
1171   pa[1] = &a[1];
1172   pa[2] = &a[2];
1173   ok(sum_parr(pa) == 6, "RPC sum_parr\n");
1174
1175   pa[0] = &a[0];
1176   pa[1] = &a[1];
1177   pa[2] = &a[2];
1178   pa[3] = &a[3];
1179   ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");
1180
1181   ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
1182
1183   get_a_bstr(&bstr);
1184   s_get_a_bstr(&bstr2);
1185   ok(!lstrcmpW((LPCWSTR)bstr, (LPCWSTR)bstr2), "bstr mismatch\n");
1186   HeapFree(GetProcessHeap(), 0, bstr - 1);
1187   HeapFree(GetProcessHeap(), 0, bstr2 - 1);
1188
1189   free_list(list);
1190
1191   if (!old_windows_version)
1192   {
1193       name.size = 10;
1194       name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
1195       get_name(&name);
1196       ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
1197       ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
1198       HeapFree(GetProcessHeap(), 0, name.name);
1199   }
1200
1201   pa2 = a;
1202   ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
1203
1204   s123 = get_s123();
1205   ok(s123->f1 == 1 && s123->f2 == 2 && s123->f3 == 3, "RPC get_s123\n");
1206   MIDL_user_free(s123);
1207
1208   full_pointer_test(&val, &val);
1209   full_pointer_null_test(&val, NULL);
1210 }
1211
1212 static int
1213 check_pyramid_doub_carr(doub_carr_t *dc)
1214 {
1215   int i, j;
1216   for (i = 0; i < dc->n; ++i)
1217     for (j = 0; j < dc->a[i]->n; ++j)
1218       if (dc->a[i]->a[j] != j + 1)
1219         return FALSE;
1220   return TRUE;
1221 }
1222
1223 static void
1224 free_pyramid_doub_carr(doub_carr_t *dc)
1225 {
1226   int i;
1227   for (i = 0; i < dc->n; ++i)
1228     MIDL_user_free(dc->a[i]);
1229   MIDL_user_free(dc);
1230 }
1231
1232 static void
1233 array_tests(void)
1234 {
1235   int m[2][3][4] =
1236   {
1237     {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
1238     {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
1239   };
1240   int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1241   int c2[] = {10, 100, 200};
1242   int c3[20];
1243   vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1244   cps_t cps;
1245   cpsc_t cpsc;
1246   cs_t *cs;
1247   int n;
1248   int ca[5] = {1, -2, 3, -4, 5};
1249   int tmp[10];
1250   doub_carr_t *dc;
1251   int *pi;
1252   pints_t api[5];
1253   numbers_struct_t *ns;
1254   refpint_t rpi[5];
1255
1256   if (!old_windows_version)
1257   {
1258       const char str1[25] = "Hello";
1259       ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
1260   }
1261
1262   ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");
1263
1264   ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
1265   ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1266   ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1267   ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1268
1269   ok(sum_conf_ptr_by_conf_ptr(1, c2, c) == 45, "RPC sum_conf_ptr_by_conf_ptr\n");
1270   ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 345, "RPC sum_conf_ptr_by_conf_ptr\n");
1271   c2[0] = 0;
1272   ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 300, "RPC sum_conf_ptr_by_conf_ptr\n");
1273
1274   ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
1275   ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
1276   ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");
1277
1278   get_number_array(c3, &n);
1279   ok(n == 10, "RPC get_num_array\n");
1280   for (; n > 0; n--)
1281     ok(c3[n-1] == c[n-1], "get_num_array returned wrong value %d @ %d\n",
1282        c3[n-1], n);
1283   ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
1284   ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
1285   ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
1286   ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");
1287
1288   ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1289   cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1290   cs->n = 5;
1291   cs->ca[0] = 3;
1292   cs->ca[1] = 5;
1293   cs->ca[2] = -2;
1294   cs->ca[3] = -1;
1295   cs->ca[4] = -4;
1296   ok(sum_cs(cs) == 1, "RPC sum_cs\n");
1297   HeapFree(GetProcessHeap(), 0, cs);
1298
1299   n = 5;
1300   cps.pn = &n;
1301   cps.ca1 = &c[2];
1302   cps.n = 3;
1303   cps.ca2 = &c[3];
1304   ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1305
1306   cpsc.a = 4;
1307   cpsc.b = 5;
1308   cpsc.c = 1;
1309   cpsc.ca = c;
1310   ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1311   cpsc.a = 4;
1312   cpsc.b = 5;
1313   cpsc.c = 0;
1314   cpsc.ca = c;
1315   ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1316
1317   cpsc.ca = NULL;
1318   ok(get_cpsc(5, &cpsc) == 45, "RPC sum_cpsc\n");
1319   ok( cpsc.a == 10, "RPC get_cpsc %u\n", cpsc.a );
1320   for (n = 0; n < 10; n++) ok( cpsc.ca[n] == n, "RPC get_cpsc[%d] = %d\n", n, cpsc.ca[n] );
1321
1322   memset( tmp, 0x33, sizeof(tmp) );
1323   cpsc.ca = tmp;
1324   ok(get_cpsc(4, &cpsc) == 28, "RPC sum_cpsc\n");
1325   ok( cpsc.a == 8, "RPC get_cpsc %u\n", cpsc.a );
1326   ok( cpsc.ca == tmp, "RPC get_cpsc %p/%p\n", cpsc.ca, tmp );
1327   for (n = 0; n < 8; n++) ok( cpsc.ca[n] == n, "RPC get_cpsc[%d] = %d\n", n, cpsc.ca[n] );
1328
1329   ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
1330   ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
1331   ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1332
1333   dc = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_t, a[2]));
1334   dc->n = 2;
1335   dc->a[0] = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_1_t, a[3]));
1336   dc->a[0]->n = 3;
1337   dc->a[0]->a[0] = 5;
1338   dc->a[0]->a[1] = 1;
1339   dc->a[0]->a[2] = 8;
1340   dc->a[1] = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_1_t, a[2]));
1341   dc->a[1]->n = 2;
1342   dc->a[1]->a[0] = 2;
1343   dc->a[1]->a[1] = 3;
1344   ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1345   HeapFree(GetProcessHeap(), 0, dc->a[0]);
1346   HeapFree(GetProcessHeap(), 0, dc->a[1]);
1347   HeapFree(GetProcessHeap(), 0, dc);
1348
1349   dc = NULL;
1350   make_pyramid_doub_carr(4, &dc);
1351   ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
1352   free_pyramid_doub_carr(dc);
1353
1354   ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1355
1356   memset(api, 0, sizeof(api));
1357   pi = HeapAlloc(GetProcessHeap(), 0, sizeof(*pi));
1358   *pi = -1;
1359   api[0].pi = pi;
1360   get_numbers(1, 1, api);
1361   ok(api[0].pi == pi, "RPC conformant varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1362   ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *api[0].pi);
1363
1364   if (!old_windows_version)
1365   {
1366       ns = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(numbers_struct_t, numbers[5]));
1367       ns->length = 5;
1368       ns->size = 5;
1369       ns->numbers[0].pi = pi;
1370       get_numbers_struct(&ns);
1371       ok(ns->numbers[0].pi == pi, "RPC conformant varying struct embedded pointer changed from %p to %p\n", pi, ns->numbers[0].pi);
1372       ok(*ns->numbers[0].pi == 5, "pi unmarshalled incorrectly %d\n", *ns->numbers[0].pi);
1373       HeapFree(GetProcessHeap(), 0, ns);
1374   }
1375   HeapFree(GetProcessHeap(), 0, pi);
1376
1377   pi = HeapAlloc(GetProcessHeap(), 0, 5 * sizeof(*pi));
1378   pi[0] = 3;  rpi[0] = &pi[0];
1379   pi[1] = 5;  rpi[1] = &pi[1];
1380   pi[2] = -2; rpi[2] = &pi[2];
1381   pi[3] = -1; rpi[3] = &pi[3];
1382   pi[4] = -4; rpi[4] = &pi[4];
1383   ok(sum_complex_array(5, rpi) == 1, "RPC sum_complex_array\n");
1384   HeapFree(GetProcessHeap(), 0, pi);
1385 }
1386
1387 void __cdecl s_authinfo_test(unsigned int protseq, int secure)
1388 {
1389     RPC_BINDING_HANDLE binding;
1390     RPC_STATUS status;
1391     ULONG level, authnsvc;
1392     RPC_AUTHZ_HANDLE privs;
1393     unsigned char *principal;
1394
1395     binding = I_RpcGetCurrentCallHandle();
1396     ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");
1397
1398     level = authnsvc = 0xdeadbeef;
1399     privs = (RPC_AUTHZ_HANDLE)0xdeadbeef;
1400     principal = (unsigned char *)0xdeadbeef;
1401
1402     if (secure || protseq == RPC_PROTSEQ_LRPC)
1403     {
1404         status = RpcBindingInqAuthClientA(binding, &privs, &principal, &level, &authnsvc, NULL);
1405         if (status == RPC_S_CANNOT_SUPPORT)
1406         {
1407             win_skip("RpcBindingInqAuthClientA not supported\n");
1408             return;
1409         }
1410         ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
1411         ok(privs != (RPC_AUTHZ_HANDLE)0xdeadbeef, "privs unchanged\n");
1412         ok(principal != (unsigned char *)0xdeadbeef, "principal unchanged\n");
1413         if (protseq != RPC_PROTSEQ_LRPC)
1414         {
1415             todo_wine
1416             ok(principal != NULL, "NULL principal\n");
1417         }
1418         if (protseq == RPC_PROTSEQ_LRPC && principal && pGetUserNameExA)
1419         {
1420             int len;
1421             char *spn;
1422
1423             len = WideCharToMultiByte(CP_ACP, 0, (const WCHAR *)privs, -1, NULL, 0, NULL, NULL);
1424             spn = HeapAlloc( GetProcessHeap(), 0, len );
1425             WideCharToMultiByte(CP_ACP, 0, (const WCHAR *)privs, -1, spn, len, NULL, NULL);
1426
1427             ok(!strcmp(domain_and_user, spn), "expected %s got %s\n", domain_and_user, spn);
1428             HeapFree( GetProcessHeap(), 0, spn );
1429         }
1430         ok(level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY, "level unchanged\n");
1431         ok(authnsvc == RPC_C_AUTHN_WINNT, "authnsvc unchanged\n");
1432
1433         status = RpcImpersonateClient(NULL);
1434         ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
1435         status = RpcRevertToSelf();
1436         ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
1437
1438     }
1439     else
1440     {
1441         status = RpcBindingInqAuthClientA(binding, &privs, &principal, &level, &authnsvc, NULL);
1442         ok(status == RPC_S_BINDING_HAS_NO_AUTH, "expected RPC_S_BINDING_HAS_NO_AUTH got %u\n", status);
1443         ok(privs == (RPC_AUTHZ_HANDLE)0xdeadbeef, "got %p\n", privs);
1444         ok(principal == (unsigned char *)0xdeadbeef, "got %s\n", principal);
1445         ok(level == 0xdeadbeef, "got %u\n", level);
1446         ok(authnsvc == 0xdeadbeef, "got %u\n", authnsvc);
1447     }
1448 }
1449
1450 static void
1451 run_tests(void)
1452 {
1453   basic_tests();
1454   union_tests();
1455   pointer_tests();
1456   array_tests();
1457   context_handle_test();
1458 }
1459
1460 static void
1461 set_auth_info(RPC_BINDING_HANDLE handle)
1462 {
1463     RPC_STATUS status;
1464     RPC_SECURITY_QOS qos;
1465
1466     if (!pGetUserNameExA)
1467         return;
1468
1469     qos.Version = 1;
1470     qos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;
1471     qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
1472     qos.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;
1473
1474     status = pRpcBindingSetAuthInfoExA(handle, (RPC_CSTR)domain_and_user, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
1475                                        RPC_C_AUTHN_WINNT, NULL, 0, &qos);
1476     ok(status == RPC_S_OK, "RpcBindingSetAuthInfoExA failed %d\n", status);
1477 }
1478
1479 static void
1480 client(const char *test)
1481 {
1482   static unsigned char iptcp[] = "ncacn_ip_tcp";
1483   static unsigned char np[] = "ncacn_np";
1484   static unsigned char ncalrpc[] = "ncalrpc";
1485   static unsigned char address[] = "127.0.0.1";
1486   static unsigned char address_np[] = "\\\\.";
1487   static unsigned char port[] = PORT;
1488   static unsigned char pipe[] = PIPE;
1489   static unsigned char guid[] = "00000000-4114-0704-2301-000000000000";
1490
1491   unsigned char *binding;
1492
1493   if (strcmp(test, "tcp_basic") == 0)
1494   {
1495     ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1496     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1497
1498     run_tests();
1499     authinfo_test(RPC_PROTSEQ_TCP, 0);
1500
1501     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1502     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1503   }
1504   else if (strcmp(test, "tcp_secure") == 0)
1505   {
1506     ok(RPC_S_OK == RpcStringBindingCompose(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1507     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1508
1509     set_auth_info(IServer_IfHandle);
1510     authinfo_test(RPC_PROTSEQ_TCP, 1);
1511
1512     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1513     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1514   }
1515   else if (strcmp(test, "ncalrpc_basic") == 0)
1516   {
1517     ok(RPC_S_OK == RpcStringBindingCompose(NULL, ncalrpc, NULL, guid, NULL, &binding), "RpcStringBindingCompose\n");
1518     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1519
1520     run_tests(); /* can cause RPC_X_BAD_STUB_DATA exception */
1521     authinfo_test(RPC_PROTSEQ_LRPC, 0);
1522
1523     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1524     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1525   }
1526   else if (strcmp(test, "ncalrpc_secure") == 0)
1527   {
1528     ok(RPC_S_OK == RpcStringBindingCompose(NULL, ncalrpc, NULL, guid, NULL, &binding), "RpcStringBindingCompose\n");
1529     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1530
1531     set_auth_info(IServer_IfHandle);
1532     authinfo_test(RPC_PROTSEQ_LRPC, 1);
1533
1534     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1535     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1536   }
1537   else if (strcmp(test, "np_basic") == 0)
1538   {
1539     ok(RPC_S_OK == RpcStringBindingCompose(NULL, np, address_np, pipe, NULL, &binding), "RpcStringBindingCompose\n");
1540     ok(RPC_S_OK == RpcBindingFromStringBinding(binding, &IServer_IfHandle), "RpcBindingFromStringBinding\n");
1541
1542     run_tests();
1543     authinfo_test(RPC_PROTSEQ_NMP, 0);
1544     stop();
1545
1546     ok(RPC_S_OK == RpcStringFree(&binding), "RpcStringFree\n");
1547     ok(RPC_S_OK == RpcBindingFree(&IServer_IfHandle), "RpcBindingFree\n");
1548   }
1549 }
1550
1551 static void
1552 server(void)
1553 {
1554   static unsigned char iptcp[] = "ncacn_ip_tcp";
1555   static unsigned char port[] = PORT;
1556   static unsigned char np[] = "ncacn_np";
1557   static unsigned char pipe[] = PIPE;
1558   static unsigned char ncalrpc[] = "ncalrpc";
1559   static unsigned char guid[] = "00000000-4114-0704-2301-000000000000";
1560   RPC_STATUS status, iptcp_status, np_status, ncalrpc_status;
1561   DWORD ret;
1562
1563   iptcp_status = RpcServerUseProtseqEp(iptcp, 20, port, NULL);
1564   ok(iptcp_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_ip_tcp) failed with status %d\n", iptcp_status);
1565
1566   ncalrpc_status = RpcServerUseProtseqEp(ncalrpc, 0, guid, NULL);
1567   ok(ncalrpc_status == RPC_S_OK, "RpcServerUseProtseqEp(ncalrpc) failed with status %d\n", ncalrpc_status);
1568
1569   np_status = RpcServerUseProtseqEp(np, 0, pipe, NULL);
1570   if (np_status == RPC_S_PROTSEQ_NOT_SUPPORTED)
1571     skip("Protocol sequence ncacn_np is not supported\n");
1572   else
1573     ok(np_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", np_status);
1574
1575   if (pRpcServerRegisterIfEx)
1576   {
1577     trace("Using RpcServerRegisterIfEx\n");
1578     status = pRpcServerRegisterIfEx(s_IServer_v0_0_s_ifspec, NULL, NULL,
1579                                     RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
1580                                     RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
1581   }
1582   else
1583     status = RpcServerRegisterIf(s_IServer_v0_0_s_ifspec, NULL, NULL);
1584   ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);
1585   status = RpcServerListen(1, 20, TRUE);
1586   ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
1587   stop_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1588   ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
1589
1590   if (iptcp_status == RPC_S_OK)
1591     run_client("tcp_basic");
1592   else
1593     skip("tcp tests skipped due to earlier failure\n");
1594
1595   if (ncalrpc_status == RPC_S_OK)
1596   {
1597     run_client("ncalrpc_basic");
1598     if (pGetUserNameExA)
1599     {
1600       /* we don't need to register RPC_C_AUTHN_WINNT for ncalrpc */
1601       run_client("ncalrpc_secure");
1602     }
1603   }
1604   else
1605     skip("lrpc tests skipped due to earlier failure\n");
1606
1607   if (np_status == RPC_S_OK)
1608     run_client("np_basic");
1609   else
1610   {
1611     skip("np_basic tests skipped due to earlier failure\n");
1612     /* np client is what signals stop_event, so bail out if we didn't run do it */
1613     return;
1614   }
1615
1616   ret = WaitForSingleObject(stop_event, 1000);
1617   ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
1618   /* if the stop event didn't fire then RpcMgmtWaitServerListen will wait
1619    * forever, so don't bother calling it in this case */
1620   if (ret == WAIT_OBJECT_0)
1621   {
1622     status = RpcMgmtWaitServerListen();
1623     todo_wine {
1624       ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);
1625     }
1626   }
1627 }
1628
1629 START_TEST(server)
1630 {
1631   int argc;
1632   char **argv;
1633
1634   InitFunctionPointers();
1635
1636   if (pGetUserNameExA)
1637   {
1638     ULONG size = 0;
1639     ok(!pGetUserNameExA(NameSamCompatible, NULL, &size), "GetUserNameExA\n");
1640     domain_and_user = HeapAlloc(GetProcessHeap(), 0, size);
1641     ok(pGetUserNameExA(NameSamCompatible, domain_and_user, &size), "GetUserNameExA\n");
1642   }
1643   else
1644     win_skip("GetUserNameExA is needed for some authentication tests\n");
1645
1646   argc = winetest_get_mainargs(&argv);
1647   progname = argv[0];
1648
1649   if (argc == 3)
1650   {
1651     RpcTryExcept
1652     {
1653       client(argv[2]);
1654     }
1655     RpcExcept(TRUE)
1656     {
1657       trace("Exception %d\n", RpcExceptionCode());
1658     }
1659     RpcEndExcept
1660   }
1661   else
1662     server();
1663
1664   HeapFree(GetProcessHeap(), 0, domain_and_user);
1665 }