glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_fs_publish.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2008, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file fs/test_fs_publish.c
17  * @brief simple testcase for publish operation (indexing, listing
18  *        indexed, directory structure)
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testing_lib.h"
24 #include "gnunet_fs_service.h"
25
26 /**
27  * File-size we use for testing.
28  */
29 #define FILESIZE (1024 * 1024 * 2)
30
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
35
36 /**
37  * How long should our test-content live?
38  */
39 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
40
41
42 static struct GNUNET_TIME_Absolute start;
43
44 static struct GNUNET_FS_Handle *fs;
45
46 static struct GNUNET_FS_PublishContext *publish;
47
48 static char *fn1;
49
50 static char *fn2;
51
52 static int err;
53
54
55 static void
56 abort_publish_task (void *cls)
57 {
58   GNUNET_FS_publish_stop (publish);
59   publish = NULL;
60   GNUNET_DISK_directory_remove (fn1);
61   GNUNET_free (fn1);
62   fn1 = NULL;
63   GNUNET_DISK_directory_remove (fn2);
64   GNUNET_free (fn2);
65   fn2 = NULL;
66 }
67
68
69 static void *
70 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
71 {
72   void *ret;
73
74   ret = NULL;
75   switch (event->status)
76   {
77   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
78     ret = event->value.publish.cctx;
79     printf ("Publish complete,  %llu kbps.\n",
80             (unsigned long long) (FILESIZE * 1000000LL /
81                                   (1 +
82                                    GNUNET_TIME_absolute_get_duration
83                                    (start).rel_value_us) / 1024));
84     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
85       GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
86     break;
87   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
88     ret = event->value.publish.cctx;
89     GNUNET_assert (publish == event->value.publish.pc);
90     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
92                 (unsigned long long) event->value.publish.completed,
93                 (unsigned long long) event->value.publish.size,
94                 event->value.publish.specifics.progress.depth,
95                 (unsigned long long) event->value.publish.specifics.
96                 progress.offset);
97     break;
98   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
99     ret = event->value.publish.cctx;
100     break;
101   case GNUNET_FS_STATUS_PUBLISH_ERROR:
102     ret = event->value.publish.cctx;
103     FPRINTF (stderr, "Error publishing file: %s\n",
104              event->value.publish.specifics.error.message);
105     err = 1;
106     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
107     {
108       FPRINTF (stderr, "Scheduling abort task for error on `%s'\n",
109                (const char *) event->value.publish.cctx);
110       GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
111     }
112     break;
113   case GNUNET_FS_STATUS_PUBLISH_START:
114     ret = event->value.publish.cctx;
115     if (0 == strcmp ("publish-context1", event->value.publish.cctx))
116     {
117       GNUNET_assert (0 ==
118                      strcmp ("publish-context-dir", event->value.publish.pctx));
119       GNUNET_assert (FILESIZE == event->value.publish.size);
120       GNUNET_assert (0 == event->value.publish.completed);
121       GNUNET_assert (1 == event->value.publish.anonymity);
122     }
123     else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
124     {
125       GNUNET_assert (0 ==
126                      strcmp ("publish-context-dir", event->value.publish.pctx));
127       GNUNET_assert (FILESIZE == event->value.publish.size);
128       GNUNET_assert (0 == event->value.publish.completed);
129       GNUNET_assert (2 == event->value.publish.anonymity);
130     }
131     else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
132     {
133       GNUNET_assert (0 == event->value.publish.completed);
134       GNUNET_assert (3 == event->value.publish.anonymity);
135     }
136     else
137       GNUNET_assert (0);
138     break;
139   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
140     if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
141       GNUNET_assert (publish == event->value.publish.pc);
142     break;
143   default:
144     printf ("Unexpected event: %d\n", event->status);
145     break;
146   }
147   return ret;
148 }
149
150
151 static void
152 run (void *cls,
153      const struct GNUNET_CONFIGURATION_Handle *cfg,
154      struct GNUNET_TESTING_Peer *peer)
155 {
156   const char *keywords[] = {
157     "down_foo",
158     "down_bar",
159   };
160   char *buf;
161   struct GNUNET_CONTAINER_MetaData *meta;
162   struct GNUNET_FS_Uri *kuri;
163   struct GNUNET_FS_FileInformation *fi1;
164   struct GNUNET_FS_FileInformation *fi2;
165   struct GNUNET_FS_FileInformation *fidir;
166   size_t i;
167   struct GNUNET_FS_BlockOptions bo;
168
169   fs = GNUNET_FS_start (cfg, "test-fs-publish", &progress_cb, NULL,
170                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
171   GNUNET_assert (NULL != fs);
172   fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
173   buf = GNUNET_malloc (FILESIZE);
174   for (i = 0; i < FILESIZE; i++)
175     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
176   GNUNET_assert (FILESIZE ==
177                  GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
178                                        GNUNET_DISK_PERM_USER_READ |
179                                        GNUNET_DISK_PERM_USER_WRITE));
180   GNUNET_free (buf);
181
182   fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
183   buf = GNUNET_malloc (FILESIZE);
184   for (i = 0; i < FILESIZE; i++)
185     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
186   GNUNET_assert (FILESIZE ==
187                  GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
188                                        GNUNET_DISK_PERM_USER_READ |
189                                        GNUNET_DISK_PERM_USER_WRITE));
190   GNUNET_free (buf);
191
192   meta = GNUNET_CONTAINER_meta_data_create ();
193   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
194   bo.content_priority = 42;
195   bo.anonymity_level = 1;
196   bo.replication_level = 0;
197   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
198
199   fi1 =
200       GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
201                                                    kuri, meta, GNUNET_YES, &bo);
202
203   GNUNET_assert (NULL != fi1);
204   bo.anonymity_level = 2;
205   fi2 =
206       GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
207                                                    kuri, meta, GNUNET_YES, &bo);
208   GNUNET_assert (NULL != fi2);
209   bo.anonymity_level = 3;
210   fidir =
211       GNUNET_FS_file_information_create_empty_directory (fs,
212                                                          "publish-context-dir",
213                                                          kuri, meta, &bo, NULL);
214   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
215   GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
216   GNUNET_FS_uri_destroy (kuri);
217   GNUNET_CONTAINER_meta_data_destroy (meta);
218   GNUNET_assert (NULL != fidir);
219   start = GNUNET_TIME_absolute_get ();
220   publish =
221       GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
222                                GNUNET_FS_PUBLISH_OPTION_NONE);
223   GNUNET_assert (publish != NULL);
224 }
225
226
227 int
228 main (int argc, char *argv[])
229 {
230   if (0 != GNUNET_TESTING_peer_run ("test-fs-publish",
231                                     "test_fs_publish_data.conf",
232                                     &run, NULL))
233     return 1;
234   return err;
235 }
236
237 /* end of test_fs_publish.c */