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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief testcase for FS (upload-search-download-unindex)
22 * @author Christian Grothoff
26 #include "gnunet_util.h"
27 #include "gnunet_fsui_lib.h"
29 #define DEBUG_VERBOSE GNUNET_NO
31 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(NULL, 0); goto FAILURE; }
34 makeName (unsigned int i)
38 fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
41 strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
42 "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
43 GNUNET_disk_directory_create_for_file (NULL, fn);
47 static enum GNUNET_FSUI_EventType lastEvent;
49 static struct GNUNET_MetaData *search_meta;
51 static struct GNUNET_ECRS_URI *search_uri;
53 static struct GNUNET_FSUI_Context *ctx;
56 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
62 case GNUNET_FSUI_search_resumed:
63 case GNUNET_FSUI_download_resumed:
64 case GNUNET_FSUI_upload_resumed:
65 case GNUNET_FSUI_unindex_resumed:
67 case GNUNET_FSUI_search_result:
69 printf ("Received search result\n");
71 search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
72 search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
74 case GNUNET_FSUI_upload_completed:
76 printf ("Upload complete.\n");
79 case GNUNET_FSUI_download_completed:
81 printf ("Download complete.\n");
84 case GNUNET_FSUI_unindex_completed:
86 printf ("Unindex complete.\n");
92 lastEvent = event->type;
96 #define START_DAEMON 1
99 main (int argc, char *argv[])
102 struct GNUNET_OS_Process *daemon;
105 struct GNUNET_ECRS_URI *uri;
106 char *filename = NULL;
115 struct GNUNET_MetaData *meta;
116 struct GNUNET_ECRS_URI *kuri;
117 struct GNUNET_GC_Configuration *cfg;
118 struct GNUNET_FSUI_UploadList *upload = NULL;
119 struct GNUNET_FSUI_SearchList *search = NULL;
120 struct GNUNET_FSUI_UnindexList *unindex = NULL;
121 struct GNUNET_FSUI_DownloadList *download = NULL;
123 cfg = GNUNET_GC_create ();
124 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
126 GNUNET_GC_free (cfg);
130 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
131 GNUNET_GE_ASSERT (NULL, daemon != NULL);
133 GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
135 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
138 /* ACTUAL TEST CODE */
139 ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */
140 GNUNET_NO, /* no resume */
141 &eventCallback, NULL);
143 filename = makeName (42);
144 GNUNET_disk_file_write (NULL, filename, "foo bar test!",
145 strlen ("foo bar test!"), "600");
146 meta = GNUNET_meta_data_create ();
148 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
149 (const char **) keywords);
151 upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
153 GNUNET_YES, GNUNET_NO, GNUNET_NO,
154 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
156 CHECK (upload != NULL);
157 GNUNET_ECRS_uri_destroy (kuri);
158 GNUNET_meta_data_destroy (meta);
160 while (lastEvent != GNUNET_FSUI_upload_completed)
163 CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
164 if (GNUNET_shutdown_test () == GNUNET_YES)
169 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
170 uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
171 search = GNUNET_FSUI_search_start (ctx, 0, uri);
172 GNUNET_ECRS_uri_destroy (uri);
173 CHECK (search != NULL);
175 while (lastEvent != GNUNET_FSUI_search_result)
178 CHECK (prog < 10000);
179 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
180 if (GNUNET_shutdown_test () == GNUNET_YES)
183 GNUNET_FSUI_search_abort (search);
184 GNUNET_FSUI_search_stop (search);
189 GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
193 while (lastEvent != GNUNET_FSUI_download_completed)
196 CHECK (prog < 10000);
197 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
198 if (GNUNET_shutdown_test () == GNUNET_YES)
201 GNUNET_FSUI_download_stop (download);
203 GNUNET_ECRS_uri_destroy (search_uri);
204 GNUNET_meta_data_destroy (search_meta);
206 unindex = GNUNET_FSUI_unindex_start (ctx, filename);
208 while (lastEvent != GNUNET_FSUI_unindex_completed)
211 CHECK (prog < 10000);
212 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
213 if (GNUNET_shutdown_test () == GNUNET_YES)
216 if (lastEvent != GNUNET_FSUI_unindex_completed)
217 GNUNET_FSUI_unindex_abort (unindex);
218 GNUNET_FSUI_unindex_stop (unindex);
221 /* END OF TEST CODE */
224 GNUNET_FSUI_stop (ctx);
225 if (filename != NULL)
228 GNUNET_free (filename);
230 if (download != NULL)
232 GNUNET_FSUI_download_abort (download);
233 GNUNET_FSUI_download_stop (download);
235 filename = makeName (43);
236 /* TODO: verify file 'filename(42)' == file 'filename(43)' */
238 GNUNET_free (filename);
241 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
242 GNUNET_OS_process_destroy (daemon);
244 GNUNET_GC_free (cfg);
246 return (ok == GNUNET_YES) ? 0 : 1;
249 /* end of test_fs.c */