make clang static analysis happy
[oweals/gnunet.git] / src / fs / test_fs_publish.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009 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 fs/test_fs_publish.c
23  * @brief simple testcase for publish operation (indexing, listing
24  *        indexed, directory structure)
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_fs_service.h"
32
33 #define VERBOSE GNUNET_NO
34
35 #define START_ARM GNUNET_YES
36
37 /**
38  * File-size we use for testing.
39  */
40 #define FILESIZE (1024 * 1024 * 2)
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
46
47 /**
48  * How long should our test-content live?
49  */ 
50 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51
52 struct PeerContext
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55 #if START_ARM
56   pid_t arm_pid;
57 #endif
58 };
59
60 static struct PeerContext p1;
61
62 static struct GNUNET_TIME_Absolute start;
63
64 static struct GNUNET_SCHEDULER_Handle *sched;
65
66 static struct GNUNET_FS_Handle *fs;
67
68 static struct GNUNET_FS_PublishContext *publish;
69
70 static struct GNUNET_FS_PublishContext *publish;
71
72 static char *fn1;
73
74 static char *fn2;
75
76 static int err;
77
78 static void
79 abort_publish_task (void *cls,
80                     const struct GNUNET_SCHEDULER_TaskContext *tc)
81 {
82   GNUNET_FS_publish_stop (publish);
83   publish = NULL;
84   GNUNET_DISK_directory_remove (fn1);
85   GNUNET_free (fn1);
86   fn1 = NULL;
87   GNUNET_DISK_directory_remove (fn2);
88   GNUNET_free (fn2);
89   fn2 = NULL;
90 }
91
92
93 static void *
94 progress_cb (void *cls, 
95              const struct GNUNET_FS_ProgressInfo *event)
96 {
97   void *ret;
98
99   ret = NULL;
100   switch (event->status)
101     {
102     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
103       ret = event->value.publish.cctx;
104       printf ("Publish complete,  %llu kbps.\n",
105               (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
106       if (0 == strcmp ("list_indexed-context-dir", 
107                        event->value.publish.cctx))      
108         GNUNET_SCHEDULER_add_continuation (sched,
109                                            &abort_publish_task,
110                                            NULL,
111                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
112       break;
113     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
114       ret = event->value.publish.cctx;
115       GNUNET_assert (publish == event->value.publish.sc);
116 #if VERBOSE
117       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
118               (unsigned long long) event->value.publish.completed,
119               (unsigned long long) event->value.publish.size,
120               event->value.publish.specifics.progress.depth,
121               (unsigned long long) event->value.publish.specifics.progress.offset);
122 #endif
123       break;
124     case GNUNET_FS_STATUS_PUBLISH_ERROR:
125       ret = event->value.publish.cctx;
126       fprintf (stderr,
127                "Error publishing file: %s\n",
128                event->value.publish.specifics.error.message);
129       err = 1;
130       if (0 == strcmp ("list_indexed-context-dir", 
131                        event->value.publish.cctx))              
132         GNUNET_SCHEDULER_add_continuation (sched,
133                                            &abort_publish_task,
134                                            NULL,
135                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
136       break;
137     case GNUNET_FS_STATUS_PUBLISH_START:
138       ret = event->value.publish.cctx;
139       if (0 == strcmp ("publish-context1", 
140                        event->value.publish.cctx))
141         {
142           GNUNET_assert (0 == strcmp ("publish-context-dir", 
143                                       event->value.publish.pctx));
144           GNUNET_assert (FILESIZE == event->value.publish.size);
145           GNUNET_assert (0 == event->value.publish.completed);
146           GNUNET_assert (1 == event->value.publish.anonymity);
147         }
148       else if (0 == strcmp ("publish-context2", 
149                             event->value.publish.cctx))
150         {
151           GNUNET_assert (0 == strcmp ("publish-context-dir", 
152                                       event->value.publish.pctx));
153           GNUNET_assert (FILESIZE == event->value.publish.size);
154           GNUNET_assert (0 == event->value.publish.completed);
155           GNUNET_assert (2 == event->value.publish.anonymity);
156         }
157       else if (0 == strcmp ("publish-context-dir", 
158                             event->value.publish.cctx))
159         {
160           GNUNET_assert (0 == event->value.publish.completed);
161           GNUNET_assert (3 == event->value.publish.anonymity);
162         }
163       else
164         GNUNET_assert (0);
165       break;
166     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
167       if (0 == strcmp ("list_indexed-context-dir", 
168                        event->value.publish.cctx))      
169         {
170           GNUNET_assert (publish == event->value.publish.sc);
171           GNUNET_SCHEDULER_add_continuation (sched,
172                                              &abort_publish_task,
173                                              NULL,
174                                              GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175         }
176       break;
177     default:
178       printf ("Unexpected event: %d\n", 
179               event->status);
180       break;
181     }
182   return ret;
183 }
184
185
186 static void
187 setup_peer (struct PeerContext *p, const char *cfgname)
188 {
189   p->cfg = GNUNET_CONFIGURATION_create ();
190 #if START_ARM
191   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
192                                         "gnunet-service-arm",
193 #if VERBOSE
194                                         "-L", "DEBUG",
195 #endif
196                                         "-c", cfgname, NULL);
197 #endif
198   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
199   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
200 }
201
202
203 static void
204 stop_arm (struct PeerContext *p)
205 {
206 #if START_ARM
207   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
208     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
209   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
210     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "ARM process %u stopped\n", p->arm_pid);
213 #endif
214   GNUNET_CONFIGURATION_destroy (p->cfg);
215 }
216
217
218 static void
219 run (void *cls,
220      struct GNUNET_SCHEDULER_Handle *s,
221      char *const *args,
222      const char *cfgfile,
223      const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   const char *keywords[] = {
226     "down_foo",
227     "down_bar",
228   };
229   char *buf;
230   struct GNUNET_CONTAINER_MetaData *meta;
231   struct GNUNET_FS_Uri *kuri;
232   struct GNUNET_FS_FileInformation *fi1;
233   struct GNUNET_FS_FileInformation *fi2;
234   struct GNUNET_FS_FileInformation *fidir;
235   size_t i;
236
237   sched = s;
238   setup_peer (&p1, "test_fs_publish_data.conf");
239   fs = GNUNET_FS_start (sched,
240                         cfg,
241                         "test-fs-publish",
242                         &progress_cb,
243                         NULL,
244                         GNUNET_FS_FLAGS_NONE,
245                         GNUNET_FS_OPTIONS_END);
246   GNUNET_assert (NULL != fs); 
247   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
248   buf = GNUNET_malloc (FILESIZE);
249   for (i = 0; i < FILESIZE; i++)
250     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
251   GNUNET_assert (FILESIZE ==
252                  GNUNET_DISK_fn_write (fn1,
253                                        buf,
254                                        FILESIZE,
255                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
256   GNUNET_free (buf);
257
258   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
259   buf = GNUNET_malloc (FILESIZE);
260   for (i = 0; i < FILESIZE; i++)
261     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
262   GNUNET_assert (FILESIZE ==
263                  GNUNET_DISK_fn_write (fn2,
264                                        buf,
265                                        FILESIZE,
266                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
267   GNUNET_free (buf);
268
269   meta = GNUNET_CONTAINER_meta_data_create ();
270   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
271   fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
272                                                      fn1,
273                                                      kuri,
274                                                      meta,
275                                                      GNUNET_YES,
276                                                      1,
277                                                      42,
278                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
279   fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
280                                                      fn2,
281                                                      kuri,
282                                                      meta,
283                                                      GNUNET_YES,
284                                                      2,
285                                                      42,
286                                                      GNUNET_TIME_relative_to_absolute (LIFETIME)); 
287   fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
288                                                              kuri,
289                                                              meta,
290                                                              3,
291                                                              42,
292                                                              GNUNET_TIME_relative_to_absolute (LIFETIME)); 
293   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
294   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
295   GNUNET_FS_uri_destroy (kuri);
296   GNUNET_CONTAINER_meta_data_destroy (meta);
297   GNUNET_assert (NULL != fidir);
298   start = GNUNET_TIME_absolute_get ();
299   publish = GNUNET_FS_publish_start (fs,
300                                      fidir,
301                                      NULL, NULL, NULL,
302                                      GNUNET_FS_PUBLISH_OPTION_NONE);
303   GNUNET_assert (publish != NULL);
304 }
305
306
307 int
308 main (int argc, char *argv[])
309 {
310   char *const argvx[] = { 
311     "test-fs-publish",
312     "-c",
313     "test_fs_publish_data.conf",
314 #if VERBOSE
315     "-L", "DEBUG",
316 #endif
317     NULL
318   };
319   struct GNUNET_GETOPT_CommandLineOption options[] = {
320     GNUNET_GETOPT_OPTION_END
321   };
322
323   GNUNET_log_setup ("test_fs_publish", 
324 #if VERBOSE
325                     "DEBUG",
326 #else
327                     "WARNING",
328 #endif
329                     NULL);
330   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
331                       argvx, "test-fs-publish",
332                       "nohelp", options, &run, NULL);
333   stop_arm (&p1);
334   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
335   GNUNET_DISK_directory_remove (fn1);
336   GNUNET_free_non_null (fn1);
337   GNUNET_DISK_directory_remove (fn2);
338   GNUNET_free_non_null (fn2);
339   return err;
340 }
341
342 /* end of test_fs_publish.c */