torture_su_root.c 12.7 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 25 26
 * 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
 *
 */

/**@ingroup su_root_ex
 * 
27
 * @file torture_su_root.c
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34 35 36 37 38 39
 *
 * 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"

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

#include <stdio.h>
#include <string.h>
44
#include <stdlib.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 54

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

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

59 60
struct test_ep_s {
  test_ep_t     *next, **prev, **list;
Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64 65 66
  int           i;
  int           s;
  su_wait_t     wait[1];
  int           registered;
  socklen_t     addrlen;
  su_sockaddr_t addr[1];
67
};
Pekka Pessi's avatar
Pekka Pessi committed
68

69 70
typedef struct test_ep_s   test_ep_at[1];

Pekka Pessi's avatar
Pekka Pessi committed
71 72 73
struct root_test_s {
  su_home_t  rt_home[1];
  int        rt_flags;
74

Pekka Pessi's avatar
Pekka Pessi committed
75 76 77 78 79 80
  su_root_t *rt_root;
  short      rt_family;
  int        rt_status;
  int        rt_received;
  int        rt_wakeup;

81
  su_clone_r rt_clone;
Pekka Pessi's avatar
Pekka Pessi committed
82

83 84 85 86
  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
87

88 89 90 91 92 93
  unsigned   rt_sent_reporter:1;
  unsigned   rt_recv_reporter:1;
  unsigned   rt_reported_reporter:1;

  unsigned :0;

94
  test_ep_at rt_ep[5];
95 96

  int rt_sockets, rt_woken;
97
};
Pekka Pessi's avatar
Pekka Pessi committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

/** Test root initialization */
int init_test(root_test_t *rt)
{
  su_sockaddr_t su[1] = {{ 0 }};
  int i;

  BEGIN();

  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);
Pekka Pessi's avatar
Pekka Pessi committed
113
    TEST_1((ep->s = su_socket(su->su_family, SOCK_DGRAM, 0)) != -1);
Pekka Pessi's avatar
Pekka Pessi committed
114 115 116
    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);
117 118 119
    if (SU_HAS_INADDR_ANY(ep->addr)) {
      inet_pton(su->su_family, 
	        su->su_family == AF_INET ? "127.0.0.1" : "::1",
120
		SU_ADDR(ep->addr));
121
    }
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125 126
  }

  END();
}

127 128 129 130 131 132 133 134 135 136 137 138
static int deinit_test(root_test_t *rt)
{
  BEGIN();

  TEST_VOID(su_root_destroy(rt->rt_root)); rt->rt_root = NULL;
  TEST_VOID(su_root_destroy(NULL));

  su_deinit();

  END();
}

Pekka Pessi's avatar
Pekka Pessi committed
139 140 141 142 143
int wakeup(root_test_t *rt,
	   su_wait_t *w,
	   test_ep_t *ep)
{
  char buffer[64];
144 145 146
  int n, error;
 
  su_wait_events(w, ep->s);
Pekka Pessi's avatar
Pekka Pessi committed
147

148 149 150 151 152 153 154
  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
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 191 192 193 194 195 196 197 198 199 200 201 202 203

  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;
204
  char *msg = "foo";
Pekka Pessi's avatar
Pekka Pessi committed
205 206 207

  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
208
  TEST_1((s = su_socket(rt->rt_family, SOCK_DGRAM, 0)) != -1);
Pekka Pessi's avatar
Pekka Pessi committed
209 210 211 212 213

  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);
214
    TEST(rt->rt_ep[i]->registered, i + 1 + SU_HAVE_PTHREADS);
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218
  }

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
219
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
220
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
221 222 223 224 225 226 227
    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, 
228 229
			    wakeups[i], rt->rt_ep[i]), 
	 rt->rt_ep[i]->registered);
Pekka Pessi's avatar
Pekka Pessi committed
230 231 232 233 234 235 236 237 238 239 240 241
  }


  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];
242
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
243
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
244 245 246 247 248 249 250 251 252 253 254
    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++) {
255 256 257 258 259 260
    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
261 262 263 264
  }

  for (i = 0; i < 5; i++) {
    test_ep_t *ep = rt->rt_ep[i];
265
    TEST_SIZE(su_sendto(s, msg, sizeof(msg), 0, ep->addr, ep->addrlen), 
266
	      sizeof(msg));
Pekka Pessi's avatar
Pekka Pessi committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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();
}

281 282 283 284 285 286 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

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));
  
330
  memset(su, 0, sulen = sizeof su->su_sin);
331 332 333 334 335 336 337 338 339 340 341 342 343
  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;

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    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()");
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
      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);
  }

403 404
  free(nodes);

405 406 407
  END();
}

408
int fail_init(su_root_t *root, root_test_t *rt)
Pekka Pessi's avatar
Pekka Pessi committed
409
{
410 411 412
  rt->rt_fail_init = 1;
  return -1;
}
Pekka Pessi's avatar
Pekka Pessi committed
413

414 415 416 417
void fail_deinit(su_root_t *root, root_test_t *rt)
{
  rt->rt_fail_deinit = 1;
}
Pekka Pessi's avatar
Pekka Pessi committed
418

419 420 421 422 423 424 425 426 427 428 429
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;
}

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
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;
}

460 461 462
static int clone_test(root_test_t rt[1])
{
  BEGIN();
Pekka Pessi's avatar
Pekka Pessi committed
463

464 465
  su_msg_r m = SU_MSG_R_INIT;

466 467 468 469
  rt->rt_fail_init = 0;
  rt->rt_fail_deinit = 0;
  rt->rt_success_init = 0;
  rt->rt_success_deinit = 0;
470 471 472
  rt->rt_sent_reporter = 0;
  rt->rt_recv_reporter = 0;
  rt->rt_reported_reporter = 0;
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

  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);

490 491 492 493 494 495 496 497
  /* 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);

498 499 500
  TEST_VOID(su_clone_wait(rt->rt_root, rt->rt_clone));

  TEST_1(rt->rt_success_deinit);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
  TEST_1(rt->rt_sent_reporter);
  TEST_1(rt->rt_recv_reporter);
  TEST_1(rt->rt_reported_reporter);

  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
520 521
  END();
}
522 523 524 525 526 527 528 529 530 531

void usage(void)
{
  fprintf(stderr, 
	  "usage: %s [-v]\n", 
	  name);
}

int main(int argc, char *argv[])
{
532
  root_test_t *rt, rt0[1] = {{{ SU_HOME_INIT(rt0) }}}, rt1[1];
533 534 535
  int retval = 0;
  int i;

536 537 538 539 540 541 542 543 544 545
  struct {
    su_port_create_f *create;
    su_clone_start_f *start;
    char const *preference;
  } prefer[] =
      {
	{ NULL, NULL, "default" },
#if HAVE_EPOLL
	{ su_epoll_port_create, su_epoll_clone_start, "epoll", },
#endif
546 547 548 549 550 551 552
#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
553 554 555 556 557 558 559 560 561
	{ 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;
562 563 564 565 566
  rt->rt_family = AF_INET;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      rt->rt_flags |= tst_verbatim;
567
#if SU_HAVE_IN6
568 569
    else if (strcmp(argv[i], "-6") == 0)
      rt->rt_family = AF_INET6;
570
#endif
571 572 573 574
    else
      usage();
  }

575 576 577 578
  i = 0;

  do {
    rt = rt1, *rt = *rt0;
579

580 581
    su_port_prefer(prefer[i].create, prefer[i].start);

582 583 584 585 586
    TEST_1(rt->rt_root = su_root_create(rt));

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

587 588 589 590 591 592 593 594 595
    retval |= init_test(rt);
    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);
596 597 598

  return retval;
}