test_gnunet_daemon_hostlist.c 6.5 KB

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