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