paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / fs / test_fs.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008 GNUnet e.V.
4
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.
9
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.
14     
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/>.
17 */
18
19 /**
20  * @file fs/test_fs.c
21  * @brief testcase for FS (upload-search-download-unindex)
22  * @author Christian Grothoff
23  */
24
25 #include "platform.h"
26 #include "gnunet_util.h"
27 #include "gnunet_fsui_lib.h"
28
29 #define DEBUG_VERBOSE GNUNET_NO
30
31 #define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(NULL, 0); goto FAILURE; }
32
33 static char *
34 makeName (unsigned int i)
35 {
36   char *fn;
37
38   fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
39                       14);
40   GNUNET_snprintf (fn,
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);
44   return fn;
45 }
46
47 static enum GNUNET_FSUI_EventType lastEvent;
48
49 static struct GNUNET_MetaData *search_meta;
50
51 static struct GNUNET_ECRS_URI *search_uri;
52
53 static struct GNUNET_FSUI_Context *ctx;
54
55 static void *
56 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
57 {
58   static char unused;
59
60   switch (event->type)
61   {
62   case GNUNET_FSUI_search_resumed:
63   case GNUNET_FSUI_download_resumed:
64   case GNUNET_FSUI_upload_resumed:
65   case GNUNET_FSUI_unindex_resumed:
66     return &unused;
67   case GNUNET_FSUI_search_result:
68 #if DEBUG_VERBOSE
69     printf ("Received search result\n");
70 #endif
71     search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
72     search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
73     break;
74   case GNUNET_FSUI_upload_completed:
75 #if DEBUG_VERBOSE
76     printf ("Upload complete.\n");
77 #endif
78     break;
79   case GNUNET_FSUI_download_completed:
80 #if DEBUG_VERBOSE
81     printf ("Download complete.\n");
82 #endif
83     break;
84   case GNUNET_FSUI_unindex_completed:
85 #if DEBUG_VERBOSE
86     printf ("Unindex complete.\n");
87 #endif
88     break;
89   default:
90     break;
91   }
92   lastEvent = event->type;
93   return NULL;
94 }
95
96 #define START_DAEMON 1
97
98 int
99 main (int argc, char *argv[])
100 {
101 #if START_DAEMON
102   struct GNUNET_OS_Process *daemon;
103 #endif
104   int ok;
105   struct GNUNET_ECRS_URI *uri;
106   char *filename = NULL;
107
108   char *keywords[] = {
109     "fsui_foo",
110     "fsui_bar",
111   };
112   char keyword[40];
113   char *fn;
114   int prog;
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;
122
123   cfg = GNUNET_GC_create ();
124   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
125   {
126     GNUNET_GC_free (cfg);
127     return -1;
128   }
129 #if START_DAEMON
130   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
131   GNUNET_GE_ASSERT (NULL, daemon != NULL);
132   CHECK (GNUNET_OK ==
133          GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
134 #endif
135   GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);        /* give apps time to start */
136   ok = GNUNET_YES;
137
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);
142   CHECK (ctx != 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 ();
147   kuri =
148       GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
149                                                (const char **) keywords);
150   /* upload */
151   upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0,  /* anonymity */
152                                      0, /* priority */
153                                      GNUNET_YES, GNUNET_NO, GNUNET_NO,
154                                      GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
155                                      meta, kuri, kuri);
156   CHECK (upload != NULL);
157   GNUNET_ECRS_uri_destroy (kuri);
158   GNUNET_meta_data_destroy (meta);
159   prog = 0;
160   while (lastEvent != GNUNET_FSUI_upload_completed)
161   {
162     prog++;
163     CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
164     if (GNUNET_shutdown_test () == GNUNET_YES)
165       break;
166   }
167
168   /* search */
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);
174   prog = 0;
175   while (lastEvent != GNUNET_FSUI_search_result)
176   {
177     prog++;
178     CHECK (prog < 10000);
179     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
180     if (GNUNET_shutdown_test () == GNUNET_YES)
181       break;
182   }
183   GNUNET_FSUI_search_abort (search);
184   GNUNET_FSUI_search_stop (search);
185
186   /* download */
187   fn = makeName (43);
188   download =
189       GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
190                                   fn, NULL, NULL);
191   GNUNET_free (fn);
192   prog = 0;
193   while (lastEvent != GNUNET_FSUI_download_completed)
194   {
195     prog++;
196     CHECK (prog < 10000);
197     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
198     if (GNUNET_shutdown_test () == GNUNET_YES)
199       break;
200   }
201   GNUNET_FSUI_download_stop (download);
202   download = NULL;
203   GNUNET_ECRS_uri_destroy (search_uri);
204   GNUNET_meta_data_destroy (search_meta);
205   /* unindex */
206   unindex = GNUNET_FSUI_unindex_start (ctx, filename);
207   prog = 0;
208   while (lastEvent != GNUNET_FSUI_unindex_completed)
209   {
210     prog++;
211     CHECK (prog < 10000);
212     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
213     if (GNUNET_shutdown_test () == GNUNET_YES)
214       break;
215   }
216   if (lastEvent != GNUNET_FSUI_unindex_completed)
217     GNUNET_FSUI_unindex_abort (unindex);
218   GNUNET_FSUI_unindex_stop (unindex);
219
220
221   /* END OF TEST CODE */
222 FAILURE:
223   if (ctx != NULL)
224     GNUNET_FSUI_stop (ctx);
225   if (filename != NULL)
226   {
227     UNLINK (filename);
228     GNUNET_free (filename);
229   }
230   if (download != NULL)
231   {
232     GNUNET_FSUI_download_abort (download);
233     GNUNET_FSUI_download_stop (download);
234   }
235   filename = makeName (43);
236   /* TODO: verify file 'filename(42)' == file 'filename(43)' */
237   UNLINK (filename);
238   GNUNET_free (filename);
239
240 #if START_DAEMON
241   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
242   GNUNET_OS_process_destroy (daemon);
243 #endif
244   GNUNET_GC_free (cfg);
245
246   return (ok == GNUNET_YES) ? 0 : 1;
247 }
248
249 /* end of test_fs.c */