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