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 3, 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;
56 struct GNUNET_OS_Process *arm_proc;
60 static struct PeerContext p1;
62 static struct GNUNET_TIME_Absolute start;
64 static struct GNUNET_FS_Handle *fs;
66 static struct GNUNET_FS_PublishContext *publish;
75 abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 GNUNET_FS_publish_stop (publish);
79 GNUNET_DISK_directory_remove (fn1);
82 GNUNET_DISK_directory_remove (fn2);
89 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
94 switch (event->status)
96 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
97 ret = event->value.publish.cctx;
98 printf ("Publish complete, %llu kbps.\n",
99 (unsigned long long) (FILESIZE * 1000 /
101 GNUNET_TIME_absolute_get_duration
102 (start).rel_value) / 1024));
103 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
104 GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
106 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
108 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
109 ret = event->value.publish.cctx;
110 GNUNET_assert (publish == event->value.publish.pc);
112 printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
113 (unsigned long long) event->value.publish.completed,
114 (unsigned long long) event->value.publish.size,
115 event->value.publish.specifics.progress.depth,
116 (unsigned long long) event->value.publish.specifics.
120 case GNUNET_FS_STATUS_PUBLISH_ERROR:
121 ret = event->value.publish.cctx;
123 "Error publishing file: %s\n",
124 event->value.publish.specifics.error.message);
126 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
128 fprintf (stderr, "Scheduling abort task for error on `%s'\n",
129 (const char *) event->value.publish.cctx);
130 GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
132 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
135 case GNUNET_FS_STATUS_PUBLISH_START:
136 ret = event->value.publish.cctx;
137 if (0 == strcmp ("publish-context1", event->value.publish.cctx))
139 GNUNET_assert (0 == strcmp ("publish-context-dir",
140 event->value.publish.pctx));
141 GNUNET_assert (FILESIZE == event->value.publish.size);
142 GNUNET_assert (0 == event->value.publish.completed);
143 GNUNET_assert (1 == event->value.publish.anonymity);
145 else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
147 GNUNET_assert (0 == strcmp ("publish-context-dir",
148 event->value.publish.pctx));
149 GNUNET_assert (FILESIZE == event->value.publish.size);
150 GNUNET_assert (0 == event->value.publish.completed);
151 GNUNET_assert (2 == event->value.publish.anonymity);
153 else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
155 GNUNET_assert (0 == event->value.publish.completed);
156 GNUNET_assert (3 == event->value.publish.anonymity);
161 case GNUNET_FS_STATUS_PUBLISH_STOPPED:
162 if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
163 GNUNET_assert (publish == event->value.publish.pc);
166 printf ("Unexpected event: %d\n", event->status);
174 setup_peer (struct PeerContext *p, const char *cfgname)
176 p->cfg = GNUNET_CONFIGURATION_create ();
178 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
179 "gnunet-service-arm",
183 "-c", cfgname, NULL);
185 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
190 stop_arm (struct PeerContext *p)
193 if (NULL != p->arm_proc)
195 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
197 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
198 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "ARM process %u stopped\n",
201 GNUNET_OS_process_get_pid (p->arm_proc));
202 GNUNET_OS_process_close (p->arm_proc);
206 GNUNET_CONFIGURATION_destroy (p->cfg);
213 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
215 const char *keywords[] = {
220 struct GNUNET_CONTAINER_MetaData *meta;
221 struct GNUNET_FS_Uri *kuri;
222 struct GNUNET_FS_FileInformation *fi1;
223 struct GNUNET_FS_FileInformation *fi2;
224 struct GNUNET_FS_FileInformation *fidir;
226 struct GNUNET_FS_BlockOptions bo;
228 setup_peer (&p1, "test_fs_publish_data.conf");
229 fs = GNUNET_FS_start (cfg,
232 NULL, GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
233 GNUNET_assert (NULL != fs);
234 fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
235 buf = GNUNET_malloc (FILESIZE);
236 for (i = 0; i < FILESIZE; i++)
237 buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
238 GNUNET_assert (FILESIZE ==
239 GNUNET_DISK_fn_write (fn1,
242 GNUNET_DISK_PERM_USER_READ |
243 GNUNET_DISK_PERM_USER_WRITE));
246 fn2 = 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 (fn2,
254 GNUNET_DISK_PERM_USER_READ |
255 GNUNET_DISK_PERM_USER_WRITE));
258 meta = GNUNET_CONTAINER_meta_data_create ();
259 kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
260 bo.content_priority = 42;
261 bo.anonymity_level = 1;
262 bo.replication_level = 0;
263 bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
265 fi1 = GNUNET_FS_file_information_create_from_file (fs,
269 meta, GNUNET_YES, &bo);
271 GNUNET_assert (NULL != fi1);
272 bo.anonymity_level = 2;
273 fi2 = GNUNET_FS_file_information_create_from_file (fs,
277 meta, GNUNET_YES, &bo);
278 GNUNET_assert (NULL != fi2);
279 bo.anonymity_level = 3;
280 fidir = GNUNET_FS_file_information_create_empty_directory (fs,
281 "publish-context-dir",
283 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
284 GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
285 GNUNET_FS_uri_destroy (kuri);
286 GNUNET_CONTAINER_meta_data_destroy (meta);
287 GNUNET_assert (NULL != fidir);
288 start = GNUNET_TIME_absolute_get ();
289 publish = GNUNET_FS_publish_start (fs,
292 GNUNET_FS_PUBLISH_OPTION_NONE);
293 GNUNET_assert (publish != NULL);
298 main (int argc, char *argv[])
300 char *const argvx[] = {
303 "test_fs_publish_data.conf",
309 struct GNUNET_GETOPT_CommandLineOption options[] = {
310 GNUNET_GETOPT_OPTION_END
313 GNUNET_log_setup ("test_fs_publish",
320 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
321 argvx, "test-fs-publish", "nohelp", options, &run, NULL);
323 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-publish/");
326 GNUNET_DISK_directory_remove (fn1);
331 GNUNET_DISK_directory_remove (fn2);
337 /* end of test_fs_publish.c */