test_testing_group_remote.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 testing/test_testing_group_remote.c
  19. * @brief testcase for testing remote and local starting and connecting
  20. * of hosts from the testing library. The test_testing_data_remote.conf
  21. * file should be modified if this testcase is intended to be used.
  22. */
  23. #include "platform.h"
  24. #include "gnunet_testing_lib.h"
  25. #define VERBOSE GNUNET_YES
  26. /**
  27. * How long until we give up on connecting the peers?
  28. */
  29. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  30. #define DEFAULT_NUM_PEERS 8;
  31. static int ok;
  32. static int peers_left;
  33. static int peers_failed;
  34. static struct GNUNET_TESTING_PeerGroup *pg;
  35. static unsigned long long num_peers;
  36. /**
  37. * Check whether peers successfully shut down.
  38. */
  39. void
  40. shutdown_callback (void *cls, const char *emsg)
  41. {
  42. if (emsg != NULL)
  43. {
  44. #if VERBOSE
  45. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed (error %s)!\n", emsg);
  46. #endif
  47. if (ok == 0)
  48. ok = 666;
  49. }
  50. else
  51. {
  52. #if VERBOSE
  53. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  54. "All peers successfully shut down!\n");
  55. #endif
  56. }
  57. }
  58. static void
  59. my_cb (void *cls,
  60. const struct GNUNET_PeerIdentity *id,
  61. const struct GNUNET_CONFIGURATION_Handle *cfg,
  62. struct GNUNET_TESTING_Daemon *d, const char *emsg)
  63. {
  64. if (emsg != NULL)
  65. {
  66. peers_failed++;
  67. }
  68. peers_left--;
  69. if (peers_left == 0)
  70. {
  71. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  72. ok = 0;
  73. }
  74. else if (peers_failed == peers_left)
  75. {
  76. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  77. "Too many peers failed, ending test!\n");
  78. GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
  79. }
  80. }
  81. static void
  82. run (void *cls,
  83. char *const *args,
  84. const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
  85. {
  86. struct GNUNET_TESTING_Host *hosts;
  87. struct GNUNET_TESTING_Host *hostpos;
  88. struct GNUNET_TESTING_Host *temphost;
  89. char *hostfile;
  90. struct stat frstat;
  91. char *buf;
  92. char *data;
  93. int count;
  94. int ret;
  95. ok = 1;
  96. #if VERBOSE
  97. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
  98. #endif
  99. if (GNUNET_SYSERR ==
  100. GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
  101. &num_peers))
  102. num_peers = DEFAULT_NUM_PEERS;
  103. GNUNET_assert (num_peers > 0 && num_peers < (unsigned long long) -1);
  104. if (GNUNET_OK !=
  105. GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
  106. &hostfile))
  107. hostfile = NULL;
  108. hosts = NULL;
  109. data = NULL;
  110. if (hostfile != NULL)
  111. {
  112. if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
  113. GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
  114. | GNUNET_DISK_PERM_USER_WRITE);
  115. if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
  116. {
  117. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  118. "Could not open file specified for host list, ending test!");
  119. ok = 1119;
  120. GNUNET_free (hostfile);
  121. return;
  122. }
  123. data = GNUNET_malloc_large (frstat.st_size);
  124. GNUNET_assert (data != NULL);
  125. if (frstat.st_size !=
  126. GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
  127. {
  128. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  129. "Could not read file %s specified for host list, ending test!",
  130. hostfile);
  131. GNUNET_free (hostfile);
  132. GNUNET_free (data);
  133. return;
  134. }
  135. GNUNET_free_non_null (hostfile);
  136. buf = data;
  137. count = 0;
  138. while (count < frstat.st_size)
  139. {
  140. count++;
  141. if (count >= frstat.st_size)
  142. break;
  143. /* if (((data[count] == '\n') || (data[count] == '\0')) && (buf != &data[count])) */
  144. if (((data[count] == '\n')) && (buf != &data[count]))
  145. {
  146. data[count] = '\0';
  147. temphost = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Host));
  148. ret =
  149. sscanf (buf, "%a[a-zA-Z0-9]@%a[a-zA-Z0-9.]:%hd",
  150. &temphost->username, &temphost->hostname,
  151. &temphost->port);
  152. if (3 == ret)
  153. {
  154. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  155. "Successfully read host %s, port %d and user %s from file\n",
  156. temphost->hostname, temphost->port,
  157. temphost->username);
  158. }
  159. else
  160. {
  161. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  162. "Error reading line `%s' in hostfile\n", buf);
  163. GNUNET_free (temphost);
  164. buf = &data[count + 1];
  165. continue;
  166. }
  167. /* temphost->hostname = buf; */
  168. temphost->next = hosts;
  169. hosts = temphost;
  170. buf = &data[count + 1];
  171. }
  172. else if ((data[count] == '\n') || (data[count] == '\0'))
  173. buf = &data[count + 1];
  174. }
  175. }
  176. peers_left = num_peers;
  177. pg = GNUNET_TESTING_daemons_start (cfg,
  178. peers_left, /* Total number of peers */
  179. peers_left, /* Number of outstanding connections */
  180. peers_left, /* Number of parallel ssh connections, or peers being started at once */
  181. TIMEOUT,
  182. NULL,
  183. NULL, &my_cb, NULL, NULL, NULL, hosts);
  184. hostpos = hosts;
  185. while (hostpos != NULL)
  186. {
  187. temphost = hostpos->next;
  188. GNUNET_free (hostpos->hostname);
  189. GNUNET_free (hostpos->username);
  190. GNUNET_free (hostpos);
  191. hostpos = temphost;
  192. }
  193. GNUNET_free_non_null (data);
  194. GNUNET_assert (pg != NULL);
  195. }
  196. static int
  197. check ()
  198. {
  199. char *const argv[] = { "test-testing",
  200. "-c",
  201. "test_testing_data_remote.conf",
  202. #if VERBOSE
  203. "-L", "DEBUG",
  204. #endif
  205. NULL
  206. };
  207. struct GNUNET_GETOPT_CommandLineOption options[] = {
  208. GNUNET_GETOPT_OPTION_END
  209. };
  210. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  211. argv, "test-testing-group", "nohelp",
  212. options, &run, &ok);
  213. return ok;
  214. }
  215. int
  216. main (int argc, char *argv[])
  217. {
  218. int ret;
  219. GNUNET_log_setup ("test-testing-group",
  220. #if VERBOSE
  221. "DEBUG",
  222. #else
  223. "WARNING",
  224. #endif
  225. NULL);
  226. ret = check ();
  227. /**
  228. * Still need to remove the base testing directory here,
  229. * because group starts will create subdirectories under this
  230. * main dir. However, we no longer need to sleep, as the
  231. * shutdown sequence won't return until everything is cleaned
  232. * up.
  233. */
  234. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
  235. return ret;
  236. }
  237. /* end of test_testing_group.c */