torture_su_port.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@internal
  25. * @file torture_su_port.c
  26. * @brief Test su_poll_port interface
  27. *
  28. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  29. * @date Created: Wed Mar 10 17:05:23 2004 ppessi
  30. */
  31. #include "config.h"
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. struct su_root_magic_s;
  35. #define SU_ROOT_MAGIC_T struct su_root_magic_s
  36. #include "su_poll_port.c"
  37. #undef HAVE_EPOLL
  38. #define HAVE_EPOLL 0
  39. #if HAVE_FUNC
  40. #elif HAVE_FUNCTION
  41. #define __func__ __FUNCTION__
  42. #else
  43. static char const __func__[] = "torture_su_port";
  44. #endif
  45. int tstflags;
  46. #define TSTFLAGS tstflags
  47. #include <sofia-sip/tstdef.h>
  48. char const *name = "torture_su_port";
  49. #if HAVE_OPEN_C
  50. int const N0 = SU_MBOX_SIZE > 0, N = 63, I = 64;
  51. #else
  52. int const N0 = SU_MBOX_SIZE > 0, N = 128, I = 129;
  53. #endif
  54. int test_sup_indices(su_port_t const *port)
  55. {
  56. int i, n;
  57. int *indices = port->sup_indices;
  58. int *reverses = port->sup_reverses;
  59. int N = port->sup_size_waits;
  60. if (indices == NULL)
  61. return N == 0 && port->sup_n_waits == 0;
  62. for (i = 0, n = 0; i < N; i++) {
  63. if (reverses[i] > 0) {
  64. if (indices[reverses[i]] != i)
  65. return 0;
  66. n++;
  67. }
  68. }
  69. if (n != port->sup_n_waits)
  70. return 0;
  71. n = 0;
  72. for (i = 1; i <= N; i++) {
  73. if (indices[i] >= 0) {
  74. if (reverses[indices[i]] != i)
  75. return 0;
  76. n++;
  77. }
  78. }
  79. if (n != port->sup_n_waits)
  80. return 0;
  81. for (i = indices[0]; -i <= N; i = indices[-i]) {
  82. if (i >= 0)
  83. return 0;
  84. n++;
  85. }
  86. return n == port->sup_size_waits;
  87. }
  88. struct su_root_magic_s
  89. {
  90. int error, *sockets, *regs, *wakeups;
  91. };
  92. static int callback(su_root_magic_t *magic,
  93. su_wait_t *w,
  94. su_wakeup_arg_t *arg)
  95. {
  96. intptr_t i = (intptr_t)arg;
  97. assert(magic);
  98. if (i <= 0 || i > I)
  99. return ++magic->error;
  100. su_wait_events(w, magic->sockets[i]);
  101. magic->wakeups[i]++;
  102. #if HAVE_POLL || HAVE_SELECT
  103. if (w->fd != magic->sockets[i])
  104. return ++magic->error;
  105. #endif
  106. return 0;
  107. }
  108. int test_wakeup(su_port_t *port, su_root_magic_t *magic)
  109. {
  110. int i;
  111. for (i = N0; i < N; i++) {
  112. su_sockaddr_t su[1]; socklen_t sulen = sizeof su;
  113. int n, woken = magic->wakeups[i];
  114. char buf[1];
  115. if (magic->regs[i] == 0)
  116. continue;
  117. if (getsockname(magic->sockets[i], &su->su_sa, &sulen) < 0)
  118. su_perror("getsockname"), exit(1);
  119. if (su_sendto(magic->sockets[1], "X", 1, 0, su, sulen) < 0)
  120. su_perror("su_sendto"), exit(1);
  121. n = su_poll_port_wait_events(port, 100);
  122. if (n != 1)
  123. return 1;
  124. if (magic->error)
  125. return 2;
  126. if (magic->wakeups[i] != woken + 1)
  127. return 3;
  128. if (recv(magic->sockets[i], buf, 1, 0) != 1)
  129. return 4;
  130. }
  131. return 0;
  132. }
  133. int test_register(void)
  134. {
  135. su_port_t *port;
  136. su_sockaddr_t su[1];
  137. intptr_t i;
  138. int sockets[256] = { 0 };
  139. int reg[256] = { 0 };
  140. int wakeups[256] = { 0 };
  141. int prioritized;
  142. su_wait_t wait[256];
  143. su_root_magic_t magic[1] = {{ 0 }};
  144. su_root_t root[1] = {{ sizeof root }};
  145. BEGIN();
  146. root->sur_magic = magic;
  147. memset(su, 0, sizeof su);
  148. su->su_len = sizeof su->su_sin;
  149. su->su_family = AF_INET;
  150. su->su_sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
  151. memset(wait, 0, sizeof wait);
  152. memset(magic->sockets = sockets, -1, sizeof sockets);
  153. memset(magic->regs = reg, 0, sizeof reg);
  154. memset(magic->wakeups = wakeups, 0, sizeof wakeups);
  155. su_root_size_hint = 16;
  156. TEST_1(port = su_poll_port_create());
  157. TEST(su_home_threadsafe(su_port_home(port)), 0);
  158. /* Before 1.12.4 su_port_create() had reference count 0 after creation */
  159. /* su_port_incref(port, "test_register"); */
  160. TEST_1(test_sup_indices(port));
  161. for (i = N0; i < N; i++) {
  162. sockets[i] = su_socket(AF_INET, SOCK_DGRAM, 0); TEST_1(sockets[i] != -1);
  163. if (bind(sockets[i], &su->su_sa, sizeof su->su_sin) != 0)
  164. perror("bind"), assert(0);
  165. TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);
  166. reg[i] = su_port_register(port, root, wait + i, callback, (void*)i, 0);
  167. TEST_1(reg[i] > 0);
  168. }
  169. TEST(port->sup_indices[0], -I);
  170. TEST_1(test_sup_indices(port));
  171. TEST(test_wakeup(port, magic), 0);
  172. for (i = 1; i < N; i += 2) {
  173. TEST(su_port_deregister(port, reg[i]), reg[i]);
  174. TEST_1(test_sup_indices(port));
  175. }
  176. TEST_1(test_sup_indices(port));
  177. prioritized = 0;
  178. for (i = N - 1; i >= N0; i -= 2) {
  179. TEST(su_wait_create(wait + i, sockets[i], SU_WAIT_IN), 0);
  180. reg[i] = su_port_register(port, root, wait + i, callback, (void *)i, 1);
  181. TEST_1(reg[i] > 0);
  182. prioritized++; /* Count number of prioritized registrations */
  183. #if HAVE_EPOLL
  184. /* With epoll we do not bother to prioritize the wait list */
  185. if (port->sup_epoll != -1) {
  186. int N = port->sup_n_waits;
  187. TEST_M(wait + i, port->sup_waits + N - 1, sizeof wait[0]);
  188. }
  189. else
  190. #endif
  191. TEST_M(wait + i, port->sup_waits, sizeof wait[0]);
  192. }
  193. TEST(port->sup_indices[0], -I);
  194. #if 0
  195. #if HAVE_EPOLL
  196. /* With epoll we do not bother to prioritize the wait list */
  197. if (port->sup_epoll != -1) {
  198. TEST_M(wait + 15, port->sup_waits + 8, sizeof wait[0]);
  199. TEST_M(wait + 13, port->sup_waits + 9, sizeof wait[0]);
  200. TEST_M(wait + 11, port->sup_waits + 10, sizeof wait[0]);
  201. TEST_M(wait + 9, port->sup_waits + 11, sizeof wait[0]);
  202. TEST_M(wait + 7, port->sup_waits + 12, sizeof wait[0]);
  203. TEST_M(wait + 5, port->sup_waits + 13, sizeof wait[0]);
  204. TEST_M(wait + 3, port->sup_waits + 14, sizeof wait[0]);
  205. TEST_M(wait + 1, port->sup_waits + 15, sizeof wait[0]);
  206. }
  207. else
  208. #endif
  209. {
  210. TEST_M(wait + 15, port->sup_waits + 7, sizeof wait[0]);
  211. TEST_M(wait + 13, port->sup_waits + 6, sizeof wait[0]);
  212. TEST_M(wait + 11, port->sup_waits + 5, sizeof wait[0]);
  213. TEST_M(wait + 9, port->sup_waits + 4, sizeof wait[0]);
  214. TEST_M(wait + 7, port->sup_waits + 3, sizeof wait[0]);
  215. TEST_M(wait + 5, port->sup_waits + 2, sizeof wait[0]);
  216. TEST_M(wait + 3, port->sup_waits + 1, sizeof wait[0]);
  217. TEST_M(wait + 1, port->sup_waits + 0, sizeof wait[0]);
  218. }
  219. #endif
  220. TEST_1(test_sup_indices(port));
  221. TEST(test_wakeup(port, magic), 0);
  222. for (i = 1; i <= 8; i++) {
  223. TEST(su_port_deregister(port, reg[i]), reg[i]); reg[i] = 0;
  224. if (i % 2 == (N - 1) % 2)
  225. prioritized--; /* Count number of prioritized registrations */
  226. }
  227. #if HAVE_EPOLL
  228. /* With epoll we do not bother to prioritize the wait list */
  229. if (port->sup_epoll == -1)
  230. #endif
  231. TEST(port->sup_pri_offset, prioritized);
  232. TEST_1(test_sup_indices(port));
  233. TEST(su_port_deregister(port, 0), -1);
  234. TEST(su_port_deregister(port, -1), -1);
  235. TEST(su_port_deregister(port, 130), -1);
  236. TEST_1(test_sup_indices(port));
  237. for (i = 1; i <= 8; i++) {
  238. TEST(su_port_deregister(port, reg[i]), -1);
  239. }
  240. TEST_VOID(su_port_decref(port, __func__));
  241. END();
  242. }
  243. void usage(int exitcode)
  244. {
  245. fprintf(stderr,
  246. "usage: %s [-v] [-a]\n",
  247. name);
  248. exit(exitcode);
  249. }
  250. int main(int argc, char *argv[])
  251. {
  252. int retval = 0;
  253. int i;
  254. #if SU_HAVE_WINSOCK
  255. if (N > SU_WAIT_MAX)
  256. N = SU_WAIT_MAX;
  257. if (I - 1 >= SU_WAIT_MAX)
  258. I = (unsigned)SU_WAIT_MAX + 1;
  259. #endif
  260. for (i = 1; argv[i]; i++) {
  261. if (strcmp(argv[i], "-v") == 0)
  262. tstflags |= tst_verbatim;
  263. else if (strcmp(argv[i], "-a") == 0)
  264. tstflags |= tst_abort;
  265. else
  266. usage(1);
  267. }
  268. #if HAVE_OPEN_C
  269. tstflags |= tst_verbatim;
  270. #endif
  271. su_init();
  272. retval |= test_register(); fflush(stdout);
  273. su_deinit();
  274. return retval;
  275. }