generate progress events when publishing directories (towards fixing #2230)
[oweals/gnunet.git] / src / fs / test_fs_unindex_persistence.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2008, 2009, 2010 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_unindex_persistence.c
23  * @brief simple testcase for simple publish + unindex operation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_fs_service.h"
30
31 /**
32  * File-size we use for testing.
33  */
34 #define FILESIZE (1024 * 1024 * 2)
35
36 /**
37  * How long until we give up on transmitting the message?
38  */
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
40
41 /**
42  * How long should our test-content live?
43  */
44 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
45
46
47 static struct GNUNET_TIME_Absolute start;
48
49 static struct GNUNET_FS_Handle *fs;
50
51 static struct GNUNET_FS_UnindexContext *unindex;
52
53 static struct GNUNET_FS_PublishContext *publish;
54
55 static char *fn;
56
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58
59
60 static void
61 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
62 {
63   GNUNET_FS_publish_stop (publish);
64   publish = NULL;
65 }
66
67
68 static void
69 abort_unindex_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   if (unindex != NULL)
72   {
73     GNUNET_FS_unindex_stop (unindex);
74     unindex = NULL;
75   }
76   if (fn != NULL)
77   {
78     GNUNET_DISK_directory_remove (fn);
79     GNUNET_free (fn);
80     fn = NULL;
81   }
82 }
83
84
85 static void *
86 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
87
88
89 static void
90 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 {
92   GNUNET_FS_stop (fs);
93   fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
94                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
95 }
96
97
98 /**
99  * Consider scheduling the restart-task.
100  * Only runs the restart task once per event
101  * category.
102  *
103  * @param ev type of the event to consider
104  */
105 static void
106 consider_restart (int ev)
107 {
108   static int prev[32];
109   static int off;
110   int i;
111
112   for (i = 0; i < off; i++)
113     if (prev[i] == ev)
114       return;
115   prev[off++] = ev;
116   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
117                                       &restart_fs_task, NULL);
118 }
119
120
121 static void *
122 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
123 {
124   switch (event->status)
125   {
126   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
127     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
129                 (unsigned long long) event->value.publish.completed,
130                 (unsigned long long) event->value.publish.size,
131                 event->value.publish.specifics.progress.depth,
132                 (unsigned long long) event->value.publish.specifics.
133                 progress.offset);
134     break;
135   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
136     break;
137   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
138     printf ("Publishing complete, %llu kbps.\n",
139             (unsigned long long) (FILESIZE * 1000000LL /
140                                   (1 +
141                                    GNUNET_TIME_absolute_get_duration
142                                    (start).rel_value_us) / 1024));
143     start = GNUNET_TIME_absolute_get ();
144     unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
145     GNUNET_assert (unindex != NULL);
146     break;
147   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
148     printf ("Unindex complete,  %llu kbps.\n",
149             (unsigned long long) (FILESIZE * 1000000LL /
150                                   (1 +
151                                    GNUNET_TIME_absolute_get_duration
152                                    (start).rel_value_us) / 1024));
153     GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
154                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
155     break;
156   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
157     consider_restart (event->status);
158     GNUNET_assert (unindex == event->value.unindex.uc);
159     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160                 "Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
161                 (unsigned long long) event->value.unindex.completed,
162                 (unsigned long long) event->value.unindex.size,
163                 event->value.unindex.specifics.progress.depth,
164                 (unsigned long long) event->value.unindex.specifics.
165                 progress.offset);
166     break;
167   case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
168     if (event->value.publish.pc == publish)
169       publish = NULL;
170     break;
171   case GNUNET_FS_STATUS_PUBLISH_RESUME:
172     if (NULL == publish)
173     {
174       publish = event->value.publish.pc;
175       return "publish-context";
176     }
177     break;
178   case GNUNET_FS_STATUS_UNINDEX_SUSPEND:
179     GNUNET_assert (event->value.unindex.uc == unindex);
180     unindex = NULL;
181     break;
182   case GNUNET_FS_STATUS_UNINDEX_RESUME:
183     GNUNET_assert (NULL == unindex);
184     unindex = event->value.unindex.uc;
185     return "unindex";
186   case GNUNET_FS_STATUS_PUBLISH_ERROR:
187     FPRINTF (stderr, "Error publishing file: %s\n",
188              event->value.publish.specifics.error.message);
189     GNUNET_break (0);
190     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
191                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
192     break;
193   case GNUNET_FS_STATUS_UNINDEX_ERROR:
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195                 "Error unindexing file: %s\n",
196                 event->value.unindex.specifics.error.message);
197     GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
198                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
199     break;
200   case GNUNET_FS_STATUS_PUBLISH_START:
201     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
202     GNUNET_assert (NULL == event->value.publish.pctx);
203     GNUNET_assert (FILESIZE == event->value.publish.size);
204     GNUNET_assert (0 == event->value.publish.completed);
205     GNUNET_assert (1 == event->value.publish.anonymity);
206     break;
207   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
208     GNUNET_assert (publish == event->value.publish.pc);
209     GNUNET_assert (FILESIZE == event->value.publish.size);
210     GNUNET_assert (1 == event->value.publish.anonymity);
211     GNUNET_FS_stop (fs);
212     fs = NULL;
213     break;
214   case GNUNET_FS_STATUS_UNINDEX_START:
215     consider_restart (event->status);
216     GNUNET_assert (unindex == NULL);
217     GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
218     GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
219     GNUNET_assert (FILESIZE == event->value.unindex.size);
220     GNUNET_assert (0 == event->value.unindex.completed);
221     break;
222   case GNUNET_FS_STATUS_UNINDEX_STOPPED:
223     GNUNET_assert (unindex == event->value.unindex.uc);
224     GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
225                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
226     break;
227   default:
228     printf ("Unexpected event: %d\n", event->status);
229     break;
230   }
231   return NULL;
232 }
233
234
235 static void
236 run (void *cls,
237      const struct GNUNET_CONFIGURATION_Handle *c,
238      struct GNUNET_TESTING_Peer *peer)
239 {
240   const char *keywords[] = {
241     "down_foo",
242     "down_bar",
243   };
244   char *buf;
245   struct GNUNET_CONTAINER_MetaData *meta;
246   struct GNUNET_FS_Uri *kuri;
247   struct GNUNET_FS_FileInformation *fi;
248   size_t i;
249   struct GNUNET_FS_BlockOptions bo;
250
251   cfg = c;
252   fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
253   fs = GNUNET_FS_start (cfg, "test-fs-unindex-persistence", &progress_cb, NULL,
254                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
255   GNUNET_assert (NULL != fs);
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 (fn, buf, FILESIZE,
261                                        GNUNET_DISK_PERM_USER_READ |
262                                        GNUNET_DISK_PERM_USER_WRITE));
263   GNUNET_free (buf);
264   meta = GNUNET_CONTAINER_meta_data_create ();
265   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
266   bo.content_priority = 42;
267   bo.anonymity_level = 1;
268   bo.replication_level = 0;
269   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
270   fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
271                                                     kuri, meta, GNUNET_YES,
272                                                     &bo);
273   GNUNET_FS_uri_destroy (kuri);
274   GNUNET_CONTAINER_meta_data_destroy (meta);
275   GNUNET_assert (NULL != fi);
276   start = GNUNET_TIME_absolute_get ();
277   publish =
278       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
279                                GNUNET_FS_PUBLISH_OPTION_NONE);
280   GNUNET_assert (publish != NULL);
281 }
282
283
284 int
285 main (int argc, char *argv[])
286 {
287   if (0 != GNUNET_TESTING_peer_run ("test-fs-unindex-persistence",
288                                     "test_fs_unindex_data.conf",
289                                     &run, NULL))
290     return 1;
291   return 0;
292 }
293
294 /* end of test_fs_unindex_persistence.c */