test_testbed_logger_api.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 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 testbed-logger/test_testbed_logger_api.c
  18. * @brief testcases for the testbed logger api
  19. * @author Sree Harsha Totakura
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_lib.h"
  24. #include "gnunet_testbed_logger_service.h"
  25. /**
  26. * Generic logging shortcut
  27. */
  28. #define LOG(kind,...) \
  29. GNUNET_log (kind, __VA_ARGS__)
  30. /**
  31. * Relative time seconds shorthand
  32. */
  33. #define TIME_REL_SECS(sec) \
  34. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
  35. /**
  36. * Opaque handle for the logging service
  37. */
  38. static struct GNUNET_TESTBED_LOGGER_Handle *h;
  39. static struct GNUNET_TESTING_Peer *peer;
  40. static char *search_dir;
  41. /**
  42. * Abort task identifier
  43. */
  44. static struct GNUNET_SCHEDULER_Task *abort_task;
  45. static struct GNUNET_SCHEDULER_Task *write_task;
  46. static int result;
  47. #define CANCEL_TASK(task) do { \
  48. if (NULL != task) \
  49. { \
  50. GNUNET_SCHEDULER_cancel (task); \
  51. task = NULL; \
  52. } \
  53. } while (0)
  54. /**
  55. * shortcut to exit during failure
  56. */
  57. #define FAIL_TEST(cond, ret) do { \
  58. if (!(cond)) { \
  59. GNUNET_break(0); \
  60. CANCEL_TASK (abort_task); \
  61. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
  62. ret; \
  63. } \
  64. } while (0)
  65. /**
  66. * Shutdown nicely
  67. *
  68. * @param cls NULL
  69. * @param tc the task context
  70. */
  71. static void
  72. shutdown_now ()
  73. {
  74. CANCEL_TASK (abort_task);
  75. CANCEL_TASK (write_task);
  76. GNUNET_free_non_null (search_dir);
  77. if (NULL != h)
  78. GNUNET_TESTBED_LOGGER_disconnect (h);
  79. GNUNET_SCHEDULER_shutdown ();
  80. }
  81. static void
  82. do_abort (void *cls)
  83. {
  84. LOG (GNUNET_ERROR_TYPE_WARNING,
  85. "Aborting\n");
  86. abort_task = NULL;
  87. shutdown_now ();
  88. }
  89. #define BSIZE 1024
  90. /**
  91. * Function called to iterate over a directory.
  92. *
  93. * @param cls closure
  94. * @param filename complete filename (absolute path)
  95. * @return #GNUNET_OK to continue to iterate,
  96. * #GNUNET_NO to stop iteration with no error,
  97. * #GNUNET_SYSERR to abort iteration with error!
  98. */
  99. static int
  100. iterator_cb (void *cls,
  101. const char *filename)
  102. {
  103. const char *fn;
  104. size_t len;
  105. uint64_t fs;
  106. LOG (GNUNET_ERROR_TYPE_DEBUG,
  107. "Iterator sees file %s\n",
  108. filename);
  109. len = strlen (filename);
  110. fn = filename + len;
  111. if (0 != strcasecmp (".dat", fn - 4))
  112. return GNUNET_OK;
  113. if (GNUNET_OK !=
  114. GNUNET_DISK_file_size (filename,
  115. &fs,
  116. GNUNET_NO,
  117. GNUNET_YES))
  118. {
  119. LOG (GNUNET_ERROR_TYPE_DEBUG,
  120. "Failed to obtain file size for file %s\n",
  121. filename);
  122. return GNUNET_SYSERR;
  123. }
  124. if ((BSIZE * 2) != fs)
  125. {
  126. LOG (GNUNET_ERROR_TYPE_DEBUG,
  127. "Unexpected file size for file %s\n",
  128. filename);
  129. /* The file size should be equal to what we
  130. have written */
  131. return GNUNET_SYSERR;
  132. }
  133. result = GNUNET_OK;
  134. return GNUNET_OK;
  135. }
  136. /**
  137. * Functions of this type are called to notify a successful
  138. * transmission of the message to the logger service
  139. *
  140. * @param cls the closure given to GNUNET_TESTBED_LOGGER_send()
  141. * @param size the amount of data sent
  142. */
  143. static void
  144. flush_comp (void *cls,
  145. size_t size)
  146. {
  147. LOG (GNUNET_ERROR_TYPE_DEBUG,
  148. "Flush running\n");
  149. FAIL_TEST (&write_task == cls,
  150. return);
  151. FAIL_TEST ((BSIZE * 2) == size,
  152. return);
  153. FAIL_TEST (GNUNET_OK ==
  154. GNUNET_TESTING_peer_stop (peer),
  155. return);
  156. LOG (GNUNET_ERROR_TYPE_DEBUG,
  157. "Peer stopped, scanning %s\n",
  158. search_dir);
  159. FAIL_TEST (GNUNET_SYSERR !=
  160. GNUNET_DISK_directory_scan (search_dir,
  161. &iterator_cb,
  162. NULL),
  163. return);
  164. shutdown_now ();
  165. }
  166. static void
  167. do_write (void *cls)
  168. {
  169. static int i;
  170. char buf[BSIZE];
  171. write_task = NULL;
  172. LOG (GNUNET_ERROR_TYPE_DEBUG,
  173. "Write task running\n");
  174. if (0 == i)
  175. write_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(1),
  176. &do_write,
  177. NULL);
  178. (void) memset (buf, i, BSIZE);
  179. GNUNET_TESTBED_LOGGER_write (h,
  180. buf,
  181. BSIZE);
  182. if (0 == i++)
  183. return;
  184. GNUNET_TESTBED_LOGGER_flush (h,
  185. &flush_comp,
  186. &write_task);
  187. }
  188. /**
  189. * Signature of the 'main' function for a (single-peer) testcase that
  190. * is run using #GNUNET_TESTING_peer_run().
  191. *
  192. * @param cls closure
  193. * @param cfg configuration of the peer that was started
  194. * @param peer identity of the peer that was created
  195. */
  196. static void
  197. test_main (void *cls,
  198. const struct GNUNET_CONFIGURATION_Handle *cfg,
  199. struct GNUNET_TESTING_Peer *p)
  200. {
  201. LOG (GNUNET_ERROR_TYPE_DEBUG,
  202. "Connecting to logger\n");
  203. FAIL_TEST (NULL != (h = GNUNET_TESTBED_LOGGER_connect (cfg)),
  204. return);
  205. FAIL_TEST (GNUNET_OK ==
  206. GNUNET_CONFIGURATION_get_value_filename (cfg,
  207. "testbed-logger",
  208. "dir",
  209. &search_dir),
  210. return);
  211. peer = p;
  212. write_task = GNUNET_SCHEDULER_add_now (&do_write,
  213. NULL);
  214. abort_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (10),
  215. &do_abort,
  216. NULL);
  217. }
  218. /**
  219. * Main function
  220. */
  221. int
  222. main (int argc, char **argv)
  223. {
  224. int ret;
  225. result = GNUNET_SYSERR;
  226. GNUNET_log_setup ("test-testbed-logger-api",
  227. "WARNING",
  228. NULL);
  229. GNUNET_break (GNUNET_OK ==
  230. GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
  231. ret = GNUNET_TESTING_service_run ("test-testbed-logger",
  232. "testbed-logger",
  233. "test_testbed_logger_api.conf",
  234. &test_main,
  235. NULL);
  236. GNUNET_break (GNUNET_OK ==
  237. GNUNET_DISK_directory_remove ("/tmp/test-testbed"));
  238. if (0 != ret)
  239. return 1;
  240. if (GNUNET_OK != result)
  241. return 2;
  242. return 0;
  243. }