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/search_ranking_test.c
23 * @brief testcase for search ranking (availability, etc)
24 * @author Christian Grothoff
28 #include "gnunet_util.h"
29 #include "gnunet_fsui_lib.h"
31 #define CHECK_VERBOSE GNUNET_EXTRA_LOGGING
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-fsui-searchranktest/FSUITEST") + 14);
41 GNUNET_snprintf (fn, strlen ("/tmp/gnunet-fsui-searchranktest/FSUITEST") + 14,
42 "/tmp/gnunet-fsui-searchranktest/FSUITEST%u", i);
43 GNUNET_disk_directory_create_for_file (NULL, fn);
47 static struct GNUNET_FSUI_SearchList *search;
49 static enum GNUNET_FSUI_EventType lastEvent;
51 static struct GNUNET_ECRS_URI *uri;
53 static int availability;
55 static unsigned int rank;
57 static enum GNUNET_FSUI_EventType waitForEvent;
60 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
66 case GNUNET_FSUI_search_resumed:
67 search = event->data.SearchResumed.sc.pos;
69 case GNUNET_FSUI_search_suspended:
72 case GNUNET_FSUI_search_update:
73 availability = event->data.SearchUpdate.availability_rank;
74 rank = event->data.SearchUpdate.applicability_rank;
76 case GNUNET_FSUI_search_paused:
77 case GNUNET_FSUI_search_restarted:
79 case GNUNET_FSUI_download_resumed:
80 case GNUNET_FSUI_upload_resumed:
81 case GNUNET_FSUI_unindex_resumed:
83 case GNUNET_FSUI_search_result:
85 printf ("Received search result\n");
87 uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
89 case GNUNET_FSUI_upload_completed:
91 printf ("Upload complete.\n");
94 case GNUNET_FSUI_download_completed:
96 printf ("Download complete.\n");
99 case GNUNET_FSUI_unindex_completed:
101 printf ("Unindex complete.\n");
104 case GNUNET_FSUI_upload_error:
105 printf ("Upload error.\n");
107 case GNUNET_FSUI_download_error:
108 printf ("Download error.\n");
110 case GNUNET_FSUI_unindex_error:
111 printf ("Unindex error.\n");
116 if (lastEvent != waitForEvent)
117 lastEvent = event->type;
121 #define START_DAEMON 1
124 main (int argc, char *argv[])
127 struct GNUNET_OS_Process *daemon;
138 struct GNUNET_MetaData *meta;
139 struct GNUNET_ECRS_URI *kuri;
140 struct GNUNET_GC_Configuration *cfg;
141 struct GNUNET_FSUI_UploadList *upload;
142 struct GNUNET_ECRS_URI *luri;
143 struct GNUNET_FSUI_Context *ctx = NULL;
146 cfg = GNUNET_GC_create ();
147 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
149 GNUNET_GC_free (cfg);
153 GNUNET_disk_directory_remove (NULL, "/tmp/gnunet-fsui-searchranktest/");
154 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
155 GNUNET_GE_ASSERT (NULL, daemon != NULL);
157 GNUNET_wait_for_daemon_running (NULL, cfg, 30 * GNUNET_CRON_SECONDS));
158 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
159 /* ACTUAL TEST CODE */
162 GNUNET_FSUI_start (NULL, cfg, "fsuisearchranktest", 32, GNUNET_YES,
163 &eventCallback, NULL);
167 GNUNET_disk_file_write (NULL, fn, "foo bar test!", strlen ("foo bar test!"),
169 meta = GNUNET_meta_data_create ();
171 GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
172 (const char **) keywords);
173 waitForEvent = GNUNET_FSUI_upload_completed;
175 GNUNET_FSUI_upload_start (ctx, fn,
176 (GNUNET_FSUI_DirectoryScanCallback) &
177 GNUNET_disk_directory_scan, NULL, 0, 0,
178 GNUNET_YES, GNUNET_NO, GNUNET_NO,
179 GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
181 CHECK (NULL != upload);
184 GNUNET_ECRS_uri_destroy (kuri);
185 GNUNET_meta_data_destroy (meta);
187 while (lastEvent != GNUNET_FSUI_upload_completed)
192 fprintf (stderr, "Upload failed to complete -- last event: %u\n",
195 CHECK (prog < 10000);
196 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
197 if (GNUNET_shutdown_test () == GNUNET_YES)
200 GNUNET_FSUI_upload_stop (upload);
203 GNUNET_snprintf (keyword, 40, "%s %s", keywords[0], keywords[1]);
204 luri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
206 search = GNUNET_FSUI_search_start (ctx, 0, luri);
207 GNUNET_ECRS_uri_destroy (luri);
209 CHECK (NULL != search);
210 GNUNET_FSUI_search_pause (search);
211 GNUNET_FSUI_search_restart (search);
212 while ((uri == NULL) && (availability < 3) && (rank != 2) &&
213 (GNUNET_shutdown_test () != GNUNET_YES))
216 CHECK (prog < 10000);
217 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
219 GNUNET_FSUI_search_abort (search);
220 GNUNET_FSUI_search_stop (search);
224 /* END OF TEST CODE */
227 GNUNET_FSUI_stop (ctx);
228 GNUNET_free_non_null (fn);
229 /* TODO: verify file 'fn(42)' == file 'fn(43)' */
238 GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
239 GNUNET_OS_process_close (daemon);
242 GNUNET_GC_free (cfg);
243 return (ok == GNUNET_YES) ? 0 : 1;
246 /* end of searchtest.c */