2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file applications/fs/fsui/recursive_download_test.c
23 * @brief testcase for fsui recursive upload-download
24 * @author Christian Grothoff
25 * @author Heikki Lindholm
29 #include "gnunet_util.h"
30 #include "gnunet_fsui_lib.h"
32 #define DEBUG_VERBOSE GNUNET_EXTRA_LOGGING
34 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
36 #define FILESIZE (1024 * 1024 * 2)
37 /* depth-first directory tree d=dir f=file .=end of level*/
38 #define DIRECTORY_TREE_SPEC "dddf.f.d"
40 static struct GNUNET_GE_Context *ectx;
42 static int download_done;
45 makeName (unsigned int i)
49 fn = GNUNET_malloc (strlen
50 ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") +
53 strlen ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST")
54 + 15, "/tmp/gnunet-fsui-recursive_download_test/FSUITEST%u/",
60 makeHierarchyHelper (const char *current, const char *tree, int index,
69 while (!done && tree[index] != '\0')
71 s = GNUNET_malloc (strlen (current) + strlen (DIR_SEPARATOR_STR) + 14);
72 GNUNET_snprintf (s, strlen (current) + strlen (DIR_SEPARATOR_STR) + 14,
73 "%s%s%u", current, DIR_SEPARATOR_STR, fi);
74 switch (tree[index++])
79 if (GNUNET_disk_directory_test (NULL, s) == GNUNET_NO)
87 GNUNET_disk_directory_create (NULL, s);
90 index = makeHierarchyHelper (s, tree, index, check);
95 /* TODO: compare file contents */
96 if (GNUNET_disk_directory_test (NULL, s) != GNUNET_NO)
104 buf = GNUNET_malloc (FILESIZE);
105 for (i = 0; i < FILESIZE; i++)
106 buf[i] = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 256);
107 GNUNET_disk_file_write (ectx, s, buf, FILESIZE, "600");
124 makeHierarchy (unsigned int i, const char *tree)
129 makeHierarchyHelper (fn, tree, 0, 0);
134 checkHierarchy (unsigned int i, const char *tree)
140 if (GNUNET_disk_directory_test (NULL, fn) != GNUNET_YES)
143 return GNUNET_SYSERR;
146 ((makeHierarchyHelper (fn, tree, 0, 1) ==
147 -1) ? GNUNET_SYSERR : GNUNET_OK);
153 static enum GNUNET_FSUI_EventType lastEvent;
154 static enum GNUNET_FSUI_EventType waitForEvent;
155 static struct GNUNET_FSUI_Context *ctx;
156 static struct GNUNET_ECRS_URI *upURI;
157 static struct GNUNET_FSUI_DownloadList *download;
160 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
164 case GNUNET_FSUI_download_suspended:
167 case GNUNET_FSUI_download_resumed:
168 download = event->data.DownloadResumed.dc.pos;
171 case GNUNET_FSUI_upload_progress:
172 #if DEBUG_VERBOSE > 1
173 printf ("Upload is progressing (%llu/%llu)...\n",
174 event->data.UploadProgress.completed,
175 event->data.UploadProgress.total);
178 case GNUNET_FSUI_upload_completed:
179 upURI = GNUNET_ECRS_uri_duplicate (event->data.UploadCompleted.uri);
181 printf ("Upload of `%s' complete.\n", event->data.UploadCompleted.filename);
184 case GNUNET_FSUI_download_completed:
186 printf ("Download of `%s' complete.\n",
187 event->data.DownloadCompleted.filename);
189 if (checkHierarchy (43, DIRECTORY_TREE_SPEC) == GNUNET_OK)
193 printf ("Hierarchy check not successful yet...\n");
196 case GNUNET_FSUI_download_progress:
197 #if DEBUG_VERBOSE > 1
198 printf ("Download is progressing (%llu/%llu)...\n",
199 event->data.DownloadProgress.completed,
200 event->data.DownloadProgress.total);
203 case GNUNET_FSUI_unindex_progress:
204 #if DEBUG_VERBOSE > 1
205 printf ("Unindex is progressing (%llu/%llu)...\n",
206 event->data.UnindexProgress.completed,
207 event->data.UnindexProgress.total);
210 case GNUNET_FSUI_unindex_completed:
212 printf ("Unindex complete.\n");
215 case GNUNET_FSUI_unindex_error:
216 fprintf (stderr, "Error unindexing: %s\n",
217 event->data.UnindexError.message);
219 case GNUNET_FSUI_upload_error:
220 fprintf (stderr, "Error uploading: %s\n", event->data.UploadError.message);
222 case GNUNET_FSUI_download_error:
223 fprintf (stderr, "Error downloading: %s\n",
224 event->data.DownloadError.message);
226 case GNUNET_FSUI_download_aborted:
228 printf ("Received download aborted event.\n");
231 case GNUNET_FSUI_unindex_suspended:
232 case GNUNET_FSUI_upload_suspended:
233 case GNUNET_FSUI_upload_started:
234 case GNUNET_FSUI_upload_stopped:
235 case GNUNET_FSUI_download_started:
236 case GNUNET_FSUI_download_stopped:
237 case GNUNET_FSUI_unindex_started:
238 case GNUNET_FSUI_unindex_stopped:
241 printf ("Unexpected event: %d\n", event->type);
244 if (lastEvent == waitForEvent)
245 return NULL; /* ignore all other events */
246 lastEvent = event->type;
251 #define START_DAEMON 1
254 main (int argc, char *argv[])
257 struct GNUNET_OS_Process *daemon;
268 struct GNUNET_MetaData *meta = NULL;
269 struct GNUNET_ECRS_URI *kuri = NULL;
270 struct GNUNET_GC_Configuration *cfg;
271 struct GNUNET_FSUI_UploadList *upload = NULL;
274 cfg = GNUNET_GC_create ();
275 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
277 GNUNET_GC_free (cfg);
280 fprintf (stderr, "Setup...\n");
282 GNUNET_disk_directory_remove (NULL,
283 "/tmp/gnunet-fsui-recursive_download_test/");
284 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
285 GNUNET_GE_ASSERT (NULL, daemon != NULL);
287 GNUNET_wait_for_daemon_running (NULL, cfg, 30 * GNUNET_CRON_SECONDS));
288 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
289 /* ACTUAL TEST CODE */
292 GNUNET_FSUI_start (NULL, cfg, "fsuirecursive_download_test", 32,
293 GNUNET_YES, &eventCallback, NULL);
295 fn = makeHierarchy (42, DIRECTORY_TREE_SPEC);
296 meta = GNUNET_meta_data_create ();
298 GNUNET_ECRS_keyword_command_line_to_uri (ectx, 2,
299 (const char **) keywords);
300 fprintf (stderr, "Uploading...\n");
301 waitForEvent = GNUNET_FSUI_upload_completed;
303 GNUNET_FSUI_upload_start (ctx, fn,
304 (GNUNET_FSUI_DirectoryScanCallback) &
305 GNUNET_disk_directory_scan, NULL, 0, 0,
306 GNUNET_YES, GNUNET_NO, GNUNET_NO,
307 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
309 CHECK (upload != NULL);
310 GNUNET_ECRS_uri_destroy (kuri);
313 while (lastEvent != GNUNET_FSUI_upload_completed)
317 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
318 if (GNUNET_shutdown_test () == GNUNET_YES)
321 GNUNET_FSUI_upload_stop (upload);
323 CHECK (upURI != NULL);
325 fprintf (stderr, "Downloading...\n");
326 waitForEvent = GNUNET_FSUI_download_completed;
327 fn43 = makeName (43);
329 GNUNET_FSUI_download_start (ctx, 0, GNUNET_YES, upURI, meta, fn43, NULL,
331 CHECK (download != NULL);
335 while (!download_done)
339 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
340 if (GNUNET_shutdown_test () == GNUNET_YES)
344 fprintf (stderr, "Cleanup...\n");
346 GNUNET_meta_data_destroy (meta);
349 if (download != NULL)
350 GNUNET_FSUI_download_stop (download);
351 GNUNET_FSUI_stop (ctx);
355 GNUNET_disk_directory_remove (NULL, fn);
359 GNUNET_ECRS_uri_destroy (kuri);
360 fn43 = makeName (43);
361 GNUNET_disk_directory_remove (NULL, fn43);
364 GNUNET_ECRS_uri_destroy (upURI);
367 GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
368 GNUNET_OS_process_close (daemon);
371 GNUNET_GC_free (cfg);
372 return (ok == GNUNET_YES) ? 0 : 1;
375 /* end of recursive_download_test.c */