test_service.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2013, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_service.c
  18. * @brief tests for service.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. /**
  24. * Message type we use for testing.
  25. */
  26. #define MY_TYPE 256
  27. #define TIMEOUT GNUNET_TIME_UNIT_SECONDS
  28. static int global_ret = 1;
  29. static struct GNUNET_MQ_Handle *mq;
  30. /**
  31. * Timeout task.
  32. */
  33. static struct GNUNET_SCHEDULER_Task *tt;
  34. static void
  35. handle_recv (void *cls,
  36. const struct GNUNET_MessageHeader *message)
  37. {
  38. struct GNUNET_SERVICE_Client *client = cls;
  39. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  40. "Received client message...\n");
  41. GNUNET_SERVICE_client_continue (client);
  42. global_ret = 2;
  43. if (NULL != mq)
  44. {
  45. GNUNET_MQ_destroy (mq);
  46. mq = NULL;
  47. }
  48. }
  49. /**
  50. * Function called when the client connects to the service.
  51. *
  52. * @param cls the name of the service
  53. * @param c connecting client
  54. * @param mq message queue to talk to the client
  55. * @return @a c so we have the client handle in the future
  56. */
  57. static void *
  58. connect_cb (void *cls,
  59. struct GNUNET_SERVICE_Client *c,
  60. struct GNUNET_MQ_Handle *mq)
  61. {
  62. /* FIXME: in the future, do something with mq
  63. to test sending messages to the client! */
  64. return c;
  65. }
  66. /**
  67. * Function called when the client disconnects.
  68. *
  69. * @param cls our service name
  70. * @param c disconnecting client
  71. * @param internal_cls must match @a c
  72. */
  73. static void
  74. disconnect_cb (void *cls,
  75. struct GNUNET_SERVICE_Client *c,
  76. void *internal_cls)
  77. {
  78. GNUNET_assert (c == internal_cls);
  79. if (2 == global_ret)
  80. {
  81. GNUNET_SCHEDULER_shutdown ();
  82. global_ret = 0;
  83. if (NULL != tt)
  84. {
  85. GNUNET_SCHEDULER_cancel (tt);
  86. tt = NULL;
  87. }
  88. }
  89. }
  90. static void
  91. timeout_task (void *cls)
  92. {
  93. tt = NULL;
  94. if (NULL != mq)
  95. {
  96. GNUNET_MQ_destroy (mq);
  97. mq = NULL;
  98. }
  99. global_ret = 33;
  100. GNUNET_SCHEDULER_shutdown ();
  101. }
  102. /**
  103. * Initialization function of the service. Starts
  104. * a client to connect to the service.
  105. *
  106. * @param cls the name of the service (const char *)
  107. * @param cfg the configuration we use
  108. * @param sh handle to the service
  109. */
  110. static void
  111. service_init (void *cls,
  112. const struct GNUNET_CONFIGURATION_Handle *cfg,
  113. struct GNUNET_SERVICE_Handle *sh)
  114. {
  115. const char *service_name = cls;
  116. struct GNUNET_MQ_Envelope *env;
  117. struct GNUNET_MessageHeader *msg;
  118. GNUNET_assert (NULL == tt);
  119. tt = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  120. &timeout_task,
  121. NULL);
  122. mq = GNUNET_CLIENT_connect (cfg,
  123. service_name,
  124. NULL,
  125. NULL,
  126. NULL);
  127. GNUNET_assert (NULL != mq);
  128. env = GNUNET_MQ_msg (msg,
  129. MY_TYPE);
  130. GNUNET_MQ_send (mq,
  131. env);
  132. }
  133. /**
  134. * Main method, starts the service and initiates
  135. * the running of the test.
  136. *
  137. * @param sname name of the service to run
  138. */
  139. static int
  140. check (const char *sname)
  141. {
  142. struct GNUNET_MQ_MessageHandler myhandlers[] = {
  143. GNUNET_MQ_hd_fixed_size (recv,
  144. MY_TYPE,
  145. struct GNUNET_MessageHeader,
  146. NULL),
  147. GNUNET_MQ_handler_end ()
  148. };
  149. char *const argv[] = {
  150. (char *) sname,
  151. "-c",
  152. "test_service_data.conf",
  153. NULL
  154. };
  155. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  156. "Starting `%s' service\n",
  157. sname);
  158. global_ret = 1;
  159. GNUNET_assert (0 ==
  160. GNUNET_SERVICE_run_ (3,
  161. argv,
  162. sname,
  163. GNUNET_SERVICE_OPTION_NONE,
  164. &service_init,
  165. &connect_cb,
  166. &disconnect_cb,
  167. (void *) sname,
  168. myhandlers));
  169. return global_ret;
  170. }
  171. int
  172. main (int argc,
  173. char *argv[])
  174. {
  175. int ret = 0;
  176. struct GNUNET_NETWORK_Handle *s = NULL;
  177. GNUNET_log_setup ("test-service",
  178. "WARNING",
  179. NULL);
  180. ret += check ("test_service");
  181. ret += check ("test_service");
  182. s = GNUNET_NETWORK_socket_create (PF_INET6,
  183. SOCK_STREAM,
  184. 0);
  185. if (NULL == s)
  186. {
  187. if ((errno == ENOBUFS) ||
  188. (errno == ENOMEM) ||
  189. (errno == ENFILE) ||
  190. (errno == EACCES))
  191. {
  192. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  193. "socket");
  194. return 1;
  195. }
  196. fprintf (stderr,
  197. "IPv6 support seems to not be available (%s), not testing it!\n",
  198. strerror (errno));
  199. }
  200. else
  201. {
  202. GNUNET_break (GNUNET_OK ==
  203. GNUNET_NETWORK_socket_close (s));
  204. ret += check ("test_service6");
  205. }
  206. return ret;
  207. }
  208. /* end of test_service.c */