glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_fs_search_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  * @file fs/test_fs_search_persistence.c
17  * @brief simple testcase for persistence of search operation
18  * @author Christian Grothoff
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_testing_lib.h"
23 #include "gnunet_fs_service.h"
24
25
26 /**
27  * File-size we use for testing.
28  */
29 #define FILESIZE 1024
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_SearchContext *search;
47
48 static struct GNUNET_FS_PublishContext *publish;
49
50 static const struct GNUNET_CONFIGURATION_Handle *cfg;
51
52 static struct GNUNET_SCHEDULER_Task * timeout_task;
53
54 static int err;
55
56
57 static void
58 abort_error (void *cls)
59 {
60   timeout_task = NULL;
61   fprintf (stderr,
62            "Timeout\n");
63   if (NULL != search)
64   {
65     GNUNET_FS_search_stop (search);
66     search = NULL;
67   }
68   if (NULL != publish)
69   {
70     GNUNET_FS_publish_stop (publish);
71     publish = NULL;
72   }
73   err = 1;
74 }
75
76
77 static void
78 abort_publish_task (void *cls)
79 {
80   if (NULL != publish)
81   {
82     GNUNET_FS_publish_stop (publish);
83     publish = NULL;
84   }
85   if (NULL != timeout_task)
86   {
87     GNUNET_SCHEDULER_cancel (timeout_task);
88     timeout_task = NULL;
89   }
90 }
91
92
93 static void
94 abort_search_task (void *cls)
95 {
96   if (NULL != search)
97   {
98     GNUNET_FS_search_stop (search);
99     search = NULL;
100   }
101 }
102
103
104 static void *
105 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
106
107
108 static void
109 restart_fs_task (void *cls)
110 {
111   GNUNET_FS_stop (fs);
112   fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
113                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
114 }
115
116
117 /**
118  * Consider scheduling the restart-task.
119  * Only runs the restart task once per event
120  * category.
121  *
122  * @param ev type of the event to consider
123  */
124 static void
125 consider_restart (int ev)
126 {
127   static int prev[32];
128   static int off;
129   int i;
130
131   for (i = 0; i < off; i++)
132     if (prev[i] == ev)
133       return;
134   prev[off++] = ev;
135   GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
136                                       &restart_fs_task, NULL);
137 }
138
139
140 static void *
141 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
142 {
143   const char *keywords[] = {
144     "down_foo"
145   };
146   struct GNUNET_FS_Uri *kuri;
147
148   switch (event->status)
149   {
150   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
151     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
153                 (unsigned long long) event->value.publish.completed,
154                 (unsigned long long) event->value.publish.size,
155                 event->value.publish.specifics.progress.depth,
156                 (unsigned long long) event->value.publish.specifics.
157                 progress.offset);
158     break;
159   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
160     break;
161   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
162     kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
163     start = GNUNET_TIME_absolute_get ();
164     GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
165                             "search");
166     GNUNET_FS_uri_destroy (kuri);
167     GNUNET_assert (search != NULL);
168     break;
169   case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
170     if (event->value.publish.pc == publish)
171       publish = NULL;
172     break;
173   case GNUNET_FS_STATUS_PUBLISH_RESUME:
174     if (NULL == publish)
175       publish = event->value.publish.pc;
176     break;
177   case GNUNET_FS_STATUS_SEARCH_RESULT:
178     /* FIXME: consider_restart (event->status); cannot be tested with
179      * search result since we exit here after the first one... */
180     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181                 "Search complete.\n");
182     GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
183     break;
184   case GNUNET_FS_STATUS_PUBLISH_ERROR:
185     FPRINTF (stderr, "Error publishing file: %s\n",
186              event->value.publish.specifics.error.message);
187     GNUNET_break (0);
188     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
189     break;
190   case GNUNET_FS_STATUS_SEARCH_ERROR:
191     FPRINTF (stderr, "Error searching file: %s\n",
192              event->value.search.specifics.error.message);
193     GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
194     break;
195   case GNUNET_FS_STATUS_SEARCH_SUSPEND:
196     if (event->value.search.sc == search)
197       search = NULL;
198     break;
199   case GNUNET_FS_STATUS_SEARCH_RESUME:
200     if (NULL == search)
201     {
202       search = event->value.search.sc;
203       return "search";
204     }
205     break;
206   case GNUNET_FS_STATUS_PUBLISH_START:
207     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
208     GNUNET_assert (NULL == event->value.publish.pctx);
209     GNUNET_assert (FILESIZE == event->value.publish.size);
210     GNUNET_assert (0 == event->value.publish.completed);
211     GNUNET_assert (1 == event->value.publish.anonymity);
212     break;
213   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
214     GNUNET_assert (publish == event->value.publish.pc);
215     GNUNET_assert (FILESIZE == event->value.publish.size);
216     GNUNET_assert (1 == event->value.publish.anonymity);
217     GNUNET_FS_stop (fs);
218     fs = NULL;
219     break;
220   case GNUNET_FS_STATUS_SEARCH_START:
221     consider_restart (event->status);
222     GNUNET_assert (search == NULL);
223     search = event->value.search.sc;
224     GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
225     GNUNET_assert (1 == event->value.search.anonymity);
226     break;
227   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
228     break;
229   case GNUNET_FS_STATUS_SEARCH_STOPPED:
230     GNUNET_assert (search == event->value.search.sc);
231     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
232     search = NULL;
233     break;
234   default:
235     FPRINTF (stderr, "Unexpected event: %d\n", event->status);
236     break;
237   }
238   return NULL;
239 }
240
241
242 static void
243 run (void *cls,
244      const struct GNUNET_CONFIGURATION_Handle *c,
245      struct GNUNET_TESTING_Peer *peer)
246 {
247   const char *keywords[] = {
248     "down_foo",
249     "down_bar"
250   };
251   char *buf;
252   struct GNUNET_CONTAINER_MetaData *meta;
253   struct GNUNET_FS_Uri *kuri;
254   struct GNUNET_FS_FileInformation *fi;
255   size_t i;
256   struct GNUNET_FS_BlockOptions bo;
257
258   cfg = c;
259   fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
260                         GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
261   GNUNET_assert (NULL != fs);
262   buf = GNUNET_malloc (FILESIZE);
263   for (i = 0; i < FILESIZE; i++)
264     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
265   meta = GNUNET_CONTAINER_meta_data_create ();
266   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
267   bo.content_priority = 42;
268   bo.anonymity_level = 1;
269   bo.replication_level = 0;
270   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
271   fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
272                                                     FILESIZE, buf, kuri, meta,
273                                                     GNUNET_NO, &bo);
274   GNUNET_FS_uri_destroy (kuri);
275   GNUNET_CONTAINER_meta_data_destroy (meta);
276   GNUNET_assert (NULL != fi);
277   start = GNUNET_TIME_absolute_get ();
278   publish =
279       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
280                                GNUNET_FS_PUBLISH_OPTION_NONE);
281   GNUNET_assert (publish != NULL);
282   timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
283                                                &abort_error, NULL);
284 }
285
286
287 int
288 main (int argc, char *argv[])
289 {
290   if (0 != GNUNET_TESTING_peer_run ("test-fs-search-persistence",
291                                     "test_fs_search_data.conf",
292                                     &run, NULL))
293     return 1;
294   return err;
295 }
296
297 /* end of test_fs_search_persistence.c */