test_fs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008 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 applications/fs/fsui/basic_fsui_test.c
  19. * @brief testcase for fsui (upload-search-download-unindex)
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util.h"
  24. #include "gnunet_fsui_lib.h"
  25. #define DEBUG_VERBOSE GNUNET_NO
  26. #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(NULL, 0); goto FAILURE; }
  27. static char *
  28. makeName (unsigned int i)
  29. {
  30. char *fn;
  31. fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
  32. 14);
  33. GNUNET_snprintf (fn,
  34. strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
  35. "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
  36. GNUNET_disk_directory_create_for_file (NULL, fn);
  37. return fn;
  38. }
  39. static enum GNUNET_FSUI_EventType lastEvent;
  40. static struct GNUNET_MetaData *search_meta;
  41. static struct GNUNET_ECRS_URI *search_uri;
  42. static struct GNUNET_FSUI_Context *ctx;
  43. static void *
  44. eventCallback (void *cls, const GNUNET_FSUI_Event * event)
  45. {
  46. static char unused;
  47. switch (event->type)
  48. {
  49. case GNUNET_FSUI_search_resumed:
  50. case GNUNET_FSUI_download_resumed:
  51. case GNUNET_FSUI_upload_resumed:
  52. case GNUNET_FSUI_unindex_resumed:
  53. return &unused;
  54. case GNUNET_FSUI_search_result:
  55. #if DEBUG_VERBOSE
  56. printf ("Received search result\n");
  57. #endif
  58. search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
  59. search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
  60. break;
  61. case GNUNET_FSUI_upload_completed:
  62. #if DEBUG_VERBOSE
  63. printf ("Upload complete.\n");
  64. #endif
  65. break;
  66. case GNUNET_FSUI_download_completed:
  67. #if DEBUG_VERBOSE
  68. printf ("Download complete.\n");
  69. #endif
  70. break;
  71. case GNUNET_FSUI_unindex_completed:
  72. #if DEBUG_VERBOSE
  73. printf ("Unindex complete.\n");
  74. #endif
  75. break;
  76. default:
  77. break;
  78. }
  79. lastEvent = event->type;
  80. return NULL;
  81. }
  82. #define START_DAEMON 1
  83. int
  84. main (int argc, char *argv[])
  85. {
  86. #if START_DAEMON
  87. struct GNUNET_OS_Process *daemon;
  88. #endif
  89. int ok;
  90. struct GNUNET_ECRS_URI *uri;
  91. char *filename = NULL;
  92. char *keywords[] = {
  93. "fsui_foo",
  94. "fsui_bar",
  95. };
  96. char keyword[40];
  97. char *fn;
  98. int prog;
  99. struct GNUNET_MetaData *meta;
  100. struct GNUNET_ECRS_URI *kuri;
  101. struct GNUNET_GC_Configuration *cfg;
  102. struct GNUNET_FSUI_UploadList *upload = NULL;
  103. struct GNUNET_FSUI_SearchList *search = NULL;
  104. struct GNUNET_FSUI_UnindexList *unindex = NULL;
  105. struct GNUNET_FSUI_DownloadList *download = NULL;
  106. cfg = GNUNET_GC_create ();
  107. if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
  108. {
  109. GNUNET_GC_free (cfg);
  110. return -1;
  111. }
  112. #if START_DAEMON
  113. daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
  114. GNUNET_GE_ASSERT (NULL, daemon != NULL);
  115. CHECK (GNUNET_OK ==
  116. GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
  117. #endif
  118. GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
  119. ok = GNUNET_YES;
  120. /* ACTUAL TEST CODE */
  121. ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */
  122. GNUNET_NO, /* no resume */
  123. &eventCallback, NULL);
  124. CHECK (ctx != NULL);
  125. filename = makeName (42);
  126. GNUNET_disk_file_write (NULL, filename, "foo bar test!",
  127. strlen ("foo bar test!"), "600");
  128. meta = GNUNET_meta_data_create ();
  129. kuri =
  130. GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
  131. (const char **) keywords);
  132. /* upload */
  133. upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
  134. 0, /* priority */
  135. GNUNET_YES, GNUNET_NO, GNUNET_NO,
  136. GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
  137. meta, kuri, kuri);
  138. CHECK (upload != NULL);
  139. GNUNET_ECRS_uri_destroy (kuri);
  140. GNUNET_meta_data_destroy (meta);
  141. prog = 0;
  142. while (lastEvent != GNUNET_FSUI_upload_completed)
  143. {
  144. prog++;
  145. CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  146. if (GNUNET_shutdown_test () == GNUNET_YES)
  147. break;
  148. }
  149. /* search */
  150. GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
  151. uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
  152. search = GNUNET_FSUI_search_start (ctx, 0, uri);
  153. GNUNET_ECRS_uri_destroy (uri);
  154. CHECK (search != NULL);
  155. prog = 0;
  156. while (lastEvent != GNUNET_FSUI_search_result)
  157. {
  158. prog++;
  159. CHECK (prog < 10000);
  160. GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  161. if (GNUNET_shutdown_test () == GNUNET_YES)
  162. break;
  163. }
  164. GNUNET_FSUI_search_abort (search);
  165. GNUNET_FSUI_search_stop (search);
  166. /* download */
  167. fn = makeName (43);
  168. download =
  169. GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
  170. fn, NULL, NULL);
  171. GNUNET_free (fn);
  172. prog = 0;
  173. while (lastEvent != GNUNET_FSUI_download_completed)
  174. {
  175. prog++;
  176. CHECK (prog < 10000);
  177. GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  178. if (GNUNET_shutdown_test () == GNUNET_YES)
  179. break;
  180. }
  181. GNUNET_FSUI_download_stop (download);
  182. download = NULL;
  183. GNUNET_ECRS_uri_destroy (search_uri);
  184. GNUNET_meta_data_destroy (search_meta);
  185. /* unindex */
  186. unindex = GNUNET_FSUI_unindex_start (ctx, filename);
  187. prog = 0;
  188. while (lastEvent != GNUNET_FSUI_unindex_completed)
  189. {
  190. prog++;
  191. CHECK (prog < 10000);
  192. GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  193. if (GNUNET_shutdown_test () == GNUNET_YES)
  194. break;
  195. }
  196. if (lastEvent != GNUNET_FSUI_unindex_completed)
  197. GNUNET_FSUI_unindex_abort (unindex);
  198. GNUNET_FSUI_unindex_stop (unindex);
  199. /* END OF TEST CODE */
  200. FAILURE:
  201. if (ctx != NULL)
  202. GNUNET_FSUI_stop (ctx);
  203. if (filename != NULL)
  204. {
  205. UNLINK (filename);
  206. GNUNET_free (filename);
  207. }
  208. if (download != NULL)
  209. {
  210. GNUNET_FSUI_download_abort (download);
  211. GNUNET_FSUI_download_stop (download);
  212. }
  213. filename = makeName (43);
  214. /* TODO: verify file 'filename(42)' == file 'filename(43)' */
  215. UNLINK (filename);
  216. GNUNET_free (filename);
  217. #if START_DAEMON
  218. GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
  219. GNUNET_OS_process_close (daemon);
  220. #endif
  221. GNUNET_GC_free (cfg);
  222. return (ok == GNUNET_YES) ? 0 : 1;
  223. }
  224. /* end of basic_fsui_test.c */