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