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/basic_fsui_test.c
23 * @brief testcase for fsui (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") +
44 14, "/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,
148 "foo bar test!", strlen ("foo bar test!"), "600");
149 meta = GNUNET_meta_data_create ();
151 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
152 (const char **) keywords);
154 upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
160 5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
161 CHECK (upload != NULL);
162 GNUNET_ECRS_uri_destroy (kuri);
163 GNUNET_meta_data_destroy (meta);
165 while (lastEvent != GNUNET_FSUI_upload_completed)
168 CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
169 if (GNUNET_shutdown_test () == GNUNET_YES)
174 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
175 uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
176 search = GNUNET_FSUI_search_start (ctx, 0, uri);
177 GNUNET_ECRS_uri_destroy (uri);
178 CHECK (search != NULL);
180 while (lastEvent != GNUNET_FSUI_search_result)
183 CHECK (prog < 10000);
184 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
185 if (GNUNET_shutdown_test () == GNUNET_YES)
188 GNUNET_FSUI_search_abort (search);
189 GNUNET_FSUI_search_stop (search);
193 download = GNUNET_FSUI_download_start (ctx,
197 search_meta, fn, NULL, NULL);
200 while (lastEvent != GNUNET_FSUI_download_completed)
203 CHECK (prog < 10000);
204 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
205 if (GNUNET_shutdown_test () == GNUNET_YES)
208 GNUNET_FSUI_download_stop (download);
210 GNUNET_ECRS_uri_destroy (search_uri);
211 GNUNET_meta_data_destroy (search_meta);
213 unindex = GNUNET_FSUI_unindex_start (ctx, filename);
215 while (lastEvent != GNUNET_FSUI_unindex_completed)
218 CHECK (prog < 10000);
219 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
220 if (GNUNET_shutdown_test () == GNUNET_YES)
223 if (lastEvent != GNUNET_FSUI_unindex_completed)
224 GNUNET_FSUI_unindex_abort (unindex);
225 GNUNET_FSUI_unindex_stop (unindex);
228 /* END OF TEST CODE */
231 GNUNET_FSUI_stop (ctx);
232 if (filename != NULL)
235 GNUNET_free (filename);
237 if (download != NULL)
239 GNUNET_FSUI_download_abort (download);
240 GNUNET_FSUI_download_stop (download);
242 filename = makeName (43);
243 /* TODO: verify file 'filename(42)' == file 'filename(43)' */
245 GNUNET_free (filename);
248 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
249 GNUNET_OS_process_close (daemon);
251 GNUNET_GC_free (cfg);
253 return (ok == GNUNET_YES) ? 0 : 1;
256 /* end of basic_fsui_test.c */