aboutsummaryrefslogtreecommitdiff
path: root/src/fs/test_fs_download_persistence.c
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/fs/test_fs_download_persistence.c
parent1a8f8c24386988cac9c7290161f1dd25b652148d (diff)
downloadgnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.tar.gz
gnunet-4b7f94c75bcc661940324abcad36a3f5b8081399.zip
tests
Diffstat (limited to 'src/fs/test_fs_download_persistence.c')
-rw-r--r--src/fs/test_fs_download_persistence.c413
1 files changed, 413 insertions, 0 deletions
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 */