2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2008 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
18 * @brief testcase for FS (upload-search-download-unindex)
19 * @author Christian Grothoff
23 #include "gnunet_util.h"
24 #include "gnunet_fsui_lib.h"
26 #define DEBUG_VERBOSE GNUNET_NO
28 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(NULL, 0); goto FAILURE; }
31 makeName (unsigned int i)
35 fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
38 strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
39 "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
40 GNUNET_disk_directory_create_for_file (NULL, fn);
44 static enum GNUNET_FSUI_EventType lastEvent;
46 static struct GNUNET_MetaData *search_meta;
48 static struct GNUNET_ECRS_URI *search_uri;
50 static struct GNUNET_FSUI_Context *ctx;
53 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
59 case GNUNET_FSUI_search_resumed:
60 case GNUNET_FSUI_download_resumed:
61 case GNUNET_FSUI_upload_resumed:
62 case GNUNET_FSUI_unindex_resumed:
64 case GNUNET_FSUI_search_result:
66 printf ("Received search result\n");
68 search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
69 search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
71 case GNUNET_FSUI_upload_completed:
73 printf ("Upload complete.\n");
76 case GNUNET_FSUI_download_completed:
78 printf ("Download complete.\n");
81 case GNUNET_FSUI_unindex_completed:
83 printf ("Unindex complete.\n");
89 lastEvent = event->type;
93 #define START_DAEMON 1
96 main (int argc, char *argv[])
99 struct GNUNET_OS_Process *daemon;
102 struct GNUNET_ECRS_URI *uri;
103 char *filename = NULL;
112 struct GNUNET_MetaData *meta;
113 struct GNUNET_ECRS_URI *kuri;
114 struct GNUNET_GC_Configuration *cfg;
115 struct GNUNET_FSUI_UploadList *upload = NULL;
116 struct GNUNET_FSUI_SearchList *search = NULL;
117 struct GNUNET_FSUI_UnindexList *unindex = NULL;
118 struct GNUNET_FSUI_DownloadList *download = NULL;
120 cfg = GNUNET_GC_create ();
121 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
123 GNUNET_GC_free (cfg);
127 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
128 GNUNET_GE_ASSERT (NULL, daemon != NULL);
130 GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
132 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
135 /* ACTUAL TEST CODE */
136 ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */
137 GNUNET_NO, /* no resume */
138 &eventCallback, NULL);
140 filename = makeName (42);
141 GNUNET_disk_file_write (NULL, filename, "foo bar test!",
142 strlen ("foo bar test!"), "600");
143 meta = GNUNET_meta_data_create ();
145 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
146 (const char **) keywords);
148 upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
150 GNUNET_YES, GNUNET_NO, GNUNET_NO,
151 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
153 CHECK (upload != NULL);
154 GNUNET_ECRS_uri_destroy (kuri);
155 GNUNET_meta_data_destroy (meta);
157 while (lastEvent != GNUNET_FSUI_upload_completed)
160 CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
161 if (GNUNET_shutdown_test () == GNUNET_YES)
166 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
167 uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
168 search = GNUNET_FSUI_search_start (ctx, 0, uri);
169 GNUNET_ECRS_uri_destroy (uri);
170 CHECK (search != NULL);
172 while (lastEvent != GNUNET_FSUI_search_result)
175 CHECK (prog < 10000);
176 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
177 if (GNUNET_shutdown_test () == GNUNET_YES)
180 GNUNET_FSUI_search_abort (search);
181 GNUNET_FSUI_search_stop (search);
186 GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
190 while (lastEvent != GNUNET_FSUI_download_completed)
193 CHECK (prog < 10000);
194 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
195 if (GNUNET_shutdown_test () == GNUNET_YES)
198 GNUNET_FSUI_download_stop (download);
200 GNUNET_ECRS_uri_destroy (search_uri);
201 GNUNET_meta_data_destroy (search_meta);
203 unindex = GNUNET_FSUI_unindex_start (ctx, filename);
205 while (lastEvent != GNUNET_FSUI_unindex_completed)
208 CHECK (prog < 10000);
209 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
210 if (GNUNET_shutdown_test () == GNUNET_YES)
213 if (lastEvent != GNUNET_FSUI_unindex_completed)
214 GNUNET_FSUI_unindex_abort (unindex);
215 GNUNET_FSUI_unindex_stop (unindex);
218 /* END OF TEST CODE */
221 GNUNET_FSUI_stop (ctx);
222 if (filename != NULL)
225 GNUNET_free (filename);
227 if (download != NULL)
229 GNUNET_FSUI_download_abort (download);
230 GNUNET_FSUI_download_stop (download);
232 filename = makeName (43);
233 /* TODO: verify file 'filename(42)' == file 'filename(43)' */
235 GNUNET_free (filename);
238 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
239 GNUNET_OS_process_destroy (daemon);
241 GNUNET_GC_free (cfg);
243 return (ok == GNUNET_YES) ? 0 : 1;
246 /* end of test_fs.c */