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_NO
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") +
54 ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") + 15,
55 "/tmp/gnunet-fsui-recursive_download_test/FSUITEST%u/", i);
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;
145 res = ((makeHierarchyHelper (fn, tree, 0, 1) == -1) ?
146 GNUNET_SYSERR : GNUNET_OK);
152 static enum GNUNET_FSUI_EventType lastEvent;
153 static enum GNUNET_FSUI_EventType waitForEvent;
154 static struct GNUNET_FSUI_Context *ctx;
155 static struct GNUNET_ECRS_URI *upURI;
156 static struct GNUNET_FSUI_DownloadList *download;
159 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
163 case GNUNET_FSUI_download_suspended:
166 case GNUNET_FSUI_download_resumed:
167 download = event->data.DownloadResumed.dc.pos;
170 case GNUNET_FSUI_upload_progress:
171 #if DEBUG_VERBOSE > 1
172 printf ("Upload is progressing (%llu/%llu)...\n",
173 event->data.UploadProgress.completed,
174 event->data.UploadProgress.total);
177 case GNUNET_FSUI_upload_completed:
178 upURI = GNUNET_ECRS_uri_duplicate (event->data.UploadCompleted.uri);
180 printf ("Upload of `%s' complete.\n", event->data.UploadCompleted.filename);
183 case GNUNET_FSUI_download_completed:
185 printf ("Download of `%s' complete.\n",
186 event->data.DownloadCompleted.filename);
188 if (checkHierarchy (43, DIRECTORY_TREE_SPEC) == GNUNET_OK)
192 printf ("Hierarchy check not successful yet...\n");
195 case GNUNET_FSUI_download_progress:
196 #if DEBUG_VERBOSE > 1
197 printf ("Download is progressing (%llu/%llu)...\n",
198 event->data.DownloadProgress.completed,
199 event->data.DownloadProgress.total);
202 case GNUNET_FSUI_unindex_progress:
203 #if DEBUG_VERBOSE > 1
204 printf ("Unindex is progressing (%llu/%llu)...\n",
205 event->data.UnindexProgress.completed,
206 event->data.UnindexProgress.total);
209 case GNUNET_FSUI_unindex_completed:
211 printf ("Unindex complete.\n");
214 case GNUNET_FSUI_unindex_error:
215 fprintf (stderr, "Error unindexing: %s\n",
216 event->data.UnindexError.message);
218 case GNUNET_FSUI_upload_error:
219 fprintf (stderr, "Error uploading: %s\n", event->data.UploadError.message);
221 case GNUNET_FSUI_download_error:
222 fprintf (stderr, "Error downloading: %s\n",
223 event->data.DownloadError.message);
225 case GNUNET_FSUI_download_aborted:
227 printf ("Received download aborted event.\n");
230 case GNUNET_FSUI_unindex_suspended:
231 case GNUNET_FSUI_upload_suspended:
232 case GNUNET_FSUI_upload_started:
233 case GNUNET_FSUI_upload_stopped:
234 case GNUNET_FSUI_download_started:
235 case GNUNET_FSUI_download_stopped:
236 case GNUNET_FSUI_unindex_started:
237 case GNUNET_FSUI_unindex_stopped:
240 printf ("Unexpected event: %d\n", event->type);
243 if (lastEvent == waitForEvent)
244 return NULL; /* ignore all other events */
245 lastEvent = event->type;
250 #define START_DAEMON 1
253 main (int argc, char *argv[])
256 struct GNUNET_OS_Process *daemon;
267 struct GNUNET_MetaData *meta = NULL;
268 struct GNUNET_ECRS_URI *kuri = NULL;
269 struct GNUNET_GC_Configuration *cfg;
270 struct GNUNET_FSUI_UploadList *upload = NULL;
273 cfg = GNUNET_GC_create ();
274 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
276 GNUNET_GC_free (cfg);
279 fprintf (stderr, "Setup...\n");
281 GNUNET_disk_directory_remove (NULL,
282 "/tmp/gnunet-fsui-recursive_download_test/");
283 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
284 GNUNET_GE_ASSERT (NULL, daemon != NULL);
286 GNUNET_wait_for_daemon_running (NULL, cfg, 30 * GNUNET_CRON_SECONDS));
287 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
288 /* ACTUAL TEST CODE */
290 ctx = GNUNET_FSUI_start (NULL,
291 cfg, "fsuirecursive_download_test", 32, GNUNET_YES,
292 &eventCallback, NULL);
294 fn = makeHierarchy (42, DIRECTORY_TREE_SPEC);
295 meta = GNUNET_meta_data_create ();
297 GNUNET_ECRS_keyword_command_line_to_uri (ectx, 2,
298 (const char **) keywords);
299 fprintf (stderr, "Uploading...\n");
300 waitForEvent = GNUNET_FSUI_upload_completed;
301 upload = GNUNET_FSUI_upload_start (ctx,
303 (GNUNET_FSUI_DirectoryScanCallback) &
304 GNUNET_disk_directory_scan, NULL, 0, 0,
305 GNUNET_YES, GNUNET_NO, GNUNET_NO,
307 5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
308 CHECK (upload != NULL);
309 GNUNET_ECRS_uri_destroy (kuri);
312 while (lastEvent != GNUNET_FSUI_upload_completed)
316 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
317 if (GNUNET_shutdown_test () == GNUNET_YES)
320 GNUNET_FSUI_upload_stop (upload);
322 CHECK (upURI != NULL);
324 fprintf (stderr, "Downloading...\n");
325 waitForEvent = GNUNET_FSUI_download_completed;
326 fn43 = makeName (43);
327 download = GNUNET_FSUI_download_start (ctx,
330 upURI, meta, fn43, NULL, 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 */