test_gnunet_service_manager.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_manager.c (A mockup testcase, not functionally complete)
  19. * @brief testcase for gnunet-service-manager.c
  20. */
  21. #include "platform.h"
  22. #include "gnunet_arm_service.h"
  23. #include "gnunet_resolver_service.h"
  24. #include "gnunet_os_lib.h"
  25. #include "gnunet_program_lib.h"
  26. /**
  27. * Timeout for starting services, very short because of the strange way start works
  28. * (by checking if running before starting, so really this time is always waited on
  29. * startup (annoying)).
  30. */
  31. #define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50)
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
  33. #define START_ARM GNUNET_YES
  34. #define VERBOSE GNUNET_NO
  35. static int ret = 1;
  36. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  37. #if START_ARM
  38. static struct GNUNET_ARM_Handle *arm;
  39. #endif
  40. static void
  41. arm_stopped (void *cls, int success)
  42. {
  43. if (success != GNUNET_NO)
  44. {
  45. GNUNET_break (0);
  46. ret = 4;
  47. }
  48. else
  49. {
  50. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  51. "ARM stopped\n");
  52. }
  53. #if START_ARM
  54. GNUNET_ARM_disconnect (arm);
  55. arm = NULL;
  56. #endif
  57. }
  58. static void
  59. hostNameResolveCB(void *cls,
  60. const struct sockaddr *addr,
  61. socklen_t addrlen)
  62. {
  63. if ( (ret == 0) || (ret == 4) )
  64. return;
  65. if (NULL == addr)
  66. {
  67. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  68. "Name not resolved!\n");
  69. #if START_ARM
  70. GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
  71. #endif
  72. ret = 3;
  73. return;
  74. }
  75. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  76. "Resolved hostname, now stopping ARM\n");
  77. ret = 0;
  78. #if START_ARM
  79. GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
  80. #endif
  81. }
  82. static void
  83. arm_notify (void *cls, int success)
  84. {
  85. if (success != GNUNET_YES)
  86. {
  87. GNUNET_break (0);
  88. ret = 1;
  89. return;
  90. }
  91. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  92. "Trying to resolve our own hostname!\n");
  93. /* connect to the resolver service */
  94. if (NULL == GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
  95. TIMEOUT,
  96. &hostNameResolveCB,
  97. NULL))
  98. {
  99. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  100. "Unable initiate connection to resolver service\n");
  101. ret = 2;
  102. #if START_ARM
  103. GNUNET_ARM_stop_service (arm, "arm", TIMEOUT, &arm_stopped, NULL);
  104. #endif
  105. }
  106. }
  107. static void
  108. run(void *cls,
  109. char * const *args,
  110. const char *cfgfile,
  111. const struct GNUNET_CONFIGURATION_Handle *c)
  112. {
  113. cfg = c;
  114. #if START_ARM
  115. arm = GNUNET_ARM_connect (cfg, NULL);
  116. GNUNET_ARM_start_service (arm, "arm", START_TIMEOUT, &arm_notify, NULL);
  117. #else
  118. arm_notify (NULL, GNUNET_YES);
  119. #endif
  120. }
  121. static void
  122. check()
  123. {
  124. char *const argv[] = {
  125. "test-gnunet-service-manager",
  126. "-c", "test_arm_api_data.conf",
  127. #if VERBOSE
  128. "-L", "DEBUG",
  129. #endif
  130. NULL
  131. };
  132. struct GNUNET_GETOPT_CommandLineOption options[] = {
  133. GNUNET_GETOPT_OPTION_END
  134. };
  135. GNUNET_assert (GNUNET_OK ==
  136. GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
  137. argv,
  138. "test-gnunet-service-manager",
  139. "nohelp", options, &run, NULL));
  140. }
  141. int
  142. main (int argc, char *argv[])
  143. {
  144. char hostname[GNUNET_OS_get_hostname_max_length() + 1];
  145. if (0 != gethostname (hostname, sizeof (hostname) - 1))
  146. {
  147. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR |
  148. GNUNET_ERROR_TYPE_BULK, "gethostname");
  149. fprintf (stderr, "Failed to determine my own hostname, testcase not run.\n");
  150. return 0;
  151. }
  152. if (NULL == gethostbyname (hostname))
  153. {
  154. fprintf (stderr, "Failed to resolve my hostname `%s', testcase not run.\n",
  155. hostname);
  156. return 0;
  157. }
  158. GNUNET_log_setup("test-gnunet-service-manager",
  159. #if VERBOSE
  160. "DEBUG",
  161. #else
  162. "WARNING",
  163. #endif
  164. NULL);
  165. check();
  166. return ret;
  167. }