torture_su_root.c 17.6 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@internal
26
 * @file torture_su_root.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35 36 37 38
 *
 * Test su_root_register functionality.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * Copyright (c) 2002 Nokia Research Center.  All rights reserved.
 *
 * @date Created: Wed Jun 12 15:18:11 2002 ppessi
 */

#include "config.h"

39
char const *name = "torture_su_root";
Pekka Pessi's avatar
Pekka Pessi committed
40 41 42

#include <stdio.h>
#include <string.h>
43
#include <stdlib.h>
44
#include <assert.h>
Pekka Pessi's avatar
Pekka Pessi committed
45 46

#define TSTFLAGS rt->rt_flags
47
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50 51 52 53

typedef struct root_test_s root_test_t;
typedef struct test_ep_s   test_ep_t;

#define SU_ROOT_MAGIC_T  root_test_t
#define SU_WAKEUP_ARG_T  test_ep_t
54
#define SU_MSG_ARG_T     root_test_t *
Pekka Pessi's avatar
Pekka Pessi committed
55

56 57
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_alloc.h>
58
#include <sofia-sip/su_log.h>
Pekka Pessi's avatar
Pekka Pessi committed
59

60 61 62 63
#if SU_HAVE_PTHREADS
#include <pthread.h>
#endif

64 65
struct test_ep_s {
  test_ep_t     *next, **prev, **list;
Pekka Pessi's avatar
Pekka Pessi committed
66 67 68 69 70 71
  int           i;
  int           s;
  su_wait_t     wait[1];
  int           registered;
  socklen_t     addrlen;
  su_sockaddr_t addr[1];
72
};
Pekka Pessi's avatar
Pekka Pessi committed
73

74 75
typedef struct test_ep_s   test_ep_at[1];

Pekka Pessi's avatar
Pekka Pessi committed
76 77 78
struct root_test_s {
  su_home_t  rt_home[1];
  int        rt_flags;
79

Pekka Pessi's avatar
Pekka Pessi committed
80 81 82 83 84 85
  su_root_t *rt_root;
  short      rt_family;
  int        rt_status;
  int        rt_received;
  int        rt_wakeup;

86
  su_clone_r rt_clone;
Pekka Pessi's avatar
Pekka Pessi committed
87

88 89 90
  unsigned   rt_msg_received;
  unsigned   rt_msg_destroyed;

91 92 93 94
  unsigned   rt_fail_init:1;
  unsigned   rt_fail_deinit:1;
  unsigned   rt_success_init:1;
  unsigned   rt_success_deinit:1;
Pekka Pessi's avatar
Pekka Pessi committed
95

96 97 98 99
  unsigned   rt_sent_reporter:1;
  unsigned   rt_recv_reporter:1;
  unsigned   rt_reported_reporter:1;

100 101
  unsigned   rt_executed:1;

102 103
  unsigned :0;

104
  test_ep_at rt_ep[5];
105 106

  int rt_sockets, rt_woken;
107 108 109 110 111 112 113 114 115 116 117

#if SU_HAVE_PTHREADS
  struct {
    /* Used to test su_root_obtain()/su_root_release() */
    pthread_mutex_t mutex[1];
    pthread_cond_t cond[1];
    pthread_t slave;
    int done;
    pthread_mutex_t deinit[1];
  } rt_sr;
#endif
118
};
Pekka Pessi's avatar
Pekka Pessi committed
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
int test_api(root_test_t *rt)
{
  BEGIN();
  TEST_1(rt->rt_root = su_root_create(NULL));
  
  TEST_VOID(su_root_destroy(NULL));
  TEST_P(su_root_name(NULL), NULL);  
  TEST_1(su_root_name(rt->rt_root) != NULL);  
  TEST(su_root_set_magic(NULL, rt), -1);
  TEST_P(su_root_magic(rt->rt_root), NULL);
  TEST(su_root_set_magic(rt->rt_root, rt), 0);
  TEST_P(su_root_magic(rt->rt_root), rt);
  TEST_P(su_root_magic(NULL), NULL);
  TEST(su_root_register(NULL, NULL, NULL, NULL, 0), -1);  
  TEST(su_root_unregister(NULL, NULL, NULL, NULL), -1);  
  TEST(su_root_deregister(NULL, 0), -1);  
  TEST(su_root_deregister(rt->rt_root, 0), -1);  
  TEST(su_root_deregister(rt->rt_root, -1), -1);  
  TEST(su_root_eventmask(NULL, 0, -1, -1), -1);  
  TEST((long int)su_root_step(NULL, 0), -1L);  
  TEST((long int)su_root_sleep(NULL, 0), -1L);  
  TEST(su_root_multishot(NULL, 0), -1);  
  TEST_VOID((void)su_root_run(NULL));  
  TEST_VOID((void)su_root_break(NULL));  
  TEST_M(su_root_task(NULL), su_task_null, sizeof su_task_null);
  TEST_M(su_root_parent(NULL), su_task_null, sizeof su_task_null);
  TEST(su_root_add_prepoll(NULL, NULL, NULL), -1);  
  TEST(su_root_remove_prepoll(NULL), -1);  
  TEST_P(su_root_gsource(NULL), NULL);  
  TEST_VOID((void)su_root_gsource(rt->rt_root));  
  TEST(su_root_yield(NULL), -1);  
  TEST(su_root_release(NULL), -1);
  TEST(su_root_obtain(NULL), -1);
  TEST(su_root_has_thread(NULL), -1);
  TEST(su_root_has_thread(rt->rt_root), 2);
  TEST(su_root_release(rt->rt_root), 0);
  TEST(su_root_has_thread(rt->rt_root), 0);
  TEST(su_root_obtain(rt->rt_root), 0);
  TEST(su_root_has_thread(rt->rt_root), 2);
  TEST_VOID((void)su_root_destroy(rt->rt_root));
  rt->rt_root = NULL;
  END();
}


#if SU_HAVE_PTHREADS

#include <pthread.h>

void *suspend_resume_test_thread(void *_rt) 
{
  root_test_t *rt = _rt;

173 174
  su_init();

175 176 177 178 179 180 181 182 183 184 185 186
  pthread_mutex_lock(rt->rt_sr.mutex);
  rt->rt_root = su_root_create(rt);
  rt->rt_sr.done = 1;
  pthread_cond_signal(rt->rt_sr.cond);
  pthread_mutex_unlock(rt->rt_sr.mutex);
  su_root_release(rt->rt_root);

  pthread_mutex_lock(rt->rt_sr.deinit);
  su_root_obtain(rt->rt_root);
  su_root_destroy(rt->rt_root);
  rt->rt_root = NULL;
  pthread_mutex_unlock(rt->rt_sr.deinit);
187 188 189

  su_deinit();

190 191 192 193
  return NULL;
}
#endif

Pekka Pessi's avatar
Pekka Pessi committed
194
/** Test root initialization */
195 196 197 198
int init_test(root_test_t *rt,
	      char const *preference,
	      su_port_create_f *create,
	      su_clone_start_f *start)
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204
{
  su_sockaddr_t su[1] = {{ 0 }};
  int i;

  BEGIN();

205 206
  su_port_prefer(create, start);

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#if SU_HAVE_PTHREADS
  pthread_mutex_init(rt->rt_sr.mutex, NULL);
  pthread_cond_init(rt->rt_sr.cond, NULL);
  pthread_mutex_init(rt->rt_sr.deinit, NULL);

  pthread_mutex_lock(rt->rt_sr.deinit);

  pthread_create(&rt->rt_sr.slave, NULL, suspend_resume_test_thread, rt);

  pthread_mutex_lock(rt->rt_sr.mutex);
  while (rt->rt_sr.done == 0)
    pthread_cond_wait(rt->rt_sr.cond, rt->rt_sr.mutex);
  pthread_mutex_unlock(rt->rt_sr.mutex);

  TEST_1(rt->rt_root);
  TEST(su_root_obtain(rt->rt_root), 0);
  TEST(su_root_has_thread(rt->rt_root), 2);
#else  
225
  TEST_1(rt->rt_root = su_root_create(rt));
226
#endif
227 228 229 230

  printf("%s: testing %s (%s) implementation\n",
	 name, preference, su_root_name(rt->rt_root));

Pekka Pessi's avatar
Pekka Pessi committed
231 232 233 234 235 236
  su->su_family = rt->rt_family;

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
    ep->i = i;
    ep->addrlen = su_sockaddr_size(su);
237
    TEST_1((ep->s = su_socket(su->su_family, SOCK_DGRAM, 0)) != -1);
Pekka Pessi's avatar
Pekka Pessi committed
238 239 240
    TEST_1(bind(ep->s, &su->su_sa, ep->addrlen) != -1);
    TEST_1(su_wait_create(ep->wait, ep->s, SU_WAIT_IN|SU_WAIT_ERR) != -1);
    TEST_1(getsockname(ep->s, &ep->addr->su_sa, &ep->addrlen) != -1);
241
    if (SU_HAS_INADDR_ANY(ep->addr)) {
242 243 244
      su_inet_pton(su->su_family,
		   su->su_family == AF_INET ? "127.0.0.1" : "::1",
		   SU_ADDR(ep->addr));
245
    }
Pekka Pessi's avatar
Pekka Pessi committed
246 247 248 249 250
  }

  END();
}

251 252 253 254
static int deinit_test(root_test_t *rt)
{
  BEGIN();

255 256 257 258 259 260 261 262 263 264
#if SU_HAVE_PTHREADS
  TEST(su_root_has_thread(rt->rt_root), 2);
  TEST(su_root_release(rt->rt_root), 0);
  TEST(su_root_has_thread(rt->rt_root), 0);
  pthread_mutex_unlock(rt->rt_sr.deinit);
  pthread_join(rt->rt_sr.slave, NULL);
  pthread_mutex_destroy(rt->rt_sr.mutex);
  pthread_cond_destroy(rt->rt_sr.cond);
  pthread_mutex_destroy(rt->rt_sr.deinit);
#else
265
  TEST_VOID(su_root_destroy(rt->rt_root)); rt->rt_root = NULL;
266
#endif  
267 268 269 270

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
271 272 273 274 275
int wakeup(root_test_t *rt,
	   su_wait_t *w,
	   test_ep_t *ep)
{
  char buffer[64];
276 277 278
  int n, error;
 
  su_wait_events(w, ep->s);
Pekka Pessi's avatar
Pekka Pessi committed
279

280 281 282 283 284 285 286
  n = recv(ep->s, buffer, sizeof(buffer), 0);
  error = su_errno();
 
  if (n < 0)
    fprintf(stderr, "%s: %s\n", "recv", su_strerror(error));

  TEST_1(n > 0);
Pekka Pessi's avatar
Pekka Pessi committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

  rt->rt_received = ep->i;
	   
  return 0;
}

static int wakeup0(root_test_t *rt, su_wait_t *w, test_ep_t *ep)
{
  rt->rt_wakeup = 0;
  return wakeup(rt, w, ep);
}
static int wakeup1(root_test_t *rt, su_wait_t *w, test_ep_t *ep)
{
  rt->rt_wakeup = 1;
  return wakeup(rt, w, ep);
}
static int wakeup2(root_test_t *rt, su_wait_t *w, test_ep_t *ep)
{
  rt->rt_wakeup = 2;
  return wakeup(rt, w, ep);
}
static int wakeup3(root_test_t *rt, su_wait_t *w, test_ep_t *ep)
{
  rt->rt_wakeup = 3;
  return wakeup(rt, w, ep);
}
static int wakeup4(root_test_t *rt, su_wait_t *w, test_ep_t *ep)
{
  rt->rt_wakeup = 4;
  return wakeup(rt, w, ep);
}

static
su_wakeup_f wakeups[5] = { wakeup0, wakeup1, wakeup2, wakeup3, wakeup4 };

static
void test_run(root_test_t *rt)
{
  rt->rt_received = -1;

  while (rt->rt_received == -1) {
    su_root_step(rt->rt_root, 200);
  }
}

static int register_test(root_test_t *rt)
{
  int i;
  int s;
336
  char *msg = "foo";
Pekka Pessi's avatar
Pekka Pessi committed
337 338 339

  BEGIN();

340
  TEST_1((s = su_socket(rt->rt_family, SOCK_DGRAM, 0)) != -1);
Pekka Pessi's avatar
Pekka Pessi committed
341 342 343 344 345

  for (i = 0; i < 5; i++) {
    rt->rt_ep[i]->registered = 
      su_root_register(rt->rt_root, rt->rt_ep[i]->wait, 
		       wakeups[i], rt->rt_ep[i], 0);
346
    TEST(rt->rt_ep[i]->registered, i + 1 + SU_HAVE_PTHREADS);
Pekka Pessi's avatar
Pekka Pessi committed
347 348 349 350
  }

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
351
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
352
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
353 354 355 356 357 358 359
    test_run(rt);
    TEST(rt->rt_received, i);
    TEST(rt->rt_wakeup, i);
  }

  for (i = 0; i < 5; i++) {
    TEST(su_root_unregister(rt->rt_root, rt->rt_ep[i]->wait, 
360 361
			    wakeups[i], rt->rt_ep[i]), 
	 rt->rt_ep[i]->registered);
Pekka Pessi's avatar
Pekka Pessi committed
362 363 364 365 366 367 368 369 370 371 372 373
  }


  for (i = 0; i < 5; i++) {
    rt->rt_ep[i]->registered = 
      su_root_register(rt->rt_root, rt->rt_ep[i]->wait, 
		       wakeups[i], rt->rt_ep[i], 1);
    TEST_1(rt->rt_ep[i]->registered > 0);
  }

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
374
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
375
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
376 377 378 379 380 381 382 383 384 385 386
    test_run(rt);
    TEST(rt->rt_received, i);
    TEST(rt->rt_wakeup, i);
  }

  for (i = 0; i < 5; i++) {
    TEST(su_root_deregister(rt->rt_root, rt->rt_ep[i]->registered), 
	 rt->rt_ep[i]->registered);
  }

  for (i = 0; i < 5; i++) {
387 388 389 390 391 392
    test_ep_t *ep = rt->rt_ep[i];
    TEST_1(su_wait_create(ep->wait, ep->s, SU_WAIT_IN|SU_WAIT_ERR) != -1);
    ep->registered = 
      su_root_register(rt->rt_root, ep->wait, 
		       wakeups[i], ep, 1);
    TEST_1(ep->registered > 0);
Pekka Pessi's avatar
Pekka Pessi committed
393 394 395 396
  }

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
397
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
398
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412
    test_run(rt);
    TEST(rt->rt_received, i);
    TEST(rt->rt_wakeup, i);
  }

  for (i = 0; i < 5; i++) {
    TEST(su_root_unregister(rt->rt_root, rt->rt_ep[i]->wait, 
			    wakeups[i], rt->rt_ep[i]), 
	 rt->rt_ep[i]->registered);
  }

  END();
}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

int wakeup_remove(root_test_t *rt, su_wait_t *w, test_ep_t *node)
{
  char buffer[64];
  ssize_t x;
  test_ep_t *n = node->next; 

  su_wait_events(w, node->s);

  x = recv(node->s, buffer, sizeof(buffer), 0);

  if (x < 0)
    fprintf(stderr, "%s: %s\n", "recv", su_strerror(su_errno()));

  if (node->prev) {		/* first run */
    *node->prev = n;

    if (n) {
      *node->prev = node->next;    
      node->next->prev = node->prev;
      sendto(n->s, "foo", 3, 0, (void *)n->addr, n->addrlen);
    }

    node->next = NULL;
    node->prev = NULL;
    
    if (!*node->list) {
      su_root_break(rt->rt_root);
    }
  }
  else {			/* second run */
    if (++rt->rt_woken == rt->rt_sockets)
      su_root_break(rt->rt_root);
  }

  return 0;
}


int event_test(root_test_t rt[1])
{
  BEGIN();
  int i = 0, N = 2048;
  test_ep_t *n, *nodes, *list = NULL;
  su_sockaddr_t su[1];
  socklen_t sulen;
  
  TEST_1(nodes = calloc(N, sizeof *nodes));
  
462
  memset(su, 0, sulen = sizeof su->su_sin);
463 464 465 466 467 468 469 470 471 472 473 474 475
  su->su_len = sizeof su->su_sin;
  su->su_family = AF_INET;
  su->su_sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */

  for (i = 0; i < N; i++) {
    n = nodes + i;
    n->s = su_socket(AF_INET, SOCK_DGRAM, 0);

    if (n->s == INVALID_SOCKET)
      break;

    n->addrlen = sizeof n->addr;

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    n->addr->su_len = sizeof n->addr;

    if (bind(n->s, (void *)su, sulen) < 0) {
      su_perror("bind()");
      su_close(n->s);
      break;
    }
      
    if (getsockname(n->s, (void *)n->addr, &n->addrlen)) {
      su_perror("getsockname()");
      su_close(n->s);
      break;
    }

    if (su_wait_create(n->wait, n->s, SU_WAIT_IN)) {
      su_perror("su_wait_create()");
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      su_close(n->s);
      break;
    }

    n->registered = su_root_register(rt->rt_root, n->wait, wakeup_remove, n, 0);
    if (n->registered < 0) {
      su_wait_destroy(n->wait);
      su_close(n->s);
      break;
    }
    
    n->list = &list, n->prev = &list;
    if ((n->next = list))
      n->next->prev = &n->next;
    list = n;
  }

  TEST_1(i >= 1);

  N = i;

  /* Wake up socket at a time */
  n = list; sendto(n->s, "foo", 3, 0, (void *)n->addr, n->addrlen);

  su_root_run(rt->rt_root);

  for (i = 0; i < N; i++) {
    n = nodes + i;
    TEST_1(n->prev == NULL);
    sendto(n->s, "bar", 3, 0, (void *)n->addr, n->addrlen);
  }

  rt->rt_sockets = N;

  /* Wake up all sockets */
  su_root_run(rt->rt_root);

  for (i = 0; i < N; i++) {
    n = nodes + i;
    su_root_deregister(rt->rt_root, n->registered);
    TEST_1(su_close(n->s) == 0);
  }

535 536
  free(nodes);

537 538 539
  END();
}

540
int fail_init(su_root_t *root, root_test_t *rt)
Pekka Pessi's avatar
Pekka Pessi committed
541
{
542 543 544
  rt->rt_fail_init = 1;
  return -1;
}
Pekka Pessi's avatar
Pekka Pessi committed
545

546 547 548 549
void fail_deinit(su_root_t *root, root_test_t *rt)
{
  rt->rt_fail_deinit = 1;
}
Pekka Pessi's avatar
Pekka Pessi committed
550

551 552 553 554 555 556 557 558 559 560 561
int success_init(su_root_t *root, root_test_t *rt)
{
  rt->rt_success_init = 1;
  return 0;
}

void success_deinit(su_root_t *root, root_test_t *rt)
{
  rt->rt_success_deinit = 1;
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
void receive_a_reporter(root_test_t *rt, 
			su_msg_r msg,
			su_msg_arg_t *arg)
{
  rt->rt_recv_reporter = 1;
}

void receive_recv_report(root_test_t *rt, 
			 su_msg_r msg,
			 su_msg_arg_t *arg)
{
  rt->rt_reported_reporter = 1;
}

void send_a_reporter_msg(root_test_t *rt, 
			 su_msg_r msg,
			 su_msg_arg_t *arg)
{
  su_msg_r m = SU_MSG_R_INIT;

  if (su_msg_create(m,
		    su_msg_from(msg),
		    su_msg_to(msg),
		    receive_a_reporter,
		    0) == 0 &&
      su_msg_report(m, receive_recv_report) == 0 &&
      su_msg_send(m) == 0)
    rt->rt_sent_reporter = 1;
}

592 593 594 595 596 597 598
static int set_execute_bit_and_return_3(void *void_rt)
{
  root_test_t *rt = void_rt;
  rt->rt_executed = 1;
  return 3;
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
static void deinit_simple_msg(su_msg_arg_t *arg)
{
  root_test_t *rt = *arg;
  rt->rt_msg_destroyed++;
}

static void receive_simple_msg(root_test_t *rt, 
			       su_msg_r msg,
			       su_msg_arg_t *arg)
{
  assert(rt == *arg);
  rt->rt_msg_received = 
    su_task_cmp(su_msg_from(msg), su_task_null) == 0 &&
    su_task_cmp(su_msg_to(msg), su_task_null) == 0;
}

615 616 617
static int clone_test(root_test_t rt[1])
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
618

619
  su_msg_r m = SU_MSG_R_INIT;
620
  int retval;
621

622 623 624 625
  rt->rt_fail_init = 0;
  rt->rt_fail_deinit = 0;
  rt->rt_success_init = 0;
  rt->rt_success_deinit = 0;
626 627 628
  rt->rt_sent_reporter = 0;
  rt->rt_recv_reporter = 0;
  rt->rt_reported_reporter = 0;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  TEST(su_clone_start(rt->rt_root,
		      rt->rt_clone,
		      rt,
		      fail_init,
		      fail_deinit), SU_FAILURE);
  TEST_1(rt->rt_fail_init);
  TEST_1(rt->rt_fail_deinit);

  TEST(su_clone_start(rt->rt_root,
		      rt->rt_clone,
		      rt,
		      success_init,
		      success_deinit), SU_SUCCESS);
  TEST_1(rt->rt_success_init);
  TEST_1(!rt->rt_success_deinit);

646 647 648 649 650 651 652
  retval = -1;
  rt->rt_executed = 0;
  TEST(su_task_execute(su_clone_task(rt->rt_clone),
		       set_execute_bit_and_return_3, rt,
		       &retval), 0);
  TEST(retval, 3);
  TEST_1(rt->rt_executed);
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

  /* Deliver message with su_msg_send() */
  TEST(su_msg_new(m, sizeof &rt), 0);
  *su_msg_data(m) = rt;

  rt->rt_msg_received = 0;
  rt->rt_msg_destroyed = 0;
  
  TEST(su_msg_deinitializer(m, deinit_simple_msg), 0);
  TEST(su_msg_send_to(m, su_clone_task(rt->rt_clone), receive_simple_msg), 0);
  
  while (rt->rt_msg_destroyed == 0)
    su_root_step(rt->rt_root, 1);

  TEST(rt->rt_msg_received, 1);
668

669 670 671 672 673 674 675 676
  /* Make sure 3-way handshake is done as expected */
  TEST(su_msg_create(m,
		     su_clone_task(rt->rt_clone),
		     su_root_task(rt->rt_root),
		     send_a_reporter_msg,
		     0), 0);
  TEST(su_msg_send(m), 0);

677 678 679
  TEST_VOID(su_clone_wait(rt->rt_root, rt->rt_clone));

  TEST_1(rt->rt_success_deinit);
680 681 682
  TEST_1(rt->rt_sent_reporter);
  TEST_1(rt->rt_recv_reporter);
  TEST_1(rt->rt_reported_reporter);
683
  
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  rt->rt_recv_reporter = 0;

  /* Make sure we can handle messages done as expected */
  TEST(su_msg_create(m,
		     su_root_task(rt->rt_root),
		     su_task_null,
		     receive_a_reporter,
		     0), 0);
  TEST(su_msg_send(m), 0);
  su_root_step(rt->rt_root, 0);
  TEST_1(rt->rt_recv_reporter);

  rt->rt_success_init = 0;
  rt->rt_success_deinit = 0;

Pekka Pessi's avatar
Pekka Pessi committed
699 700
  END();
}
701

702
void usage(int exitcode)
703 704
{
  fprintf(stderr, 
705
	  "usage: %s [-v] [-a]\n", 
706
	  name);
707
  exit(exitcode);
708 709 710 711
}

int main(int argc, char *argv[])
{
712
  root_test_t *rt, rt0[1] = {{{ SU_HOME_INIT(rt0) }}}, rt1[1];
713 714 715
  int retval = 0;
  int i;

716 717 718
  struct {
    su_port_create_f *create;
    su_clone_start_f *start;
719
    char const *name;
720 721 722 723 724 725
  } prefer[] =
      {
	{ NULL, NULL, "default" },
#if HAVE_EPOLL
	{ su_epoll_port_create, su_epoll_clone_start, "epoll", },
#endif
726 727 728 729 730 731 732
#if HAVE_KQUEUE
	{ su_kqueue_port_create, su_kqueue_clone_start, "kqueue", },
#endif
#if HAVE_SYS_DEVPOLL_H
	{ su_devpoll_port_create, su_devpoll_clone_start, "devpoll", },
#endif
#if HAVE_POLL_PORT
733 734 735 736 737 738 739 740 741
	{ su_poll_port_create, su_poll_clone_start, "poll" },
#endif
#if HAVE_SELECT
	{ su_select_port_create, su_select_clone_start, "select" },
#endif
	{ NULL, NULL }
      };

  rt = rt0;
742 743 744 745 746
  rt->rt_family = AF_INET;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      rt->rt_flags |= tst_verbatim;
747 748
    else if (strcmp(argv[i], "-a") == 0)
      rt->rt_flags |= tst_abort;
749
#if SU_HAVE_IN6
750 751
    else if (strcmp(argv[i], "-6") == 0)
      rt->rt_family = AF_INET6;
752
#endif
753
    else
754
      usage(1);
755 756
  }

757 758 759 760
#if HAVE_OPEN_C
  rt->rt_flags |= tst_verbatim;
#endif

761 762
  i = 0;

763 764
  su_init();

765 766
  do {
    rt = rt1, *rt = *rt0;
767

768 769
    retval |= test_api(rt);

770
    retval |= init_test(rt, prefer[i].name, prefer[i].create, prefer[i].start);
771 772 773 774 775 776 777 778
    retval |= register_test(rt);
    retval |= event_test(rt);
    su_root_threading(rt->rt_root, 1);
    retval |= clone_test(rt);
    su_root_threading(rt->rt_root, 0);
    retval |= clone_test(rt);
    retval |= deinit_test(rt);
  } while (prefer[++i].create);
779

780 781
  su_deinit();

782 783
  return retval;
}