comments
[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 3, 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   struct GNUNET_OS_Process *arm_proc;
60 #endif
61 };
62
63 static struct PeerContext p1;
64
65 static struct GNUNET_TIME_Absolute start;
66
67 static struct GNUNET_FS_Handle *fs;
68
69 static struct GNUNET_FS_PublishContext *publish;
70
71 static char *fn1;
72
73 static char *fn2;
74
75 static int err;
76
77 static void
78 abort_publish_task (void *cls,
79                     const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_FS_publish_stop (publish);
82   publish = NULL;
83   GNUNET_DISK_directory_remove (fn1);
84   GNUNET_free (fn1);
85   fn1 = NULL;
86   GNUNET_DISK_directory_remove (fn2);
87   GNUNET_free (fn2);
88   fn2 = NULL;
89 }
90
91
92 static void
93 list_indexed_task (void *cls,
94                    const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96
97   GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
98                                      NULL,
99                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
100 }
101
102
103 static void *
104 progress_cb (void *cls, 
105              const struct GNUNET_FS_ProgressInfo *event)
106 {
107   void *ret;
108
109   ret = NULL;
110   switch (event->status)
111     {
112     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
113       ret = event->value.publish.cctx;
114       printf ("Publish complete,  %llu kbps.\n",
115               (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024));
116       if (0 == strcmp ("list_indexed-context-dir", 
117                        event->value.publish.cctx))      
118         GNUNET_SCHEDULER_add_continuation (&list_indexed_task,
119                                            NULL,
120                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
121         
122       break;
123     case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
124       ret = event->value.publish.cctx;
125       GNUNET_assert (publish == event->value.publish.pc);
126 #if VERBOSE
127       printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
128               (unsigned long long) event->value.publish.completed,
129               (unsigned long long) event->value.publish.size,
130               event->value.publish.specifics.progress.depth,
131               (unsigned long long) event->value.publish.specifics.progress.offset);
132 #endif
133       break;
134     case GNUNET_FS_STATUS_PUBLISH_ERROR:
135       ret = event->value.publish.cctx;
136       fprintf (stderr,
137                "Error publishing file: %s\n",
138                event->value.publish.specifics.error.message);
139       err = 1;
140       if (0 == strcmp ("list_indexed-context-dir", 
141                        event->value.publish.cctx))              
142         GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
143                                            NULL,
144                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
145       break;
146     case GNUNET_FS_STATUS_PUBLISH_START:
147       ret = event->value.publish.cctx;
148       if (0 == strcmp ("list_indexed-context1", 
149                        event->value.publish.cctx))
150         {
151           GNUNET_assert (0 == strcmp ("list_indexed-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 (1 == event->value.publish.anonymity);
156         }
157       else if (0 == strcmp ("list_indexed-context2", 
158                             event->value.publish.cctx))
159         {
160           GNUNET_assert (0 == strcmp ("list_indexed-context-dir", 
161                                       event->value.publish.pctx));
162           GNUNET_assert (FILESIZE == event->value.publish.size);
163           GNUNET_assert (0 == event->value.publish.completed);
164           GNUNET_assert (2 == event->value.publish.anonymity);
165         }
166       else if (0 == strcmp ("list_indexed-context-dir", 
167                             event->value.publish.cctx))
168         {
169           GNUNET_assert (0 == event->value.publish.completed);
170           GNUNET_assert (3 == event->value.publish.anonymity);
171         }
172       else
173         GNUNET_assert (0);
174       break;
175     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
176       if (0 == strcmp ("list_indexed-context-dir", 
177                        event->value.publish.cctx))      
178         {
179           GNUNET_assert (publish == event->value.publish.pc);
180           publish = NULL;
181         }
182       break;
183     default:
184       printf ("Unexpected event: %d\n", 
185               event->status);
186       break;
187     }
188   return ret;
189 }
190
191
192 static void
193 setup_peer (struct PeerContext *p, const char *cfgname)
194 {
195   p->cfg = GNUNET_CONFIGURATION_create ();
196 #if START_ARM
197   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
198                                         "gnunet-service-arm",
199 #if VERBOSE
200                                         "-L", "DEBUG",
201 #endif
202                                         "-c", cfgname, NULL);
203 #endif
204   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
205 }
206
207
208 static void
209 stop_arm (struct PeerContext *p)
210 {
211 #if START_ARM
212   if (NULL != p->arm_proc)
213     {
214       if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
215         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
216       if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
217         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
218       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219                   "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
220       GNUNET_OS_process_close (p->arm_proc);
221       p->arm_proc = NULL;
222     }
223 #endif
224   GNUNET_CONFIGURATION_destroy (p->cfg);
225 }
226
227
228 static void
229 run (void *cls,
230      char *const *args,
231      const char *cfgfile,
232      const struct GNUNET_CONFIGURATION_Handle *cfg)
233 {
234   const char *keywords[] = {
235     "down_foo",
236     "down_bar",
237   };
238   char *buf;
239   struct GNUNET_CONTAINER_MetaData *meta;
240   struct GNUNET_FS_Uri *kuri;
241   struct GNUNET_FS_FileInformation *fi1;
242   struct GNUNET_FS_FileInformation *fi2;
243   struct GNUNET_FS_FileInformation *fidir;
244   size_t i;
245   struct GNUNET_FS_BlockOptions bo;
246
247   setup_peer (&p1, "test_fs_list_indexed_data.conf");
248   fs = GNUNET_FS_start (cfg,
249                         "test-fs-list_indexed",
250                         &progress_cb,
251                         NULL,
252                         GNUNET_FS_FLAGS_NONE,
253                         GNUNET_FS_OPTIONS_END);
254   GNUNET_assert (NULL != fs); 
255   fn1 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
256   buf = GNUNET_malloc (FILESIZE);
257   for (i = 0; i < FILESIZE; i++)
258     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
259   GNUNET_assert (FILESIZE ==
260                  GNUNET_DISK_fn_write (fn1,
261                                        buf,
262                                        FILESIZE,
263                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
264   GNUNET_free (buf);
265
266   fn2 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
267   buf = GNUNET_malloc (FILESIZE);
268   for (i = 0; i < FILESIZE; i++)
269     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
270   GNUNET_assert (FILESIZE ==
271                  GNUNET_DISK_fn_write (fn2,
272                                        buf,
273                                        FILESIZE,
274                                        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
275   GNUNET_free (buf);
276
277   meta = GNUNET_CONTAINER_meta_data_create ();
278   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
279   bo.content_priority = 42;
280   bo.anonymity_level = 1;
281   bo.replication_level = 0;
282   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME); 
283   fi1 = GNUNET_FS_file_information_create_from_file (fs,
284                                                      "list_indexed-context1",
285                                                      fn1,
286                                                      kuri,
287                                                      meta,
288                                                      GNUNET_YES,
289                                                      &bo);
290   GNUNET_assert (NULL != fi1);
291   bo.anonymity_level = 2;
292   fi2 = GNUNET_FS_file_information_create_from_file (fs,
293                                                      "list_indexed-context2",
294                                                      fn2,
295                                                      kuri,
296                                                      meta,
297                                                      GNUNET_YES,
298                                                      &bo);
299   GNUNET_assert (NULL != fi2);
300   bo.anonymity_level = 3;
301   fidir = GNUNET_FS_file_information_create_empty_directory (fs,
302                                                              "list_indexed-context-dir",
303                                                              kuri,
304                                                              meta,
305                                                              &bo);
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 */