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