test_statistics_api.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2012, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file statistics/test_statistics_api.c
  18. * @brief testcase for statistics_api.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_statistics_service.h"
  24. static struct GNUNET_STATISTICS_Handle *h;
  25. static struct GNUNET_STATISTICS_GetHandle *g;
  26. static void
  27. do_shutdown ()
  28. {
  29. if (NULL != g)
  30. {
  31. GNUNET_STATISTICS_get_cancel (g);
  32. g = NULL;
  33. }
  34. GNUNET_STATISTICS_destroy (h, GNUNET_NO);
  35. h = NULL;
  36. }
  37. static int
  38. check_1 (void *cls,
  39. const char *subsystem,
  40. const char *name,
  41. uint64_t value,
  42. int is_persistent)
  43. {
  44. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  45. "Received value %llu for `%s:%s\n",
  46. (unsigned long long) value,
  47. subsystem,
  48. name);
  49. GNUNET_assert (0 == strcmp (name, "test-1"));
  50. GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
  51. GNUNET_assert (value == 1);
  52. GNUNET_assert (is_persistent == GNUNET_NO);
  53. return GNUNET_OK;
  54. }
  55. static int
  56. check_2 (void *cls,
  57. const char *subsystem,
  58. const char *name,
  59. uint64_t value,
  60. int is_persistent)
  61. {
  62. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  63. "Received value %llu for `%s:%s\n",
  64. (unsigned long long) value,
  65. subsystem,
  66. name);
  67. GNUNET_assert (0 == strcmp (name, "test-2"));
  68. GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
  69. GNUNET_assert (value == 2);
  70. GNUNET_assert (is_persistent == GNUNET_NO);
  71. return GNUNET_OK;
  72. }
  73. static int
  74. check_3 (void *cls,
  75. const char *subsystem,
  76. const char *name,
  77. uint64_t value,
  78. int is_persistent)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  81. "Received value %llu for `%s:%s\n",
  82. (unsigned long long) value,
  83. subsystem,
  84. name);
  85. GNUNET_assert (0 == strcmp (name, "test-3"));
  86. GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
  87. GNUNET_assert (value == 3);
  88. GNUNET_assert (is_persistent == GNUNET_YES);
  89. return GNUNET_OK;
  90. }
  91. static void
  92. next_fin (void *cls,
  93. int success)
  94. {
  95. int *ok = cls;
  96. g = NULL;
  97. GNUNET_SCHEDULER_shutdown ();
  98. GNUNET_assert (success == GNUNET_OK);
  99. *ok = 0;
  100. }
  101. static void
  102. next (void *cls, int success)
  103. {
  104. g = NULL;
  105. GNUNET_assert (success == GNUNET_OK);
  106. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  107. "Issuing GET request\n");
  108. GNUNET_break (NULL !=
  109. GNUNET_STATISTICS_get (h, NULL, "test-2",
  110. &next_fin,
  111. &check_2, cls));
  112. }
  113. static void
  114. run (void *cls,
  115. char *const *args,
  116. const char *cfgfile,
  117. const struct GNUNET_CONFIGURATION_Handle *cfg)
  118. {
  119. h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
  120. if (NULL == h)
  121. {
  122. GNUNET_break (0);
  123. return;
  124. }
  125. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  126. NULL);
  127. GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
  128. GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
  129. GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
  130. GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
  131. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  132. "Issuing GET request\n");
  133. GNUNET_break (NULL !=
  134. (g = GNUNET_STATISTICS_get (h, NULL, "test-1",
  135. &next,
  136. &check_1, cls)));
  137. }
  138. static void
  139. run_more (void *cls,
  140. char *const *args,
  141. const char *cfgfile,
  142. const struct GNUNET_CONFIGURATION_Handle *cfg)
  143. {
  144. h = GNUNET_STATISTICS_create ("test-statistics-api",
  145. cfg);
  146. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  147. NULL);
  148. GNUNET_break (NULL !=
  149. (g = GNUNET_STATISTICS_get (h, NULL,
  150. "test-3",
  151. &next_fin,
  152. &check_3, cls)));
  153. }
  154. int
  155. main (int argc, char *argv_ign[])
  156. {
  157. int ok = 1;
  158. char *const argv[] = { "test-statistics-api",
  159. "-c",
  160. "test_statistics_api_data.conf",
  161. "-L", "WARNING",
  162. NULL };
  163. struct GNUNET_GETOPT_CommandLineOption options[] = {
  164. GNUNET_GETOPT_OPTION_END
  165. };
  166. struct GNUNET_OS_Process *proc;
  167. char *binary;
  168. GNUNET_log_setup ("test_statistics_api",
  169. "WARNING",
  170. NULL);
  171. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
  172. proc =
  173. GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
  174. | GNUNET_OS_USE_PIPE_CONTROL,
  175. NULL, NULL, NULL,
  176. binary,
  177. "gnunet-service-statistics",
  178. "-c", "test_statistics_api_data.conf", NULL);
  179. GNUNET_assert (NULL != proc);
  180. GNUNET_PROGRAM_run (5, argv,
  181. "test-statistics-api", "nohelp",
  182. options, &run,
  183. &ok);
  184. if (0 != GNUNET_OS_process_kill (proc,
  185. GNUNET_TERM_SIG))
  186. {
  187. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  188. ok = 1;
  189. }
  190. GNUNET_OS_process_wait (proc);
  191. GNUNET_OS_process_destroy (proc);
  192. proc = NULL;
  193. if (ok != 0)
  194. {
  195. GNUNET_free (binary);
  196. return ok;
  197. }
  198. ok = 1;
  199. /* restart to check persistence! */
  200. proc =
  201. GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
  202. | GNUNET_OS_USE_PIPE_CONTROL,
  203. NULL, NULL, NULL,
  204. binary,
  205. "gnunet-service-statistics",
  206. "-c", "test_statistics_api_data.conf",
  207. NULL);
  208. GNUNET_PROGRAM_run (5, argv,
  209. "test-statistics-api", "nohelp",
  210. options,
  211. &run_more, &ok);
  212. if (0 != GNUNET_OS_process_kill (proc,
  213. GNUNET_TERM_SIG))
  214. {
  215. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  216. ok = 1;
  217. }
  218. GNUNET_OS_process_wait (proc);
  219. GNUNET_OS_process_destroy (proc);
  220. proc = NULL;
  221. GNUNET_free (binary);
  222. return ok;
  223. }
  224. /* end of test_statistics_api.c */