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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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); \
37 makeName (unsigned int i)
41 fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST")
44 strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
45 "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
46 GNUNET_disk_directory_create_for_file (NULL, fn);
51 static enum GNUNET_FSUI_EventType lastEvent;
53 static struct GNUNET_MetaData *search_meta;
55 static struct GNUNET_ECRS_URI *search_uri;
57 static struct GNUNET_FSUI_Context *ctx;
60 eventCallback (void *cls, const GNUNET_FSUI_Event *event)
66 case GNUNET_FSUI_search_resumed:
67 case GNUNET_FSUI_download_resumed:
68 case GNUNET_FSUI_upload_resumed:
69 case GNUNET_FSUI_unindex_resumed:
72 case GNUNET_FSUI_search_result:
74 printf ("Received search result\n");
76 search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
77 search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
80 case GNUNET_FSUI_upload_completed:
82 printf ("Upload complete.\n");
86 case GNUNET_FSUI_download_completed:
88 printf ("Download complete.\n");
92 case GNUNET_FSUI_unindex_completed:
94 printf ("Unindex complete.\n");
101 lastEvent = event->type;
106 #define START_DAEMON 1
109 main (int argc, char *argv[])
112 struct GNUNET_OS_Process *daemon;
115 struct GNUNET_ECRS_URI *uri;
116 char *filename = NULL;
125 struct GNUNET_MetaData *meta;
126 struct GNUNET_ECRS_URI *kuri;
127 struct GNUNET_GC_Configuration *cfg;
128 struct GNUNET_FSUI_UploadList *upload = NULL;
129 struct GNUNET_FSUI_SearchList *search = NULL;
130 struct GNUNET_FSUI_UnindexList *unindex = NULL;
131 struct GNUNET_FSUI_DownloadList *download = NULL;
133 cfg = GNUNET_GC_create ();
134 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
136 GNUNET_GC_free (cfg);
140 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
141 GNUNET_GE_ASSERT (NULL, daemon != NULL);
143 GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
145 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
148 /* ACTUAL TEST CODE */
149 ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32, /* thread pool size */
150 GNUNET_NO, /* no resume */
151 &eventCallback, NULL);
153 filename = makeName (42);
154 GNUNET_disk_file_write (NULL, filename, "foo bar test!",
155 strlen ("foo bar test!"), "600");
156 meta = GNUNET_meta_data_create ();
158 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
159 (const char **) keywords);
161 upload = GNUNET_FSUI_upload_start (ctx, filename,
162 (GNUNET_FSUI_DirectoryScanCallback)
163 & GNUNET_disk_directory_scan, NULL, 0, /* anonymity */
165 GNUNET_YES, GNUNET_NO, GNUNET_NO,
166 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
168 CHECK (upload != NULL);
169 GNUNET_ECRS_uri_destroy (kuri);
170 GNUNET_meta_data_destroy (meta);
172 while (lastEvent != GNUNET_FSUI_upload_completed)
175 CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
176 if (GNUNET_shutdown_test () == GNUNET_YES)
181 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
182 uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
183 search = GNUNET_FSUI_search_start (ctx, 0, uri);
184 GNUNET_ECRS_uri_destroy (uri);
185 CHECK (search != NULL);
187 while (lastEvent != GNUNET_FSUI_search_result)
190 CHECK (prog < 10000);
191 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
192 if (GNUNET_shutdown_test () == GNUNET_YES)
195 GNUNET_FSUI_search_abort (search);
196 GNUNET_FSUI_search_stop (search);
201 GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
205 while (lastEvent != GNUNET_FSUI_download_completed)
208 CHECK (prog < 10000);
209 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
210 if (GNUNET_shutdown_test () == GNUNET_YES)
213 GNUNET_FSUI_download_stop (download);
215 GNUNET_ECRS_uri_destroy (search_uri);
216 GNUNET_meta_data_destroy (search_meta);
218 unindex = GNUNET_FSUI_unindex_start (ctx, filename);
220 while (lastEvent != GNUNET_FSUI_unindex_completed)
223 CHECK (prog < 10000);
224 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
225 if (GNUNET_shutdown_test () == GNUNET_YES)
228 if (lastEvent != GNUNET_FSUI_unindex_completed)
229 GNUNET_FSUI_unindex_abort (unindex);
230 GNUNET_FSUI_unindex_stop (unindex);
233 /* END OF TEST CODE */
236 GNUNET_FSUI_stop (ctx);
237 if (filename != NULL)
240 GNUNET_free (filename);
242 if (download != NULL)
244 GNUNET_FSUI_download_abort (download);
245 GNUNET_FSUI_download_stop (download);
247 filename = makeName (43);
248 /* TODO: verify file 'filename(42)' == file 'filename(43)' */
250 GNUNET_free (filename);
253 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
254 GNUNET_OS_process_destroy (daemon);
256 GNUNET_GC_free (cfg);
258 return (ok == GNUNET_YES) ? 0 : 1;
262 /* end of test_fs.c */