2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file fs/test_fs_publish.c
23 * @brief simple testcase for publish operation (indexing, listing
24 * indexed, directory structure)
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_fs_service.h"
33 #define VERBOSE GNUNET_NO
35 #define START_ARM GNUNET_YES
38 * File-size we use for testing.
40 #define FILESIZE (1024 * 1024 * 2)
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
48 * How long should our test-content live?
50 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
54 struct GNUNET_CONFIGURATION_Handle *cfg;
60 static struct PeerContext p1;
62 static struct GNUNET_TIME_Absolute start;
64 static struct GNUNET_SCHEDULER_Handle *sched;
66 static struct GNUNET_FS_Handle *fs;
68 static struct GNUNET_FS_PublishContext *publish;
70 static struct GNUNET_FS_PublishContext *publish;
79 abort_publish_task (void *cls,
80 const struct GNUNET_SCHEDULER_TaskContext *tc)
82 GNUNET_FS_publish_stop (publish);
84 GNUNET_DISK_directory_remove (fn1);
87 GNUNET_DISK_directory_remove (fn2);
94 progress_cb (void *cls,
95 const struct GNUNET_FS_ProgressInfo *event)
100 switch (event->status)
102 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
103 ret = event->value.publish.cctx;
104 printf ("Publish complete, %llu kbps.\n",
105 (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
106 if (0 == strcmp ("list_indexed-context-dir",
107 event->value.publish.cctx))
108 GNUNET_SCHEDULER_add_continuation (sched,
111 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
113 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
114 ret = event->value.publish.cctx;
115 GNUNET_assert (publish == event->value.publish.sc);
117 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
118 (unsigned long long) event->value.publish.completed,
119 (unsigned long long) event->value.publish.size,
120 event->value.publish.specifics.progress.depth,
121 (unsigned long long) event->value.publish.specifics.progress.offset);
124 case GNUNET_FS_STATUS_PUBLISH_ERROR:
125 ret = event->value.publish.cctx;
127 "Error publishing file: %s\n",
128 event->value.publish.specifics.error.message);
130 if (0 == strcmp ("list_indexed-context-dir",
131 event->value.publish.cctx))
132 GNUNET_SCHEDULER_add_continuation (sched,
135 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
137 case GNUNET_FS_STATUS_PUBLISH_START:
138 ret = event->value.publish.cctx;
139 if (0 == strcmp ("publish-context1",
140 event->value.publish.cctx))
142 GNUNET_assert (0 == strcmp ("publish-context-dir",
143 event->value.publish.pctx));
144 GNUNET_assert (FILESIZE == event->value.publish.size);
145 GNUNET_assert (0 == event->value.publish.completed);
146 GNUNET_assert (1 == event->value.publish.anonymity);
148 else if (0 == strcmp ("publish-context2",
149 event->value.publish.cctx))
151 GNUNET_assert (0 == strcmp ("publish-context-dir",
152 event->value.publish.pctx));
153 GNUNET_assert (FILESIZE == event->value.publish.size);
154 GNUNET_assert (0 == event->value.publish.completed);
155 GNUNET_assert (2 == event->value.publish.anonymity);
157 else if (0 == strcmp ("publish-context-dir",
158 event->value.publish.cctx))
160 GNUNET_assert (0 == event->value.publish.completed);
161 GNUNET_assert (3 == event->value.publish.anonymity);
166 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
167 if (0 == strcmp ("list_indexed-context-dir",
168 event->value.publish.cctx))
170 GNUNET_assert (publish == event->value.publish.sc);
171 GNUNET_SCHEDULER_add_continuation (sched,
174 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 printf ("Unexpected event: %d\n",
187 setup_peer (struct PeerContext *p, const char *cfgname)
189 p->cfg = GNUNET_CONFIGURATION_create ();
191 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
192 "gnunet-service-arm",
196 "-c", cfgname, NULL);
198 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
203 stop_arm (struct PeerContext *p)
206 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
207 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
208 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
209 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "ARM process %u stopped\n", p->arm_pid);
213 GNUNET_CONFIGURATION_destroy (p->cfg);
219 struct GNUNET_SCHEDULER_Handle *s,
222 const struct GNUNET_CONFIGURATION_Handle *cfg)
224 const char *keywords[] = {
229 struct GNUNET_CONTAINER_MetaData *meta;
230 struct GNUNET_FS_Uri *kuri;
231 struct GNUNET_FS_FileInformation *fi1;
232 struct GNUNET_FS_FileInformation *fi2;
233 struct GNUNET_FS_FileInformation *fidir;
237 setup_peer (&p1, "test_fs_publish_data.conf");
238 fs = GNUNET_FS_start (sched,
243 GNUNET_FS_FLAGS_NONE,
244 GNUNET_FS_OPTIONS_END);
245 GNUNET_assert (NULL != fs);
246 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
247 buf = GNUNET_malloc (FILESIZE);
248 for (i = 0; i < FILESIZE; i++)
249 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
250 GNUNET_assert (FILESIZE ==
251 GNUNET_DISK_fn_write (fn1,
254 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
257 fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
258 buf = GNUNET_malloc (FILESIZE);
259 for (i = 0; i < FILESIZE; i++)
260 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
261 GNUNET_assert (FILESIZE ==
262 GNUNET_DISK_fn_write (fn2,
265 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
268 meta = GNUNET_CONTAINER_meta_data_create ();
269 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
270 fi1 = GNUNET_FS_file_information_create_from_file ("publish-context1",
277 GNUNET_TIME_relative_to_absolute (LIFETIME));
278 fi2 = GNUNET_FS_file_information_create_from_file ("publish-context2",
285 GNUNET_TIME_relative_to_absolute (LIFETIME));
286 fidir = GNUNET_FS_file_information_create_empty_directory ("publish-context-dir",
291 GNUNET_TIME_relative_to_absolute (LIFETIME));
292 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
293 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
294 GNUNET_FS_uri_destroy (kuri);
295 GNUNET_CONTAINER_meta_data_destroy (meta);
296 GNUNET_assert (NULL != fidir);
297 start = GNUNET_TIME_absolute_get ();
298 publish = GNUNET_FS_publish_start (fs,
301 GNUNET_FS_PUBLISH_OPTION_NONE);
302 GNUNET_assert (publish != NULL);
307 main (int argc, char *argv[])
309 char *const argvx[] = {
312 "test_fs_publish_data.conf",
318 struct GNUNET_GETOPT_CommandLineOption options[] = {
319 GNUNET_GETOPT_OPTION_END
322 GNUNET_log_setup ("test_fs_publish",
329 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
330 argvx, "test-fs-publish",
331 "nohelp", options, &run, NULL);
333 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
334 GNUNET_DISK_directory_remove (fn1);
335 GNUNET_free_non_null (fn1);
336 GNUNET_DISK_directory_remove (fn2);
337 GNUNET_free_non_null (fn2);
341 /* end of test_fs_publish.c */