removing dead/legacy server/connection logic, except for in tcp/wlan/bt plugins ...
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file fs/test_fs.c
23  * @brief testcase for FS (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") + 14,
44                    "/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, filename, "foo bar test!",
147                           strlen ("foo bar test!"), "600");
148   meta = GNUNET_meta_data_create ();
149   kuri =
150       GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
151                                                (const char **) keywords);
152   /* upload */
153   upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0,  /* anonymity */
154                                      0, /* priority */
155                                      GNUNET_YES, GNUNET_NO, GNUNET_NO,
156                                      GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
157                                      meta, kuri, kuri);
158   CHECK (upload != NULL);
159   GNUNET_ECRS_uri_destroy (kuri);
160   GNUNET_meta_data_destroy (meta);
161   prog = 0;
162   while (lastEvent != GNUNET_FSUI_upload_completed)
163   {
164     prog++;
165     CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
166     if (GNUNET_shutdown_test () == GNUNET_YES)
167       break;
168   }
169
170   /* search */
171   GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
172   uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
173   search = GNUNET_FSUI_search_start (ctx, 0, uri);
174   GNUNET_ECRS_uri_destroy (uri);
175   CHECK (search != NULL);
176   prog = 0;
177   while (lastEvent != GNUNET_FSUI_search_result)
178   {
179     prog++;
180     CHECK (prog < 10000);
181     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
182     if (GNUNET_shutdown_test () == GNUNET_YES)
183       break;
184   }
185   GNUNET_FSUI_search_abort (search);
186   GNUNET_FSUI_search_stop (search);
187
188   /* download */
189   fn = makeName (43);
190   download =
191       GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
192                                   fn, NULL, NULL);
193   GNUNET_free (fn);
194   prog = 0;
195   while (lastEvent != GNUNET_FSUI_download_completed)
196   {
197     prog++;
198     CHECK (prog < 10000);
199     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
200     if (GNUNET_shutdown_test () == GNUNET_YES)
201       break;
202   }
203   GNUNET_FSUI_download_stop (download);
204   download = NULL;
205   GNUNET_ECRS_uri_destroy (search_uri);
206   GNUNET_meta_data_destroy (search_meta);
207   /* unindex */
208   unindex = GNUNET_FSUI_unindex_start (ctx, filename);
209   prog = 0;
210   while (lastEvent != GNUNET_FSUI_unindex_completed)
211   {
212     prog++;
213     CHECK (prog < 10000);
214     GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
215     if (GNUNET_shutdown_test () == GNUNET_YES)
216       break;
217   }
218   if (lastEvent != GNUNET_FSUI_unindex_completed)
219     GNUNET_FSUI_unindex_abort (unindex);
220   GNUNET_FSUI_unindex_stop (unindex);
221
222
223   /* END OF TEST CODE */
224 FAILURE:
225   if (ctx != NULL)
226     GNUNET_FSUI_stop (ctx);
227   if (filename != NULL)
228   {
229     UNLINK (filename);
230     GNUNET_free (filename);
231   }
232   if (download != NULL)
233   {
234     GNUNET_FSUI_download_abort (download);
235     GNUNET_FSUI_download_stop (download);
236   }
237   filename = makeName (43);
238   /* TODO: verify file 'filename(42)' == file 'filename(43)' */
239   UNLINK (filename);
240   GNUNET_free (filename);
241
242 #if START_DAEMON
243   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
244   GNUNET_OS_process_destroy (daemon);
245 #endif
246   GNUNET_GC_free (cfg);
247
248   return (ok == GNUNET_YES) ? 0 : 1;
249 }
250
251 /* end of test_fs.c */