-fixes for api change in testing library
[oweals/gnunet.git] / src / fs / test_fs_publish_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  * @file fs/test_fs_publish_persistence.c
22  * @brief simple testcase for persistence of simple publish operation
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib-new.h"
28 #include "gnunet_fs_service.h"
29
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 const struct GNUNET_CONFIGURATION_Handle *cfg;
52
53 static struct GNUNET_FS_PublishContext *publish;
54
55 static struct GNUNET_FS_PublishContext *publish;
56
57 static char *fn1;
58
59 static char *fn2;
60
61 static int err;
62
63 static GNUNET_SCHEDULER_TaskIdentifier rtask;
64
65
66 static void
67 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69   GNUNET_FS_publish_stop (publish);
70   publish = NULL;
71   GNUNET_DISK_directory_remove (fn1);
72   GNUNET_free (fn1);
73   fn1 = NULL;
74   GNUNET_DISK_directory_remove (fn2);
75   GNUNET_free (fn2);
76   fn2 = NULL;
77   GNUNET_FS_stop (fs);
78   fs = NULL;
79   if (GNUNET_SCHEDULER_NO_TASK != rtask)
80   {
81     GNUNET_SCHEDULER_cancel (rtask);
82     rtask = GNUNET_SCHEDULER_NO_TASK;
83   }
84 }
85
86
87 static void *
88 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
89
90
91 static void
92 restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
93 {
94   rtask = GNUNET_SCHEDULER_NO_TASK;
95   GNUNET_FS_stop (fs);
96   fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
97                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
98 }
99
100
101 /**
102  * Consider scheduling the restart-task.
103  * Only runs the restart task once per event
104  * category.
105  *
106  * @param ev type of the event to consider
107  */
108 static void
109 consider_restart (int ev)
110 {
111   static int prev[32];
112   static int off;
113   int i;
114
115   for (i = 0; i < off; i++)
116     if (prev[i] == ev)
117       return;
118   prev[off++] = ev;
119   rtask =
120       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
121                                           &restart_fs_task, NULL);
122 }
123
124
125 static void *
126 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
127 {
128   void *ret;
129
130   ret = NULL;
131   switch (event->status)
132   {
133   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
134     consider_restart (event->status);
135     ret = event->value.publish.cctx;
136     printf ("Publish complete,  %llu kbps.\n",
137             (unsigned long long) (FILESIZE * 1000LL /
138                                   (1 +
139                                    GNUNET_TIME_absolute_get_duration
140                                    (start).rel_value) / 1024));
141     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
142       GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
143     break;
144   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
145     consider_restart (event->status);
146     ret = event->value.publish.cctx;
147     GNUNET_assert (publish == event->value.publish.pc);
148 #if VERBOSE
149     printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
150             (unsigned long long) event->value.publish.completed,
151             (unsigned long long) event->value.publish.size,
152             event->value.publish.specifics.progress.depth,
153             (unsigned long long) event->value.publish.specifics.
154             progress.offset);
155 #endif
156     break;
157   case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
158     if (event->value.publish.pc == publish)
159       publish = NULL;
160     break;
161   case GNUNET_FS_STATUS_PUBLISH_RESUME:
162     if (NULL == publish)
163     {
164       GNUNET_assert (GNUNET_YES ==
165                      GNUNET_FS_file_information_is_directory (event->
166                                                               value.publish.
167                                                               fi));
168       publish = event->value.publish.pc;
169       return "publish-context-dir";
170     }
171     break;
172   case GNUNET_FS_STATUS_PUBLISH_ERROR:
173     ret = event->value.publish.cctx;
174     FPRINTF (stderr, "Error publishing file: %s\n",
175              event->value.publish.specifics.error.message);
176     err = 1;
177     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
178     break;
179   case GNUNET_FS_STATUS_PUBLISH_START:
180     consider_restart (event->status);
181     publish = event->value.publish.pc;
182     ret = event->value.publish.cctx;
183     if (0 == strcmp ("publish-context1", event->value.publish.cctx))
184     {
185       GNUNET_assert (0 ==
186                      strcmp ("publish-context-dir", event->value.publish.pctx));
187       GNUNET_assert (FILESIZE == event->value.publish.size);
188       GNUNET_assert (0 == event->value.publish.completed);
189       GNUNET_assert (1 == event->value.publish.anonymity);
190     }
191     else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
192     {
193       GNUNET_assert (0 ==
194                      strcmp ("publish-context-dir", event->value.publish.pctx));
195       GNUNET_assert (FILESIZE == event->value.publish.size);
196       GNUNET_assert (0 == event->value.publish.completed);
197       GNUNET_assert (2 == event->value.publish.anonymity);
198     }
199     else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
200     {
201       GNUNET_assert (0 == event->value.publish.completed);
202       GNUNET_assert (3 == event->value.publish.anonymity);
203     }
204     else
205       GNUNET_assert (0);
206     break;
207   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
208     consider_restart (event->status);
209     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
210       GNUNET_assert (publish == event->value.publish.pc);
211     break;
212   default:
213     printf ("Unexpected event: %d\n", event->status);
214     break;
215   }
216   return ret;
217 }
218
219
220 static void
221 run (void *cls, 
222      const struct GNUNET_CONFIGURATION_Handle *c,
223      struct GNUNET_TESTING_Peer *peer)
224 {
225   const char *keywords[] = {
226     "down_foo",
227     "down_bar",
228   };
229   char *buf;
230   struct GNUNET_CONTAINER_MetaData *meta;
231   struct GNUNET_FS_Uri *kuri;
232   struct GNUNET_FS_FileInformation *fi1;
233   struct GNUNET_FS_FileInformation *fi2;
234   struct GNUNET_FS_FileInformation *fidir;
235   size_t i;
236   struct GNUNET_FS_BlockOptions bo;
237
238   cfg = c;
239   fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
240                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
241   GNUNET_assert (NULL != fs);
242   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
243   buf = GNUNET_malloc (FILESIZE);
244   for (i = 0; i < FILESIZE; i++)
245     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
246   GNUNET_assert (FILESIZE ==
247                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
248                                        GNUNET_DISK_PERM_USER_READ |
249                                        GNUNET_DISK_PERM_USER_WRITE));
250   GNUNET_free (buf);
251
252   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
253   buf = GNUNET_malloc (FILESIZE);
254   for (i = 0; i < FILESIZE; i++)
255     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
256   GNUNET_assert (FILESIZE ==
257                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
258                                        GNUNET_DISK_PERM_USER_READ |
259                                        GNUNET_DISK_PERM_USER_WRITE));
260   GNUNET_free (buf);
261
262   meta = GNUNET_CONTAINER_meta_data_create ();
263   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
264   bo.content_priority = 42;
265   bo.anonymity_level = 1;
266   bo.replication_level = 0;
267   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
268   fi1 =
269       GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
270                                                    kuri, meta, GNUNET_YES, &bo);
271   GNUNET_assert (NULL != fi1);
272   bo.anonymity_level = 2;
273   fi2 =
274       GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
275                                                    kuri, meta, GNUNET_YES, &bo);
276   GNUNET_assert (NULL != fi2);
277   bo.anonymity_level = 3;
278   fidir =
279       GNUNET_FS_file_information_create_empty_directory (fs,
280                                                          "publish-context-dir",
281                                                          kuri, meta, &bo, NULL);
282   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
283   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
284   GNUNET_FS_uri_destroy (kuri);
285   GNUNET_CONTAINER_meta_data_destroy (meta);
286   GNUNET_assert (NULL != fidir);
287   start = GNUNET_TIME_absolute_get ();
288   GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
289                            GNUNET_FS_PUBLISH_OPTION_NONE);
290   GNUNET_assert (publish != NULL);
291 }
292
293
294 int
295 main (int argc, char *argv[])
296 {
297   if (0 != GNUNET_TESTING_peer_run ("test-fs-publish-persistence",
298                                     "test_fs_publish_data.conf",
299                                     &run, NULL))
300     return 1;
301   return err;
302 }
303
304 /* end of test_fs_publish_persistence.c */