test_connection.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file util/test_connection.c
  19. * @brief tests for connection.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #define PORT 12435
  24. static struct GNUNET_CONNECTION_Handle *csock;
  25. static struct GNUNET_CONNECTION_Handle *asock;
  26. static struct GNUNET_CONNECTION_Handle *lsock;
  27. static size_t sofar;
  28. static struct GNUNET_NETWORK_Handle *ls;
  29. static struct GNUNET_CONFIGURATION_Handle *cfg;
  30. /**
  31. * Create and initialize a listen socket for the server.
  32. *
  33. * @return -1 on error, otherwise the listen socket
  34. */
  35. static struct GNUNET_NETWORK_Handle *
  36. open_listen_socket ()
  37. {
  38. const static int on = 1;
  39. struct sockaddr_in sa;
  40. struct GNUNET_NETWORK_Handle *desc;
  41. memset (&sa, 0, sizeof (sa));
  42. #if HAVE_SOCKADDR_IN_SIN_LEN
  43. sa.sin_len = sizeof (sa);
  44. #endif
  45. sa.sin_port = htons (PORT);
  46. sa.sin_family = AF_INET;
  47. desc = GNUNET_NETWORK_socket_create (AF_INET, SOCK_STREAM, 0);
  48. GNUNET_assert (desc != NULL);
  49. if (GNUNET_NETWORK_socket_setsockopt
  50. (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
  51. GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt");
  52. GNUNET_assert (GNUNET_OK ==
  53. GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa,
  54. sizeof (sa)));
  55. GNUNET_NETWORK_socket_listen (desc, 5);
  56. return desc;
  57. }
  58. static void
  59. receive_check (void *cls, const void *buf, size_t available,
  60. const struct sockaddr *addr, socklen_t addrlen, int errCode)
  61. {
  62. int *ok = cls;
  63. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive validates incoming data\n");
  64. GNUNET_assert (buf != NULL); /* no timeout */
  65. if (0 == memcmp (&"Hello World"[sofar], buf, available))
  66. sofar += available;
  67. if (sofar < 12)
  68. {
  69. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive needs more data\n");
  70. GNUNET_CONNECTION_receive (asock, 1024,
  71. GNUNET_TIME_relative_multiply
  72. (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
  73. cls);
  74. }
  75. else
  76. {
  77. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receive closes accepted socket\n");
  78. *ok = 0;
  79. GNUNET_CONNECTION_destroy (asock);
  80. GNUNET_CONNECTION_destroy (csock);
  81. }
  82. }
  83. static void
  84. run_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  85. {
  86. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test accepts connection\n");
  87. asock = GNUNET_CONNECTION_create_from_accept (NULL, NULL, ls);
  88. GNUNET_assert (asock != NULL);
  89. GNUNET_assert (GNUNET_YES == GNUNET_CONNECTION_check (asock));
  90. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys listen socket\n");
  91. GNUNET_CONNECTION_destroy (lsock);
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  93. "Test asks to receive on accepted socket\n");
  94. GNUNET_CONNECTION_receive (asock, 1024,
  95. GNUNET_TIME_relative_multiply
  96. (GNUNET_TIME_UNIT_SECONDS, 5), &receive_check,
  97. cls);
  98. }
  99. static size_t
  100. make_hello (void *cls, size_t size, void *buf)
  101. {
  102. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  103. "Test prepares to transmit on connect socket\n");
  104. GNUNET_assert (size >= 12);
  105. strcpy ((char *) buf, "Hello World");
  106. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test destroys client socket\n");
  107. return 12;
  108. }
  109. static void
  110. task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  111. {
  112. ls = open_listen_socket ();
  113. lsock = GNUNET_CONNECTION_create_from_existing (ls);
  114. GNUNET_assert (lsock != NULL);
  115. csock = GNUNET_CONNECTION_create_from_connect (cfg, "localhost", PORT);
  116. GNUNET_assert (csock != NULL);
  117. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test asks for write notification\n");
  118. GNUNET_assert (NULL !=
  119. GNUNET_CONNECTION_notify_transmit_ready (csock, 12,
  120. GNUNET_TIME_UNIT_SECONDS,
  121. &make_hello, NULL));
  122. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test prepares to accept\n");
  123. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, ls, &run_accept,
  124. cls);
  125. }
  126. int
  127. main (int argc, char *argv[])
  128. {
  129. int ok;
  130. GNUNET_log_setup ("test_connection",
  131. "WARNING",
  132. NULL);
  133. ok = 1;
  134. cfg = GNUNET_CONFIGURATION_create ();
  135. GNUNET_CONFIGURATION_set_value_string (cfg, "resolver", "HOSTNAME",
  136. "localhost");
  137. GNUNET_SCHEDULER_run (&task, &ok);
  138. GNUNET_CONFIGURATION_destroy (cfg);
  139. return ok;
  140. }
  141. /* end of test_connection.c */