test_fs_download_recursive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/recursive_download_test.c
  19. * @brief testcase for fsui recursive upload-download
  20. * @author Christian Grothoff
  21. * @author Heikki Lindholm
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util.h"
  25. #include "gnunet_fsui_lib.h"
  26. #define DEBUG_VERBOSE GNUNET_NO
  27. #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
  28. #define FILESIZE (1024 * 1024 * 2)
  29. /* depth-first directory tree d=dir f=file .=end of level*/
  30. #define DIRECTORY_TREE_SPEC "dddf.f.d"
  31. static struct GNUNET_GE_Context *ectx;
  32. static int download_done;
  33. static char *
  34. makeName (unsigned int i)
  35. {
  36. char *fn;
  37. fn =
  38. GNUNET_malloc (strlen
  39. ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") +
  40. 15);
  41. GNUNET_snprintf (fn,
  42. strlen
  43. ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") + 15,
  44. "/tmp/gnunet-fsui-recursive_download_test/FSUITEST%u/", i);
  45. return fn;
  46. }
  47. static int
  48. makeHierarchyHelper (const char *current, const char *tree, int index,
  49. int check)
  50. {
  51. unsigned int fi, i;
  52. int done;
  53. char *s, *buf;
  54. fi = 0;
  55. done = 0;
  56. while (!done && tree[index] != '\0')
  57. {
  58. s = GNUNET_malloc (strlen (current) + strlen (DIR_SEPARATOR_STR) + 14);
  59. GNUNET_snprintf (s, strlen (current) + strlen (DIR_SEPARATOR_STR) + 14,
  60. "%s%s%u", current, DIR_SEPARATOR_STR, fi);
  61. switch (tree[index++])
  62. {
  63. case 'd':
  64. if (check)
  65. {
  66. if (GNUNET_disk_directory_test (NULL, s) == GNUNET_NO)
  67. {
  68. index = -1;
  69. done = 1;
  70. }
  71. }
  72. else
  73. {
  74. GNUNET_disk_directory_create (NULL, s);
  75. }
  76. if (!done)
  77. index = makeHierarchyHelper (s, tree, index, check);
  78. break;
  79. case 'f':
  80. if (check)
  81. {
  82. /* TODO: compare file contents */
  83. if (GNUNET_disk_directory_test (NULL, s) != GNUNET_NO)
  84. {
  85. index = -1;
  86. done = 1;
  87. }
  88. }
  89. else
  90. {
  91. buf = GNUNET_malloc (FILESIZE);
  92. for (i = 0; i < FILESIZE; i++)
  93. buf[i] = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 256);
  94. GNUNET_disk_file_write (ectx, s, buf, FILESIZE, "600");
  95. GNUNET_free (buf);
  96. }
  97. break;
  98. case '.':
  99. done = 1;
  100. break;
  101. default:
  102. break;
  103. }
  104. GNUNET_free (s);
  105. fi++;
  106. }
  107. return index;
  108. }
  109. static char *
  110. makeHierarchy (unsigned int i, const char *tree)
  111. {
  112. char *fn;
  113. fn = makeName (i);
  114. makeHierarchyHelper (fn, tree, 0, 0);
  115. return fn;
  116. }
  117. static int
  118. checkHierarchy (unsigned int i, const char *tree)
  119. {
  120. char *fn;
  121. int res;
  122. fn = makeName (i);
  123. if (GNUNET_disk_directory_test (NULL, fn) != GNUNET_YES)
  124. {
  125. GNUNET_free (fn);
  126. return GNUNET_SYSERR;
  127. }
  128. res = ((makeHierarchyHelper (fn, tree, 0, 1) == -1) ?
  129. GNUNET_SYSERR : GNUNET_OK);
  130. GNUNET_free (fn);
  131. return res;
  132. }
  133. static enum GNUNET_FSUI_EventType lastEvent;
  134. static enum GNUNET_FSUI_EventType waitForEvent;
  135. static struct GNUNET_FSUI_Context *ctx;
  136. static struct GNUNET_ECRS_URI *upURI;
  137. static struct GNUNET_FSUI_DownloadList *download;
  138. static void *
  139. eventCallback (void *cls, const GNUNET_FSUI_Event * event)
  140. {
  141. switch (event->type)
  142. {
  143. case GNUNET_FSUI_download_suspended:
  144. download = NULL;
  145. break;
  146. case GNUNET_FSUI_download_resumed:
  147. download = event->data.DownloadResumed.dc.pos;
  148. break;
  149. break;
  150. case GNUNET_FSUI_upload_progress:
  151. #if DEBUG_VERBOSE > 1
  152. printf ("Upload is progressing (%llu/%llu)...\n",
  153. event->data.UploadProgress.completed,
  154. event->data.UploadProgress.total);
  155. #endif
  156. break;
  157. case GNUNET_FSUI_upload_completed:
  158. upURI = GNUNET_ECRS_uri_duplicate (event->data.UploadCompleted.uri);
  159. #if DEBUG_VERBOSE
  160. printf ("Upload of `%s' complete.\n",
  161. event->data.UploadCompleted.filename);
  162. #endif
  163. break;
  164. case GNUNET_FSUI_download_completed:
  165. #if DEBUG_VERBOSE
  166. printf ("Download of `%s' complete.\n",
  167. event->data.DownloadCompleted.filename);
  168. #endif
  169. if (checkHierarchy (43, DIRECTORY_TREE_SPEC) == GNUNET_OK)
  170. download_done = 1;
  171. #if DEBUG_VERBOSE
  172. else
  173. printf ("Hierarchy check not successful yet...\n");
  174. #endif
  175. break;
  176. case GNUNET_FSUI_download_progress:
  177. #if DEBUG_VERBOSE > 1
  178. printf ("Download is progressing (%llu/%llu)...\n",
  179. event->data.DownloadProgress.completed,
  180. event->data.DownloadProgress.total);
  181. #endif
  182. break;
  183. case GNUNET_FSUI_unindex_progress:
  184. #if DEBUG_VERBOSE > 1
  185. printf ("Unindex is progressing (%llu/%llu)...\n",
  186. event->data.UnindexProgress.completed,
  187. event->data.UnindexProgress.total);
  188. #endif
  189. break;
  190. case GNUNET_FSUI_unindex_completed:
  191. #if DEBUG_VERBOSE
  192. printf ("Unindex complete.\n");
  193. #endif
  194. break;
  195. case GNUNET_FSUI_unindex_error:
  196. fprintf (stderr, "Error unindexing: %s\n",
  197. event->data.UnindexError.message);
  198. break;
  199. case GNUNET_FSUI_upload_error:
  200. fprintf (stderr, "Error uploading: %s\n",
  201. event->data.UploadError.message);
  202. break;
  203. case GNUNET_FSUI_download_error:
  204. fprintf (stderr, "Error downloading: %s\n",
  205. event->data.DownloadError.message);
  206. break;
  207. case GNUNET_FSUI_download_aborted:
  208. #if DEBUG_VERBOSE
  209. printf ("Received download aborted event.\n");
  210. #endif
  211. break;
  212. case GNUNET_FSUI_unindex_suspended:
  213. case GNUNET_FSUI_upload_suspended:
  214. case GNUNET_FSUI_upload_started:
  215. case GNUNET_FSUI_upload_stopped:
  216. case GNUNET_FSUI_download_started:
  217. case GNUNET_FSUI_download_stopped:
  218. case GNUNET_FSUI_unindex_started:
  219. case GNUNET_FSUI_unindex_stopped:
  220. break;
  221. default:
  222. printf ("Unexpected event: %d\n", event->type);
  223. break;
  224. }
  225. if (lastEvent == waitForEvent)
  226. return NULL; /* ignore all other events */
  227. lastEvent = event->type;
  228. return NULL;
  229. }
  230. #define START_DAEMON 1
  231. int
  232. main (int argc, char *argv[])
  233. {
  234. #if START_DAEMON
  235. struct GNUNET_OS_Process *daemon;
  236. #endif
  237. int ok;
  238. char *fn = NULL;
  239. char *fn43 = NULL;
  240. char *keywords[] = {
  241. "down_foo",
  242. "down_bar",
  243. };
  244. int prog;
  245. struct GNUNET_MetaData *meta = NULL;
  246. struct GNUNET_ECRS_URI *kuri = NULL;
  247. struct GNUNET_GC_Configuration *cfg;
  248. struct GNUNET_FSUI_UploadList *upload = NULL;
  249. ok = GNUNET_YES;
  250. cfg = GNUNET_GC_create ();
  251. if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
  252. {
  253. GNUNET_GC_free (cfg);
  254. return -1;
  255. }
  256. fprintf(stderr,
  257. "Setup...\n");
  258. #if START_DAEMON
  259. GNUNET_disk_directory_remove (NULL,
  260. "/tmp/gnunet-fsui-recursive_download_test/");
  261. daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
  262. GNUNET_GE_ASSERT (NULL, daemon != NULL);
  263. CHECK (GNUNET_OK ==
  264. GNUNET_wait_for_daemon_running (NULL, cfg,
  265. 30 * GNUNET_CRON_SECONDS));
  266. GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
  267. /* ACTUAL TEST CODE */
  268. #endif
  269. ctx = GNUNET_FSUI_start (NULL,
  270. cfg, "fsuirecursive_download_test", 32, GNUNET_YES,
  271. &eventCallback, NULL);
  272. CHECK (ctx != NULL);
  273. fn = makeHierarchy (42, DIRECTORY_TREE_SPEC);
  274. meta = GNUNET_meta_data_create ();
  275. kuri =
  276. GNUNET_ECRS_keyword_command_line_to_uri (ectx, 2,
  277. (const char **) keywords);
  278. fprintf(stderr,
  279. "Uploading...\n");
  280. waitForEvent = GNUNET_FSUI_upload_completed;
  281. upload = GNUNET_FSUI_upload_start (ctx,
  282. fn,
  283. (GNUNET_FSUI_DirectoryScanCallback) &
  284. GNUNET_disk_directory_scan, NULL, 0, 0,
  285. GNUNET_YES, GNUNET_NO, GNUNET_NO,
  286. GNUNET_get_time () +
  287. 5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
  288. CHECK (upload != NULL);
  289. GNUNET_ECRS_uri_destroy (kuri);
  290. kuri = NULL;
  291. prog = 0;
  292. while (lastEvent != GNUNET_FSUI_upload_completed)
  293. {
  294. prog++;
  295. CHECK (prog < 5000);
  296. GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  297. if (GNUNET_shutdown_test () == GNUNET_YES)
  298. break;
  299. }
  300. GNUNET_FSUI_upload_stop (upload);
  301. upload = NULL;
  302. CHECK (upURI != NULL);
  303. fprintf(stderr,
  304. "Downloading...\n");
  305. waitForEvent = GNUNET_FSUI_download_completed;
  306. fn43 = makeName (43);
  307. download = GNUNET_FSUI_download_start (ctx,
  308. 0,
  309. GNUNET_YES,
  310. upURI, meta, fn43, NULL, NULL);
  311. CHECK (download != NULL);
  312. GNUNET_free (fn43);
  313. fn43 = NULL;
  314. prog = 0;
  315. while (!download_done)
  316. {
  317. prog++;
  318. CHECK (prog < 5000);
  319. GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
  320. if (GNUNET_shutdown_test () == GNUNET_YES)
  321. break;
  322. }
  323. FAILURE:
  324. fprintf(stderr,
  325. "Cleanup...\n");
  326. if (meta != NULL)
  327. GNUNET_meta_data_destroy (meta);
  328. if (ctx != NULL)
  329. {
  330. if (download != NULL)
  331. GNUNET_FSUI_download_stop (download);
  332. GNUNET_FSUI_stop (ctx);
  333. }
  334. if (fn != NULL)
  335. {
  336. GNUNET_disk_directory_remove (NULL, fn);
  337. GNUNET_free (fn);
  338. }
  339. if (kuri != NULL)
  340. GNUNET_ECRS_uri_destroy (kuri);
  341. fn43 = makeName (43);
  342. GNUNET_disk_directory_remove (NULL, fn43);
  343. GNUNET_free (fn43);
  344. if (upURI != NULL)
  345. GNUNET_ECRS_uri_destroy (upURI);
  346. #if START_DAEMON
  347. GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
  348. GNUNET_OS_process_close (daemon);
  349. daemon = NULL;
  350. #endif
  351. GNUNET_GC_free (cfg);
  352. return (ok == GNUNET_YES) ? 0 : 1;
  353. }
  354. /* end of recursive_download_test.c */