fix
[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 2, 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 =
41     GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
42                    14);
43   GNUNET_snprintf (fn,
44                    strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
45                    14, "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
46   GNUNET_disk_directory_create_for_file (NULL, fn);
47   return fn;
48 }
49
50 static volatile enum GNUNET_FSUI_EventType lastEvent;
51
52 static struct GNUNET_MetaData *search_meta;
53
54 static struct GNUNET_ECRS_URI *search_uri;
55
56 static struct GNUNET_FSUI_Context *ctx;
57
58 static void *
59 eventCallback (void *cls, const GNUNET_FSUI_Event * event)
60 {
61   static char unused;
62
63   switch (event->type)
64     {
65     case GNUNET_FSUI_search_resumed:
66     case GNUNET_FSUI_download_resumed:
67     case GNUNET_FSUI_upload_resumed:
68     case GNUNET_FSUI_unindex_resumed:
69       return &unused;
70     case GNUNET_FSUI_search_result:
71 #if DEBUG_VERBOSE
72       printf ("Received search result\n");
73 #endif
74       search_uri =
75         GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
76       search_meta =
77         GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
78       break;
79     case GNUNET_FSUI_upload_completed:
80 #if DEBUG_VERBOSE
81       printf ("Upload complete.\n");
82 #endif
83       break;
84     case GNUNET_FSUI_download_completed:
85 #if DEBUG_VERBOSE
86       printf ("Download complete.\n");
87 #endif
88       break;
89     case GNUNET_FSUI_unindex_completed:
90 #if DEBUG_VERBOSE
91       printf ("Unindex complete.\n");
92 #endif
93       break;
94     default:
95       break;
96     }
97   lastEvent = event->type;
98   return NULL;
99 }
100
101 #define START_DAEMON 1
102
103 int
104 main (int argc, char *argv[])
105 {
106 #if START_DAEMON
107   pid_t daemon;
108 #endif
109   int ok;
110   struct GNUNET_ECRS_URI *uri;
111   char *filename = NULL;
112   char *keywords[] = {
113     "fsui_foo",
114     "fsui_bar",
115   };
116   char keyword[40];
117   char *fn;
118   int prog;
119   struct GNUNET_MetaData *meta;
120   struct GNUNET_ECRS_URI *kuri;
121   struct GNUNET_GC_Configuration *cfg;
122   struct GNUNET_FSUI_UploadList *upload = NULL;
123   struct GNUNET_FSUI_SearchList *search = NULL;
124   struct GNUNET_FSUI_UnindexList *unindex = NULL;
125   struct GNUNET_FSUI_DownloadList *download = NULL;
126
127   cfg = GNUNET_GC_create ();
128   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
129     {
130       GNUNET_GC_free (cfg);
131       return -1;
132     }
133 #if START_DAEMON
134   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
135   GNUNET_GE_ASSERT (NULL, daemon > 0);
136   CHECK (GNUNET_OK ==
137          GNUNET_wait_for_daemon_running (NULL, cfg,
138                                          60 * GNUNET_CRON_SECONDS));
139 #endif
140   GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);        /* give apps time to start */
141   ok = GNUNET_YES;
142
143   /* ACTUAL TEST CODE */
144   ctx = GNUNET_FSUI_start (NULL, cfg, "basic_fsui_test", 32,    /* thread pool size */
145                            GNUNET_NO,   /* no resume */
146                            &eventCallback, NULL);
147   CHECK (ctx != NULL);
148   filename = makeName (42);
149   GNUNET_disk_file_write (NULL,
150                           filename,
151                           "foo bar test!", strlen ("foo bar test!"), "600");
152   meta = GNUNET_meta_data_create ();
153   kuri =
154     GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
155                                              (const char **) keywords);
156   /* upload */
157   upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0,  /* anonymity */
158                                      0, /* priority */
159                                      GNUNET_YES,
160                                      GNUNET_NO,
161                                      GNUNET_NO,
162                                      GNUNET_get_time () +
163                                      5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
164   CHECK (upload != NULL);
165   GNUNET_ECRS_uri_destroy (kuri);
166   GNUNET_meta_data_destroy (meta);
167   prog = 0;
168   while (lastEvent != GNUNET_FSUI_upload_completed)
169     {
170       prog++;
171       CHECK (prog <
172              10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
173       if (GNUNET_shutdown_test () == GNUNET_YES)
174         break;
175     }
176
177   /* search */
178   GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
179   uri = GNUNET_ECRS_keyword_string_to_uri (NULL, keyword);
180   search = GNUNET_FSUI_search_start (ctx, 0, uri);
181   GNUNET_ECRS_uri_destroy (uri);
182   CHECK (search != NULL);
183   prog = 0;
184   while (lastEvent != GNUNET_FSUI_search_result)
185     {
186       prog++;
187       CHECK (prog < 10000);
188       GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
189       if (GNUNET_shutdown_test () == GNUNET_YES)
190         break;
191     }
192   GNUNET_FSUI_search_abort (search);
193   GNUNET_FSUI_search_stop (search);
194
195   /* download */
196   fn = makeName (43);
197   download = GNUNET_FSUI_download_start (ctx,
198                                          0,
199                                          GNUNET_NO,
200                                          search_uri,
201                                          search_meta, fn, NULL, NULL);
202   GNUNET_free (fn);
203   prog = 0;
204   while (lastEvent != GNUNET_FSUI_download_completed)
205     {
206       prog++;
207       CHECK (prog < 10000);
208       GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
209       if (GNUNET_shutdown_test () == GNUNET_YES)
210         break;
211     }
212   GNUNET_FSUI_download_stop (download);
213   download = NULL;
214   GNUNET_ECRS_uri_destroy (search_uri);
215   GNUNET_meta_data_destroy (search_meta);
216   /* unindex */
217   unindex = GNUNET_FSUI_unindex_start (ctx, filename);
218   prog = 0;
219   while (lastEvent != GNUNET_FSUI_unindex_completed)
220     {
221       prog++;
222       CHECK (prog < 10000);
223       GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
224       if (GNUNET_shutdown_test () == GNUNET_YES)
225         break;
226     }
227   if (lastEvent != GNUNET_FSUI_unindex_completed)
228     GNUNET_FSUI_unindex_abort (unindex);
229   GNUNET_FSUI_unindex_stop (unindex);
230
231
232   /* END OF TEST CODE */
233 FAILURE:
234   if (ctx != NULL)
235     GNUNET_FSUI_stop (ctx);
236   if (filename != NULL)
237     {
238       UNLINK (filename);
239       GNUNET_free (filename);
240     }
241   if (download != NULL)
242     {
243       GNUNET_FSUI_download_abort (download);
244       GNUNET_FSUI_download_stop (download);
245     }
246   filename = makeName (43);
247   /* TODO: verify file 'filename(42)' == file 'filename(43)' */
248   UNLINK (filename);
249   GNUNET_free (filename);
250
251 #if START_DAEMON
252   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
253 #endif
254   GNUNET_GC_free (cfg);
255
256   return (ok == GNUNET_YES) ? 0 : 1;
257 }
258
259 /* end of basic_fsui_test.c */