test_gnunet_service_arm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 arm/test_gnunet_service_arm.c
  19. * @brief testcase for gnunet-service-arm.c; tests ARM by making it start the resolver
  20. * @author Safey
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_resolver_service.h"
  26. #include "gnunet_os_lib.h"
  27. #include "gnunet_program_lib.h"
  28. /**
  29. * Timeout for starting services, very short because of the strange way start works
  30. * (by checking if running before starting, so really this time is always waited on
  31. * startup (annoying)).
  32. */
  33. #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
  34. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  35. static int ret = 1;
  36. static int resolved_ok = 0;
  37. static int asked_for_a_list = 0;
  38. static struct GNUNET_ARM_Handle *arm;
  39. static void
  40. trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  41. {
  42. GNUNET_ARM_disconnect_and_free (arm);
  43. arm = NULL;
  44. }
  45. static void
  46. arm_stop_cb (void *cls,
  47. enum GNUNET_ARM_RequestStatus status,
  48. const char *servicename,
  49. enum GNUNET_ARM_Result result)
  50. {
  51. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  52. GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
  53. if (result != GNUNET_ARM_RESULT_STOPPED)
  54. ret = 4;
  55. GNUNET_SCHEDULER_add_now (trigger_disconnect, NULL);
  56. }
  57. static void
  58. service_list (void *cls,
  59. enum GNUNET_ARM_RequestStatus rs,
  60. unsigned int count, const char *const*list)
  61. {
  62. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  63. "%u services are are currently running\n",
  64. count);
  65. if (GNUNET_ARM_REQUEST_SENT_OK != rs)
  66. goto stop_arm;
  67. if (1 == count)
  68. {
  69. GNUNET_break (0 == strcasecmp (list[0], "resolver (gnunet-service-resolver)"));
  70. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got service list, now stopping arm\n");
  71. ret = 0;
  72. }
  73. stop_arm:
  74. GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
  75. }
  76. static void
  77. hostNameResolveCB (void *cls, const struct sockaddr *addr, socklen_t addrlen)
  78. {
  79. if ((ret == 0) || (ret == 4) || (resolved_ok == 1))
  80. return;
  81. if (NULL == addr)
  82. {
  83. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Name not resolved!\n");
  84. ret = 3;
  85. GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
  86. }
  87. else if (asked_for_a_list == 0)
  88. {
  89. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  90. "Resolved hostname, now checking the service list\n");
  91. GNUNET_ARM_request_service_list (arm, TIMEOUT, service_list, NULL);
  92. asked_for_a_list = 1;
  93. resolved_ok = 1;
  94. }
  95. }
  96. static void
  97. arm_start_cb (void *cls,
  98. enum GNUNET_ARM_RequestStatus status,
  99. const char *servicename,
  100. enum GNUNET_ARM_Result result)
  101. {
  102. GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
  103. GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
  104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  105. "Trying to resolve our own hostname!\n");
  106. /* connect to the resolver service */
  107. if (NULL == GNUNET_RESOLVER_hostname_resolve (
  108. AF_UNSPEC, TIMEOUT, &hostNameResolveCB, NULL))
  109. {
  110. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  111. "Unable initiate connection to resolver service\n");
  112. ret = 2;
  113. GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
  114. }
  115. }
  116. static void
  117. run (void *cls, char *const *args, const char *cfgfile,
  118. const struct GNUNET_CONFIGURATION_Handle *c)
  119. {
  120. char *armconfig;
  121. if (NULL != cfgfile)
  122. {
  123. if (GNUNET_OK !=
  124. GNUNET_CONFIGURATION_get_value_filename (c, "arm", "CONFIG",
  125. &armconfig))
  126. {
  127. GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle
  128. *) c, "arm", "CONFIG",
  129. cfgfile);
  130. }
  131. else
  132. GNUNET_free (armconfig);
  133. }
  134. arm = GNUNET_ARM_connect (c, NULL, NULL);
  135. GNUNET_ARM_request_service_start (arm, "arm",
  136. GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
  137. }
  138. int
  139. main (int argc, char *av[])
  140. {
  141. static char *const argv[] = {
  142. "test-gnunet-service-arm",
  143. "-c", "test_arm_api_data.conf",
  144. NULL
  145. };
  146. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  147. GNUNET_GETOPT_OPTION_END
  148. };
  149. char hostname[GNUNET_OS_get_hostname_max_length () + 1];
  150. if (0 != gethostname (hostname, sizeof (hostname) - 1))
  151. {
  152. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  153. "gethostname");
  154. FPRINTF (stderr,
  155. "%s", "Failed to determine my own hostname, testcase not run.\n");
  156. return 0;
  157. }
  158. if (NULL == gethostbyname (hostname))
  159. {
  160. FPRINTF (stderr,
  161. "Failed to resolve my hostname `%s', testcase not run.\n",
  162. hostname);
  163. return 0;
  164. }
  165. GNUNET_log_setup ("test-gnunet-service-arm",
  166. "WARNING",
  167. NULL);
  168. GNUNET_break (GNUNET_OK ==
  169. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  170. argv, "test-gnunet-service-arm",
  171. "nohelp", options, &run, NULL));
  172. return ret;
  173. }
  174. /* end of test_gnunet_service_arm.c */