torture_su_port.c 8.06 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
Pekka Pessi's avatar
Pekka Pessi committed
26
 * @file torture_su_port.c
27
 * @brief Test su_poll_port interface
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30 31 32 33 34
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @date Created: Wed Mar 10 17:05:23 2004 ppessi
 */

#include "config.h"

35 36 37
#include <stdio.h>
#include <stdlib.h>

38 39 40
struct su_root_magic_s;

#define SU_ROOT_MAGIC_T struct su_root_magic_s
41 42

#include "su_poll_port.c"
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45 46
#undef HAVE_EPOLL
#define HAVE_EPOLL 0

47 48 49 50 51 52 53
#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
54 55 56 57
int tstflags;

#define TSTFLAGS tstflags

58
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
59 60 61

char const *name = "torture_su_port";

62 63 64
#if HAVE_OPEN_C
int const N0 = SU_MBOX_SIZE > 0, N = 63, I = 64;
#else
65
int const N0 = SU_MBOX_SIZE > 0, N = 128, I = 129;
66
#endif
Pekka Pessi's avatar
Pekka Pessi committed
67 68 69

int test_sup_indices(su_port_t const *port)
{
70 71 72 73 74 75 76
  int i, n;
  int *indices = port->sup_indices;
  int *reverses = port->sup_reverses;
  int N = port->sup_size_waits;

  if (indices == NULL)
    return N == 0 && port->sup_n_waits == 0;
Pekka Pessi's avatar
Pekka Pessi committed
77

78 79 80
  for (i = 0, n = 0; i < N; i++) {
    if (reverses[i] > 0) {
      if (indices[reverses[i]] != i)
Pekka Pessi's avatar
Pekka Pessi committed
81 82 83 84 85
	return 0;
      n++;
    }
  }

86 87 88 89
  if (n != port->sup_n_waits)
    return 0;
  
  n = 0;
Pekka Pessi's avatar
Pekka Pessi committed
90

91 92 93 94 95 96 97 98 99 100
  for (i = 1; i <= N; i++) {
    if (indices[i] >= 0) {
      if (reverses[indices[i]] != i)
	return 0;
      n++;
    }
  }

  if (n != port->sup_n_waits)
    return 0;
Pekka Pessi's avatar
Pekka Pessi committed
101

102 103
  for (i = indices[0]; -i <= N; i = indices[-i]) {
    if (i >= 0)
Pekka Pessi's avatar
Pekka Pessi committed
104
      return 0;
105
    n++;
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108 109 110
  }

  return n == port->sup_size_waits;
}

111 112 113 114 115 116 117 118 119
struct su_root_magic_s 
{
  int error, *sockets, *regs, *wakeups;
};

static int callback(su_root_magic_t *magic, 
		    su_wait_t *w,
		    su_wakeup_arg_t *arg)
{
120
  intptr_t i = (intptr_t)arg;
121 122

  assert(magic);
123
 
124
  if (i <= 0 || i > I)
125 126
    return ++magic->error;

127 128
  su_wait_events(w, magic->sockets[i]);

129 130
  magic->wakeups[i]++;

Martti Mela's avatar
Martti Mela committed
131
#if HAVE_POLL || HAVE_SELECT
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  if (w->fd != magic->sockets[i])
    return ++magic->error;
#endif

  return 0;
}

int test_wakeup(su_port_t *port, su_root_magic_t *magic)
{
  int i;

  for (i = N0; i < N; i++) {
    su_sockaddr_t su[1]; socklen_t sulen = sizeof su;
    int n, woken = magic->wakeups[i];
    char buf[1];
    if (magic->regs[i] == 0)
      continue;

    if (getsockname(magic->sockets[i], &su->su_sa, &sulen) < 0)
      su_perror("getsockname"), exit(1);
152
    if (su_sendto(magic->sockets[1], "X", 1, 0, su, sulen) < 0)
153
      su_perror("su_sendto"), exit(1);
154
    n = su_poll_port_wait_events(port, 100);
155

156 157 158 159 160 161 162 163 164 165 166 167 168
    if (n != 1)
      return 1;
    if (magic->error)
      return 2;
    if (magic->wakeups[i] != woken + 1)
      return 3;
    if (recv(magic->sockets[i], buf, 1, 0) != 1)
      return 4;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
169 170 171 172
int test_register(void)
{
  su_port_t *port;
  su_sockaddr_t su[1];
173
  intptr_t i;
174 175 176
  int sockets[256] = { 0 };
  int reg[256] = { 0 };
  int wakeups[256] = { 0 };
177
  int prioritized;
178
  su_wait_t wait[256];
179 180
  su_root_magic_t magic[1] = {{ 0 }};
  su_root_t root[1] = {{ sizeof root }};
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183

  BEGIN();

184 185
  root->sur_magic = magic;

Pekka Pessi's avatar
Pekka Pessi committed
186 187 188
  memset(su, 0, sizeof su);
  su->su_len = sizeof su->su_sin;
  su->su_family = AF_INET;
189
  su->su_sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
Pekka Pessi's avatar
Pekka Pessi committed
190 191 192

  memset(wait, 0, sizeof wait);

193 194 195
  memset(magic->sockets = sockets, -1, sizeof sockets);
  memset(magic->regs = reg, 0, sizeof reg);
  memset(magic->wakeups = wakeups, 0, sizeof wakeups);
196

Pekka Pessi's avatar
Pekka Pessi committed
197 198
  su_root_size_hint = 16;

199
  TEST_1(port = su_poll_port_create());
200
  TEST(su_home_threadsafe(su_port_home(port)), 0);
201 202
  /* Before 1.12.4 su_port_create() had reference count 0 after creation */
  /* su_port_incref(port, "test_register"); */
Pekka Pessi's avatar
Pekka Pessi committed
203 204 205

  TEST_1(test_sup_indices(port));

206
  for (i = N0; i < N; i++) {
Pekka Pessi's avatar
Pekka Pessi committed
207 208
    sockets[i] = su_socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sockets[i] != -1);

209 210
    if (bind(sockets[i], &su->su_sa, sizeof su->su_sin) != 0)
      perror("bind"), assert(0);
Pekka Pessi's avatar
Pekka Pessi committed
211 212 213
    
    TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);

214
    reg[i] = su_port_register(port, root, wait + i, callback, (void*)i, 0);
Pekka Pessi's avatar
Pekka Pessi committed
215 216 217 218

    TEST_1(reg[i] > 0);
  }

219
  TEST(port->sup_indices[0], -I);
Pekka Pessi's avatar
Pekka Pessi committed
220
  TEST_1(test_sup_indices(port));
221
  TEST(test_wakeup(port, magic), 0);
Pekka Pessi's avatar
Pekka Pessi committed
222

223
  for (i = 1; i < N; i += 2) {
Pekka Pessi's avatar
Pekka Pessi committed
224
    TEST(su_port_deregister(port, reg[i]), reg[i]);
225
    TEST_1(test_sup_indices(port));
Pekka Pessi's avatar
Pekka Pessi committed
226 227 228 229
  }

  TEST_1(test_sup_indices(port));

230 231
  prioritized = 0;

232
  for (i = N - 1; i >= N0; i -= 2) {
Pekka Pessi's avatar
Pekka Pessi committed
233
    TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);
234
    reg[i] = su_port_register(port, root, wait + i, callback, (void *)i, 1);
Pekka Pessi's avatar
Pekka Pessi committed
235
    TEST_1(reg[i] > 0);
236
    prioritized++;		/* Count number of prioritized registrations */
237 238 239 240 241 242 243 244
#if HAVE_EPOLL
    /* With epoll we do not bother to prioritize the wait list */
    if (port->sup_epoll != -1) {
      int N = port->sup_n_waits;
      TEST_M(wait + i, port->sup_waits + N - 1, sizeof wait[0]);
    }
    else
#endif
Pekka Pessi's avatar
Pekka Pessi committed
245 246 247
    TEST_M(wait + i, port->sup_waits, sizeof wait[0]);
  }

248 249
  TEST(port->sup_indices[0], -I);

250
#if 0
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
#if HAVE_EPOLL
  /* With epoll we do not bother to prioritize the wait list */
  if (port->sup_epoll != -1) {
    TEST_M(wait + 15, port->sup_waits + 8, sizeof wait[0]);
    TEST_M(wait + 13, port->sup_waits + 9, sizeof wait[0]);
    TEST_M(wait + 11, port->sup_waits + 10, sizeof wait[0]);
    TEST_M(wait + 9, port->sup_waits + 11, sizeof wait[0]);
    TEST_M(wait + 7, port->sup_waits + 12, sizeof wait[0]);
    TEST_M(wait + 5, port->sup_waits + 13, sizeof wait[0]);
    TEST_M(wait + 3, port->sup_waits + 14, sizeof wait[0]);
    TEST_M(wait + 1, port->sup_waits + 15, sizeof wait[0]);
  }
  else
#endif
  {
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269 270 271 272 273
  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]);
274
  }
275
#endif
Pekka Pessi's avatar
Pekka Pessi committed
276 277

  TEST_1(test_sup_indices(port));
278
  TEST(test_wakeup(port, magic), 0);
Pekka Pessi's avatar
Pekka Pessi committed
279 280

  for (i = 1; i <= 8; i++) {
281
    TEST(su_port_deregister(port, reg[i]), reg[i]); reg[i] = 0;
282 283
    if (i % 2 == (N - 1) % 2)
      prioritized--;		/* Count number of prioritized registrations */
Pekka Pessi's avatar
Pekka Pessi committed
284 285
  }

286 287 288 289
#if HAVE_EPOLL
  /* With epoll we do not bother to prioritize the wait list */
  if (port->sup_epoll == -1) 
#endif
290
    TEST(port->sup_pri_offset, prioritized);
Pekka Pessi's avatar
Pekka Pessi committed
291 292 293 294 295

  TEST_1(test_sup_indices(port));

  TEST(su_port_deregister(port, 0), -1);
  TEST(su_port_deregister(port, -1), -1);
296
  TEST(su_port_deregister(port, 130), -1);
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303

  TEST_1(test_sup_indices(port));

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

304
  TEST_VOID(su_port_decref(port, __func__));
Pekka Pessi's avatar
Pekka Pessi committed
305 306 307 308

  END();
}

309
void usage(int exitcode)
Pekka Pessi's avatar
Pekka Pessi committed
310 311
{
  fprintf(stderr,
312
	  "usage: %s [-v] [-a]\n",
Pekka Pessi's avatar
Pekka Pessi committed
313
	  name);
314
  exit(exitcode);
Pekka Pessi's avatar
Pekka Pessi committed
315 316 317 318 319 320 321
}

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

322
#if SU_HAVE_WINSOCK
323 324
  if (N > SU_WAIT_MAX)
    N = SU_WAIT_MAX;
325 326 327
  if (I - 1 >= SU_WAIT_MAX)
    I = (unsigned)SU_WAIT_MAX + 1;
#endif
328

Pekka Pessi's avatar
Pekka Pessi committed
329 330 331
  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
332 333
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
Pekka Pessi's avatar
Pekka Pessi committed
334
    else
335
      usage(1);
Pekka Pessi's avatar
Pekka Pessi committed
336 337
  }

338 339 340 341
#if HAVE_OPEN_C
  tstflags |= tst_verbatim;
#endif

342 343
  su_init();

Pekka Pessi's avatar
Pekka Pessi committed
344 345
  retval |= test_register(); fflush(stdout);

346 347
  su_deinit();

Pekka Pessi's avatar
Pekka Pessi committed
348 349
  return retval;
}