torture_su_port.c 4.43 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 27 28 29 30 31 32 33 34 35 36
 * 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
 *
 */

/**
 * @file torture_su_port.c
 * @brief Test su_port interface
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @date Created: Wed Mar 10 17:05:23 2004 ppessi
 */

#include "config.h"

#include "su_port.c"

37 38 39 40 41 42 43
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
static char const __func__[] = "torture_su_port";
#endif

Pekka Pessi's avatar
Pekka Pessi committed
44 45 46 47 48
int tstflags;

#define TSTFLAGS tstflags

#include <stdio.h>
49
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55 56 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 97 98 99 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 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

char const *name = "torture_su_port";

static int callback(su_root_magic_t *magic, 
		    su_wait_t *w,
		    su_wakeup_arg_t *arg)
{
  return 0;
}

int test_sup_indices(su_port_t const *port)
{
  int i, n = 0;

  for (i = 0; i < port->sup_size_waits; i++) {
    if (port->sup_indices[i] >= 0) {
      if (port->sup_reverses[port->sup_indices[i]] != i)
	return 0;
      n++;
    }
  }

  for (i = port->sup_free_index; i != -1; i = port->sup_indices[i]) {
    if (i >= 0)
      return 0;

    n++;
    i = -2 - i;

    if (i >= port->sup_size_waits)
      return 0;
  }

  return n == port->sup_size_waits;
}

int test_register(void)
{
  su_port_t *port;
  su_sockaddr_t su[1];
  int i;
  int sockets[32] = { 0 };
  int reg[32] = { 0 };
  su_wait_t wait[32];

  BEGIN();

  memset(su, 0, sizeof su);
  su->su_len = sizeof su->su_sin;
  su->su_family = AF_INET;

  memset(wait, 0, sizeof wait);

  su_root_size_hint = 16;

  TEST_1(port = su_port_create());
  TEST(su_port_threadsafe(port), 0);
  SU_PORT_INCREF(port, __func__);

  TEST_1(test_sup_indices(port));

  for (i = 1; i < 16 + !SU_HAVE_MBOX; i++) {
    sockets[i] = su_socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sockets[i] != -1);

    TEST(bind(sockets[i], &su->su_sa, sizeof su->su_sin), 0);
    
    TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);

    reg[i] = su_port_register(port, NULL, wait + i, callback, port, 0);

    TEST_1(reg[i] > 0);
  }

  TEST(port->sup_free_index, -1);
  TEST_1(test_sup_indices(port));

  for (i = 1; i < 16; i += 2) {
    TEST(su_port_deregister(port, reg[i]), reg[i]);
  }

  TEST_1(test_sup_indices(port));

  for (i = 15; i > 0; i -= 2) {
    TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);
    reg[i] = su_port_register(port, NULL, wait + i, callback, port, 1);
    TEST_1(reg[i] > 0);
    TEST_M(wait + i, port->sup_waits, sizeof wait[0]);
  }

  TEST(port->sup_free_index, -1);

  TEST_M(wait + 15, port->sup_waits + 7, sizeof wait[0]);
  TEST_M(wait + 13, port->sup_waits + 6, sizeof wait[0]);
  TEST_M(wait + 11, port->sup_waits + 5, sizeof wait[0]);
  TEST_M(wait + 9, port->sup_waits + 4, sizeof wait[0]);
  TEST_M(wait + 7, port->sup_waits + 3, sizeof wait[0]);
  TEST_M(wait + 5, port->sup_waits + 2, sizeof wait[0]);
  TEST_M(wait + 3, port->sup_waits + 1, sizeof wait[0]);
  TEST_M(wait + 1, port->sup_waits + 0, sizeof wait[0]);

  TEST_1(test_sup_indices(port));

  for (i = 1; i <= 8; i++) {
    TEST(su_port_deregister(port, reg[i]), reg[i]);
  }

  TEST(port->sup_pri_offset, 4);

  TEST_1(test_sup_indices(port));

  TEST(su_port_deregister(port, 0), -1);
  TEST(su_port_deregister(port, -1), -1);
  TEST(su_port_deregister(port, 20), -1);

  TEST_1(test_sup_indices(port));

  for (i = 1; i <= 8; i++) {
    TEST(su_port_deregister(port, reg[i]), -1);
  }

  TEST_VOID(su_port_decref(port, 1, __func__));

  END();
}

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

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
    else
      usage();
  }

194 195
  su_init();

Pekka Pessi's avatar
Pekka Pessi committed
196 197
  retval |= test_register(); fflush(stdout);

198 199
  su_deinit();

Pekka Pessi's avatar
Pekka Pessi committed
200 201
  return retval;
}