summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-05-05 12:19:54 +0000
committerChristian Grothoff <christian@grothoff.org>2010-05-05 12:19:54 +0000
commit4b7f94c75bcc661940324abcad36a3f5b8081399 (patch)
tree97d140e5791a087de78a8269563b08af8bf106f0 /src
parent1a8f8c24386988cac9c7290161f1dd25b652148d (diff)
downloadgnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.tar.gz
gnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.zip
tests
Diffstat (limited to 'src')
-rw-r--r--src/fs/Makefile.am31
-rw-r--r--src/fs/test_fs_download_persistence.c413
-rw-r--r--src/fs/test_fs_publish_persistence.c394
-rw-r--r--src/fs/test_fs_search_persistence.c445
-rw-r--r--src/fs/test_fs_unindex_persistence.c366
5 files changed, 1498 insertions, 151 deletions
diff --git a/src/fs/Makefile.am b/src/fs/Makefile.am
index e497f3e39..66618f9c5 100644
--- a/src/fs/Makefile.am
+++ b/src/fs/Makefile.am
@@ -119,15 +119,19 @@ check_PROGRAMS = \
119 test_fs_collection \ 119 test_fs_collection \
120 test_fs_directory \ 120 test_fs_directory \
121 test_fs_download \ 121 test_fs_download \
122 test_fs_download_persistence \
122 test_fs_file_information \ 123 test_fs_file_information \
123 test_fs_getopt \ 124 test_fs_getopt \
124 test_fs_list_indexed \ 125 test_fs_list_indexed \
125 test_fs_namespace \ 126 test_fs_namespace \
126 test_fs_publish \ 127 test_fs_publish \
128 test_fs_publish_persistence \
127 test_fs_search \ 129 test_fs_search \
130 test_fs_search_persistence \
128 test_fs_start_stop \ 131 test_fs_start_stop \
129 test_fs_test_lib \ 132 test_fs_test_lib \
130 test_fs_unindex \ 133 test_fs_unindex \
134 test_fs_unindex_persistence \
131 test_fs_uri \ 135 test_fs_uri \
132 test_gnunet_service_fs_p2p 136 test_gnunet_service_fs_p2p
133 137
@@ -175,6 +179,13 @@ test_fs_download_LDADD = \
175 $(top_builddir)/src/arm/libgnunetarm.la \ 179 $(top_builddir)/src/arm/libgnunetarm.la \
176 $(top_builddir)/src/util/libgnunetutil.la 180 $(top_builddir)/src/util/libgnunetutil.la
177 181
182test_fs_download_persistence_SOURCES = \
183 test_fs_download_persistence.c
184test_fs_download_persistence_LDADD = \
185 $(top_builddir)/src/fs/libgnunetfs.la \
186 $(top_builddir)/src/arm/libgnunetarm.la \
187 $(top_builddir)/src/util/libgnunetutil.la
188
178test_fs_file_information_SOURCES = \ 189test_fs_file_information_SOURCES = \
179 test_fs_file_information.c 190 test_fs_file_information.c
180test_fs_file_information_LDADD = \ 191test_fs_file_information_LDADD = \
@@ -208,12 +219,25 @@ test_fs_publish_LDADD = \
208 $(top_builddir)/src/arm/libgnunetarm.la \ 219 $(top_builddir)/src/arm/libgnunetarm.la \
209 $(top_builddir)/src/util/libgnunetutil.la 220 $(top_builddir)/src/util/libgnunetutil.la
210 221
222test_fs_publish_persistence_SOURCES = \
223 test_fs_publish_persistence.c
224test_fs_publish_persistence_LDADD = \
225 $(top_builddir)/src/fs/libgnunetfs.la \
226 $(top_builddir)/src/arm/libgnunetarm.la \
227 $(top_builddir)/src/util/libgnunetutil.la
228
211test_fs_search_SOURCES = \ 229test_fs_search_SOURCES = \
212 test_fs_search.c 230 test_fs_search.c
213test_fs_search_LDADD = $(top_builddir)/src/fs/libgnunetfs.la \ 231test_fs_search_LDADD = $(top_builddir)/src/fs/libgnunetfs.la \
214 $(top_builddir)/src/arm/libgnunetarm.la \ 232 $(top_builddir)/src/arm/libgnunetarm.la \
215 $(top_builddir)/src/util/libgnunetutil.la 233 $(top_builddir)/src/util/libgnunetutil.la
216 234
235test_fs_search_persistence_SOURCES = \
236 test_fs_search_persistence.c
237test_fs_search_persistence_LDADD = $(top_builddir)/src/fs/libgnunetfs.la \
238 $(top_builddir)/src/arm/libgnunetarm.la \
239 $(top_builddir)/src/util/libgnunetutil.la
240
217test_fs_start_stop_SOURCES = \ 241test_fs_start_stop_SOURCES = \
218 test_fs_start_stop.c 242 test_fs_start_stop.c
219test_fs_start_stop_LDADD = \ 243test_fs_start_stop_LDADD = \
@@ -228,6 +252,13 @@ test_fs_unindex_LDADD = \
228 $(top_builddir)/src/arm/libgnunetarm.la \ 252 $(top_builddir)/src/arm/libgnunetarm.la \
229 $(top_builddir)/src/util/libgnunetutil.la 253 $(top_builddir)/src/util/libgnunetutil.la
230 254
255test_fs_unindex_persistence_SOURCES = \
256 test_fs_unindex_persistence.c
257test_fs_unindex_persistence_LDADD = \
258 $(top_builddir)/src/fs/libgnunetfs.la \
259 $(top_builddir)/src/arm/libgnunetarm.la \
260 $(top_builddir)/src/util/libgnunetutil.la
261
231test_fs_uri_SOURCES = \ 262test_fs_uri_SOURCES = \
232 test_fs_uri.c 263 test_fs_uri.c
233test_fs_uri_LDADD = \ 264test_fs_uri_LDADD = \
diff --git a/src/fs/test_fs_download_persistence.c b/src/fs/test_fs_download_persistence.c
new file mode 100644
index 000000000..dda7369ef
--- /dev/null
+++ b/src/fs/test_fs_download_persistence.c
@@ -0,0 +1,413 @@
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 2, 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_download_persistence.c
23 * @brief simple testcase for persistence of simple download operation
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_arm_service.h"
30#include "gnunet_fs_service.h"
31
32#define VERBOSE GNUNET_NO
33
34#define START_ARM GNUNET_YES
35
36/**
37 * File-size we use for testing.
38 */
39#define FILESIZE (1024 * 1024 * 2)
40
41/**
42 * How long until we give up on transmitting the message?
43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46/**
47 * How long should our test-content live?
48 */
49#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51struct PeerContext
52{
53 struct GNUNET_CONFIGURATION_Handle *cfg;
54#if START_ARM
55 pid_t arm_pid;
56#endif
57};
58
59static struct PeerContext p1;
60
61static struct GNUNET_TIME_Absolute start;
62
63static struct GNUNET_SCHEDULER_Handle *sched;
64
65static const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67static struct GNUNET_FS_Handle *fs;
68
69static struct GNUNET_FS_DownloadContext *download;
70
71static struct GNUNET_FS_PublishContext *publish;
72
73static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
74
75static char *fn;
76
77static int err;
78
79static void
80timeout_kill_task (void *cls,
81 const struct GNUNET_SCHEDULER_TaskContext *tc)
82{
83 if (download != NULL)
84 {
85 GNUNET_FS_download_stop (download, GNUNET_YES);
86 download = NULL;
87 }
88 else if (publish != NULL)
89 {
90 GNUNET_FS_publish_stop (publish);
91 publish = NULL;
92 }
93 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
94 err = 1;
95}
96
97static void
98abort_publish_task (void *cls,
99 const struct GNUNET_SCHEDULER_TaskContext *tc)
100{
101 if (publish != NULL)
102 {
103 GNUNET_FS_publish_stop (publish);
104 publish = NULL;
105 }
106}
107
108
109static void
110abort_download_task (void *cls,
111 const struct GNUNET_SCHEDULER_TaskContext *tc)
112{
113 uint64_t size;
114
115 if (download != NULL)
116 {
117 GNUNET_FS_download_stop (download, GNUNET_YES);
118 download = NULL;
119 }
120 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
121 GNUNET_assert (size == FILESIZE);
122 GNUNET_DISK_directory_remove (fn);
123 GNUNET_free (fn);
124 fn = NULL;
125 GNUNET_SCHEDULER_cancel (sched, timeout_kill);
126 timeout_kill = GNUNET_SCHEDULER_NO_TASK;
127}
128
129
130static void *
131progress_cb (void *cls,
132 const struct GNUNET_FS_ProgressInfo *event);
133
134
135static void
136restart_fs_task (void *cls,
137 const struct GNUNET_SCHEDULER_TaskContext *tc)
138{
139 GNUNET_FS_stop (fs);
140 fs = GNUNET_FS_start (sched,
141 cfg,
142 "test-fs-download-persistence",
143 &progress_cb,
144 NULL,
145 GNUNET_FS_FLAGS_PERSISTENCE,
146 GNUNET_FS_OPTIONS_END);
147}
148
149
150/**
151 * Consider scheduling the restart-task.
152 * Only runs the restart task once per event
153 * category.
154 *
155 * @param ev type of the event to consider
156 */
157static void
158consider_restart (int ev)
159{
160 static int prev[32];
161 static int off;
162 int i;
163 for (i=0;i<off;i++)
164 if (prev[i] == ev)
165 return;
166 prev[off++] = ev;
167 GNUNET_SCHEDULER_add_with_priority (sched,
168 GNUNET_SCHEDULER_PRIORITY_URGENT,
169 &restart_fs_task,
170 NULL);
171}
172
173
174static void *
175progress_cb (void *cls,
176 const struct GNUNET_FS_ProgressInfo *event)
177{
178
179 switch (event->status)
180 {
181 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
182#if VERBOSE
183 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
184 (unsigned long long) event->value.publish.completed,
185 (unsigned long long) event->value.publish.size,
186 event->value.publish.specifics.progress.depth,
187 (unsigned long long) event->value.publish.specifics.progress.offset);
188#endif
189 break;
190 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
191 printf ("Publishing complete, %llu kbps.\n",
192 (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
193 fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
194 start = GNUNET_TIME_absolute_get ();
195 download = GNUNET_FS_download_start (fs,
196 event->value.publish.specifics.completed.chk_uri,
197 NULL,
198 fn, NULL,
199 0,
200 FILESIZE,
201 1,
202 GNUNET_FS_DOWNLOAD_OPTION_NONE,
203 "download",
204 NULL);
205 GNUNET_assert (download != NULL);
206 break;
207 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
208 consider_restart (event->status);
209 printf ("Download complete, %llu kbps.\n",
210 (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
211 GNUNET_SCHEDULER_add_now (sched,
212 &abort_download_task,
213 NULL);
214 break;
215 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
216 consider_restart (event->status);
217 GNUNET_assert (download == event->value.download.dc);
218#if VERBOSE
219 printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
220 (unsigned long long) event->value.download.completed,
221 (unsigned long long) event->value.download.size,
222 event->value.download.specifics.progress.depth,
223 (unsigned long long) event->value.download.specifics.progress.offset);
224#endif
225 break;
226 case GNUNET_FS_STATUS_PUBLISH_ERROR:
227 fprintf (stderr,
228 "Error publishing file: %s\n",
229 event->value.publish.specifics.error.message);
230 GNUNET_break (0);
231 GNUNET_SCHEDULER_add_continuation (sched,
232 &abort_publish_task,
233 NULL,
234 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
235 break;
236 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
237 fprintf (stderr,
238 "Error downloading file: %s\n",
239 event->value.download.specifics.error.message);
240 GNUNET_SCHEDULER_add_now (sched,
241 &abort_download_task,
242 NULL);
243 break;
244 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
245 consider_restart (event->status);
246 break;
247 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
248 consider_restart (event->status);
249 break;
250 case GNUNET_FS_STATUS_PUBLISH_START:
251 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
252 GNUNET_assert (NULL == event->value.publish.pctx);
253 GNUNET_assert (FILESIZE == event->value.publish.size);
254 GNUNET_assert (0 == event->value.publish.completed);
255 GNUNET_assert (1 == event->value.publish.anonymity);
256 break;
257 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
258 GNUNET_assert (publish == event->value.publish.sc);
259 GNUNET_assert (FILESIZE == event->value.publish.size);
260 GNUNET_assert (1 == event->value.publish.anonymity);
261 GNUNET_FS_stop (fs);
262 fs = NULL;
263 break;
264 case GNUNET_FS_STATUS_DOWNLOAD_START:
265 consider_restart (event->status);
266 GNUNET_assert (download == NULL);
267 GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
268 GNUNET_assert (NULL == event->value.download.pctx);
269 GNUNET_assert (NULL != event->value.download.uri);
270 GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
271 GNUNET_assert (FILESIZE == event->value.download.size);
272 GNUNET_assert (0 == event->value.download.completed);
273 GNUNET_assert (1 == event->value.download.anonymity);
274 break;
275 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
276 consider_restart (event->status);
277 GNUNET_assert (download == event->value.download.dc);
278 GNUNET_SCHEDULER_add_continuation (sched,
279 &abort_publish_task,
280 NULL,
281 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
282 break;
283 default:
284 printf ("Unexpected event: %d\n",
285 event->status);
286 break;
287 }
288 return NULL;
289}
290
291
292static void
293setup_peer (struct PeerContext *p, const char *cfgname)
294{
295 p->cfg = GNUNET_CONFIGURATION_create ();
296#if START_ARM
297 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
298 "gnunet-service-arm",
299#if VERBOSE
300 "-L", "DEBUG",
301#endif
302 "-c", cfgname, NULL);
303#endif
304 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
305}
306
307
308static void
309stop_arm (struct PeerContext *p)
310{
311#if START_ARM
312 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
313 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
314 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
315 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "ARM process %u stopped\n", p->arm_pid);
318#endif
319 GNUNET_CONFIGURATION_destroy (p->cfg);
320}
321
322
323static void
324run (void *cls,
325 struct GNUNET_SCHEDULER_Handle *s,
326 char *const *args,
327 const char *cfgfile,
328 const struct GNUNET_CONFIGURATION_Handle *c)
329{
330 const char *keywords[] = {
331 "down_foo",
332 "down_bar",
333 };
334 char *buf;
335 struct GNUNET_CONTAINER_MetaData *meta;
336 struct GNUNET_FS_Uri *kuri;
337 struct GNUNET_FS_FileInformation *fi;
338 size_t i;
339
340 sched = s;
341 cfg = c;
342 setup_peer (&p1, "test_fs_download_data.conf");
343 fs = GNUNET_FS_start (sched,
344 cfg,
345 "test-fs-download-persistence",
346 &progress_cb,
347 NULL,
348 GNUNET_FS_FLAGS_PERSISTENCE,
349 GNUNET_FS_OPTIONS_END);
350 GNUNET_assert (NULL != fs);
351 buf = GNUNET_malloc (FILESIZE);
352 for (i = 0; i < FILESIZE; i++)
353 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
354 meta = GNUNET_CONTAINER_meta_data_create ();
355 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
356 fi = GNUNET_FS_file_information_create_from_data (fs,
357 "publish-context",
358 FILESIZE,
359 buf,
360 kuri,
361 meta,
362 GNUNET_NO,
363 1,
364 42,
365 GNUNET_TIME_relative_to_absolute (LIFETIME));
366 GNUNET_FS_uri_destroy (kuri);
367 GNUNET_CONTAINER_meta_data_destroy (meta);
368 GNUNET_assert (NULL != fi);
369 timeout_kill = GNUNET_SCHEDULER_add_delayed (sched,
370 TIMEOUT,
371 &timeout_kill_task,
372 NULL);
373 start = GNUNET_TIME_absolute_get ();
374 publish = GNUNET_FS_publish_start (fs,
375 fi,
376 NULL, NULL, NULL,
377 GNUNET_FS_PUBLISH_OPTION_NONE);
378 GNUNET_assert (publish != NULL);
379}
380
381
382int
383main (int argc, char *argv[])
384{
385 char *const argvx[] = {
386 "test-fs-download-persistence",
387 "-c",
388 "test_fs_download_data.conf",
389#if VERBOSE
390 "-L", "DEBUG",
391#endif
392 NULL
393 };
394 struct GNUNET_GETOPT_CommandLineOption options[] = {
395 GNUNET_GETOPT_OPTION_END
396 };
397
398 GNUNET_log_setup ("test_fs_download_persistence",
399#if VERBOSE
400 "DEBUG",
401#else
402 "WARNING",
403#endif
404 NULL);
405 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
406 argvx, "test-fs-download-persistence",
407 "nohelp", options, &run, NULL);
408 stop_arm (&p1);
409 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
410 return err;
411}
412
413/* end of test_fs_download_persistence.c */
diff --git a/src/fs/test_fs_publish_persistence.c b/src/fs/test_fs_publish_persistence.c
new file mode 100644
index 000000000..eb4002bbe
--- /dev/null
+++ b/src/fs/test_fs_publish_persistence.c
@@ -0,0 +1,394 @@
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 2, 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_publish_persistence.c
23 * @brief simple testcase for persistence of simple publish operation
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_arm_service.h"
30#include "gnunet_fs_service.h"
31
32#define VERBOSE GNUNET_NO
33
34#define START_ARM GNUNET_YES
35
36/**
37 * File-size we use for testing.
38 */
39#define FILESIZE (1024 * 1024 * 2)
40
41/**
42 * How long until we give up on transmitting the message?
43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46/**
47 * How long should our test-content live?
48 */
49#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51struct PeerContext
52{
53 struct GNUNET_CONFIGURATION_Handle *cfg;
54#if START_ARM
55 pid_t arm_pid;
56#endif
57};
58
59static struct PeerContext p1;
60
61static struct GNUNET_TIME_Absolute start;
62
63static struct GNUNET_SCHEDULER_Handle *sched;
64
65static struct GNUNET_FS_Handle *fs;
66
67static const struct GNUNET_CONFIGURATION_Handle *cfg;
68
69static struct GNUNET_FS_PublishContext *publish;
70
71static struct GNUNET_FS_PublishContext *publish;
72
73static char *fn1;
74
75static char *fn2;
76
77static int err;
78
79static void
80abort_publish_task (void *cls,
81 const struct GNUNET_SCHEDULER_TaskContext *tc)
82{
83 GNUNET_FS_publish_stop (publish);
84 publish = NULL;
85 GNUNET_DISK_directory_remove (fn1);
86 GNUNET_free (fn1);
87 fn1 = NULL;
88 GNUNET_DISK_directory_remove (fn2);
89 GNUNET_free (fn2);
90 fn2 = NULL;
91}
92
93
94static void *
95progress_cb (void *cls,
96 const struct GNUNET_FS_ProgressInfo *event);
97
98
99static void
100restart_fs_task (void *cls,
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
102{
103 GNUNET_FS_stop (fs);
104 fs = GNUNET_FS_start (sched,
105 cfg,
106 "test-fs-publish-persistence",
107 &progress_cb,
108 NULL,
109 GNUNET_FS_FLAGS_PERSISTENCE,
110 GNUNET_FS_OPTIONS_END);
111}
112
113
114/**
115 * Consider scheduling the restart-task.
116 * Only runs the restart task once per event
117 * category.
118 *
119 * @param ev type of the event to consider
120 */
121static void
122consider_restart (int ev)
123{
124 static int prev[32];
125 static int off;
126 int i;
127 for (i=0;i<off;i++)
128 if (prev[i] == ev)
129 return;
130 prev[off++] = ev;
131 GNUNET_SCHEDULER_add_with_priority (sched,
132 GNUNET_SCHEDULER_PRIORITY_URGENT,
133 &restart_fs_task,
134 NULL);
135}
136
137
138static void *
139progress_cb (void *cls,
140 const struct GNUNET_FS_ProgressInfo *event)
141{
142 void *ret;
143
144 ret = NULL;
145 switch (event->status)
146 {
147 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
148 consider_restart (event->status);
149 ret = event->value.publish.cctx;
150 printf ("Publish complete, %llu kbps.\n",
151 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
152 if (0 == strcmp ("list_indexed-context-dir",
153 event->value.publish.cctx))
154 GNUNET_SCHEDULER_add_continuation (sched,
155 &abort_publish_task,
156 NULL,
157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
158 break;
159 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
160 consider_restart (event->status);
161 ret = event->value.publish.cctx;
162 GNUNET_assert (publish == event->value.publish.sc);
163#if VERBOSE
164 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
165 (unsigned long long) event->value.publish.completed,
166 (unsigned long long) event->value.publish.size,
167 event->value.publish.specifics.progress.depth,
168 (unsigned long long) event->value.publish.specifics.progress.offset);
169#endif
170 break;
171 case GNUNET_FS_STATUS_PUBLISH_ERROR:
172 ret = event->value.publish.cctx;
173 fprintf (stderr,
174 "Error publishing file: %s\n",
175 event->value.publish.specifics.error.message);
176 err = 1;
177 if (0 == strcmp ("list_indexed-context-dir",
178 event->value.publish.cctx))
179 GNUNET_SCHEDULER_add_continuation (sched,
180 &abort_publish_task,
181 NULL,
182 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
183 break;
184 case GNUNET_FS_STATUS_PUBLISH_START:
185 consider_restart (event->status);
186 ret = event->value.publish.cctx;
187 if (0 == strcmp ("publish-context1",
188 event->value.publish.cctx))
189 {
190 GNUNET_assert (0 == strcmp ("publish-context-dir",
191 event->value.publish.pctx));
192 GNUNET_assert (FILESIZE == event->value.publish.size);
193 GNUNET_assert (0 == event->value.publish.completed);
194 GNUNET_assert (1 == event->value.publish.anonymity);
195 }
196 else if (0 == strcmp ("publish-context2",
197 event->value.publish.cctx))
198 {
199 GNUNET_assert (0 == strcmp ("publish-context-dir",
200 event->value.publish.pctx));
201 GNUNET_assert (FILESIZE == event->value.publish.size);
202 GNUNET_assert (0 == event->value.publish.completed);
203 GNUNET_assert (2 == event->value.publish.anonymity);
204 }
205 else if (0 == strcmp ("publish-context-dir",
206 event->value.publish.cctx))
207 {
208 GNUNET_assert (0 == event->value.publish.completed);
209 GNUNET_assert (3 == event->value.publish.anonymity);
210 }
211 else
212 GNUNET_assert (0);
213 break;
214 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
215 consider_restart (event->status);
216 if (0 == strcmp ("list_indexed-context-dir",
217 event->value.publish.cctx))
218 {
219 GNUNET_assert (publish == event->value.publish.sc);
220 GNUNET_SCHEDULER_add_continuation (sched,
221 &abort_publish_task,
222 NULL,
223 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
224 }
225 break;
226 default:
227 printf ("Unexpected event: %d\n",
228 event->status);
229 break;
230 }
231 return ret;
232}
233
234
235static void
236setup_peer (struct PeerContext *p, const char *cfgname)
237{
238 p->cfg = GNUNET_CONFIGURATION_create ();
239#if START_ARM
240 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
241 "gnunet-service-arm",
242#if VERBOSE
243 "-L", "DEBUG",
244#endif
245 "-c", cfgname, NULL);
246#endif
247 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
248}
249
250
251static void
252stop_arm (struct PeerContext *p)
253{
254#if START_ARM
255 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
257 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "ARM process %u stopped\n", p->arm_pid);
261#endif
262 GNUNET_CONFIGURATION_destroy (p->cfg);
263}
264
265
266static void
267run (void *cls,
268 struct GNUNET_SCHEDULER_Handle *s,
269 char *const *args,
270 const char *cfgfile,
271 const struct GNUNET_CONFIGURATION_Handle *c)
272{
273 const char *keywords[] = {
274 "down_foo",
275 "down_bar",
276 };
277 char *buf;
278 struct GNUNET_CONTAINER_MetaData *meta;
279 struct GNUNET_FS_Uri *kuri;
280 struct GNUNET_FS_FileInformation *fi1;
281 struct GNUNET_FS_FileInformation *fi2;
282 struct GNUNET_FS_FileInformation *fidir;
283 size_t i;
284
285 sched = s;
286 cfg = c;
287 setup_peer (&p1, "test_fs_publish_data.conf");
288 fs = GNUNET_FS_start (sched,
289 cfg,
290 "test-fs-publish-persistence",
291 &progress_cb,
292 NULL,
293 GNUNET_FS_FLAGS_PERSISTENCE,
294 GNUNET_FS_OPTIONS_END);
295 GNUNET_assert (NULL != fs);
296 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
297 buf = GNUNET_malloc (FILESIZE);
298 for (i = 0; i < FILESIZE; i++)
299 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
300 GNUNET_assert (FILESIZE ==
301 GNUNET_DISK_fn_write (fn1,
302 buf,
303 FILESIZE,
304 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
305 GNUNET_free (buf);
306
307 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
308 buf = GNUNET_malloc (FILESIZE);
309 for (i = 0; i < FILESIZE; i++)
310 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
311 GNUNET_assert (FILESIZE ==
312 GNUNET_DISK_fn_write (fn2,
313 buf,
314 FILESIZE,
315 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
316 GNUNET_free (buf);
317
318 meta = GNUNET_CONTAINER_meta_data_create ();
319 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
320 fi1 = GNUNET_FS_file_information_create_from_file (fs,
321 "publish-context1",
322 fn1,
323 kuri,
324 meta,
325 GNUNET_YES,
326 1,
327 42,
328 GNUNET_TIME_relative_to_absolute (LIFETIME));
329 fi2 = GNUNET_FS_file_information_create_from_file (fs,
330 "publish-context2",
331 fn2,
332 kuri,
333 meta,
334 GNUNET_YES,
335 2,
336 42,
337 GNUNET_TIME_relative_to_absolute (LIFETIME));
338 fidir = GNUNET_FS_file_information_create_empty_directory (fs,
339 "publish-context-dir",
340 kuri,
341 meta,
342 3,
343 42,
344 GNUNET_TIME_relative_to_absolute (LIFETIME));
345 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
346 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
347 GNUNET_FS_uri_destroy (kuri);
348 GNUNET_CONTAINER_meta_data_destroy (meta);
349 GNUNET_assert (NULL != fidir);
350 start = GNUNET_TIME_absolute_get ();
351 publish = GNUNET_FS_publish_start (fs,
352 fidir,
353 NULL, NULL, NULL,
354 GNUNET_FS_PUBLISH_OPTION_NONE);
355 GNUNET_assert (publish != NULL);
356}
357
358
359int
360main (int argc, char *argv[])
361{
362 char *const argvx[] = {
363 "test-fs-publish-persistence",
364 "-c",
365 "test_fs_publish_data.conf",
366#if VERBOSE
367 "-L", "DEBUG",
368#endif
369 NULL
370 };
371 struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END
373 };
374
375 GNUNET_log_setup ("test_fs_publish_persistence",
376#if VERBOSE
377 "DEBUG",
378#else
379 "WARNING",
380#endif
381 NULL);
382 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
383 argvx, "test-fs-publish",
384 "nohelp", options, &run, NULL);
385 stop_arm (&p1);
386 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
387 GNUNET_DISK_directory_remove (fn1);
388 GNUNET_free_non_null (fn1);
389 GNUNET_DISK_directory_remove (fn2);
390 GNUNET_free_non_null (fn2);
391 return err;
392}
393
394/* end of test_fs_publish_persistence.c */
diff --git a/src/fs/test_fs_search_persistence.c b/src/fs/test_fs_search_persistence.c
index ad0e12b4b..af4344515 100644
--- a/src/fs/test_fs_search_persistence.c
+++ b/src/fs/test_fs_search_persistence.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors) 3 (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -19,195 +19,338 @@
19*/ 19*/
20 20
21/** 21/**
22 * @file applications/fs/fsui/search_persistence_test.c 22 * @file fs/test_fs_search_persistence.c
23 * @brief testcase for fsui download persistence for search 23 * @brief simple testcase for persistence of search operation
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 */ 25 */
26 26
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_util.h" 28#include "gnunet_util_lib.h"
29#include "gnunet_fsui_lib.h" 29#include "gnunet_arm_service.h"
30#include "gnunet_fs_service.h"
30 31
31#define DEBUG_VERBOSE GNUNET_NO 32#define VERBOSE GNUNET_NO
32 33
33#define UPLOAD_PREFIX "/tmp/gnunet-fsui-search_persistence_test" 34#define START_ARM GNUNET_YES
35
36/**
37 * File-size we use for testing.
38 */
39#define FILESIZE 1024
40
41/**
42 * How long until we give up on transmitting the message?
43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
45
46/**
47 * How long should our test-content live?
48 */
49#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
50
51struct PeerContext
52{
53 struct GNUNET_CONFIGURATION_Handle *cfg;
54 struct GNUNET_PeerIdentity id;
55#if START_ARM
56 pid_t arm_pid;
57#endif
58};
59
60static struct PeerContext p1;
61
62static struct GNUNET_TIME_Absolute start;
63
64static struct GNUNET_SCHEDULER_Handle *sched;
65
66static struct GNUNET_FS_Handle *fs;
67
68static struct GNUNET_FS_SearchContext *search;
69
70static struct GNUNET_FS_PublishContext *publish;
71
72static const struct GNUNET_CONFIGURATION_Handle *cfg;
73
74static void
75abort_publish_task (void *cls,
76 const struct GNUNET_SCHEDULER_TaskContext *tc)
77{
78 GNUNET_FS_publish_stop (publish);
79 publish = NULL;
80}
34 81
35#define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
36 82
37static struct GNUNET_GE_Context *ectx; 83static void
84abort_search_task (void *cls,
85 const struct GNUNET_SCHEDULER_TaskContext *tc)
86{
87 if (search != NULL)
88 GNUNET_FS_search_stop (search);
89 search = NULL;
90}
38 91
39static struct GNUNET_FSUI_Context *ctx;
40static struct GNUNET_FSUI_SearchList *search;
41static int have_error;
42 92
43static void * 93static void *
44eventCallback (void *cls, const GNUNET_FSUI_Event * event) 94progress_cb (void *cls,
95 const struct GNUNET_FS_ProgressInfo *event);
96
97
98static void
99restart_fs_task (void *cls,
100 const struct GNUNET_SCHEDULER_TaskContext *tc)
45{ 101{
46 switch (event->type) 102 GNUNET_FS_stop (fs);
103 fs = GNUNET_FS_start (sched,
104 cfg,
105 "test-fs-search-persistence",
106 &progress_cb,
107 NULL,
108 GNUNET_FS_FLAGS_PERSISTENCE,
109 GNUNET_FS_OPTIONS_END);
110}
111
112
113
114
115/**
116 * Consider scheduling the restart-task.
117 * Only runs the restart task once per event
118 * category.
119 *
120 * @param ev type of the event to consider
121 */
122static void
123consider_restart (int ev)
124{
125 static int prev[32];
126 static int off;
127 int i;
128 for (i=0;i<off;i++)
129 if (prev[i] == ev)
130 return;
131 prev[off++] = ev;
132 GNUNET_SCHEDULER_add_with_priority (sched,
133 GNUNET_SCHEDULER_PRIORITY_URGENT,
134 &restart_fs_task,
135 NULL);
136}
137
138
139static void *
140progress_cb (void *cls,
141 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)
47 { 149 {
48 case GNUNET_FSUI_search_suspended: 150 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
49 search = NULL; 151#if VERBOSE
152 printf ("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.progress.offset);
157#endif
50 break; 158 break;
51 case GNUNET_FSUI_search_resumed: 159 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
52#if DEBUG_VERBOSE 160 kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
53 printf ("Search resuming\n"); 161 start = GNUNET_TIME_absolute_get ();
54#endif 162 search = GNUNET_FS_search_start (fs,
55 search = event->data.SearchResumed.sc.pos; 163 kuri,
56 break; 164 1,
57 case GNUNET_FSUI_search_result: 165 GNUNET_FS_SEARCH_OPTION_NONE,
58#if DEBUG_VERBOSE 166 "search");
59 printf ("Received search result\n"); 167 GNUNET_FS_uri_destroy (kuri);
60#endif 168 GNUNET_assert (search != NULL);
61 break; 169 break;
62 case GNUNET_FSUI_upload_progress: 170 case GNUNET_FS_STATUS_SEARCH_RESULT:
63#if DEBUG_VERBOSE 171 consider_restart (event->status);
64 printf ("Upload is progressing (%llu/%llu)...\n", 172#if VERBOSE
65 event->data.UploadProgress.completed, 173 printf ("Search complete.\n");
66 event->data.UploadProgress.total);
67#endif 174#endif
175 GNUNET_SCHEDULER_add_continuation (sched,
176 &abort_search_task,
177 NULL,
178 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
68 break; 179 break;
69 case GNUNET_FSUI_upload_completed: 180 case GNUNET_FS_STATUS_PUBLISH_ERROR:
70#if DEBUG_VERBOSE 181 fprintf (stderr,
71 printf ("Upload complete.\n"); 182 "Error publishing file: %s\n",
72#endif 183 event->value.publish.specifics.error.message);
184 GNUNET_break (0);
185 GNUNET_SCHEDULER_add_continuation (sched,
186 &abort_publish_task,
187 NULL,
188 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
73 break; 189 break;
74 case GNUNET_FSUI_unindex_progress: 190 case GNUNET_FS_STATUS_SEARCH_ERROR:
75#if DEBUG_VERBOSE 191 fprintf (stderr,
76 printf ("Unindex is progressing (%llu/%llu)...\n", 192 "Error searching file: %s\n",
77 event->data.UnindexProgress.completed, 193 event->value.search.specifics.error.message);
78 event->data.UnindexProgress.total); 194 GNUNET_SCHEDULER_add_continuation (sched,
79#endif 195 &abort_search_task,
196 NULL,
197 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
80 break; 198 break;
81 case GNUNET_FSUI_unindex_completed: 199 case GNUNET_FS_STATUS_PUBLISH_START:
82#if DEBUG_VERBOSE 200 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
83 printf ("Unindex complete.\n"); 201 GNUNET_assert (NULL == event->value.publish.pctx);
84#endif 202 GNUNET_assert (FILESIZE == event->value.publish.size);
203 GNUNET_assert (0 == event->value.publish.completed);
204 GNUNET_assert (1 == event->value.publish.anonymity);
85 break; 205 break;
86 case GNUNET_FSUI_unindex_error: 206 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
87 case GNUNET_FSUI_upload_error: 207 GNUNET_assert (publish == event->value.publish.sc);
88 case GNUNET_FSUI_download_error: 208 GNUNET_assert (FILESIZE == event->value.publish.size);
89 fprintf (stderr, "Received ERROR: %d\n", event->type); 209 GNUNET_assert (1 == event->value.publish.anonymity);
90 GNUNET_GE_BREAK (ectx, 0); 210 GNUNET_FS_stop (fs);
211 fs = NULL;
91 break; 212 break;
92 case GNUNET_FSUI_download_aborted: 213 case GNUNET_FS_STATUS_SEARCH_START:
93#if DEBUG_VERBOSE 214 consider_restart (event->status);
94 printf ("Received download aborted event.\n"); 215 GNUNET_assert (search == NULL);
95#endif 216 GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
217 GNUNET_assert (1 == event->value.search.anonymity);
96 break; 218 break;
97 case GNUNET_FSUI_unindex_suspended: 219 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
98 case GNUNET_FSUI_upload_suspended:
99#if DEBUG_VERBOSE
100 fprintf (stderr, "Received SUSPENDING: %d\n", event->type);
101#endif
102 break; 220 break;
103 case GNUNET_FSUI_upload_started: 221 case GNUNET_FS_STATUS_SEARCH_STOPPED:
104 case GNUNET_FSUI_upload_stopped: 222 consider_restart (event->status);
105 case GNUNET_FSUI_search_started: 223 GNUNET_assert (search == event->value.search.sc);
106 case GNUNET_FSUI_search_aborted: 224 GNUNET_SCHEDULER_add_continuation (sched,
107 case GNUNET_FSUI_search_stopped: 225 &abort_publish_task,
108 case GNUNET_FSUI_search_update: 226 NULL,
109 case GNUNET_FSUI_unindex_started: 227 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
110 case GNUNET_FSUI_unindex_stopped:
111 break; 228 break;
112 default: 229 default:
113 printf ("Unexpected event: %d\n", event->type); 230 fprintf (stderr,
231 "Unexpected event: %d\n",
232 event->status);
114 break; 233 break;
115 } 234 }
116 return NULL; 235 return NULL;
117} 236}
118 237
119#define FILESIZE (1024)
120 238
121#define START_DAEMON 1 239static void
240setup_peer (struct PeerContext *p, const char *cfgname)
241{
242 p->cfg = GNUNET_CONFIGURATION_create ();
243#if START_ARM
244 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
245 "gnunet-service-arm",
246#if VERBOSE
247 "-L", "DEBUG",
248#endif
249 "-c", cfgname, NULL);
250#endif
251 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
252}
122 253
123int 254
124main (int argc, char *argv[]) 255static void
256stop_arm (struct PeerContext *p)
125{ 257{
126#if START_DAEMON 258#if START_ARM
127 pid_t daemon; 259 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
260 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
261 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
262 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "ARM process %u stopped\n", p->arm_pid);
128#endif 265#endif
129 int ok; 266 GNUNET_CONFIGURATION_destroy (p->cfg);
130 struct GNUNET_ECRS_URI *uri = NULL; 267}
131 char *keywords[] = { 268
269
270static void
271run (void *cls,
272 struct GNUNET_SCHEDULER_Handle *s,
273 char *const *args,
274 const char *cfgfile,
275 const struct GNUNET_CONFIGURATION_Handle *c)
276{
277 const char *keywords[] = {
132 "down_foo", 278 "down_foo",
133 "down_bar", 279 "down_bar"
134 }; 280 };
135 char keyword[40]; 281 char *buf;
136 int prog; 282 struct GNUNET_CONTAINER_MetaData *meta;
137 struct GNUNET_GC_Configuration *cfg; 283 struct GNUNET_FS_Uri *kuri;
138 int suspendRestart = 0; 284 struct GNUNET_FS_FileInformation *fi;
285 size_t i;
139 286
287 sched = s;
288 cfg = c;
289 setup_peer (&p1, "test_fs_search_data.conf");
290 fs = GNUNET_FS_start (sched,
291 cfg,
292 "test-fs-search-persistence",
293 &progress_cb,
294 NULL,
295 GNUNET_FS_FLAGS_PERSISTENCE,
296 GNUNET_FS_OPTIONS_END);
297 GNUNET_assert (NULL != fs);
298 buf = GNUNET_malloc (FILESIZE);
299 for (i = 0; i < FILESIZE; i++)
300 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
301 meta = GNUNET_CONTAINER_meta_data_create ();
302 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
303 fi = GNUNET_FS_file_information_create_from_data (fs,
304 "publish-context",
305 FILESIZE,
306 buf,
307 kuri,
308 meta,
309 GNUNET_NO,
310 1,
311 42,
312 GNUNET_TIME_relative_to_absolute (LIFETIME));
313 GNUNET_FS_uri_destroy (kuri);
314 GNUNET_CONTAINER_meta_data_destroy (meta);
315 GNUNET_assert (NULL != fi);
316 start = GNUNET_TIME_absolute_get ();
317 publish = GNUNET_FS_publish_start (fs,
318 fi,
319 NULL, NULL, NULL,
320 GNUNET_FS_PUBLISH_OPTION_NONE);
321 GNUNET_assert (publish != NULL);
322}
140 323
141 ok = GNUNET_YES; 324
142 cfg = GNUNET_GC_create (); 325int
143 if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf")) 326main (int argc, char *argv[])
144 { 327{
145 GNUNET_GC_free (cfg); 328 char *const argvx[] = {
146 return -1; 329 "test-fs-search-persistence",
147 } 330 "-c",
148#if START_DAEMON 331 "test_fs_search_data.conf",
149 daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO); 332#if VERBOSE
150 GNUNET_GE_ASSERT (NULL, daemon > 0); 333 "-L", "DEBUG",
151 CHECK (GNUNET_OK ==
152 GNUNET_wait_for_daemon_running (NULL, cfg,
153 30 * GNUNET_CRON_SECONDS));
154 GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
155 /* ACTUAL TEST CODE */
156#endif
157 ctx = GNUNET_FSUI_start (NULL,
158 cfg, "search_persistence_test", 32, GNUNET_YES,
159 &eventCallback, NULL);
160 CHECK (ctx != NULL);
161 GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
162 uri = GNUNET_ECRS_keyword_string_to_uri (ectx, keyword);
163 search = GNUNET_FSUI_search_start (ctx, 0, uri);
164 CHECK (search != NULL);
165 prog = 0;
166 suspendRestart = 10;
167 while (prog < 100)
168 {
169 prog++;
170 GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
171 if ((suspendRestart > 0)
172 && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 10) == 0))
173 {
174#if 1
175#if DEBUG_VERBOSE
176 printf ("Testing FSUI suspend-resume\n");
177#endif
178 GNUNET_FSUI_stop (ctx); /* download possibly incomplete
179 at this point, thus testing resume */
180 CHECK (search == NULL);
181 ctx = GNUNET_FSUI_start (NULL,
182 cfg,
183 "search_persistence_test", 32, GNUNET_YES,
184 &eventCallback, NULL);
185#if DEBUG_VERBOSE
186 printf ("Resumed...\n");
187#endif
188#endif 334#endif
189 suspendRestart--; 335 NULL
190 } 336 };
191 if (GNUNET_shutdown_test () == GNUNET_YES) 337 struct GNUNET_GETOPT_CommandLineOption options[] = {
192 break; 338 GNUNET_GETOPT_OPTION_END
193 } 339 };
194 GNUNET_FSUI_search_abort (search); 340
195 GNUNET_FSUI_search_stop (search); 341 GNUNET_log_setup ("test_fs_search_persistence",
196 search = NULL; 342#if VERBOSE
197 /* END OF TEST CODE */ 343 "DEBUG",
198FAILURE: 344#else
199 if (ctx != NULL) 345 "WARNING",
200 GNUNET_FSUI_stop (ctx);
201 if (uri != NULL)
202 GNUNET_ECRS_uri_destroy (uri);
203
204#if START_DAEMON
205 GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
206#endif 346#endif
207 GNUNET_GC_free (cfg); 347 NULL);
208 if (have_error) 348 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
209 ok = GNUNET_NO; 349 argvx, "test-fs-search-persistencce",
210 return (ok == GNUNET_YES) ? 0 : 1; 350 "nohelp", options, &run, NULL);
351 stop_arm (&p1);
352 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
353 return 0;
211} 354}
212 355
213/* end of search_persistence_test.c */ 356/* end of test_fs_search_persistence.c */
diff --git a/src/fs/test_fs_unindex_persistence.c b/src/fs/test_fs_unindex_persistence.c
new file mode 100644
index 000000000..c964004c1
--- /dev/null
+++ b/src/fs/test_fs_unindex_persistence.c
@@ -0,0 +1,366 @@
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 2, 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_arm_service.h"
29#include "gnunet_fs_service.h"
30
31#define VERBOSE GNUNET_NO
32
33#define START_ARM GNUNET_YES
34
35/**
36 * File-size we use for testing.
37 */
38#define FILESIZE (1024 * 1024 * 2)
39
40/**
41 * How long until we give up on transmitting the message?
42 */
43#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44
45/**
46 * How long should our test-content live?
47 */
48#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
49
50struct PeerContext
51{
52 struct GNUNET_CONFIGURATION_Handle *cfg;
53#if START_ARM
54 pid_t arm_pid;
55#endif
56};
57
58static struct PeerContext p1;
59
60static struct GNUNET_TIME_Absolute start;
61
62static struct GNUNET_SCHEDULER_Handle *sched;
63
64static struct GNUNET_FS_Handle *fs;
65
66static struct GNUNET_FS_UnindexContext *unindex;
67
68static struct GNUNET_FS_PublishContext *publish;
69
70static char *fn;
71
72static const struct GNUNET_CONFIGURATION_Handle *cfg;
73
74static void
75abort_publish_task (void *cls,
76 const struct GNUNET_SCHEDULER_TaskContext *tc)
77{
78 GNUNET_FS_publish_stop (publish);
79 publish = NULL;
80}
81
82
83static void
84abort_unindex_task (void *cls,
85 const struct GNUNET_SCHEDULER_TaskContext *tc)
86{
87 GNUNET_FS_unindex_stop (unindex);
88 unindex = NULL;
89 GNUNET_DISK_directory_remove (fn);
90 GNUNET_free (fn);
91 fn = NULL;
92}
93
94
95static void *
96progress_cb (void *cls,
97 const struct GNUNET_FS_ProgressInfo *event);
98
99
100static void
101restart_fs_task (void *cls,
102 const struct GNUNET_SCHEDULER_TaskContext *tc)
103{
104 GNUNET_FS_stop (fs);
105 fs = GNUNET_FS_start (sched,
106 cfg,
107 "test-fs-unindex-persistence",
108 &progress_cb,
109 NULL,
110 GNUNET_FS_FLAGS_PERSISTENCE,
111 GNUNET_FS_OPTIONS_END);
112}
113
114
115/**
116 * Consider scheduling the restart-task.
117 * Only runs the restart task once per event
118 * category.
119 *
120 * @param ev type of the event to consider
121 */
122static void
123consider_restart (int ev)
124{
125 static int prev[32];
126 static int off;
127 int i;
128 for (i=0;i<off;i++)
129 if (prev[i] == ev)
130 return;
131 prev[off++] = ev;
132 GNUNET_SCHEDULER_add_with_priority (sched,
133 GNUNET_SCHEDULER_PRIORITY_URGENT,
134 &restart_fs_task,
135 NULL);
136}
137
138
139static void *
140progress_cb (void *cls,
141 const struct GNUNET_FS_ProgressInfo *event)
142{
143 switch (event->status)
144 {
145 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
146#if VERBOSE
147 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
148 (unsigned long long) event->value.publish.completed,
149 (unsigned long long) event->value.publish.size,
150 event->value.publish.specifics.progress.depth,
151 (unsigned long long) event->value.publish.specifics.progress.offset);
152#endif
153 break;
154 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
155 printf ("Publishing complete, %llu kbps.\n",
156 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
157 start = GNUNET_TIME_absolute_get ();
158 unindex = GNUNET_FS_unindex_start (fs,
159 fn,
160 "unindex");
161 GNUNET_assert (unindex != NULL);
162 break;
163 case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
164 consider_restart (event->status);
165 printf ("Unindex complete, %llu kbps.\n",
166 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
167 GNUNET_SCHEDULER_add_continuation (sched,
168 &abort_unindex_task,
169 NULL,
170 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
171 break;
172 case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
173 consider_restart (event->status);
174 GNUNET_assert (unindex == event->value.unindex.uc);
175#if VERBOSE
176 printf ("Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
177 (unsigned long long) event->value.unindex.completed,
178 (unsigned long long) event->value.unindex.size,
179 event->value.unindex.specifics.progress.depth,
180 (unsigned long long) event->value.unindex.specifics.progress.offset);
181#endif
182 break;
183 case GNUNET_FS_STATUS_PUBLISH_ERROR:
184 fprintf (stderr,
185 "Error publishing file: %s\n",
186 event->value.publish.specifics.error.message);
187 GNUNET_break (0);
188 GNUNET_SCHEDULER_add_continuation (sched,
189 &abort_publish_task,
190 NULL,
191 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
192 break;
193 case GNUNET_FS_STATUS_UNINDEX_ERROR:
194 fprintf (stderr,
195 "Error unindexing file: %s\n",
196 event->value.unindex.specifics.error.message);
197 GNUNET_SCHEDULER_add_continuation (sched,
198 &abort_unindex_task,
199 NULL,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
201 break;
202 case GNUNET_FS_STATUS_PUBLISH_START:
203 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
204 GNUNET_assert (NULL == event->value.publish.pctx);
205 GNUNET_assert (FILESIZE == event->value.publish.size);
206 GNUNET_assert (0 == event->value.publish.completed);
207 GNUNET_assert (1 == event->value.publish.anonymity);
208 break;
209 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
210 GNUNET_assert (publish == event->value.publish.sc);
211 GNUNET_assert (FILESIZE == event->value.publish.size);
212 GNUNET_assert (1 == event->value.publish.anonymity);
213 GNUNET_FS_stop (fs);
214 fs = NULL;
215 break;
216 case GNUNET_FS_STATUS_UNINDEX_START:
217 consider_restart (event->status);
218 GNUNET_assert (unindex == NULL);
219 GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
220 GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
221 GNUNET_assert (FILESIZE == event->value.unindex.size);
222 GNUNET_assert (0 == event->value.unindex.completed);
223 break;
224 case GNUNET_FS_STATUS_UNINDEX_STOPPED:
225 consider_restart (event->status);
226 GNUNET_assert (unindex == event->value.unindex.uc);
227 GNUNET_SCHEDULER_add_continuation (sched,
228 &abort_publish_task,
229 NULL,
230 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
231 break;
232 default:
233 printf ("Unexpected event: %d\n",
234 event->status);
235 break;
236 }
237 return NULL;
238}
239
240
241static void
242setup_peer (struct PeerContext *p, const char *cfgname)
243{
244 p->cfg = GNUNET_CONFIGURATION_create ();
245#if START_ARM
246 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
247 "gnunet-service-arm",
248#if VERBOSE
249 "-L", "DEBUG",
250#endif
251 "-c", cfgname, NULL);
252#endif
253 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
254}
255
256
257static void
258stop_arm (struct PeerContext *p)
259{
260#if START_ARM
261 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
262 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
263 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
264 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "ARM process %u stopped\n", p->arm_pid);
267#endif
268 GNUNET_CONFIGURATION_destroy (p->cfg);
269}
270
271
272static void
273run (void *cls,
274 struct GNUNET_SCHEDULER_Handle *s,
275 char *const *args,
276 const char *cfgfile,
277 const struct GNUNET_CONFIGURATION_Handle *c)
278{
279 const char *keywords[] = {
280 "down_foo",
281 "down_bar",
282 };
283 char *buf;
284 struct GNUNET_CONTAINER_MetaData *meta;
285 struct GNUNET_FS_Uri *kuri;
286 struct GNUNET_FS_FileInformation *fi;
287 size_t i;
288
289 sched = s;
290 cfg = c;
291 setup_peer (&p1, "test_fs_unindex_data.conf");
292 fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
293 fs = GNUNET_FS_start (sched,
294 cfg,
295 "test-fs-unindex-persistence",
296 &progress_cb,
297 NULL,
298 GNUNET_FS_FLAGS_PERSISTENCE,
299 GNUNET_FS_OPTIONS_END);
300 GNUNET_assert (NULL != fs);
301 buf = GNUNET_malloc (FILESIZE);
302 for (i = 0; i < FILESIZE; i++)
303 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
304 GNUNET_assert (FILESIZE ==
305 GNUNET_DISK_fn_write (fn,
306 buf,
307 FILESIZE,
308 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
309 GNUNET_free (buf);
310 meta = GNUNET_CONTAINER_meta_data_create ();
311 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
312 fi = GNUNET_FS_file_information_create_from_file (fs,
313 "publish-context",
314 fn,
315 kuri,
316 meta,
317 GNUNET_YES,
318 1,
319 42,
320 GNUNET_TIME_relative_to_absolute (LIFETIME));
321 GNUNET_FS_uri_destroy (kuri);
322 GNUNET_CONTAINER_meta_data_destroy (meta);
323 GNUNET_assert (NULL != fi);
324 start = GNUNET_TIME_absolute_get ();
325 publish = GNUNET_FS_publish_start (fs,
326 fi,
327 NULL, NULL, NULL,
328 GNUNET_FS_PUBLISH_OPTION_NONE);
329 GNUNET_assert (publish != NULL);
330}
331
332
333int
334main (int argc, char *argv[])
335{
336 char *const argvx[] = {
337 "test-fs-unindex",
338 "-c",
339 "test_fs_unindex_data.conf",
340#if VERBOSE
341 "-L", "DEBUG",
342#endif
343 NULL
344 };
345 struct GNUNET_GETOPT_CommandLineOption options[] = {
346 GNUNET_GETOPT_OPTION_END
347 };
348
349 GNUNET_log_setup ("test_fs_unindex_persistence",
350#if VERBOSE
351 "DEBUG",
352#else
353 "WARNING",
354#endif
355 NULL);
356 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
357 argvx, "test-fs-unindex",
358 "nohelp", options, &run, NULL);
359 stop_arm (&p1);
360 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-unindex/");
361 GNUNET_DISK_directory_remove (fn);
362 GNUNET_free_non_null (fn);
363 return 0;
364}
365
366/* end of test_fs_unindex_persistence.c */