test_gnunet_daemon_topology.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 topology/test_gnunet_daemon_topology.c
  19. * @brief testcase for topology maintenance code
  20. */
  21. #include "platform.h"
  22. #include "gnunet_testing_lib.h"
  23. #define VERBOSE GNUNET_NO
  24. #define NUM_PEERS 2
  25. /**
  26. * How long until we give up on connecting the peers?
  27. */
  28. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
  29. #define CONNECT_ATTEMPTS 3
  30. static int ok;
  31. static int peers_left;
  32. static int connect_left;
  33. static struct GNUNET_TESTING_PeerGroup *pg;
  34. static struct GNUNET_TESTING_Daemon *first;
  35. static struct GNUNET_TESTING_Daemon *last;
  36. /**
  37. * Check whether peers successfully shut down.
  38. */
  39. static void
  40. shutdown_callback (void *cls,
  41. const char *emsg)
  42. {
  43. if (emsg != NULL)
  44. {
  45. #if VERBOSE
  46. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  47. "Shutdown of peers failed!\n");
  48. #endif
  49. if (ok == 0)
  50. ok = 666;
  51. }
  52. else
  53. {
  54. #if VERBOSE
  55. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  56. "All peers successfully shut down!\n");
  57. #endif
  58. }
  59. }
  60. static void
  61. clean_up_task (void *cls,
  62. const struct GNUNET_SCHEDULER_TaskContext *tc)
  63. {
  64. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  65. ok = 0;
  66. }
  67. static void
  68. notify_connect_complete(void *cls,
  69. const struct GNUNET_PeerIdentity *first,
  70. const struct GNUNET_PeerIdentity *second,
  71. unsigned int distance,
  72. const struct GNUNET_CONFIGURATION_Handle *first_cfg,
  73. const struct GNUNET_CONFIGURATION_Handle *second_cfg,
  74. struct GNUNET_TESTING_Daemon *first_daemon,
  75. struct GNUNET_TESTING_Daemon *second_daemon,
  76. const char *emsg)
  77. {
  78. if (NULL != emsg)
  79. {
  80. fprintf (stderr,
  81. "Failed to connect two peers: %s\n",
  82. emsg);
  83. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  84. GNUNET_assert (0);
  85. return;
  86. }
  87. connect_left--;
  88. if (connect_left == 0)
  89. {
  90. /* FIXME: check that topology adds a few more links
  91. in addition to those that were seeded */
  92. GNUNET_SCHEDULER_add_now (&clean_up_task,
  93. NULL);
  94. }
  95. }
  96. static
  97. void my_cb(void *cls,
  98. const struct GNUNET_PeerIdentity *id,
  99. const struct GNUNET_CONFIGURATION_Handle *cfg,
  100. struct GNUNET_TESTING_Daemon *d,
  101. const char *emsg)
  102. {
  103. GNUNET_assert (id != NULL);
  104. peers_left--;
  105. if (first == NULL)
  106. {
  107. connect_left = NUM_PEERS;
  108. first = d;
  109. last = d;
  110. return;
  111. }
  112. GNUNET_TESTING_daemons_connect (last, d, TIMEOUT, CONNECT_ATTEMPTS,
  113. GNUNET_YES,
  114. &notify_connect_complete,
  115. NULL);
  116. if (peers_left == 0)
  117. {
  118. /* close circle */
  119. GNUNET_TESTING_daemons_connect (d, first, TIMEOUT, CONNECT_ATTEMPTS,
  120. GNUNET_YES,
  121. &notify_connect_complete,
  122. NULL);
  123. }
  124. }
  125. static void
  126. run (void *cls,
  127. char *const *args,
  128. const char *cfgfile,
  129. const struct GNUNET_CONFIGURATION_Handle *cfg)
  130. {
  131. ok = 1;
  132. #if VERBOSE
  133. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  134. "Starting daemons.\n");
  135. #endif
  136. peers_left = NUM_PEERS;
  137. pg = GNUNET_TESTING_daemons_start (cfg,
  138. peers_left,
  139. peers_left,
  140. peers_left,
  141. TIMEOUT,
  142. NULL, NULL,
  143. &my_cb, NULL, NULL, NULL, NULL);
  144. GNUNET_assert (pg != NULL);
  145. }
  146. static int
  147. check ()
  148. {
  149. char *const argv[] = {
  150. "test-gnunet-daemon-topology",
  151. "-c",
  152. "test_gnunet_daemon_topology_data.conf",
  153. #if VERBOSE
  154. "-L", "DEBUG",
  155. #endif
  156. NULL
  157. };
  158. struct GNUNET_GETOPT_CommandLineOption options[] = {
  159. GNUNET_GETOPT_OPTION_END
  160. };
  161. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  162. argv, "test-gnunet-daemon-topology", "nohelp",
  163. options, &run, &ok);
  164. return ok;
  165. }
  166. int
  167. main (int argc, char *argv[])
  168. {
  169. int ret;
  170. GNUNET_log_setup ("test-gnunet-daemon-topology",
  171. #if VERBOSE
  172. "DEBUG",
  173. #else
  174. "WARNING",
  175. #endif
  176. NULL);
  177. ret = check ();
  178. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology");
  179. return ret;
  180. }
  181. /* end of test_gnunet_daemon_topology.c */