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