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