glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_fs_list_indexed.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008, 2009 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
16 /**
17  * @file fs/test_fs_list_indexed.c
18  * @brief simple testcase for list_indexed operation (indexing, listing
19  *        indexed)
20  * @author Christian Grothoff
21  *
22  * TODO:
23  * - actually call list_indexed API!
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_fs_service.h"
29
30 /**
31  * File-size we use for testing.
32  */
33 #define FILESIZE (1024 * 1024 * 2)
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39
40 /**
41  * How long should our test-content live?
42  */
43 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
44
45
46 static struct GNUNET_TIME_Absolute start;
47
48 static struct GNUNET_FS_Handle *fs;
49
50 static struct GNUNET_FS_PublishContext *publish;
51
52 static char *fn1;
53
54 static char *fn2;
55
56 static int err;
57
58
59 static void
60 abort_publish_task (void *cls)
61 {
62   GNUNET_FS_publish_stop (publish);
63   publish = NULL;
64   GNUNET_DISK_directory_remove (fn1);
65   GNUNET_free (fn1);
66   fn1 = NULL;
67   GNUNET_DISK_directory_remove (fn2);
68   GNUNET_free (fn2);
69   fn2 = NULL;
70 }
71
72
73 static void
74 list_indexed_task (void *cls)
75 {
76   GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
77 }
78
79
80 static void *
81 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
82 {
83   void *ret;
84
85   ret = NULL;
86   switch (event->status)
87   {
88   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
89     ret = event->value.publish.cctx;
90     printf ("Publish complete,  %llu kbps.\n",
91             (unsigned long long) (FILESIZE * 1000000LL /
92                                   (1 +
93                                    GNUNET_TIME_absolute_get_duration
94                                    (start).rel_value_us) / 1024));
95     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
96       GNUNET_SCHEDULER_add_now (&list_indexed_task, NULL);
97
98     break;
99   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
100     ret = event->value.publish.cctx;
101     GNUNET_assert (publish == event->value.publish.pc);
102     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
103                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
104                 (unsigned long long) event->value.publish.completed,
105                 (unsigned long long) event->value.publish.size,
106                 event->value.publish.specifics.progress.depth,
107                 (unsigned long long) event->value.publish.specifics.
108                 progress.offset);
109     break;
110   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
111     ret = event->value.publish.cctx;
112     break;
113   case GNUNET_FS_STATUS_PUBLISH_ERROR:
114     ret = event->value.publish.cctx;
115     FPRINTF (stderr, "Error publishing file: %s\n",
116              event->value.publish.specifics.error.message);
117     err = 1;
118     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
119       GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
120     break;
121   case GNUNET_FS_STATUS_PUBLISH_START:
122     ret = event->value.publish.cctx;
123     if (0 == strcmp ("list_indexed-context1", event->value.publish.cctx))
124     {
125       GNUNET_assert (0 ==
126                      strcmp ("list_indexed-context-dir",
127                              event->value.publish.pctx));
128       GNUNET_assert (FILESIZE == event->value.publish.size);
129       GNUNET_assert (0 == event->value.publish.completed);
130       GNUNET_assert (1 == event->value.publish.anonymity);
131     }
132     else if (0 == strcmp ("list_indexed-context2", event->value.publish.cctx))
133     {
134       GNUNET_assert (0 ==
135                      strcmp ("list_indexed-context-dir",
136                              event->value.publish.pctx));
137       GNUNET_assert (FILESIZE == event->value.publish.size);
138       GNUNET_assert (0 == event->value.publish.completed);
139       GNUNET_assert (2 == event->value.publish.anonymity);
140     }
141     else if (0 ==
142              strcmp ("list_indexed-context-dir", event->value.publish.cctx))
143     {
144       GNUNET_assert (0 == event->value.publish.completed);
145       GNUNET_assert (3 == event->value.publish.anonymity);
146     }
147     else
148       GNUNET_assert (0);
149     break;
150   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
151     if (0 == strcmp ("list_indexed-context-dir", event->value.publish.cctx))
152     {
153       GNUNET_assert (publish == event->value.publish.pc);
154       publish = NULL;
155     }
156     break;
157   default:
158     printf ("Unexpected event: %d\n", event->status);
159     break;
160   }
161   return ret;
162 }
163
164
165 static void
166 run (void *cls,
167      const struct GNUNET_CONFIGURATION_Handle *cfg,
168      struct GNUNET_TESTING_Peer *peer)
169 {
170   const char *keywords[] = {
171     "down_foo",
172     "down_bar",
173   };
174   char *buf;
175   struct GNUNET_CONTAINER_MetaData *meta;
176   struct GNUNET_FS_Uri *kuri;
177   struct GNUNET_FS_FileInformation *fi1;
178   struct GNUNET_FS_FileInformation *fi2;
179   struct GNUNET_FS_FileInformation *fidir;
180   size_t i;
181   struct GNUNET_FS_BlockOptions bo;
182
183   fs = GNUNET_FS_start (cfg, "test-fs-list_indexed", &progress_cb, NULL,
184                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
185   GNUNET_assert (NULL != fs);
186   fn1 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
187   buf = GNUNET_malloc (FILESIZE);
188   for (i = 0; i < FILESIZE; i++)
189     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
190   GNUNET_assert (FILESIZE ==
191                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
192                                        GNUNET_DISK_PERM_USER_READ |
193                                        GNUNET_DISK_PERM_USER_WRITE));
194   GNUNET_free (buf);
195
196   fn2 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
197   buf = GNUNET_malloc (FILESIZE);
198   for (i = 0; i < FILESIZE; i++)
199     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
200   GNUNET_assert (FILESIZE ==
201                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
202                                        GNUNET_DISK_PERM_USER_READ |
203                                        GNUNET_DISK_PERM_USER_WRITE));
204   GNUNET_free (buf);
205
206   meta = GNUNET_CONTAINER_meta_data_create ();
207   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
208   bo.content_priority = 42;
209   bo.anonymity_level = 1;
210   bo.replication_level = 0;
211   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
212   fi1 =
213       GNUNET_FS_file_information_create_from_file (fs, "list_indexed-context1",
214                                                    fn1, kuri, meta, GNUNET_YES,
215                                                    &bo);
216   GNUNET_assert (NULL != fi1);
217   bo.anonymity_level = 2;
218   fi2 =
219       GNUNET_FS_file_information_create_from_file (fs, "list_indexed-context2",
220                                                    fn2, kuri, meta, GNUNET_YES,
221                                                    &bo);
222   GNUNET_assert (NULL != fi2);
223   bo.anonymity_level = 3;
224   fidir =
225       GNUNET_FS_file_information_create_empty_directory (fs,
226                                                          "list_indexed-context-dir",
227                                                          kuri, meta, &bo, NULL);
228   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
229   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
230   GNUNET_FS_uri_destroy (kuri);
231   GNUNET_CONTAINER_meta_data_destroy (meta);
232   GNUNET_assert (NULL != fidir);
233   start = GNUNET_TIME_absolute_get ();
234   publish =
235       GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
236                                GNUNET_FS_PUBLISH_OPTION_NONE);
237   GNUNET_assert (publish != NULL);
238 }
239
240
241 int
242 main (int argc, char *argv[])
243 {
244   if (0 != GNUNET_TESTING_peer_run ("test-fs-list-indexed",
245                                     "test_fs_list_indexed_data.conf",
246                                     &run, NULL))
247     return 1;
248   return 0;
249 }
250
251 /* end of test_fs_list_indexed.c */