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 volatile int download_done;
45 makeName (unsigned int i)
51 ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") +
55 ("/tmp/gnunet-fsui-recursive_download_test/FSUITEST") + 15,
56 "/tmp/gnunet-fsui-recursive_download_test/FSUITEST%u/", i);
61 makeHierarchyHelper (const char *current, const char *tree, int index,
70 while (!done && tree[index] != '\0')
72 s = GNUNET_malloc (strlen (current) + strlen (DIR_SEPARATOR_STR) + 14);
73 GNUNET_snprintf (s, strlen (current) + strlen (DIR_SEPARATOR_STR) + 14,
74 "%s%s%u", current, DIR_SEPARATOR_STR, fi);
75 switch (tree[index++])
80 if (GNUNET_disk_directory_test (NULL, s) == GNUNET_NO)
88 GNUNET_disk_directory_create (NULL, s);
91 index = makeHierarchyHelper (s, tree, index, check);
96 /* TODO: compare file contents */
97 if (GNUNET_disk_directory_test (NULL, s) != GNUNET_NO)
105 buf = GNUNET_malloc (FILESIZE);
106 for (i = 0; i < FILESIZE; i++)
107 buf[i] = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 256);
108 GNUNET_disk_file_write (ectx, s, buf, FILESIZE, "600");
125 makeHierarchy (unsigned int i, const char *tree)
130 makeHierarchyHelper (fn, tree, 0, 0);
135 checkHierarchy (unsigned int i, const char *tree)
141 if (GNUNET_disk_directory_test (NULL, fn) != GNUNET_YES)
144 return GNUNET_SYSERR;
146 res = ((makeHierarchyHelper (fn, tree, 0, 1) == -1) ?
147 GNUNET_SYSERR : GNUNET_OK);
153 static volatile enum GNUNET_FSUI_EventType lastEvent;
154 static volatile 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",
182 event->data.UploadCompleted.filename);
185 case GNUNET_FSUI_download_completed:
187 printf ("Download of `%s' complete.\n",
188 event->data.DownloadCompleted.filename);
190 if (checkHierarchy (43, DIRECTORY_TREE_SPEC) == GNUNET_OK)
194 printf ("Hierarchy check not successful yet...\n");
197 case GNUNET_FSUI_download_progress:
198 #if DEBUG_VERBOSE > 1
199 printf ("Download is progressing (%llu/%llu)...\n",
200 event->data.DownloadProgress.completed,
201 event->data.DownloadProgress.total);
204 case GNUNET_FSUI_unindex_progress:
205 #if DEBUG_VERBOSE > 1
206 printf ("Unindex is progressing (%llu/%llu)...\n",
207 event->data.UnindexProgress.completed,
208 event->data.UnindexProgress.total);
211 case GNUNET_FSUI_unindex_completed:
213 printf ("Unindex complete.\n");
216 case GNUNET_FSUI_unindex_error:
217 fprintf (stderr, "Error unindexing: %s\n",
218 event->data.UnindexError.message);
220 case GNUNET_FSUI_upload_error:
221 fprintf (stderr, "Error uploading: %s\n",
222 event->data.UploadError.message);
224 case GNUNET_FSUI_download_error:
225 fprintf (stderr, "Error downloading: %s\n",
226 event->data.DownloadError.message);
228 case GNUNET_FSUI_download_aborted:
230 printf ("Received download aborted event.\n");
233 case GNUNET_FSUI_unindex_suspended:
234 case GNUNET_FSUI_upload_suspended:
235 case GNUNET_FSUI_upload_started:
236 case GNUNET_FSUI_upload_stopped:
237 case GNUNET_FSUI_download_started:
238 case GNUNET_FSUI_download_stopped:
239 case GNUNET_FSUI_unindex_started:
240 case GNUNET_FSUI_unindex_stopped:
243 printf ("Unexpected event: %d\n", event->type);
246 if (lastEvent == waitForEvent)
247 return NULL; /* ignore all other events */
248 lastEvent = event->type;
253 #define START_DAEMON 1
256 main (int argc, char *argv[])
269 struct GNUNET_MetaData *meta = NULL;
270 struct GNUNET_ECRS_URI *kuri = NULL;
271 struct GNUNET_GC_Configuration *cfg;
272 struct GNUNET_FSUI_UploadList *upload = NULL;
275 cfg = GNUNET_GC_create ();
276 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
278 GNUNET_GC_free (cfg);
284 GNUNET_disk_directory_remove (NULL,
285 "/tmp/gnunet-fsui-recursive_download_test/");
286 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
287 GNUNET_GE_ASSERT (NULL, daemon > 0);
289 GNUNET_wait_for_daemon_running (NULL, cfg,
290 30 * GNUNET_CRON_SECONDS));
291 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
292 /* ACTUAL TEST CODE */
294 ctx = GNUNET_FSUI_start (NULL,
295 cfg, "fsuirecursive_download_test", 32, GNUNET_YES,
296 &eventCallback, NULL);
298 fn = makeHierarchy (42, DIRECTORY_TREE_SPEC);
299 meta = GNUNET_meta_data_create ();
301 GNUNET_ECRS_keyword_command_line_to_uri (ectx, 2,
302 (const char **) keywords);
305 waitForEvent = GNUNET_FSUI_upload_completed;
306 upload = GNUNET_FSUI_upload_start (ctx,
308 (GNUNET_FSUI_DirectoryScanCallback) &
309 GNUNET_disk_directory_scan, NULL, 0, 0,
310 GNUNET_YES, GNUNET_NO, GNUNET_NO,
312 5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
313 CHECK (upload != NULL);
314 GNUNET_ECRS_uri_destroy (kuri);
317 while (lastEvent != GNUNET_FSUI_upload_completed)
321 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
322 if (GNUNET_shutdown_test () == GNUNET_YES)
325 GNUNET_FSUI_upload_stop (upload);
327 CHECK (upURI != NULL);
331 waitForEvent = GNUNET_FSUI_download_completed;
332 fn43 = makeName (43);
333 download = GNUNET_FSUI_download_start (ctx,
336 upURI, meta, fn43, NULL, NULL);
337 CHECK (download != NULL);
341 while (!download_done)
345 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
346 if (GNUNET_shutdown_test () == GNUNET_YES)
353 GNUNET_meta_data_destroy (meta);
356 if (download != NULL)
357 GNUNET_FSUI_download_stop (download);
358 GNUNET_FSUI_stop (ctx);
362 GNUNET_disk_directory_remove (NULL, fn);
366 GNUNET_ECRS_uri_destroy (kuri);
367 fn43 = makeName (43);
368 GNUNET_disk_directory_remove (NULL, fn43);
371 GNUNET_ECRS_uri_destroy (upURI);
374 GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
376 GNUNET_GC_free (cfg);
377 return (ok == GNUNET_YES) ? 0 : 1;
380 /* end of recursive_download_test.c */