server.c 73.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Tests for WIDL and RPC server/clients.
 *
 * Copyright (C) Google 2007 (Dan Hipschman)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21
#define COBJMACROS
22
#include <windows.h>
23
#include <ole2.h>
24
#include <oleauto.h>
25 26
#include <secext.h>
#include <rpcdce.h>
27
#include <netfw.h>
28 29
#include "wine/test.h"
#include "server.h"
30
#define SKIP_TYPE_DECLS
31
#include "server_interp.h"
32
#include "server_defines.h"
33

34
#include <stddef.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37 38 39 40 41 42 43

#define PORT "4114"
#define PIPE "\\pipe\\wine_rpcrt4_test"

#define INT_CODE 4198

static const char *progname;
44
static BOOL old_windows_version;
45

46
static HANDLE stop_event, stop_wait_event;
47

48 49
static void (WINAPI *pNDRSContextMarshall2)(RPC_BINDING_HANDLE, NDR_SCONTEXT, void*, NDR_RUNDOWN, void*, ULONG);
static NDR_SCONTEXT (WINAPI *pNDRSContextUnmarshall2)(RPC_BINDING_HANDLE, void*, ULONG, void*, ULONG);
50 51
static RPC_STATUS (WINAPI *pRpcServerRegisterIfEx)(RPC_IF_HANDLE,UUID*, RPC_MGR_EPV*, unsigned int,
                   unsigned int,RPC_IF_CALLBACK_FN*);
52 53 54 55 56
static RPC_STATUS (WINAPI *pRpcBindingSetAuthInfoExA)(RPC_BINDING_HANDLE, RPC_CSTR, ULONG, ULONG,
                                                      RPC_AUTH_IDENTITY_HANDLE, ULONG, RPC_SECURITY_QOS *);
static RPC_STATUS (WINAPI *pRpcServerRegisterAuthInfoA)(RPC_CSTR, ULONG, RPC_AUTH_KEY_RETRIEVAL_FN, LPVOID);

static char *domain_and_user;
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static int (__cdecl *int_return)(void);
static int (__cdecl *square)(int x);
static int (__cdecl *sum)(int x, int y);
static signed char (__cdecl *sum_char)(signed char x, signed char y);
static short (__cdecl *sum_short)(short x, short y);
static int (__cdecl *sum_float)(float x, float y);
static int (__cdecl *sum_double_int)(int x, double y);
static hyper (__cdecl *sum_hyper)(hyper x, hyper y);
static int (__cdecl *sum_hyper_int)(hyper x, hyper y);
static int (__cdecl *sum_char_hyper)(signed char x, hyper y);
static void (__cdecl *square_out)(int x, int *y);
static void (__cdecl *square_ref)(int *x);
static int (__cdecl *str_length)(const char *s);
static int (__cdecl *str_t_length)(str_t s);
static int (__cdecl *cstr_length)(const char *s, int n);
static int (__cdecl *dot_self)(vector_t *v);
static double (__cdecl *square_half)(double x, double *y);
static float (__cdecl *square_half_float)(float x, float *y);
static LONG (__cdecl *square_half_long)(LONG x, LONG *y);
static int (__cdecl *sum_fixed_array)(int a[5]);
static int (__cdecl *pints_sum)(pints_t *pints);
static double (__cdecl *ptypes_sum)(ptypes_t *ptypes);
static int (__cdecl *dot_pvectors)(pvectors_t *pvectors);
static int (__cdecl *sum_sp)(sp_t *sp);
static double (__cdecl *square_sun)(sun_t *su);
static int (__cdecl *test_list_length)(test_list_t *ls);
static int (__cdecl *sum_fixed_int_3d)(int m[2][3][4]);
static int (__cdecl *sum_conf_array)(int x[], int n);
static int (__cdecl *sum_conf_ptr_by_conf_ptr)(int n1, int *n2_then_x1, int *x2);
static int (__cdecl *sum_unique_conf_array)(int x[], int n);
static int (__cdecl *sum_unique_conf_ptr)(int *x, int n);
static int (__cdecl *sum_var_array)(int x[20], int n);
static int (__cdecl *dot_two_vectors)(vector_t vs[2]);
static void (__cdecl *get_number_array)(int x[20], int *n);
static int (__cdecl *sum_cs)(cs_t *cs);
static int (__cdecl *sum_cps)(cps_t *cps);
static int (__cdecl *sum_cpsc)(cpsc_t *cpsc);
static int (__cdecl *get_cpsc)(int n, cpsc_t *cpsc);
static int (__cdecl *sum_complex_array)(int n, refpint_t pi[]);
97 98 99
static int (__cdecl *sum_blob)(cs_blob_t *blob);
static int (__cdecl *sum_data)(cs_data_t *data);
static int (__cdecl *sum_container)(cs_container_t *container);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static int (__cdecl *square_puint)(puint_t p);
static int (__cdecl *sum_puints)(puints_t *p);
static int (__cdecl *sum_cpuints)(cpuints_t *p);
static int (__cdecl *dot_copy_vectors)(vector_t u, vector_t v);
static double (__cdecl *square_encu)(encu_t *eu);
static double (__cdecl *square_unencu)(int t, unencu_t *eu);
static int (__cdecl *sum_parr)(int *a[3]);
static int (__cdecl *sum_pcarr)(int *a[], int n);
static int (__cdecl *enum_ord)(e_t e);
static double (__cdecl *square_encue)(encue_t *eue);
static void (__cdecl *check_se2)(se_t *s);
static int (__cdecl *sum_toplev_conf_2n)(int *x, int n);
static int (__cdecl *sum_toplev_conf_cond)(int *x, int a, int b, int c);
static int (__cdecl *square_test_us)(test_us_t *tus);
static double (__cdecl *sum_aligns)(aligns_t *a);
static int (__cdecl *sum_padded)(padded_t *p);
static int (__cdecl *sum_padded2)(padded_t ps[2]);
static int (__cdecl *sum_padded_conf)(padded_t *ps, int n);
static int (__cdecl *sum_bogus)(bogus_t *b);
static void (__cdecl *check_null)(int *null);
static int (__cdecl *str_struct_len)(str_struct_t *s);
static int (__cdecl *wstr_struct_len)(wstr_struct_t *s);
static int (__cdecl *sum_doub_carr)(doub_carr_t *dc);
static void (__cdecl *make_pyramid_doub_carr)(unsigned char n, doub_carr_t **dc);
static unsigned (__cdecl *hash_bstr)(bstr_t s);
static void (__cdecl *get_a_bstr)(bstr_t *s);
static void (__cdecl *get_name)(name_t *name);
static void (__cdecl *get_names)(int *n, str_array_t *names);
static void (__cdecl *get_namesw)(int *n, wstr_array_t *names);
static int (__cdecl *sum_pcarr2)(int n, int **pa);
static int (__cdecl *sum_L1_norms)(int n, vector_t *vs);
static s123_t* (__cdecl *get_s123)(void);
static void (__cdecl *get_numbers)(int length, int size, pints_t pn[]);
static void (__cdecl *get_numbers_struct)(numbers_struct_t **ns);
static str_t (__cdecl *get_filename)(void);
static rint_t (__cdecl *echo_ranged_int)(int i, int j, int k);
static rint_t (__cdecl *echo_ranged_int2)(int i);
static void (__cdecl *get_ranged_enum)(renum_t *re);
static void (__cdecl *context_handle_test)(void);
static void (__cdecl *full_pointer_test)(int *a, int *b);
static void (__cdecl *full_pointer_null_test)(int *a, int *b);
static void (__cdecl *authinfo_test)(unsigned int protseq, int secure);
static void (__cdecl *stop)(void);
static void (__cdecl *stop_autolisten)(void);
static void (__cdecl *ip_test)(ipu_t *a);
static int (__cdecl *sum_ptr_array)(int *a[2]);
static int (__cdecl *sum_array_ptr)(int (*a)[2]);
147
static ctx_handle_t (__cdecl *get_handle)(void);
148 149
static void (__cdecl *get_handle_by_ptr)(ctx_handle_t *r);
static void (__cdecl *test_handle)(ctx_handle_t ctx_handle);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

#define SERVER_FUNCTIONS \
    X(int_return) \
    X(square) \
    X(sum) \
    X(sum_char) \
    X(sum_short) \
    X(sum_float) \
    X(sum_double_int) \
    X(sum_hyper) \
    X(sum_hyper_int) \
    X(sum_char_hyper) \
    X(square_out) \
    X(square_ref) \
    X(str_length) \
    X(str_t_length) \
    X(cstr_length) \
    X(dot_self) \
    X(square_half) \
    X(square_half_float) \
    X(square_half_long) \
    X(sum_fixed_array) \
    X(pints_sum) \
    X(ptypes_sum) \
    X(dot_pvectors) \
    X(sum_sp) \
    X(square_sun) \
    X(test_list_length) \
    X(sum_fixed_int_3d) \
    X(sum_conf_array) \
    X(sum_conf_ptr_by_conf_ptr) \
    X(sum_unique_conf_array) \
    X(sum_unique_conf_ptr) \
    X(sum_var_array) \
    X(dot_two_vectors) \
    X(get_number_array) \
    X(sum_cs) \
    X(sum_cps) \
    X(sum_cpsc) \
    X(get_cpsc) \
    X(sum_complex_array) \
191 192 193
    X(sum_blob) \
    X(sum_data) \
    X(sum_container) \
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    X(square_puint) \
    X(sum_puints) \
    X(sum_cpuints) \
    X(dot_copy_vectors) \
    X(square_encu) \
    X(square_unencu) \
    X(sum_parr) \
    X(sum_pcarr) \
    X(enum_ord) \
    X(square_encue) \
    X(check_se2) \
    X(sum_toplev_conf_2n) \
    X(sum_toplev_conf_cond) \
    X(square_test_us) \
    X(sum_aligns) \
    X(sum_padded) \
    X(sum_padded2) \
    X(sum_padded_conf) \
    X(sum_bogus) \
    X(check_null) \
    X(str_struct_len) \
    X(wstr_struct_len) \
    X(sum_doub_carr) \
    X(make_pyramid_doub_carr) \
    X(hash_bstr) \
    X(get_a_bstr) \
    X(get_name) \
    X(get_names) \
    X(get_namesw) \
    X(sum_pcarr2) \
    X(sum_L1_norms) \
    X(get_s123) \
    X(get_numbers) \
    X(get_numbers_struct) \
    X(get_filename) \
    X(echo_ranged_int) \
    X(echo_ranged_int2) \
    X(get_ranged_enum) \
    X(context_handle_test) \
    X(full_pointer_test) \
    X(full_pointer_null_test) \
    X(authinfo_test) \
    X(stop) \
    X(stop_autolisten) \
    X(ip_test) \
    X(sum_ptr_array) \
240 241 242 243
    X(sum_array_ptr) \
    X(get_handle) \
    X(get_handle_by_ptr) \
    X(test_handle)
244

245 246 247
/* type check statements generated in header file */
fnprintf *p_printf = printf;

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static BOOL is_interp;

static void set_interp_interface(void)
{
    is_interp = TRUE;

#define X(name) name = interp_##name;
    SERVER_FUNCTIONS
#undef X
}

static void set_mixed_interface(void)
{
    is_interp = FALSE;

#define X(name) name = mixed_##name;
    SERVER_FUNCTIONS
#undef X
}

268 269 270 271 272 273
static void InitFunctionPointers(void)
{
    HMODULE hrpcrt4 = GetModuleHandleA("rpcrt4.dll");

    pNDRSContextMarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextMarshall2");
    pNDRSContextUnmarshall2 = (void *)GetProcAddress(hrpcrt4, "NDRSContextUnmarshall2");
274
    pRpcServerRegisterIfEx = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterIfEx");
275 276
    pRpcBindingSetAuthInfoExA = (void *)GetProcAddress(hrpcrt4, "RpcBindingSetAuthInfoExA");
    pRpcServerRegisterAuthInfoA = (void *)GetProcAddress(hrpcrt4, "RpcServerRegisterAuthInfoA");
277 278

    if (!pNDRSContextMarshall2) old_windows_version = TRUE;
279 280
}

281
void __RPC_FAR *__RPC_USER
282
midl_user_allocate(SIZE_T n)
283
{
284
  return HeapAlloc(GetProcessHeap(), 0, n);
285 286 287 288 289
}

void __RPC_USER
midl_user_free(void __RPC_FAR *p)
{
290
  HeapFree(GetProcessHeap(), 0, p);
291 292
}

293 294 295 296 297 298 299 300
static char *
xstrdup(const char *s)
{
  char *d = HeapAlloc(GetProcessHeap(), 0, strlen(s) + 1);
  strcpy(d, s);
  return d;
}

301
int __cdecl s_int_return(void)
302 303 304 305
{
  return INT_CODE;
}

306
int __cdecl s_square(int x)
307 308 309 310
{
  return x * x;
}

311
int __cdecl s_sum(int x, int y)
312 313 314 315
{
  return x + y;
}

316
signed char __cdecl s_sum_char(signed char x, signed char y)
317 318 319 320
{
    return x + y;
}

321
short __cdecl s_sum_short(short x, short y)
322 323 324 325
{
    return x + y;
}

326
int __cdecl s_sum_float(float x, float y)
327 328 329 330
{
    return x + y;
}

331
int __cdecl s_sum_double_int(int x, double y)
332 333 334 335
{
    return x + y;
}

336
hyper __cdecl s_sum_hyper(hyper x, hyper y)
337 338 339 340
{
    return x + y;
}

341
int __cdecl s_sum_hyper_int(hyper x, hyper y)
342
{
343
    return x + y;
344 345
}

346
int __cdecl s_sum_char_hyper(signed char x, hyper y)
347
{
348
    return x + y;
349 350
}

351
void __cdecl s_square_out(int x, int *y)
352
{
353
  *y = s_square(x);
354 355
}

356
void __cdecl s_square_ref(int *x)
357
{
358
  *x = s_square(*x);
359 360
}

361
int __cdecl s_str_length(const char *s)
362 363 364 365
{
  return strlen(s);
}

366
int __cdecl s_str_t_length(str_t s)
367 368 369 370
{
  return strlen(s);
}

371
int __cdecl s_cstr_length(const char *s, int n)
372 373 374 375 376 377 378
{
  int len = 0;
  while (0 < n-- && *s++)
    ++len;
  return len;
}

379
int __cdecl s_dot_self(vector_t *v)
380
{
381
  return s_square(v->x) + s_square(v->y) + s_square(v->z);
382 383
}

384
double __cdecl s_square_half(double x, double *y)
385 386 387 388 389
{
  *y = x / 2.0;
  return x * x;
}

390
float __cdecl s_square_half_float(float x, float *y)
391 392 393 394 395
{
  *y = x / 2.0f;
  return x * x;
}

396
LONG __cdecl s_square_half_long(LONG x, LONG *y)
397 398 399 400 401
{
  *y = x / 2;
  return x * x;
}

402
int __cdecl s_sum_fixed_array(int a[5])
403 404 405 406
{
  return a[0] + a[1] + a[2] + a[3] + a[4];
}

407
int __cdecl s_pints_sum(pints_t *pints)
408 409 410 411
{
  return *pints->pi + **pints->ppi + ***pints->pppi;
}

412
double __cdecl s_ptypes_sum(ptypes_t *pt)
413 414 415 416
{
  return *pt->pc + *pt->ps + *pt->pl + *pt->pf + *pt->pd;
}

417
int __cdecl s_dot_pvectors(pvectors_t *p)
418 419 420 421
{
  return p->pu->x * (*p->pv)->x + p->pu->y * (*p->pv)->y + p->pu->z * (*p->pv)->z;
}

422
int __cdecl s_sum_sp(sp_t *sp)
423 424 425 426
{
  return sp->x + sp->s->x;
}

427
double __cdecl s_square_sun(sun_t *su)
428
{
429
  switch (su->s)
430
  {
431
  case SUN_I: return su->u.i * su->u.i;
432
  case SUN_F1:
433 434
  case SUN_F2: return su->u.f * su->u.f;
  case SUN_PI: return (*su->u.pi) * (*su->u.pi);
435 436 437 438 439
  default:
    return 0.0;
  }
}

440
int __cdecl s_test_list_length(test_list_t *list)
441 442
{
  return (list->t == TL_LIST
443
          ? 1 + s_test_list_length(list->u.tail)
444 445 446
          : 0);
}

447
int __cdecl s_sum_fixed_int_3d(int m[2][3][4])
448 449 450 451 452 453 454 455 456 457 458 459
{
  int i, j, k;
  int sum = 0;

  for (i = 0; i < 2; ++i)
    for (j = 0; j < 3; ++j)
      for (k = 0; k < 4; ++k)
        sum += m[i][j][k];

  return sum;
}

460
int __cdecl s_sum_conf_array(int x[], int n)
461 462 463 464 465 466 467 468 469 470
{
  int *p = x, *end = p + n;
  int sum = 0;

  while (p < end)
    sum += *p++;

  return sum;
}

471
int __cdecl s_sum_conf_ptr_by_conf_ptr(int n1, int *n2_then_x1, int *x2)
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
{
  int i;
  int sum = 0;
  if(n1 == 0)
    return 0;

  for(i = 1; i < n1; ++i)
    sum += n2_then_x1[i];

  for(i = 0; i < *n2_then_x1; ++i)
    sum += x2[i];

  return sum;
}

487
int __cdecl s_sum_unique_conf_array(int x[], int n)
488
{
489
  return s_sum_conf_array(x, n);
490 491
}

492
int __cdecl s_sum_unique_conf_ptr(int *x, int n)
493
{
494
  return x ? s_sum_conf_array(x, n) : 0;
495 496
}

497
int __cdecl s_sum_var_array(int x[20], int n)
498 499 500 501
{
  ok(0 <= n, "RPC sum_var_array\n");
  ok(n <= 20, "RPC sum_var_array\n");

502
  return s_sum_conf_array(x, n);
503 504
}

505
int __cdecl s_sum_complex_array(int n, refpint_t pi[])
506 507 508 509 510 511 512
{
  int total = 0;
  for (; n > 0; n--)
    total += *pi[n - 1];
  return total;
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
int __cdecl s_sum_blob(cs_blob_t *blob)
{
  int i, total = 0;

  for (i = 0; i < blob->n; i++)
    total += blob->ca[i];

  return total;
}

int __cdecl s_sum_data(cs_data_t *data)
{
  int i, total = 0;

  for (i = 0; i < data->blob.n; i++)
    total += data->blob.ca[i];

  return total;
}

int __cdecl s_sum_container(cs_container_t *container)
{
  int i, total = 0;

  for (i = 0; i < container->data.blob.n; i++)
    total += container->data.blob.ca[i];

  return total;
}

543
int __cdecl s_dot_two_vectors(vector_t vs[2])
544 545 546 547
{
  return vs[0].x * vs[1].x + vs[0].y * vs[1].y + vs[0].z * vs[1].z;
}

548
void __cdecl s_get_number_array(int x[20], int *n)
549 550 551
{
  int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  memcpy(x, c, sizeof(c));
552
  *n = ARRAY_SIZE(c);
553 554
}

555
int __cdecl s_sum_cs(cs_t *cs)
556
{
557
  return s_sum_conf_array(cs->ca, cs->n);
558 559
}

560
int __cdecl s_sum_cps(cps_t *cps)
561 562 563 564 565 566 567 568 569 570 571 572 573
{
  int sum = 0;
  int i;

  for (i = 0; i < *cps->pn; ++i)
    sum += cps->ca1[i];

  for (i = 0; i < 2 * cps->n; ++i)
    sum += cps->ca2[i];

  return sum;
}

574
int __cdecl s_sum_cpsc(cpsc_t *cpsc)
575 576 577 578 579 580 581 582
{
  int sum = 0;
  int i;
  for (i = 0; i < (cpsc->c ? cpsc->a : cpsc->b); ++i)
    sum += cpsc->ca[i];
  return sum;
}

583
int __cdecl s_get_cpsc(int n, cpsc_t *cpsc)
584 585 586
{
  int i, ret;

587
  cpsc->a = 2 * n;
588 589
  cpsc->b = 2;
  cpsc->c = 1;
590 591
  cpsc->ca = MIDL_user_allocate( cpsc->a * sizeof(int) );
  for (i = ret = 0; i < cpsc->a; i++) cpsc->ca[i] = i;
592 593 594 595
  for (i = ret = 0; i < cpsc->a; i++) ret += cpsc->ca[i];
  return ret;
}

596
int __cdecl s_square_puint(puint_t p)
597 598 599 600 601
{
  int n = atoi(p);
  return n * n;
}

602
int __cdecl s_sum_puints(puints_t *p)
603 604 605 606 607 608 609 610
{
  int sum = 0;
  int i;
  for (i = 0; i < p->n; ++i)
    sum += atoi(p->ps[i]);
  return sum;
}

611
int __cdecl s_sum_cpuints(cpuints_t *p)
612 613 614 615 616 617 618 619
{
  int sum = 0;
  int i;
  for (i = 0; i < p->n; ++i)
    sum += atoi(p->ps[i]);
  return sum;
}

620
int __cdecl s_dot_copy_vectors(vector_t u, vector_t v)
621 622 623 624
{
  return u.x * v.x + u.y * v.y + u.z * v.z;
}

625
int __cdecl s_square_test_us(test_us_t *tus)
626 627 628 629 630
{
  int n = atoi(tus->us.x);
  return n * n;
}

631
double __cdecl s_square_encu(encu_t *eu)
632 633 634 635 636 637 638 639 640 641
{
  switch (eu->t)
  {
  case ENCU_I: return eu->tagged_union.i * eu->tagged_union.i;
  case ENCU_F: return eu->tagged_union.f * eu->tagged_union.f;
  default:
    return 0.0;
  }
}

642
double __cdecl s_square_unencu(int t, unencu_t *eu)
643 644 645 646 647 648 649 650 651 652
{
  switch (t)
  {
  case ENCU_I: return eu->i * eu->i;
  case ENCU_F: return eu->f * eu->f;
  default:
    return 0.0;
  }
}

653
void __cdecl s_check_se2(se_t *s)
654 655 656 657
{
  ok(s->f == E2, "check_se2\n");
}

658
int __cdecl s_sum_parr(int *a[3])
659
{
660
  return s_sum_pcarr(a, 3);
661 662
}

663
int __cdecl s_sum_pcarr(int *a[], int n)
664 665 666 667 668 669 670
{
  int i, s = 0;
  for (i = 0; i < n; ++i)
    s += *a[i];
  return s;
}

671
int __cdecl s_enum_ord(e_t e)
672 673 674 675 676 677 678 679 680 681 682 683
{
  switch (e)
  {
  case E1: return 1;
  case E2: return 2;
  case E3: return 3;
  case E4: return 4;
  default:
    return 0;
  }
}

684
double __cdecl s_square_encue(encue_t *eue)
685 686 687 688 689 690 691 692 693 694
{
  switch (eue->t)
  {
  case E1: return eue->tagged_union.i1 * eue->tagged_union.i1;
  case E2: return eue->tagged_union.f2 * eue->tagged_union.f2;
  default:
    return 0.0;
  }
}

695
int __cdecl s_sum_toplev_conf_2n(int *x, int n)
696 697 698 699 700 701 702 703
{
  int sum = 0;
  int i;
  for (i = 0; i < 2 * n; ++i)
    sum += x[i];
  return sum;
}

704
int __cdecl s_sum_toplev_conf_cond(int *x, int a, int b, int c)
705 706 707 708 709 710 711 712 713
{
  int sum = 0;
  int n = c ? a : b;
  int i;
  for (i = 0; i < n; ++i)
    sum += x[i];
  return sum;
}

714
double __cdecl s_sum_aligns(aligns_t *a)
715 716 717 718
{
  return a->c + a->i + a->s + a->d;
}

719
int __cdecl s_sum_padded(padded_t *p)
720 721 722 723
{
  return p->i + p->c;
}

724
int __cdecl s_sum_padded2(padded_t ps[2])
725
{
726
  return s_sum_padded(&ps[0]) + s_sum_padded(&ps[1]);
727 728
}

729
int __cdecl s_sum_padded_conf(padded_t *ps, int n)
730 731 732 733
{
  int sum = 0;
  int i;
  for (i = 0; i < n; ++i)
734
    sum += s_sum_padded(&ps[i]);
735 736 737
  return sum;
}

738
int __cdecl s_sum_bogus(bogus_t *b)
739 740 741 742
{
  return *b->h.p1 + *b->p2 + *b->p3 + b->c;
}

743
void __cdecl s_check_null(int *null)
744 745 746 747
{
  ok(!null, "RPC check_null\n");
}

748
int __cdecl s_str_struct_len(str_struct_t *s)
749 750 751 752
{
  return lstrlenA(s->s);
}

753
int __cdecl s_wstr_struct_len(wstr_struct_t *s)
754 755 756 757
{
  return lstrlenW(s->s);
}

758
int __cdecl s_sum_doub_carr(doub_carr_t *dc)
759 760 761 762 763 764 765 766 767
{
  int i, j;
  int sum = 0;
  for (i = 0; i < dc->n; ++i)
    for (j = 0; j < dc->a[i]->n; ++j)
      sum += dc->a[i]->a[j];
  return sum;
}

768
void __cdecl s_make_pyramid_doub_carr(unsigned char n, doub_carr_t **dc)
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
{
  doub_carr_t *t;
  int i, j;
  t = MIDL_user_allocate(FIELD_OFFSET(doub_carr_t, a[n]));
  t->n = n;
  for (i = 0; i < n; ++i)
  {
    int v = i + 1;
    t->a[i] = MIDL_user_allocate(FIELD_OFFSET(doub_carr_1_t, a[v]));
    t->a[i]->n = v;
    for (j = 0; j < v; ++j)
      t->a[i]->a[j] = j + 1;
  }
  *dc = t;
}

785
unsigned __cdecl s_hash_bstr(bstr_t b)
786 787 788 789 790 791 792 793 794 795
{
  short n = b[-1];
  short *s = b;
  unsigned hash = 0;
  short i;
  for (i = 0; i < n; ++i)
    hash = 5 * hash + (unsigned) s[i];
  return hash;
}

796
void __cdecl s_get_a_bstr(bstr_t *b)
797 798 799 800 801 802 803 804
{
  bstr_t bstr;
  short str[] = {5, 'W', 'i', 'n', 'e', 0};
  bstr = HeapAlloc(GetProcessHeap(), 0, sizeof(str));
  memcpy(bstr, str, sizeof(str));
  *b = bstr + 1;
}

805
void __cdecl s_get_name(name_t *name)
806 807 808 809 810 811 812 813
{
  const char bossman[] = "Jeremy White";
  memcpy(name->name, bossman, min(name->size, sizeof(bossman)));
  /* ensure nul-termination */
  if (name->size < sizeof(bossman))
    name->name[name->size - 1] = 0;
}

814
void __cdecl s_get_names(int *n, str_array_t *names)
815 816 817 818 819 820 821 822 823 824 825 826 827
{
  str_array_t list;

  list = MIDL_user_allocate(2 * sizeof(list[0]));
  list[0] = MIDL_user_allocate(6);
  strcpy(list[0], "Hello");
  list[1] = MIDL_user_allocate(7);
  strcpy(list[1], "World!");

  *names = list;
  *n = 2;
}

828
void __cdecl s_get_namesw(int *n, wstr_array_t *names)
829 830 831 832
{
  wstr_array_t list;

  list = MIDL_user_allocate(2 * sizeof(list[0]));
833 834 835 836
  list[0] = MIDL_user_allocate(sizeof(L"Hello"));
  lstrcpyW(list[0], L"Hello");
  list[1] = MIDL_user_allocate(sizeof(L"World!"));
  lstrcpyW(list[1], L"World!");
837 838 839 840 841

  *names = list;
  *n = 2;
}

842
int __cdecl s_sum_pcarr2(int n, int **pa)
843
{
844
  return s_sum_conf_array(*pa, n);
845 846
}

847
int __cdecl s_sum_L1_norms(int n, vector_t *vs)
848 849 850 851 852 853 854 855
{
  int i;
  int sum = 0;
  for (i = 0; i < n; ++i)
    sum += abs(vs[i].x) + abs(vs[i].y) + abs(vs[i].z);
  return sum;
}

856
s123_t * __cdecl s_get_s123(void)
857 858 859 860 861 862 863 864
{
  s123_t *s = MIDL_user_allocate(sizeof *s);
  s->f1 = 1;
  s->f2 = 2;
  s->f3 = 3;
  return s;
}

865
str_t __cdecl s_get_filename(void)
866 867 868 869
{
    return (char *)__FILE__;
}

870
int __cdecl s_echo_ranged_int(int i, int j, int k)
871
{
872 873 874
    return min( 100, i + j + k );
}

875
int __cdecl s_echo_ranged_int2(int i)
876 877
{
    return i;
878 879
}

880
void __cdecl s_get_ranged_enum(renum_t *re)
881 882 883 884
{
    *re = RE3;
}

885
void __cdecl s_context_handle_test(void)
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
{
    NDR_SCONTEXT h;
    RPC_BINDING_HANDLE binding;
    RPC_STATUS status;
    unsigned char buf[20];
    static RPC_SERVER_INTERFACE server_if =
    {
        sizeof(RPC_SERVER_INTERFACE),
        {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
        {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
        NULL,
        0,
        0,
        0,
        0,
        0,
    };

    binding = I_RpcGetCurrentCallHandle();
    ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");

907 908 909 910 911 912
    if (!pNDRSContextMarshall2 || !pNDRSContextUnmarshall2)
    {
        win_skip("NDRSContextMarshall2 or NDRSContextUnmarshall2 not exported from rpcrt4.dll\n");
        return;
    }

913
    h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
914 915 916 917
    ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");

    /* marshal a context handle with NULL userContext */
    memset(buf, 0xcc, sizeof(buf));
918
    pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
919 920 921
    ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
    ok(UuidIsNil((UUID *)&buf[4], &status), "uuid should have been nil\n");

922
    h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
923 924 925 926 927
    ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");

    /* marshal a context handle with non-NULL userContext */
    memset(buf, 0xcc, sizeof(buf));
    h->userContext = (void *)0xdeadbeef;
928
    pNDRSContextMarshall2(binding, h, buf, NULL, NULL, 0);
929 930 931
    ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
    ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");

932 933 934
    /* raises ERROR_INVALID_HANDLE exception on Vista upwards */
    if (0)
    {
935
    h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, NULL, 0);
936 937 938 939
    ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
    ok(h->userContext == (void *)0xdeadbeef, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);

    /* marshal a context handle with an interface specified */
940
    h = pNDRSContextUnmarshall2(binding, NULL, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
941 942 943 944
    ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");

    memset(buf, 0xcc, sizeof(buf));
    h->userContext = (void *)0xcafebabe;
945
    pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
946 947 948
    ok(*(ULONG *)buf == 0, "attributes should have been set to 0 instead of 0x%x\n", *(ULONG *)buf);
    ok(!UuidIsNil((UUID *)&buf[4], &status), "uuid should not have been nil\n");

949
    h = pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if.InterfaceId, 0);
950 951
    ok(h != NULL, "NDRSContextUnmarshall2 returned NULL\n");
    ok(h->userContext == (void *)0xcafebabe, "userContext of interface didn't unmarshal properly: %p\n", h->userContext);
952
    }
953 954 955 956 957 958 959

    /* test same interface data, but different pointer */
    /* raises ERROR_INVALID_HANDLE exception */
    if (0)
    {
        RPC_SERVER_INTERFACE server_if_clone = server_if;

960
        pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if_clone.InterfaceId, 0);
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
    }

    /* test different interface data, but different pointer */
    /* raises ERROR_INVALID_HANDLE exception */
    if (0)
    {
        static RPC_SERVER_INTERFACE server_if2 =
        {
            sizeof(RPC_SERVER_INTERFACE),
            {{0x00000000,0x4114,0x0704,{0x23,0x01,0x00,0x00,0x00,0x00,0x00,0x00}},{1,0}},
            {{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},
            NULL,
            0,
            0,
            0,
            0,
            0,
        };
979
        pNDRSContextMarshall2(binding, h, buf, NULL, &server_if.InterfaceId, 0);
980

981
        pNDRSContextUnmarshall2(binding, buf, NDR_LOCAL_DATA_REPRESENTATION, &server_if2.InterfaceId, 0);
982
    }
983 984 985 986

    binding = NULL;
    status = RpcBindingServerFromClient(NULL, &binding);

987 988
    ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
    ok(binding != NULL, "binding is NULL\n");
989 990 991 992

    if (status == RPC_S_OK && binding != NULL)
    {
        unsigned char* string_binding = NULL;
993 994 995 996 997
        unsigned char* object_uuid = NULL;
        unsigned char* protseq = NULL;
        unsigned char* network_address = NULL;
        unsigned char* endpoint = NULL;
        unsigned char* network_options = NULL;
998 999 1000 1001 1002

        status = RpcBindingToStringBindingA(binding, &string_binding);
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
        ok(string_binding != NULL, "string_binding is NULL\n");

1003
        status = RpcStringBindingParseA(string_binding, &object_uuid, &protseq, &network_address, &endpoint, &network_options);
1004
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
1005 1006 1007 1008 1009 1010 1011 1012 1013
        ok(protseq != NULL && *protseq != '\0', "protseq is %s\n", protseq);
        ok(network_address != NULL && *network_address != '\0', "network_address is %s\n", network_address);

        todo_wine
        {
            ok(object_uuid != NULL && *object_uuid == '\0', "object_uuid is %s\n", object_uuid);
            ok(endpoint != NULL && *endpoint == '\0', "endpoint is %s\n", endpoint);
            ok(network_options != NULL && *network_options == '\0', "network_options is %s\n", network_options);
        }
1014 1015

        RpcStringFreeA(&string_binding);
1016 1017 1018 1019 1020
        RpcStringFreeA(&object_uuid);
        RpcStringFreeA(&protseq);
        RpcStringFreeA(&network_address);
        RpcStringFreeA(&endpoint);
        RpcStringFreeA(&network_options);
1021 1022
        RpcBindingFree(&binding);
    }
1023 1024
}

1025
void __cdecl s_get_numbers(int length, int size, pints_t n[])
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
{
    int i;
    for (i = 0; i < length; i++)
    {
        n[i].pi = midl_user_allocate(sizeof(*n[i].pi));
        *n[i].pi = i;
        n[i].ppi = NULL;
        n[i].pppi = NULL;
    }
}

1037
void __cdecl s_get_numbers_struct(numbers_struct_t **ns)
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
{
    int i;
    *ns = midl_user_allocate(FIELD_OFFSET(numbers_struct_t, numbers[5]));
    if (!*ns) return;
    (*ns)->length = 5;
    (*ns)->size = 5;
    for (i = 0; i < (*ns)->length; i++)
    {
        (*ns)->numbers[i].pi = NULL;
        (*ns)->numbers[i].ppi = NULL;
        (*ns)->numbers[i].pppi = NULL;
    }
    (*ns)->numbers[0].pi = midl_user_allocate(sizeof(*(*ns)->numbers[i].pi));
    *(*ns)->numbers[0].pi = 5;
}

1054
void __cdecl s_full_pointer_test(int *a, int *b)
1055 1056 1057 1058 1059 1060
{
    ok(*a == 42, "Expected *a to be 42 instead of %d\n", *a);
    ok(*b == 42, "Expected *b to be 42 instead of %d\n", *a);
    ok(a == b, "Expected a (%p) to point to the same memory as b (%p)\n", a, b);
}

1061
void __cdecl s_full_pointer_null_test(int *a, int *b)
1062 1063 1064 1065 1066
{
    ok(*a == 42, "Expected *a to be 42 instead of %d\n", *a);
    ok(b == NULL, "Expected b to be NULL instead of %p\n", b);
}

1067
void __cdecl s_stop(void)
1068
{
1069 1070 1071 1072 1073
  if (!stop_wait_event)
  {
    ok(RPC_S_OK == RpcMgmtStopServerListening(NULL), "RpcMgmtStopServerListening\n");
    ok(RPC_S_OK == RpcServerUnregisterIf(NULL, NULL, FALSE), "RpcServerUnregisterIf\n");
  }
1074
  ok(SetEvent(stop_event), "SetEvent\n");
1075 1076 1077 1078 1079 1080
  if (stop_wait_event)
  {
    DWORD ret;
    ret = WaitForSingleObject(stop_wait_event, 10000);
    ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
  }
1081 1082
}

1083
void __cdecl s_stop_autolisten(void)
1084 1085 1086
{
    RPC_STATUS status;
    status = RpcServerUnregisterIf(NULL, NULL, FALSE);
1087
    todo_wine
1088 1089 1090
    ok(status == RPC_S_UNKNOWN_MGR_TYPE, "got %u\n", status);
}

1091
void __cdecl s_ip_test(ipu_t *a)
1092 1093 1094 1095 1096 1097 1098 1099
{
    STATSTG st;
    HRESULT hr;

    hr = IStream_Stat(a->tagged_union.stream, &st, STATFLAG_NONAME);
    ok(hr == S_OK, "got %#x\n", hr);
}

1100
int __cdecl s_sum_ptr_array(int *a[2])
1101 1102 1103 1104
{
    return *a[0] + *a[1];
}

1105
int __cdecl s_sum_array_ptr(int (*a)[2])
1106 1107 1108 1109
{
    return (*a)[0] + (*a)[1];
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
ctx_handle_t __cdecl s_get_handle(void)
{
    return (ctx_handle_t)0xdeadbeef;
}

void __cdecl s_get_handle_by_ptr(ctx_handle_t *r)
{
    *r = (ctx_handle_t)0xdeadbeef;
}

void __cdecl s_test_handle(ctx_handle_t ctx_handle)
{
    ok(ctx_handle == (ctx_handle_t)0xdeadbeef, "Unexpected ctx_handle %p\n", ctx_handle);
}

void __RPC_USER ctx_handle_t_rundown(ctx_handle_t ctx_handle)
{
    ok(ctx_handle == (ctx_handle_t)0xdeadbeef, "Unexpected ctx_handle %p\n", ctx_handle);
}

1130 1131 1132 1133 1134 1135
static void
make_cmdline(char buffer[MAX_PATH], const char *test)
{
  sprintf(buffer, "%s server %s", progname, test);
}

1136
static void
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
run_client(const char *test)
{
  char cmdline[MAX_PATH];
  PROCESS_INFORMATION info;
  STARTUPINFOA startup;

  memset(&startup, 0, sizeof startup);
  startup.cb = sizeof startup;

  make_cmdline(cmdline, test);
  ok(CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
1148
  wait_child_process( info.hProcess );
1149 1150 1151 1152 1153 1154 1155
  ok(CloseHandle(info.hProcess), "CloseHandle\n");
  ok(CloseHandle(info.hThread), "CloseHandle\n");
}

static void
basic_tests(void)
{
1156
  char string[] = "I am a string";
1157
  WCHAR wstring[] = L"I am a wstring";
1158 1159 1160 1161 1162 1163
  int f[5] = {1, 3, 0, -2, -4};
  vector_t a = {1, 3, 7};
  vector_t vec1 = {4, -2, 1}, vec2 = {-5, 2, 3}, *pvec2 = &vec2;
  pvectors_t pvecs = {&vec1, &pvec2};
  sp_inner_t spi = {42};
  sp_t sp = {-13, &spi};
1164
  aligns_t aligns;
1165 1166
  pints_t pints;
  ptypes_t ptypes;
1167
  padded_t padded;
1168
  padded_t padded2[2];
1169
  bogus_t bogus;
1170
  int i1, i2, i3, *pi2, *pi3, **ppi3;
1171 1172
  double u, v;
  float s, t;
1173
  LONG q, r;
1174 1175
  short h;
  char c;
1176
  int x;
1177
  hyper y;
1178 1179
  str_struct_t ss = {string};
  wstr_struct_t ws = {wstring};
1180
  str_t str;
1181
  se_t se;
1182
  renum_t re;
1183 1184 1185 1186

  ok(int_return() == INT_CODE, "RPC int_return\n");

  ok(square(7) == 49, "RPC square\n");
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
  x = sum(23, -4);
  ok(x == 19, "RPC sum got %d\n", x);
  c = sum_char(-23, 50);
  ok(c == 27, "RPC sum_char got %d\n", (int)c);
  h = sum_short(1122, -344);
  ok(h == 778, "RPC sum_short got %d\n", (int)h);
  x = sum_float(123.45, -32.2);
  ok(x == 91, "RPC sum_float got %d\n", x);
  x = sum_double_int(-78, 148.46);
  ok(x == 70, "RPC sum_double_int got %d\n", x);
  y = sum_hyper((hyper)0x12345678 << 16, (hyper)0x33557799 << 16);
1198
  ok(y == (hyper)0x4589ce11 << 16, "RPC hyper got %s\n", wine_dbgstr_longlong(y));
1199 1200 1201 1202
  x = sum_hyper_int((hyper)0x24242424 << 16, -((hyper)0x24241212 << 16));
  ok(x == 0x12120000, "RPC hyper_int got 0x%x\n", x);
  x = sum_char_hyper( 12, ((hyper)0x42424242 << 32) | 0x33334444 );
  ok(x == 0x33334450, "RPC char_hyper got 0x%x\n", x);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

  x = 0;
  square_out(11, &x);
  ok(x == 121, "RPC square_out\n");

  x = 5;
  square_ref(&x);
  ok(x == 25, "RPC square_ref\n");

  ok(str_length(string) == strlen(string), "RPC str_length\n");
1213
  ok(str_t_length(string) == strlen(string), "RPC str_length\n");
1214 1215
  ok(dot_self(&a) == 59, "RPC dot_self\n");

1216 1217 1218
  ok(str_struct_len(&ss) == lstrlenA(string), "RPC str_struct_len\n");
  ok(wstr_struct_len(&ws) == lstrlenW(wstring), "RPC str_struct_len\n");

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
  v = 0.0;
  u = square_half(3.0, &v);
  ok(u == 9.0, "RPC square_half\n");
  ok(v == 1.5, "RPC square_half\n");

  t = 0.0f;
  s = square_half_float(3.0f, &t);
  ok(s == 9.0f, "RPC square_half_float\n");
  ok(t == 1.5f, "RPC square_half_float\n");

  r = 0;
  q = square_half_long(3, &r);
  ok(q == 9, "RPC square_half_long\n");
  ok(r == 1, "RPC square_half_long\n");
1233

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  i1 = 19;
  i2 = -3;
  i3 = -29;
  pi2 = &i2;
  pi3 = &i3;
  ppi3 = &pi3;
  pints.pi = &i1;
  pints.ppi = &pi2;
  pints.pppi = &ppi3;
  ok(pints_sum(&pints) == -13, "RPC pints_sum\n");

  c = 10;
  h = 3;
  q = 14;
  s = -5.0f;
  u = 11.0;
  ptypes.pc = &c;
  ptypes.ps = &h;
  ptypes.pl = &q;
  ptypes.pf = &s;
  ptypes.pd = &u;
  ok(ptypes_sum(&ptypes) == 33.0, "RPC ptypes_sum\n");

1257
  ok(dot_pvectors(&pvecs) == -21, "RPC dot_pvectors\n");
1258
  ok(dot_copy_vectors(vec1, vec2) == -21, "RPC dot_copy_vectors\n");
1259
  ok(sum_fixed_array(f) == -2, "RPC sum_fixed_array\n");
1260
  ok(sum_sp(&sp) == 29, "RPC sum_sp\n");
1261 1262 1263 1264 1265

  ok(enum_ord(E1) == 1, "RPC enum_ord\n");
  ok(enum_ord(E2) == 2, "RPC enum_ord\n");
  ok(enum_ord(E3) == 3, "RPC enum_ord\n");
  ok(enum_ord(E4) == 4, "RPC enum_ord\n");
1266

1267 1268 1269
  se.f = E2;
  check_se2(&se);

1270 1271 1272 1273 1274
  memset(&aligns, 0, sizeof(aligns));
  aligns.c = 3;
  aligns.i = 4;
  aligns.s = 5;
  aligns.d = 6.0;
1275
  ok(sum_aligns(&aligns) == 18.0, "RPC sum_aligns\n");
1276 1277 1278 1279

  padded.i = -3;
  padded.c = 8;
  ok(sum_padded(&padded) == 5, "RPC sum_padded\n");
1280 1281 1282 1283 1284
  padded2[0].i = -5;
  padded2[0].c = 1;
  padded2[1].i = 3;
  padded2[1].c = 7;
  ok(sum_padded2(padded2) == 6, "RPC sum_padded2\n");
1285 1286 1287 1288 1289
  padded2[0].i = -5;
  padded2[0].c = 1;
  padded2[1].i = 3;
  padded2[1].c = 7;
  ok(sum_padded_conf(padded2, 2) == 6, "RPC sum_padded_conf\n");
1290 1291 1292 1293 1294 1295 1296 1297 1298

  i1 = 14;
  i2 = -7;
  i3 = -4;
  bogus.h.p1 = &i1;
  bogus.p2 = &i2;
  bogus.p3 = &i3;
  bogus.c = 9;
  ok(sum_bogus(&bogus) == 12, "RPC sum_bogus\n");
1299 1300

  check_null(NULL);
1301

1302
  if (!is_interp || sizeof(void*) != 8) { /* broken in widl for win64 */
1303 1304
  str = get_filename();
  ok(!strcmp(str, __FILE__), "get_filename() returned %s instead of %s\n", str, __FILE__);
1305
  midl_user_free(str);
1306
  }
1307

1308
  x = echo_ranged_int(0,0,0);
1309
  ok(x == 0, "echo_ranged_int() returned %d instead of 0\n", x);
1310
  x = echo_ranged_int(10,20,100);
1311
  ok(x == 100, "echo_ranged_int() returned %d instead of 100\n", x);
1312 1313
  x = echo_ranged_int2(40);
  ok(x == 40, "echo_ranged_int() returned %d instead of 40\n", x);
1314

1315 1316
  if (!old_windows_version)
  {
1317
      re = 0xdeadbeef;
1318
      get_ranged_enum(&re);
1319 1320 1321
      ok(re == RE3 ||
         broken(re == MAKELONG(re, 0xdead)), /* Win 8, Win 10 */
         "get_ranged_enum() returned %x instead of RE3\n", re);
1322
  }
1323 1324
}

1325 1326 1327
static void
union_tests(void)
{
1328
  encue_t eue;
1329
  encu_t eu;
1330
  unencu_t uneu;
1331
  sun_t su;
1332 1333
  ipu_t ipu;
  LONG ref;
1334
  int i;
1335

1336 1337 1338
  su.s = SUN_I;
  su.u.i = 9;
  ok(square_sun(&su) == 81.0, "RPC square_sun\n");
1339

1340 1341 1342
  su.s = SUN_F1;
  su.u.f = 5.0;
  ok(square_sun(&su) == 25.0, "RPC square_sun\n");
1343

1344 1345 1346
  su.s = SUN_F2;
  su.u.f = -2.0;
  ok(square_sun(&su) == 4.0, "RPC square_sun\n");
1347

1348 1349
  su.s = SUN_PI;
  su.u.pi = &i;
1350
  i = 11;
1351
  ok(square_sun(&su) == 121.0, "RPC square_sun\n");
1352 1353 1354 1355 1356 1357 1358 1359

  eu.t = ENCU_I;
  eu.tagged_union.i = 7;
  ok(square_encu(&eu) == 49.0, "RPC square_encu\n");

  eu.t = ENCU_F;
  eu.tagged_union.f = 3.0;
  ok(square_encu(&eu) == 9.0, "RPC square_encu\n");
1360

1361 1362 1363 1364 1365 1366
  uneu.i = 4;
  ok(square_unencu(ENCU_I, &uneu) == 16.0, "RPC square_unencu\n");

  uneu.f = 5.0;
  ok(square_unencu(ENCU_F, &uneu) == 25.0, "RPC square_unencu\n");

1367 1368 1369 1370 1371 1372 1373
  eue.t = E1;
  eue.tagged_union.i1 = 8;
  ok(square_encue(&eue) == 64.0, "RPC square_encue\n");

  eue.t = E2;
  eue.tagged_union.f2 = 10.0;
  ok(square_encue(&eue) == 100.0, "RPC square_encue\n");
1374 1375 1376 1377 1378 1379 1380 1381 1382

  CoInitializeEx(NULL, COINIT_MULTITHREADED);

  CreateStreamOnHGlobal(NULL, TRUE, &ipu.tagged_union.stream);
  ip_test(&ipu);
  ref = IStream_Release(ipu.tagged_union.stream);
  ok(!ref, "got %u refs\n", ref);

  CoUninitialize();
1383
}
1384

1385 1386 1387 1388 1389
static test_list_t *
null_list(void)
{
  test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
  n->t = TL_NULL;
1390
  n->u.x = 0;
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
  return n;
}

static test_list_t *
make_list(test_list_t *tail)
{
  test_list_t *n = HeapAlloc(GetProcessHeap(), 0, sizeof *n);
  n->t = TL_LIST;
  n->u.tail = tail;
  return n;
}

static void
free_list(test_list_t *list)
{
  if (list->t == TL_LIST)
    free_list(list->u.tail);
  HeapFree(GetProcessHeap(), 0, list);
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
ULONG __RPC_USER
puint_t_UserSize(ULONG *flags, ULONG start, puint_t *p)
{
  return start + sizeof(int);
}

unsigned char * __RPC_USER
puint_t_UserMarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
{
  int n = atoi(*p);
  memcpy(buffer, &n, sizeof n);
  return buffer + sizeof n;
}

unsigned char * __RPC_USER
puint_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, puint_t *p)
{
  int n;
  memcpy(&n, buffer, sizeof n);
  *p = HeapAlloc(GetProcessHeap(), 0, 10);
  sprintf(*p, "%d", n);
  return buffer + sizeof n;
}

void __RPC_USER
puint_t_UserFree(ULONG *flags, puint_t *p)
{
  HeapFree(GetProcessHeap(), 0, *p);
}

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
ULONG __RPC_USER
us_t_UserSize(ULONG *flags, ULONG start, us_t *pus)
{
  return start + sizeof(struct wire_us);
}

unsigned char * __RPC_USER
us_t_UserMarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
{
  struct wire_us wus;
  wus.x = atoi(pus->x);
  memcpy(buffer, &wus, sizeof wus);
  return buffer + sizeof wus;
}

unsigned char * __RPC_USER
us_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, us_t *pus)
{
  struct wire_us wus;
  memcpy(&wus, buffer, sizeof wus);
  pus->x = HeapAlloc(GetProcessHeap(), 0, 10);
  sprintf(pus->x, "%d", wus.x);
  return buffer + sizeof wus;
}

void __RPC_USER
us_t_UserFree(ULONG *flags, us_t *pus)
{
  HeapFree(GetProcessHeap(), 0, pus->x);
}

1472 1473 1474
ULONG __RPC_USER
bstr_t_UserSize(ULONG *flags, ULONG start, bstr_t *b)
{
1475
  return start + FIELD_OFFSET(user_bstr_t, data[(*b)[-1]]);
1476 1477 1478 1479 1480
}

unsigned char * __RPC_USER
bstr_t_UserMarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
{
1481
  wire_bstr_t wb = (wire_bstr_t) buffer;
1482 1483
  wb->n = (*b)[-1];
  memcpy(&wb->data, *b, wb->n * sizeof wb->data[0]);
1484
  return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1485 1486 1487 1488 1489
}

unsigned char * __RPC_USER
bstr_t_UserUnmarshal(ULONG *flags, unsigned char *buffer, bstr_t *b)
{
1490
  wire_bstr_t wb = (wire_bstr_t) buffer;
1491 1492 1493 1494
  short *data = HeapAlloc(GetProcessHeap(), 0, (wb->n + 1) * sizeof *data);
  data[0] = wb->n;
  memcpy(&data[1], wb->data, wb->n * sizeof data[1]);
  *b = &data[1];
1495
  return buffer + FIELD_OFFSET(user_bstr_t, data[wb->n]);
1496 1497 1498 1499 1500 1501 1502 1503
}

void __RPC_USER
bstr_t_UserFree(ULONG *flags, bstr_t *b)
{
  HeapFree(GetProcessHeap(), 0, &((*b)[-1]));
}

1504 1505 1506
static void
pointer_tests(void)
{
1507 1508
  int a[] = {1, 2, 3, 4};
  char p1[] = "11";
1509
  test_list_t *list = make_list(make_list(make_list(null_list())));
1510
  test_us_t tus = {{p1}};
1511
  int *pa[4];
1512 1513
  puints_t pus;
  cpuints_t cpus;
1514
  short bstr_data[] = { 5, 'H', 'e', 'l', 'l', 'o' };
1515
  bstr_t bstr = &bstr_data[1], bstr2;
1516 1517
  name_t name;
  void *buffer;
1518
  int *pa2;
1519
  s123_t *s123;
1520
  int val = 42;
1521 1522

  ok(test_list_length(list) == 3, "RPC test_list_length\n");
1523
  ok(square_puint(p1) == 121, "RPC square_puint\n");
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
  pus.n = 4;
  pus.ps = HeapAlloc(GetProcessHeap(), 0, pus.n * sizeof pus.ps[0]);
  pus.ps[0] = xstrdup("5");
  pus.ps[1] = xstrdup("6");
  pus.ps[2] = xstrdup("7");
  pus.ps[3] = xstrdup("8");
  ok(sum_puints(&pus) == 26, "RPC sum_puints\n");
  HeapFree(GetProcessHeap(), 0, pus.ps[0]);
  HeapFree(GetProcessHeap(), 0, pus.ps[1]);
  HeapFree(GetProcessHeap(), 0, pus.ps[2]);
  HeapFree(GetProcessHeap(), 0, pus.ps[3]);
  HeapFree(GetProcessHeap(), 0, pus.ps);
  cpus.n = 4;
  cpus.ps = HeapAlloc(GetProcessHeap(), 0, cpus.n * sizeof cpus.ps[0]);
  cpus.ps[0] = xstrdup("5");
  cpus.ps[1] = xstrdup("6");
  cpus.ps[2] = xstrdup("7");
  cpus.ps[3] = xstrdup("8");
  ok(sum_cpuints(&cpus) == 26, "RPC sum_puints\n");
  HeapFree(GetProcessHeap(), 0, cpus.ps[0]);
  HeapFree(GetProcessHeap(), 0, cpus.ps[1]);
  HeapFree(GetProcessHeap(), 0, cpus.ps[2]);
  HeapFree(GetProcessHeap(), 0, cpus.ps[3]);
  HeapFree(GetProcessHeap(), 0, cpus.ps);
1548
  ok(square_test_us(&tus) == 121, "RPC square_test_us\n");
1549

1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  pa[0] = &a[0];
  pa[1] = &a[1];
  pa[2] = &a[2];
  ok(sum_parr(pa) == 6, "RPC sum_parr\n");

  pa[0] = &a[0];
  pa[1] = &a[1];
  pa[2] = &a[2];
  pa[3] = &a[3];
  ok(sum_pcarr(pa, 4) == 10, "RPC sum_pcarr\n");

1561
  ok(hash_bstr(bstr) == s_hash_bstr(bstr), "RPC hash_bstr_data\n");
1562

1563
  get_a_bstr(&bstr);
1564
  s_get_a_bstr(&bstr2);
1565 1566 1567 1568
  ok(!lstrcmpW((LPCWSTR)bstr, (LPCWSTR)bstr2), "bstr mismatch\n");
  HeapFree(GetProcessHeap(), 0, bstr - 1);
  HeapFree(GetProcessHeap(), 0, bstr2 - 1);

1569
  free_list(list);
1570

1571 1572
  if (!old_windows_version)
  {
1573 1574 1575 1576
      int n;
      str_array_t names;
      wstr_array_t namesw;

1577 1578 1579 1580 1581 1582
      name.size = 10;
      name.name = buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, name.size);
      get_name(&name);
      ok(name.name == buffer, "[in,out] pointer should have stayed as %p but instead changed to %p\n", name.name, buffer);
      ok(!strcmp(name.name, "Jeremy Wh"), "name didn't unmarshall properly, expected \"Jeremy Wh\", but got \"%s\"\n", name.name);
      HeapFree(GetProcessHeap(), 0, name.name);
1583

1584
      if (!is_interp) { /* broken in widl */
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
      n = -1;
      names = NULL;
      get_names(&n, &names);
      ok(n == 2, "expected 2, got %d\n", n);
      ok(!strcmp(names[0], "Hello"), "expected Hello, got %s\n", names[0]);
      ok(!strcmp(names[1], "World!"), "expected World!, got %s\n", names[1]);
      MIDL_user_free(names[0]);
      MIDL_user_free(names[1]);
      MIDL_user_free(names);

      n = -1;
      namesw = NULL;
      get_namesw(&n, &namesw);
      ok(n == 2, "expected 2, got %d\n", n);
1599 1600
      ok(!lstrcmpW(namesw[0], L"Hello"), "expected Hello, got %s\n", wine_dbgstr_w(namesw[0]));
      ok(!lstrcmpW(namesw[1], L"World!"), "expected World!, got %s\n", wine_dbgstr_w(namesw[1]));
1601 1602 1603
      MIDL_user_free(namesw[0]);
      MIDL_user_free(namesw[1]);
      MIDL_user_free(namesw);
1604
      }
1605
  }
1606

1607
  if (!is_interp) { /* broken in widl */
1608 1609
  pa2 = a;
  ok(sum_pcarr2(4, &pa2) == 10, "RPC sum_pcarr2\n");
1610
  }
1611 1612 1613 1614

  s123 = get_s123();
  ok(s123->f1 == 1 && s123->f2 == 2 && s123->f3 == 3, "RPC get_s123\n");
  MIDL_user_free(s123);
1615 1616 1617

  full_pointer_test(&val, &val);
  full_pointer_null_test(&val, NULL);
1618 1619
}

1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
static int
check_pyramid_doub_carr(doub_carr_t *dc)
{
  int i, j;
  for (i = 0; i < dc->n; ++i)
    for (j = 0; j < dc->a[i]->n; ++j)
      if (dc->a[i]->a[j] != j + 1)
        return FALSE;
  return TRUE;
}

static void
free_pyramid_doub_carr(doub_carr_t *dc)
{
  int i;
  for (i = 0; i < dc->n; ++i)
    MIDL_user_free(dc->a[i]);
1637
  MIDL_user_free(dc);
1638 1639
}

1640 1641 1642
static void
array_tests(void)
{
1643
  int m[2][3][4] =
1644 1645 1646 1647
  {
    {{1, 2, 3, 4}, {-1, -3, -5, -7}, {0, 2, 4, 6}},
    {{1, -2, 3, -4}, {2, 3, 5, 7}, {-4, -1, -14, 4114}}
  };
1648
  int c[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
1649
  int c2[] = {10, 100, 200};
1650
  int c3[20];
1651
  vector_t vs[2] = {{1, -2, 3}, {4, -5, -6}};
1652
  cps_t cps;
1653
  cpsc_t cpsc;
1654 1655 1656
  cs_blob_t blob;
  cs_data_t data;
  cs_container_t container;
1657
  cs_t *cs;
1658
  int n;
1659
  int ca[5] = {1, -2, 3, -4, 5};
1660
  int tmp[10];
1661
  doub_carr_t *dc;
1662 1663
  int *pi;
  pints_t api[5];
1664
  numbers_struct_t *ns;
1665
  refpint_t rpi[5];
1666
  int i0 = 1, i1 = 2, *ptr_array[2] = {&i0, &i1}, array[2] = {3, 4};
1667

1668 1669 1670 1671 1672
  if (!old_windows_version)
  {
      const char str1[25] = "Hello";
      ok(cstr_length(str1, sizeof str1) == strlen(str1), "RPC cstr_length\n");
  }
1673

1674 1675 1676 1677 1678 1679 1680
  ok(sum_fixed_int_3d(m) == 4116, "RPC sum_fixed_int_3d\n");

  ok(sum_conf_array(c, 10) == 45, "RPC sum_conf_array\n");
  ok(sum_conf_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
  ok(sum_conf_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
  ok(sum_conf_array(&c[2], 0) == 0, "RPC sum_conf_array\n");

1681 1682
  ok(sum_conf_ptr_by_conf_ptr(1, c2, c) == 45, "RPC sum_conf_ptr_by_conf_ptr\n");
  ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 345, "RPC sum_conf_ptr_by_conf_ptr\n");
1683
  c2[0] = 0;
1684
  ok(sum_conf_ptr_by_conf_ptr(3, c2, c) == 300, "RPC sum_conf_ptr_by_conf_ptr\n");
1685

1686 1687 1688 1689
  ok(sum_unique_conf_array(ca, 4) == -2, "RPC sum_unique_conf_array\n");
  ok(sum_unique_conf_ptr(ca, 5) == 3, "RPC sum_unique_conf_array\n");
  ok(sum_unique_conf_ptr(NULL, 10) == 0, "RPC sum_unique_conf_array\n");

1690 1691 1692 1693 1694
  get_number_array(c3, &n);
  ok(n == 10, "RPC get_num_array\n");
  for (; n > 0; n--)
    ok(c3[n-1] == c[n-1], "get_num_array returned wrong value %d @ %d\n",
       c3[n-1], n);
1695 1696 1697 1698 1699 1700
  ok(sum_var_array(c, 10) == 45, "RPC sum_conf_array\n");
  ok(sum_var_array(&c[5], 2) == 11, "RPC sum_conf_array\n");
  ok(sum_var_array(&c[7], 1) == 7, "RPC sum_conf_array\n");
  ok(sum_var_array(&c[2], 0) == 0, "RPC sum_conf_array\n");

  ok(dot_two_vectors(vs) == -4, "RPC dot_two_vectors\n");
1701
  cs = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(cs_t, ca[5]));
1702 1703 1704 1705 1706 1707 1708 1709
  cs->n = 5;
  cs->ca[0] = 3;
  cs->ca[1] = 5;
  cs->ca[2] = -2;
  cs->ca[3] = -1;
  cs->ca[4] = -4;
  ok(sum_cs(cs) == 1, "RPC sum_cs\n");
  HeapFree(GetProcessHeap(), 0, cs);
1710 1711 1712 1713 1714 1715

  n = 5;
  cps.pn = &n;
  cps.ca1 = &c[2];
  cps.n = 3;
  cps.ca2 = &c[3];
1716
  ok(sum_cps(&cps) == 53, "RPC sum_cps\n");
1717 1718 1719 1720 1721

  cpsc.a = 4;
  cpsc.b = 5;
  cpsc.c = 1;
  cpsc.ca = c;
1722
  ok(sum_cpsc(&cpsc) == 6, "RPC sum_cpsc\n");
1723 1724 1725 1726
  cpsc.a = 4;
  cpsc.b = 5;
  cpsc.c = 0;
  cpsc.ca = c;
1727
  ok(sum_cpsc(&cpsc) == 10, "RPC sum_cpsc\n");
1728

1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
  cpsc.ca = NULL;
  ok(get_cpsc(5, &cpsc) == 45, "RPC sum_cpsc\n");
  ok( cpsc.a == 10, "RPC get_cpsc %u\n", cpsc.a );
  for (n = 0; n < 10; n++) ok( cpsc.ca[n] == n, "RPC get_cpsc[%d] = %d\n", n, cpsc.ca[n] );

  memset( tmp, 0x33, sizeof(tmp) );
  cpsc.ca = tmp;
  ok(get_cpsc(4, &cpsc) == 28, "RPC sum_cpsc\n");
  ok( cpsc.a == 8, "RPC get_cpsc %u\n", cpsc.a );
  ok( cpsc.ca == tmp, "RPC get_cpsc %p/%p\n", cpsc.ca, tmp );
  for (n = 0; n < 8; n++) ok( cpsc.ca[n] == n, "RPC get_cpsc[%d] = %d\n", n, cpsc.ca[n] );

1741 1742 1743
  ok(sum_toplev_conf_2n(c, 3) == 15, "RPC sum_toplev_conf_2n\n");
  ok(sum_toplev_conf_cond(c, 5, 6, 1) == 10, "RPC sum_toplev_conf_cond\n");
  ok(sum_toplev_conf_cond(c, 5, 6, 0) == 15, "RPC sum_toplev_conf_cond\n");
1744

1745
  dc = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_t, a[2]));
1746
  dc->n = 2;
1747
  dc->a[0] = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_1_t, a[3]));
1748 1749 1750 1751
  dc->a[0]->n = 3;
  dc->a[0]->a[0] = 5;
  dc->a[0]->a[1] = 1;
  dc->a[0]->a[2] = 8;
1752
  dc->a[1] = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(doub_carr_1_t, a[2]));
1753 1754 1755 1756
  dc->a[1]->n = 2;
  dc->a[1]->a[0] = 2;
  dc->a[1]->a[1] = 3;
  ok(sum_doub_carr(dc) == 19, "RPC sum_doub_carr\n");
1757 1758 1759
  HeapFree(GetProcessHeap(), 0, dc->a[0]);
  HeapFree(GetProcessHeap(), 0, dc->a[1]);
  HeapFree(GetProcessHeap(), 0, dc);
1760 1761 1762 1763 1764

  dc = NULL;
  make_pyramid_doub_carr(4, &dc);
  ok(check_pyramid_doub_carr(dc), "RPC make_pyramid_doub_carr\n");
  free_pyramid_doub_carr(dc);
1765 1766

  ok(sum_L1_norms(2, vs) == 21, "RPC sum_L1_norms\n");
1767 1768 1769 1770 1771 1772 1773

  memset(api, 0, sizeof(api));
  pi = HeapAlloc(GetProcessHeap(), 0, sizeof(*pi));
  *pi = -1;
  api[0].pi = pi;
  get_numbers(1, 1, api);
  ok(api[0].pi == pi, "RPC conformant varying array [out] pointer changed from %p to %p\n", pi, api[0].pi);
1774 1775
  ok(*api[0].pi == 0, "pi unmarshalled incorrectly %d\n", *api[0].pi);

1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
  if (!old_windows_version)
  {
      ns = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(numbers_struct_t, numbers[5]));
      ns->length = 5;
      ns->size = 5;
      ns->numbers[0].pi = pi;
      get_numbers_struct(&ns);
      ok(ns->numbers[0].pi == pi, "RPC conformant varying struct embedded pointer changed from %p to %p\n", pi, ns->numbers[0].pi);
      ok(*ns->numbers[0].pi == 5, "pi unmarshalled incorrectly %d\n", *ns->numbers[0].pi);
      HeapFree(GetProcessHeap(), 0, ns);
  }
1787
  HeapFree(GetProcessHeap(), 0, pi);
1788 1789 1790 1791 1792 1793 1794 1795 1796

  pi = HeapAlloc(GetProcessHeap(), 0, 5 * sizeof(*pi));
  pi[0] = 3;  rpi[0] = &pi[0];
  pi[1] = 5;  rpi[1] = &pi[1];
  pi[2] = -2; rpi[2] = &pi[2];
  pi[3] = -1; rpi[3] = &pi[3];
  pi[4] = -4; rpi[4] = &pi[4];
  ok(sum_complex_array(5, rpi) == 1, "RPC sum_complex_array\n");
  HeapFree(GetProcessHeap(), 0, pi);
1797 1798 1799

  ok(sum_ptr_array(ptr_array) == 3, "RPC sum_ptr_array\n");
  ok(sum_array_ptr(&array) == 7, "RPC sum_array_ptr\n");
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814

  blob.n = ARRAY_SIZE(c);
  blob.ca = c;
  n = sum_blob(&blob);
  ok(n == 45, "RPC sum_blob = %d\n", n);

  data.blob.n = ARRAY_SIZE(c);
  data.blob.ca = c;
  n = sum_data(&data);
  ok(n == 45, "RPC sum_data = %d\n", n);

  container.data.blob.n = ARRAY_SIZE(c);
  container.data.blob.ca = c;
  n = sum_container(&container);
  ok(n == 45, "RPC sum_container = %d\n", n);
1815 1816
}

1817
void __cdecl s_authinfo_test(unsigned int protseq, int secure)
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
{
    RPC_BINDING_HANDLE binding;
    RPC_STATUS status;
    ULONG level, authnsvc;
    RPC_AUTHZ_HANDLE privs;
    unsigned char *principal;

    binding = I_RpcGetCurrentCallHandle();
    ok(binding != NULL, "I_RpcGetCurrentCallHandle returned NULL\n");

    level = authnsvc = 0xdeadbeef;
    privs = (RPC_AUTHZ_HANDLE)0xdeadbeef;
    principal = (unsigned char *)0xdeadbeef;

    if (secure || protseq == RPC_PROTSEQ_LRPC)
    {
        status = RpcBindingInqAuthClientA(binding, &privs, &principal, &level, &authnsvc, NULL);
1835 1836 1837 1838 1839
        if (status == RPC_S_CANNOT_SUPPORT)
        {
            win_skip("RpcBindingInqAuthClientA not supported\n");
            return;
        }
1840 1841 1842 1843 1844 1845 1846 1847
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
        ok(privs != (RPC_AUTHZ_HANDLE)0xdeadbeef, "privs unchanged\n");
        ok(principal != (unsigned char *)0xdeadbeef, "principal unchanged\n");
        if (protseq != RPC_PROTSEQ_LRPC)
        {
            todo_wine
            ok(principal != NULL, "NULL principal\n");
        }
1848
        if (protseq == RPC_PROTSEQ_LRPC && principal)
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
        {
            int len;
            char *spn;

            len = WideCharToMultiByte(CP_ACP, 0, (const WCHAR *)privs, -1, NULL, 0, NULL, NULL);
            spn = HeapAlloc( GetProcessHeap(), 0, len );
            WideCharToMultiByte(CP_ACP, 0, (const WCHAR *)privs, -1, spn, len, NULL, NULL);

            ok(!strcmp(domain_and_user, spn), "expected %s got %s\n", domain_and_user, spn);
            HeapFree( GetProcessHeap(), 0, spn );
        }
        ok(level == RPC_C_AUTHN_LEVEL_PKT_PRIVACY, "level unchanged\n");
        ok(authnsvc == RPC_C_AUTHN_WINNT, "authnsvc unchanged\n");
1862 1863 1864 1865 1866 1867 1868 1869 1870
        RpcStringFreeA(&principal);

        status = RpcBindingInqAuthClientA(NULL, &privs, &principal, &level, &authnsvc, NULL);
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
        RpcStringFreeA(&principal);

        status = RpcBindingInqAuthClientExA(NULL, &privs, &principal, &level, &authnsvc, NULL, 0);
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
        RpcStringFreeA(&principal);
1871 1872 1873 1874 1875 1876

        status = RpcImpersonateClient(NULL);
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);
        status = RpcRevertToSelf();
        ok(status == RPC_S_OK, "expected RPC_S_OK got %u\n", status);

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
    }
    else
    {
        status = RpcBindingInqAuthClientA(binding, &privs, &principal, &level, &authnsvc, NULL);
        ok(status == RPC_S_BINDING_HAS_NO_AUTH, "expected RPC_S_BINDING_HAS_NO_AUTH got %u\n", status);
        ok(privs == (RPC_AUTHZ_HANDLE)0xdeadbeef, "got %p\n", privs);
        ok(principal == (unsigned char *)0xdeadbeef, "got %s\n", principal);
        ok(level == 0xdeadbeef, "got %u\n", level);
        ok(authnsvc == 0xdeadbeef, "got %u\n", authnsvc);
    }
}

1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
static void test_handle_return(void)
{
    ctx_handle_t handle, handle2;

    handle = get_handle();
    test_handle(handle);
    get_handle_by_ptr(&handle2);
    test_handle(handle2);
}

1899 1900 1901 1902 1903 1904 1905
static void
run_tests(void)
{
  basic_tests();
  union_tests();
  pointer_tests();
  array_tests();
1906
  context_handle_test();
1907
  test_handle_return();
1908 1909
}

1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
static void
set_auth_info(RPC_BINDING_HANDLE handle)
{
    RPC_STATUS status;
    RPC_SECURITY_QOS qos;

    qos.Version = 1;
    qos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;
    qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
    qos.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;

    status = pRpcBindingSetAuthInfoExA(handle, (RPC_CSTR)domain_and_user, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
                                       RPC_C_AUTHN_WINNT, NULL, 0, &qos);
    ok(status == RPC_S_OK, "RpcBindingSetAuthInfoExA failed %d\n", status);
}

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
#define test_is_server_listening(a,b) _test_is_server_listening(__LINE__,a,b)
static void _test_is_server_listening(unsigned line, RPC_BINDING_HANDLE binding, RPC_STATUS expected_status)
{
    RPC_STATUS status;
    status = RpcMgmtIsServerListening(binding);
    ok_(__FILE__,line)(status == expected_status, "RpcMgmtIsServerListening returned %u, expected %u\n",
                       status, expected_status);
}

#define test_is_server_listening2(a,b,c) _test_is_server_listening2(__LINE__,a,b,c)
static void _test_is_server_listening2(unsigned line, RPC_BINDING_HANDLE binding, RPC_STATUS expected_status,
        RPC_STATUS expected_status2)
{
    RPC_STATUS status;
    status = RpcMgmtIsServerListening(binding);
    ok_(__FILE__,line)(status == expected_status || status == expected_status2,
                       "RpcMgmtIsServerListening returned %u, expected %u or %u\n",
                       status, expected_status, expected_status2);
}

1946 1947 1948
static void
client(const char *test)
{
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
  static unsigned char iptcp[] = "ncacn_ip_tcp";
  static unsigned char np[] = "ncacn_np";
  static unsigned char ncalrpc[] = "ncalrpc";
  static unsigned char address[] = "127.0.0.1";
  static unsigned char address_np[] = "\\\\.";
  static unsigned char port[] = PORT;
  static unsigned char pipe[] = PIPE;
  static unsigned char guid[] = "00000000-4114-0704-2301-000000000000";

  unsigned char *binding;

1960 1961
  if (strcmp(test, "tcp_basic") == 0)
  {
1962
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1963
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
1964

1965
    run_tests();
1966
    authinfo_test(RPC_PROTSEQ_TCP, 0);
1967
    test_is_server_listening2(IMixedServer_IfHandle, RPC_S_OK, RPC_S_ACCESS_DENIED);
1968

1969
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
1970
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
1971 1972 1973
  }
  else if (strcmp(test, "tcp_secure") == 0)
  {
1974
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, iptcp, address, port, NULL, &binding), "RpcStringBindingCompose\n");
1975
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
1976

1977
    set_auth_info(IMixedServer_IfHandle);
1978
    authinfo_test(RPC_PROTSEQ_TCP, 1);
1979
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_ACCESS_DENIED);
1980

1981
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
1982
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
1983
  }
1984 1985
  else if (strcmp(test, "ncalrpc_basic") == 0)
  {
1986
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, ncalrpc, NULL, guid, NULL, &binding), "RpcStringBindingCompose\n");
1987
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
1988 1989 1990

    run_tests(); /* can cause RPC_X_BAD_STUB_DATA exception */
    authinfo_test(RPC_PROTSEQ_LRPC, 0);
1991
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_OK);
1992

1993
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
1994
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
1995
  }
1996 1997 1998
  else if (strcmp(test, "ncalrpc_autolisten") == 0)
  {
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, ncalrpc, NULL, guid, NULL, &binding), "RpcStringBindingCompose\n");
1999
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
2000 2001 2002

    run_tests();
    authinfo_test(RPC_PROTSEQ_LRPC, 0);
2003
    todo_wine
2004
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_NOT_LISTENING);
2005

2006 2007 2008
    stop_autolisten();
    ok(int_return() == INT_CODE, "RPC int_return\n");

2009
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
2010
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
2011
  }
2012 2013
  else if (strcmp(test, "ncalrpc_secure") == 0)
  {
2014
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, ncalrpc, NULL, guid, NULL, &binding), "RpcStringBindingCompose\n");
2015
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
2016

2017
    set_auth_info(IMixedServer_IfHandle);
2018
    authinfo_test(RPC_PROTSEQ_LRPC, 1);
2019
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_OK);
2020

2021
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
2022
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
2023
  }
2024 2025
  else if (strcmp(test, "np_basic") == 0)
  {
2026
    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, np, address_np, pipe, NULL, &binding), "RpcStringBindingCompose\n");
2027
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
2028

2029
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_OK);
2030
    run_tests();
2031
    authinfo_test(RPC_PROTSEQ_NMP, 0);
2032
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_OK);
2033
    stop();
2034
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_NOT_LISTENING);
2035

2036
    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
2037
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
2038 2039 2040 2041 2042 2043
  }
  else if (strcmp(test, "np_basic_interp") == 0)
  {
    set_interp_interface();

    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, np, address_np, pipe, NULL, &binding), "RpcStringBindingCompose\n");
2044
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IInterpServer_IfHandle), "RpcBindingFromStringBinding\n");
2045

2046
    test_is_server_listening(IInterpServer_IfHandle, RPC_S_OK);
2047 2048
    run_tests();
    authinfo_test(RPC_PROTSEQ_NMP, 0);
2049
    test_is_server_listening(IInterpServer_IfHandle, RPC_S_OK);
2050 2051

    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
2052
    ok(RPC_S_OK == RpcBindingFree(&IInterpServer_IfHandle), "RpcBindingFree\n");
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
  }
}

static void
server(void)
{
  static unsigned char iptcp[] = "ncacn_ip_tcp";
  static unsigned char port[] = PORT;
  static unsigned char np[] = "ncacn_np";
  static unsigned char pipe[] = PIPE;
2063 2064 2065
  static unsigned char ncalrpc[] = "ncalrpc";
  static unsigned char guid[] = "00000000-4114-0704-2301-000000000000";
  RPC_STATUS status, iptcp_status, np_status, ncalrpc_status;
2066
  DWORD ret;
2067

2068 2069 2070
  /* needed for tests involving interface pointers */
  CoInitializeEx(NULL, COINIT_MULTITHREADED);

2071
  iptcp_status = RpcServerUseProtseqEpA(iptcp, 20, port, NULL);
2072
  ok(iptcp_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_ip_tcp) failed with status %d\n", iptcp_status);
2073

2074
  ncalrpc_status = RpcServerUseProtseqEpA(ncalrpc, 0, guid, NULL);
2075 2076
  ok(ncalrpc_status == RPC_S_OK, "RpcServerUseProtseqEp(ncalrpc) failed with status %d\n", ncalrpc_status);

2077
  np_status = RpcServerUseProtseqEpA(np, 0, pipe, NULL);
2078 2079 2080
  if (np_status == RPC_S_PROTSEQ_NOT_SUPPORTED)
    skip("Protocol sequence ncacn_np is not supported\n");
  else
2081
    ok(np_status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", np_status);
2082

2083 2084 2085
  if (pRpcServerRegisterIfEx)
  {
    trace("Using RpcServerRegisterIfEx\n");
2086
    status = pRpcServerRegisterIfEx(s_IMixedServer_v0_0_s_ifspec, NULL, NULL,
2087 2088
                                    RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
                                    RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
2089
    ok(status == RPC_S_OK, "RpcServerRegisterIfEx failed with status %d\n", status);
2090
    status = pRpcServerRegisterIfEx(s_IInterpServer_v0_0_s_ifspec, NULL, NULL,
2091 2092 2093
                                    RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH,
                                    RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
    ok(status == RPC_S_OK, "RpcServerRegisterIfEx failed with status %d\n", status);
2094 2095
  }
  else
2096
  {
2097
    status = RpcServerRegisterIf(s_IMixedServer_v0_0_s_ifspec, NULL, NULL);
2098
    ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);
2099
    status = RpcServerRegisterIf(s_IInterpServer_v0_0_s_ifspec, NULL, NULL);
2100 2101
    ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);
  }
2102
  test_is_server_listening(NULL, RPC_S_NOT_LISTENING);
2103
  status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
2104
  ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
2105
  test_is_server_listening(NULL, RPC_S_OK);
2106
  stop_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2107
  ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
2108

2109 2110 2111
  if (iptcp_status == RPC_S_OK)
    run_client("tcp_basic");
  else
2112
    skip("tcp tests skipped due to earlier failure\n");
2113 2114

  if (ncalrpc_status == RPC_S_OK)
2115
  {
2116
    run_client("ncalrpc_basic");
2117 2118 2119

    /* we don't need to register RPC_C_AUTHN_WINNT for ncalrpc */
    run_client("ncalrpc_secure");
2120
  }
2121
  else
2122
    skip("lrpc tests skipped due to earlier failure\n");
2123

2124
  if (np_status == RPC_S_OK)
2125 2126
  {
    run_client("np_basic_interp");
2127
    run_client("np_basic");
2128
  }
2129 2130 2131 2132 2133 2134
  else
  {
    skip("np_basic tests skipped due to earlier failure\n");
    /* np client is what signals stop_event, so bail out if we didn't run do it */
    return;
  }
2135

2136 2137
  ret = WaitForSingleObject(stop_event, 1000);
  ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
2138

2139 2140 2141 2142 2143
  /* if the stop event didn't fire then RpcMgmtWaitServerListen will wait
   * forever, so don't bother calling it in this case */
  if (ret == WAIT_OBJECT_0)
  {
    status = RpcMgmtWaitServerListen();
2144
    ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);
2145
  }
2146 2147 2148

  CloseHandle(stop_event);
  stop_event = NULL;
2149

2150 2151
  if (pRpcServerRegisterIfEx)
  {
2152
    status = pRpcServerRegisterIfEx(s_IMixedServer_v0_0_s_ifspec, NULL, NULL,
2153 2154 2155 2156 2157 2158
        RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH | RPC_IF_AUTOLISTEN,
        RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
    ok(status == RPC_S_OK, "RpcServerRegisterIf() failed: %u\n", status);

    run_client("ncalrpc_autolisten");

2159
    status = RpcServerUnregisterIf(s_IMixedServer_v0_0_s_ifspec, NULL, TRUE);
2160 2161 2162
    ok(status == RPC_S_OK, "RpcServerUnregisterIf() failed: %u\n", status);
  }

2163
  CoUninitialize();
2164 2165
}

2166 2167 2168 2169
static DWORD WINAPI listen_test_client_thread(void *binding)
{
    RPC_STATUS status;

2170
    status = RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle);
2171 2172
    ok(status == RPC_S_OK, "RpcBindingFromStringBinding\n");

2173
    test_is_server_listening(IMixedServer_IfHandle, RPC_S_OK);
2174 2175 2176
    stop();
    trace("stopped\n");

2177
    status = RpcBindingFree(&IMixedServer_IfHandle);
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    ok(status == RPC_S_OK, "RpcBindingFree\n");
    return 0;
}

static DWORD WINAPI wait_listen_proc(void *arg)
{
    RPC_STATUS status;

    trace("waiting\n");
    status = RpcMgmtWaitServerListen();
    ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);
    trace("done\n");

    return 0;
}

static void test_stop_wait_for_call(unsigned char *binding)
{
    HANDLE client_thread, wait_listen_thread;
    RPC_STATUS status;
    DWORD ret;

2200
    status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
    ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
    test_is_server_listening(NULL, RPC_S_OK);

    stop_wait_event = CreateEventW(NULL, FALSE, FALSE, NULL);
    ok(stop_wait_event != NULL, "CreateEvent failed with error %d\n", GetLastError());
    stop_event = CreateEventW(NULL, FALSE, FALSE, NULL);
    ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());

    wait_listen_thread = CreateThread(NULL, 0, wait_listen_proc, 0, 0, NULL);
    ok(wait_listen_thread != NULL, "CreateThread failed\n");

    client_thread = CreateThread(NULL, 0, listen_test_client_thread, binding, 0, NULL);
    ok(client_thread != NULL, "CreateThread failed\n");
    CloseHandle(client_thread);

    ret = WaitForSingleObject(stop_event, 10000);
    ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");

    status = RpcMgmtStopServerListening(NULL);
    ok(status == RPC_S_OK, "RpcMgmtStopServerListening\n");
    test_is_server_listening(NULL, RPC_S_NOT_LISTENING);

    ret = WaitForSingleObject(wait_listen_thread, 500);
    ok(WAIT_TIMEOUT == ret, "WaitForSingleObject\n");

    SetEvent(stop_wait_event);

    ret = WaitForSingleObject(wait_listen_thread, 10000);
    ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject returned %u\n", ret);

    CloseHandle(wait_listen_thread);

    CloseHandle(stop_wait_event);
    stop_wait_event = NULL;
    CloseHandle(stop_event);
    stop_event = NULL;
}

2239 2240 2241
static void test_server_listening(void)
{
    static unsigned char np[] = "ncacn_np";
2242
    static unsigned char address_np[] = "\\\\.";
2243
    static unsigned char pipe[] = PIPE "listen_test";
2244 2245 2246
    static unsigned char ncalrpc[] = "ncalrpc";
    static unsigned char guid[] = "00000000-4114-0704-2302-000000000000";
    unsigned char *binding;
2247 2248 2249 2250 2251
    RPC_STATUS status;

    status = RpcServerUseProtseqEpA(np, 0, pipe, NULL);
    ok(status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", status);

2252
    status = RpcServerRegisterIf(s_IMixedServer_v0_0_s_ifspec, NULL, NULL);
2253 2254 2255
    ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);

    test_is_server_listening(NULL, RPC_S_NOT_LISTENING);
2256
    status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
2257 2258 2259
    ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);
    test_is_server_listening(NULL, RPC_S_OK);

2260
    status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
    ok(status == RPC_S_ALREADY_LISTENING, "RpcServerListen failed with status %d\n", status);

    status = RpcMgmtStopServerListening(NULL);
    ok(status == RPC_S_OK, "RpcMgmtStopServerListening\n");
    test_is_server_listening(NULL, RPC_S_NOT_LISTENING);

    status = RpcMgmtWaitServerListen();
    ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);

    status = RpcMgmtWaitServerListen();
    ok(status == RPC_S_NOT_LISTENING, "RpcMgmtWaitServerListening failed with status %d\n", status);
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292

    /* test that server stop waits for a call in progress */
    status = RpcStringBindingComposeA(NULL, np, address_np, pipe, NULL, &binding);
    ok(status == RPC_S_OK, "RpcStringBindingCompose\n");

    test_stop_wait_for_call(binding);

    status = RpcStringFreeA(&binding);
    ok(status == RPC_S_OK, "RpcStringFree\n");

    /* repeat the test using ncalrpc */
    status = RpcServerUseProtseqEpA(ncalrpc, 0, guid, NULL);
    ok(status == RPC_S_OK, "RpcServerUseProtseqEp(ncalrpc) failed with status %d\n", status);

    status = RpcStringBindingComposeA(NULL, ncalrpc, NULL, guid, NULL, &binding);
    ok(status == RPC_S_OK, "RpcStringBindingCompose\n");

    test_stop_wait_for_call(binding);

    status = RpcStringFreeA(&binding);
    ok(status == RPC_S_OK, "RpcStringFree\n");
2293 2294
}

2295
static HANDLE create_server_process(void)
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
{
    SECURITY_ATTRIBUTES sec_attr = { sizeof(sec_attr), NULL, TRUE };
    HANDLE ready_event;
    char cmdline[MAX_PATH];
    PROCESS_INFORMATION info;
    STARTUPINFOA startup;
    DWORD ret;

    memset(&startup, 0, sizeof startup);
    startup.cb = sizeof startup;

    ready_event = CreateEventW(&sec_attr, TRUE, FALSE, NULL);
    ok(ready_event != NULL, "CreateEvent failed: %u\n", GetLastError());

    sprintf(cmdline, "%s server run %lx", progname, (UINT_PTR)ready_event);
    trace("running server process...\n");
    ok(CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info), "CreateProcess\n");
    ret = WaitForSingleObject(ready_event, 10000);
    ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");

    ok(CloseHandle(info.hThread), "CloseHandle\n");
    ok(CloseHandle(ready_event), "CloseHandle\n");
    return info.hProcess;
}

static void run_server(HANDLE ready_event)
{
    static unsigned char np[] = "ncacn_np";
    static unsigned char pipe[] = PIPE "term_test";
    RPC_STATUS status;
    BOOL ret;

    status = RpcServerUseProtseqEpA(np, 0, pipe, NULL);
    ok(status == RPC_S_OK, "RpcServerUseProtseqEp(ncacn_np) failed with status %d\n", status);

2331
    status = RpcServerRegisterIf(s_IMixedServer_v0_0_s_ifspec, NULL, NULL);
2332 2333 2334
    ok(status == RPC_S_OK, "RpcServerRegisterIf failed with status %d\n", status);

    test_is_server_listening(NULL, RPC_S_NOT_LISTENING);
2335
    status = RpcServerListen(1, RPC_C_LISTEN_MAX_CALLS_DEFAULT, TRUE);
2336 2337 2338 2339 2340 2341 2342 2343
    ok(status == RPC_S_OK, "RpcServerListen failed with status %d\n", status);

    stop_event = CreateEventW(NULL, FALSE, FALSE, NULL);
    ok(stop_event != NULL, "CreateEvent failed with error %d\n", GetLastError());

    ret = SetEvent(ready_event);
    ok(ret, "SetEvent failed: %u\n", GetLastError());

2344
    ret = WaitForSingleObject(stop_event, 5000);
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
    ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");

    status = RpcMgmtWaitServerListen();
    ok(status == RPC_S_OK, "RpcMgmtWaitServerListening failed with status %d\n", status);

    CloseHandle(stop_event);
    stop_event = NULL;
}

static DWORD WINAPI basic_tests_thread(void *arg)
{
    basic_tests();
    return 0;
}

static void test_reconnect(void)
{
    static unsigned char np[] = "ncacn_np";
    static unsigned char address_np[] = "\\\\.";
    static unsigned char pipe[] = PIPE "term_test";
    unsigned char *binding;
    HANDLE threads[32];
    HANDLE server_process;
    unsigned i;
    DWORD ret;

    server_process = create_server_process();

    ok(RPC_S_OK == RpcStringBindingComposeA(NULL, np, address_np, pipe, NULL, &binding), "RpcStringBindingCompose\n");
2374
    ok(RPC_S_OK == RpcBindingFromStringBindingA(binding, &IMixedServer_IfHandle), "RpcBindingFromStringBinding\n");
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390

    for (i = 0; i < ARRAY_SIZE(threads); i++)
    {
        threads[i] = CreateThread(NULL, 0, basic_tests_thread, 0, 0, NULL);
        ok(threads[i] != NULL, "CreateThread failed: %u\n", GetLastError());
    }

    for (i = 0; i < ARRAY_SIZE(threads); i++)
    {
        ret = WaitForSingleObject(threads[i], 10000);
        ok(WAIT_OBJECT_0 == ret, "WaitForSingleObject\n");
        CloseHandle(threads[i]);
    }

    stop();

2391
    wait_child_process(server_process);
2392 2393 2394 2395 2396 2397 2398 2399
    ok(CloseHandle(server_process), "CloseHandle\n");

    /* create new server, rpcrt4 will connect to it once sending to existing connection fails
     * that current connection is broken. */
    server_process = create_server_process();
    basic_tests();
    stop();

2400
    wait_child_process(server_process);
2401 2402 2403
    ok(CloseHandle(server_process), "CloseHandle\n");

    ok(RPC_S_OK == RpcStringFreeA(&binding), "RpcStringFree\n");
2404
    ok(RPC_S_OK == RpcBindingFree(&IMixedServer_IfHandle), "RpcBindingFree\n");
2405 2406
}

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
static BOOL is_process_elevated(void)
{
    HANDLE token;
    if (OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token ))
    {
        TOKEN_ELEVATION_TYPE type;
        DWORD size;
        BOOL ret;

        ret = GetTokenInformation( token, TokenElevationType, &type, sizeof(type), &size );
        CloseHandle( token );
        return (ret && type == TokenElevationTypeFull);
    }
    return FALSE;
}

static BOOL is_firewall_enabled(void)
{
    HRESULT hr, init;
    INetFwMgr *mgr = NULL;
    INetFwPolicy *policy = NULL;
    INetFwProfile *profile = NULL;
    VARIANT_BOOL enabled = VARIANT_FALSE;

    init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );

    hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
                           (void **)&mgr );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
    if (hr != S_OK) goto done;

    hr = INetFwProfile_get_FirewallEnabled( profile, &enabled );
    ok( hr == S_OK, "got %08x\n", hr );

done:
    if (policy) INetFwPolicy_Release( policy );
    if (profile) INetFwProfile_Release( profile );
    if (mgr) INetFwMgr_Release( mgr );
    if (SUCCEEDED( init )) CoUninitialize();
    return (enabled == VARIANT_TRUE);
}

enum firewall_op
{
    APP_ADD,
    APP_REMOVE
};

static HRESULT set_firewall( enum firewall_op op )
{
    HRESULT hr, init;
    INetFwMgr *mgr = NULL;
    INetFwPolicy *policy = NULL;
    INetFwProfile *profile = NULL;
    INetFwAuthorizedApplication *app = NULL;
    INetFwAuthorizedApplications *apps = NULL;
    BSTR name, image = SysAllocStringLen( NULL, MAX_PATH );

    if (!GetModuleFileNameW( NULL, image, MAX_PATH ))
    {
        SysFreeString( image );
        return E_FAIL;
    }
    init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED );

    hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr,
                           (void **)&mgr );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = INetFwMgr_get_LocalPolicy( mgr, &policy );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = INetFwPolicy_get_CurrentProfile( policy, &profile );
    if (hr != S_OK) goto done;

2491
    hr = INetFwProfile_get_AuthorizedApplications( profile, &apps );
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER,
                           &IID_INetFwAuthorizedApplication, (void **)&app );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image );
    if (hr != S_OK) goto done;

2503
    name = SysAllocString( L"rpcrt4_test" );
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
    hr = INetFwAuthorizedApplication_put_Name( app, name );
    SysFreeString( name );
    ok( hr == S_OK, "got %08x\n", hr );
    if (hr != S_OK) goto done;

    if (op == APP_ADD)
        hr = INetFwAuthorizedApplications_Add( apps, app );
    else if (op == APP_REMOVE)
        hr = INetFwAuthorizedApplications_Remove( apps, image );
    else
        hr = E_INVALIDARG;

done:
    if (app) INetFwAuthorizedApplication_Release( app );
    if (apps) INetFwAuthorizedApplications_Release( apps );
    if (policy) INetFwPolicy_Release( policy );
    if (profile) INetFwProfile_Release( profile );
    if (mgr) INetFwMgr_Release( mgr );
    if (SUCCEEDED( init )) CoUninitialize();
    SysFreeString( image );
    return hr;
}

2527 2528
START_TEST(server)
{
2529
  ULONG size = 0;
2530 2531
  int argc;
  char **argv;
2532
  BOOL firewall_enabled = is_firewall_enabled(), firewall_disabled = FALSE;
2533

2534
  InitFunctionPointers();
2535
  set_mixed_interface();
2536

2537 2538 2539
  ok(!GetUserNameExA(NameSamCompatible, NULL, &size), "GetUserNameExA\n");
  domain_and_user = HeapAlloc(GetProcessHeap(), 0, size);
  ok(GetUserNameExA(NameSamCompatible, domain_and_user, &size), "GetUserNameExA\n");
2540

2541 2542 2543 2544
  argc = winetest_get_mainargs(&argv);
  progname = argv[0];

  if (argc == 3)
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
  {
    RpcTryExcept
    {
      client(argv[2]);
    }
    RpcExcept(TRUE)
    {
      trace("Exception %d\n", RpcExceptionCode());
    }
    RpcEndExcept
  }
2556 2557
  else if (argc == 4)
  {
2558 2559 2560 2561 2562 2563
    if (!strcmp(argv[3], "listen"))
    {
      test_server_listening();
    }
    else if(!strcmp(argv[2], "run"))
    {
2564 2565 2566
      ULONG event;
      sscanf(argv[3], "%x", &event);
      run_server(ULongToHandle(event));
2567
    }
2568
  }
2569
  else
2570 2571 2572
  {
    if (firewall_enabled)
    {
2573
      if (is_process_elevated())
2574
      {
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588
        HRESULT hr = set_firewall(APP_ADD);
        if (hr == S_OK)
        {
          firewall_enabled = FALSE;
          firewall_disabled = TRUE;
        }
        else
        {
          skip("can't authorize app in firewall %08x\n", hr);
        }
      }
      else
      {
          trace("no privileges, skipping tests to avoid firewall dialog\n");
2589 2590
      }
    }
2591 2592 2593 2594 2595 2596 2597 2598 2599

    if (!firewall_enabled) server();

    /* Those tests cause occasional crashes on winxp and win2k3 */
    if (GetProcAddress(GetModuleHandleA("rpcrt4.dll"), "RpcExceptionFilter"))
        test_reconnect();
    else
        win_skip("Skipping reconnect tests on too old Windows version\n");

2600
    run_client("test listen");
2601
    if (firewall_disabled) set_firewall(APP_REMOVE);
2602
  }
2603 2604

  HeapFree(GetProcessHeap(), 0, domain_and_user);
2605
}