test_gnunet_daemon_hostlist.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 hostlist/test_gnunet_daemon_hostlist.c
  19. * @brief test for gnunet_daemon_hostslist.c
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_transport_service.h"
  26. /**
  27. * How long until we give up on transmitting the message?
  28. */
  29. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
  30. static int ok;
  31. static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  32. struct PeerContext
  33. {
  34. struct GNUNET_CONFIGURATION_Handle *cfg;
  35. struct GNUNET_TRANSPORT_Handle *th;
  36. struct GNUNET_MessageHeader *hello;
  37. struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
  38. struct GNUNET_OS_Process *arm_proc;
  39. };
  40. static struct PeerContext p1;
  41. static struct PeerContext p2;
  42. static void
  43. clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  44. {
  45. if (p1.th != NULL)
  46. {
  47. if (p1.ghh != NULL)
  48. {
  49. GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
  50. p1.ghh = NULL;
  51. }
  52. GNUNET_TRANSPORT_disconnect (p1.th);
  53. p1.th = NULL;
  54. }
  55. if (p2.th != NULL)
  56. {
  57. if (p2.ghh != NULL)
  58. {
  59. GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
  60. p2.ghh = NULL;
  61. }
  62. GNUNET_TRANSPORT_disconnect (p2.th);
  63. p2.th = NULL;
  64. }
  65. GNUNET_SCHEDULER_shutdown ();
  66. }
  67. /**
  68. * Timeout, give up.
  69. */
  70. static void
  71. timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  72. {
  73. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  74. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  75. "Timeout trying to connect peers, test failed.\n");
  76. clean_up (NULL, tc);
  77. }
  78. /**
  79. * Function called to notify transport users that another
  80. * peer connected to us.
  81. *
  82. * @param cls closure
  83. * @param peer the peer that connected
  84. * @param latency current latency of the connection
  85. * @param distance in overlay hops, as given by transport plugin
  86. */
  87. static void
  88. notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
  89. {
  90. if (peer == NULL)
  91. return;
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
  93. ok = 0;
  94. if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
  95. {
  96. GNUNET_SCHEDULER_cancel (timeout_task);
  97. timeout_task = GNUNET_SCHEDULER_NO_TASK;
  98. }
  99. GNUNET_SCHEDULER_add_now (&clean_up, NULL);
  100. }
  101. static void
  102. process_hello (void *cls, const struct GNUNET_MessageHeader *message)
  103. {
  104. struct PeerContext *p = cls;
  105. GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
  106. p->ghh = NULL;
  107. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  108. "Received HELLO, starting hostlist service.\n");
  109. }
  110. static void
  111. setup_peer (struct PeerContext *p, const char *cfgname)
  112. {
  113. char *binary;
  114. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  115. p->cfg = GNUNET_CONFIGURATION_create ();
  116. p->arm_proc =
  117. GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  118. NULL, NULL, NULL,
  119. binary,
  120. "gnunet-service-arm",
  121. "-c", cfgname, NULL);
  122. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  123. p->th =
  124. GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
  125. GNUNET_assert (p->th != NULL);
  126. p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
  127. GNUNET_free (binary);
  128. }
  129. static void
  130. waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  131. {
  132. struct PeerContext *p = cls;
  133. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
  134. if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
  135. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  136. if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
  137. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  138. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
  139. GNUNET_OS_process_get_pid (p->arm_proc));
  140. GNUNET_OS_process_destroy (p->arm_proc);
  141. p->arm_proc = NULL;
  142. GNUNET_CONFIGURATION_destroy (p->cfg);
  143. }
  144. static void
  145. stop_arm (struct PeerContext *p)
  146. {
  147. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
  148. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
  149. }
  150. /**
  151. * Try again to connect to transport service.
  152. */
  153. static void
  154. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  155. {
  156. stop_arm (&p1);
  157. stop_arm (&p2);
  158. }
  159. static void
  160. run (void *cls, char *const *args, const char *cfgfile,
  161. const struct GNUNET_CONFIGURATION_Handle *cfg)
  162. {
  163. GNUNET_assert (ok == 1);
  164. ok++;
  165. timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
  166. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  167. NULL);
  168. setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
  169. setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
  170. }
  171. static int
  172. check ()
  173. {
  174. char *const argv[] = { "test-gnunet-daemon-hostlist",
  175. "-c", "test_gnunet_daemon_hostlist_data.conf",
  176. NULL
  177. };
  178. struct GNUNET_GETOPT_CommandLineOption options[] = {
  179. GNUNET_GETOPT_OPTION_END
  180. };
  181. ok = 1;
  182. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
  183. "test-gnunet-daemon-hostlist", "nohelp", options, &run,
  184. &ok);
  185. return ok;
  186. }
  187. int
  188. main (int argc, char *argv[])
  189. {
  190. int ret;
  191. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
  192. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
  193. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
  194. GNUNET_log_setup ("test-gnunet-daemon-hostlist",
  195. "WARNING",
  196. NULL);
  197. ret = check ();
  198. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
  199. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
  200. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
  201. return ret;
  202. }
  203. /* end of test_gnunet_daemon_hostlist.c */