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.
23 * @brief testcase for FS (upload-search-download-unindex)
24 * @author Christian Grothoff
28 #include "gnunet_util.h"
29 #include "gnunet_fsui_lib.h"
31 #define DEBUG_VERBOSE GNUNET_NO
33 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(NULL, 0); goto FAILURE; }
36 makeName (unsigned int i)
40 fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
43 strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
44 "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
45 GNUNET_disk_directory_create_for_file (NULL, fn);
49 static enum GNUNET_FSUI_EventType lastEvent;
51 static struct GNUNET_MetaData *search_meta;
53 static struct GNUNET_ECRS_URI *search_uri;
55 static struct GNUNET_FSUI_Context *ctx;
58 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
64 case GNUNET_FSUI_search_resumed:
65 case GNUNET_FSUI_download_resumed:
66 case GNUNET_FSUI_upload_resumed:
67 case GNUNET_FSUI_unindex_resumed:
69 case GNUNET_FSUI_search_result:
71 printf ("Received search result\n");
73 search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
74 search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
76 case GNUNET_FSUI_upload_completed:
78 printf ("Upload complete.\n");
81 case GNUNET_FSUI_download_completed:
83 printf ("Download complete.\n");
86 case GNUNET_FSUI_unindex_completed:
88 printf ("Unindex complete.\n");
94 lastEvent = event->type;
98 #define START_DAEMON 1
101 main (int argc, char *argv[])
104 struct GNUNET_OS_Process *daemon;
107 struct GNUNET_ECRS_URI *uri;
108 char *filename = NULL;
117 struct GNUNET_MetaData *meta;
118 struct GNUNET_ECRS_URI *kuri;
119 struct GNUNET_GC_Configuration *cfg;
120 struct GNUNET_FSUI_UploadList *upload = NULL;
121 struct GNUNET_FSUI_SearchList *search = NULL;
122 struct GNUNET_FSUI_UnindexList *unindex = NULL;
123 struct GNUNET_FSUI_DownloadList *download = NULL;
125 cfg = GNUNET_GC_create ();
126 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
128 GNUNET_GC_free (cfg);
132 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
133 GNUNET_GE_ASSERT (NULL, daemon != NULL);
135 GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
137 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
140 /* ACTUAL TEST CODE */
141 ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */
142 GNUNET_NO, /* no resume */
143 &eventCallback, NULL);
145 filename = makeName (42);
146 GNUNET_disk_file_write (NULL, filename, "foo bar test!",
147 strlen ("foo bar test!"), "600");
148 meta = GNUNET_meta_data_create ();
150 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
151 (const char **) keywords);
153 upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
155 GNUNET_YES, GNUNET_NO, GNUNET_NO,
156 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
158 CHECK (upload != NULL);
159 GNUNET_ECRS_uri_destroy (kuri);
160 GNUNET_meta_data_destroy (meta);
162 while (lastEvent != GNUNET_FSUI_upload_completed)
165 CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
166 if (GNUNET_shutdown_test () == GNUNET_YES)
171 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
172 uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
173 search = GNUNET_FSUI_search_start (ctx, 0, uri);
174 GNUNET_ECRS_uri_destroy (uri);
175 CHECK (search != NULL);
177 while (lastEvent != GNUNET_FSUI_search_result)
180 CHECK (prog < 10000);
181 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
182 if (GNUNET_shutdown_test () == GNUNET_YES)
185 GNUNET_FSUI_search_abort (search);
186 GNUNET_FSUI_search_stop (search);
191 GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
195 while (lastEvent != GNUNET_FSUI_download_completed)
198 CHECK (prog < 10000);
199 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
200 if (GNUNET_shutdown_test () == GNUNET_YES)
203 GNUNET_FSUI_download_stop (download);
205 GNUNET_ECRS_uri_destroy (search_uri);
206 GNUNET_meta_data_destroy (search_meta);
208 unindex = GNUNET_FSUI_unindex_start (ctx, filename);
210 while (lastEvent != GNUNET_FSUI_unindex_completed)
213 CHECK (prog < 10000);
214 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
215 if (GNUNET_shutdown_test () == GNUNET_YES)
218 if (lastEvent != GNUNET_FSUI_unindex_completed)
219 GNUNET_FSUI_unindex_abort (unindex);
220 GNUNET_FSUI_unindex_stop (unindex);
223 /* END OF TEST CODE */
226 GNUNET_FSUI_stop (ctx);
227 if (filename != NULL)
230 GNUNET_free (filename);
232 if (download != NULL)
234 GNUNET_FSUI_download_abort (download);
235 GNUNET_FSUI_download_stop (download);
237 filename = makeName (43);
238 /* TODO: verify file 'filename(42)' == file 'filename(43)' */
240 GNUNET_free (filename);
243 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
244 GNUNET_OS_process_destroy (daemon);
246 GNUNET_GC_free (cfg);
248 return (ok == GNUNET_YES) ? 0 : 1;
251 /* end of test_fs.c */