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