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