aboutsummaryrefslogtreecommitdiff
path: root/src/fs/test_fs_list_indexed.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-10-26 07:58:07 +0000
committerChristian Grothoff <christian@grothoff.org>2009-10-26 07:58:07 +0000
commit6fd13d1aaff11441a664de3816d6ca26f8c04ae9 (patch)
treec8d7db930d64cb0ff114c480b22f1c90131855ee /src/fs/test_fs_list_indexed.c
parent6fcb1627fe29487aa40adcb779f12a6e724ac840 (diff)
downloadgnunet-6fd13d1aaff11441a664de3816d6ca26f8c04ae9.tar.gz
gnunet-6fd13d1aaff11441a664de3816d6ca26f8c04ae9.zip
additional fs tests or fs test harnesses
Diffstat (limited to 'src/fs/test_fs_list_indexed.c')
-rw-r--r--src/fs/test_fs_list_indexed.c322
1 files changed, 322 insertions, 0 deletions
diff --git a/src/fs/test_fs_list_indexed.c b/src/fs/test_fs_list_indexed.c
new file mode 100644
index 000000000..1585e2117
--- /dev/null
+++ b/src/fs/test_fs_list_indexed.c
@@ -0,0 +1,322 @@
1/*
2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009 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_list_indexed.c
23 * @brief simple testcase for list_indexed operation (indexing, listing
24 * indexed)
25 * @author Christian Grothoff
26 *
27 * TODO:
28 * - actually call list_indexed API!
29 */
30
31#include "platform.h"
32#include "gnunet_util_lib.h"
33#include "gnunet_arm_service.h"
34#include "gnunet_fs_service.h"
35
36#define VERBOSE GNUNET_NO
37
38#define START_ARM GNUNET_YES
39
40/**
41 * File-size we use for testing.
42 */
43#define FILESIZE (1024 * 1024 * 2)
44
45/**
46 * How long until we give up on transmitting the message?
47 */
48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49
50/**
51 * How long should our test-content live?
52 */
53#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
54
55struct PeerContext
56{
57 struct GNUNET_CONFIGURATION_Handle *cfg;
58 struct GNUNET_PeerIdentity id;
59#if START_ARM
60 pid_t arm_pid;
61#endif
62};
63
64static struct PeerContext p1;
65
66static struct GNUNET_TIME_Absolute start;
67
68static struct GNUNET_SCHEDULER_Handle *sched;
69
70static struct GNUNET_FS_Handle *fs;
71
72static struct GNUNET_FS_PublishContext *publish;
73
74static char *fn1;
75
76static char *fn2;
77
78static int err;
79
80static void
81abort_publish_task (void *cls,
82 const struct GNUNET_SCHEDULER_TaskContext *tc)
83{
84 GNUNET_FS_publish_stop (publish);
85 publish = NULL;
86 GNUNET_DISK_directory_remove (fn1);
87 GNUNET_free (fn1);
88 fn1 = NULL;
89 GNUNET_DISK_directory_remove (fn2);
90 GNUNET_free (fn2);
91 fn2 = NULL;
92}
93
94
95static void
96list_indexed_task (void *cls,
97 const struct GNUNET_SCHEDULER_TaskContext *tc)
98{
99
100 GNUNET_SCHEDULER_add_continuation (sched,
101 GNUNET_NO,
102 &abort_publish_task,
103 NULL,
104 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
105}
106
107
108static void *
109progress_cb (void *cls,
110 const struct GNUNET_FS_ProgressInfo *event)
111{
112
113 switch (event->status)
114 {
115 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
116 printf ("Publish complete, %llu kbps.\n",
117 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
118 GNUNET_SCHEDULER_add_continuation (sched,
119 GNUNET_NO,
120 &list_indexed_task,
121 NULL,
122 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
123 break;
124 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
125 GNUNET_assert (publish == event->value.publish.sc);
126#if VERBOSE
127 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
128 (unsigned long long) event->value.publish.completed,
129 (unsigned long long) event->value.publish.size,
130 event->value.publish.specifics.progress.depth,
131 (unsigned long long) event->value.publish.specifics.progress.offset);
132#endif
133 break;
134 case GNUNET_FS_STATUS_PUBLISH_ERROR:
135 fprintf (stderr,
136 "Error publishing file: %s\n",
137 event->value.publish.specifics.error.message);
138 err = 1;
139 GNUNET_SCHEDULER_add_continuation (sched,
140 GNUNET_NO,
141 &abort_publish_task,
142 NULL,
143 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
144 break;
145 case GNUNET_FS_STATUS_PUBLISH_START:
146 GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
147 GNUNET_assert (NULL == event->value.publish.pctx);
148 GNUNET_assert (FILESIZE == event->value.publish.size);
149 GNUNET_assert (0 == event->value.publish.completed);
150 GNUNET_assert (1 == event->value.publish.anonymity);
151 break;
152 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
153 GNUNET_assert (publish == event->value.publish.sc);
154 publish = NULL;
155 break;
156 default:
157 printf ("Unexpected event: %d\n",
158 event->status);
159 break;
160 }
161 return NULL;
162}
163
164
165static void
166setup_peer (struct PeerContext *p, const char *cfgname)
167{
168 p->cfg = GNUNET_CONFIGURATION_create ();
169#if START_ARM
170 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
171 "gnunet-service-arm",
172#if VERBOSE
173 "-L", "DEBUG",
174#endif
175 "-c", cfgname, NULL);
176 sleep (1); /* allow ARM to start */
177#endif
178 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
179 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
180}
181
182
183static void
184stop_arm (struct PeerContext *p)
185{
186#if START_ARM
187 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
188 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
189 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
190 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "ARM process %u stopped\n", p->arm_pid);
193#endif
194 GNUNET_CONFIGURATION_destroy (p->cfg);
195}
196
197
198static void
199run (void *cls,
200 struct GNUNET_SCHEDULER_Handle *s,
201 char *const *args,
202 const char *cfgfile,
203 const struct GNUNET_CONFIGURATION_Handle *cfg)
204{
205 const char *keywords[] = {
206 "down_foo",
207 "down_bar",
208 };
209 char *buf;
210 struct GNUNET_CONTAINER_MetaData *meta;
211 struct GNUNET_FS_Uri *kuri;
212 struct GNUNET_FS_FileInformation *fi1;
213 struct GNUNET_FS_FileInformation *fi2;
214 struct GNUNET_FS_FileInformation *fidir;
215 size_t i;
216
217 sched = s;
218 setup_peer (&p1, "test_fs_list_indexed_data.conf");
219 fs = GNUNET_FS_start (sched,
220 cfg,
221 "test-fs-list_indexed",
222 &progress_cb,
223 NULL,
224 GNUNET_FS_FLAGS_NONE,
225 GNUNET_FS_OPTIONS_END);
226 GNUNET_assert (NULL != fs);
227 fn1 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
228 buf = GNUNET_malloc (FILESIZE);
229 for (i = 0; i < FILESIZE; i++)
230 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
231 GNUNET_assert (FILESIZE ==
232 GNUNET_DISK_fn_write (fn1,
233 buf,
234 FILESIZE,
235 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
236 GNUNET_free (buf);
237
238 fn2 = GNUNET_DISK_mktemp ("gnunet-list_indexed-test-dst");
239 buf = GNUNET_malloc (FILESIZE);
240 for (i = 0; i < FILESIZE; i++)
241 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
242 GNUNET_assert (FILESIZE ==
243 GNUNET_DISK_fn_write (fn2,
244 buf,
245 FILESIZE,
246 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
247 GNUNET_free (buf);
248
249 meta = GNUNET_CONTAINER_meta_data_create ();
250 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
251 fi1 = GNUNET_FS_file_information_create_from_file ("list_indexed-context1",
252 fn1,
253 kuri,
254 meta,
255 GNUNET_YES,
256 1,
257 42,
258 GNUNET_TIME_relative_to_absolute (LIFETIME));
259 fi2 = GNUNET_FS_file_information_create_from_file ("list_indexed-context2",
260 fn2,
261 kuri,
262 meta,
263 GNUNET_YES,
264 1,
265 42,
266 GNUNET_TIME_relative_to_absolute (LIFETIME));
267 fidir = GNUNET_FS_file_information_create_empty_directory ("list_indexed-context-dir",
268 kuri,
269 meta,
270 1,
271 42,
272 GNUNET_TIME_relative_to_absolute (LIFETIME));
273 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
274 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
275 GNUNET_FS_uri_destroy (kuri);
276 GNUNET_CONTAINER_meta_data_destroy (meta);
277 GNUNET_assert (NULL != fidir);
278 start = GNUNET_TIME_absolute_get ();
279 publish = GNUNET_FS_publish_start (fs,
280 fidir,
281 NULL, NULL, NULL,
282 GNUNET_FS_PUBLISH_OPTION_NONE);
283 GNUNET_assert (publish != NULL);
284}
285
286
287int
288main (int argc, char *argv[])
289{
290 char *const argvx[] = {
291 "test-fs-list_indexed",
292 "-c",
293 "test_fs_list_indexed_data.conf",
294#if VERBOSE
295 "-L", "DEBUG",
296#endif
297 NULL
298 };
299 struct GNUNET_GETOPT_CommandLineOption options[] = {
300 GNUNET_GETOPT_OPTION_END
301 };
302
303 GNUNET_log_setup ("test_fs_list_indexed",
304#if VERBOSE
305 "DEBUG",
306#else
307 "WARNING",
308#endif
309 NULL);
310 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
311 argvx, "test-fs-list_indexed",
312 "nohelp", options, &run, NULL);
313 stop_arm (&p1);
314 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-list_indexed/");
315 GNUNET_DISK_directory_remove (fn1);
316 GNUNET_free_non_null (fn1);
317 GNUNET_DISK_directory_remove (fn2);
318 GNUNET_free_non_null (fn2);
319 return err;
320}
321
322/* end of test_fs_list_indexed.c */