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