paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / fs / test_fs_unindex.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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file fs/test_fs_unindex.c
21  * @brief simple testcase for simple publish + unindex operation
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_fs_service.h"
27 #include "gnunet_testing_lib.h"
28
29
30 /**
31  * File-size we use for testing.
32  */
33 #define FILESIZE (1024 * 1024 * 2)
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39
40 /**
41  * How long should our test-content live?
42  */
43 #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
44
45
46 static struct GNUNET_TIME_Absolute start;
47
48 static struct GNUNET_FS_Handle *fs;
49
50 static struct GNUNET_FS_UnindexContext *unindex;
51
52 static struct GNUNET_FS_PublishContext *publish;
53
54 static char *fn;
55
56
57 static void
58 abort_publish_task (void *cls)
59 {
60   GNUNET_FS_publish_stop (publish);
61   publish = NULL;
62 }
63
64
65 static void
66 abort_unindex_task (void *cls)
67 {
68   GNUNET_FS_unindex_stop (unindex);
69   unindex = NULL;
70   GNUNET_DISK_directory_remove (fn);
71   GNUNET_free (fn);
72   fn = NULL;
73 }
74
75
76 static void *
77 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
78 {
79
80   switch (event->status)
81   {
82   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
83     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
85                 (unsigned long long) event->value.publish.completed,
86                 (unsigned long long) event->value.publish.size,
87                 event->value.publish.specifics.progress.depth,
88                 (unsigned long long) event->value.publish.specifics.
89                 progress.offset);
90     break;
91   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
92     break;
93   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
94     printf ("Publishing complete, %llu kbps.\n",
95             (unsigned long long) (FILESIZE * 1000000LL /
96                                   (1 +
97                                    GNUNET_TIME_absolute_get_duration
98                                    (start).rel_value_us) / 1024));
99     start = GNUNET_TIME_absolute_get ();
100     unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
101     GNUNET_assert (unindex != NULL);
102     break;
103   case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
104     printf ("Unindex complete,  %llu kbps.\n",
105             (unsigned long long) (FILESIZE * 1000000LL /
106                                   (1 +
107                                    GNUNET_TIME_absolute_get_duration
108                                    (start).rel_value_us) / 1024));
109     GNUNET_SCHEDULER_add_now (&abort_unindex_task, NULL);
110     break;
111   case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
112     GNUNET_assert (unindex == event->value.unindex.uc);
113     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114                 "Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
115                 (unsigned long long) event->value.unindex.completed,
116                 (unsigned long long) event->value.unindex.size,
117                 event->value.unindex.specifics.progress.depth,
118                 (unsigned long long) event->value.unindex.specifics.
119                 progress.offset);
120     break;
121   case GNUNET_FS_STATUS_PUBLISH_ERROR:
122     FPRINTF (stderr, "Error publishing file: %s\n",
123              event->value.publish.specifics.error.message);
124     GNUNET_break (0);
125     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
126     break;
127   case GNUNET_FS_STATUS_UNINDEX_ERROR:
128     FPRINTF (stderr, "Error unindexing file: %s\n",
129              event->value.unindex.specifics.error.message);
130     GNUNET_SCHEDULER_add_now (&abort_unindex_task, NULL);
131     break;
132   case GNUNET_FS_STATUS_PUBLISH_START:
133     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
134     GNUNET_assert (NULL == event->value.publish.pctx);
135     GNUNET_assert (FILESIZE == event->value.publish.size);
136     GNUNET_assert (0 == event->value.publish.completed);
137     GNUNET_assert (1 == event->value.publish.anonymity);
138     break;
139   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
140     GNUNET_assert (publish == event->value.publish.pc);
141     GNUNET_assert (FILESIZE == event->value.publish.size);
142     GNUNET_assert (1 == event->value.publish.anonymity);
143     GNUNET_FS_stop (fs);
144     fs = NULL;
145     break;
146   case GNUNET_FS_STATUS_UNINDEX_START:
147     GNUNET_assert (unindex == NULL);
148     GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
149     GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
150     GNUNET_assert (FILESIZE == event->value.unindex.size);
151     GNUNET_assert (0 == event->value.unindex.completed);
152     break;
153   case GNUNET_FS_STATUS_UNINDEX_STOPPED:
154     GNUNET_assert (unindex == event->value.unindex.uc);
155     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
156     break;
157   default:
158     printf ("Unexpected event: %d\n", event->status);
159     break;
160   }
161   return NULL;
162 }
163
164
165 static void
166 run (void *cls,
167      const struct GNUNET_CONFIGURATION_Handle *cfg,
168      struct GNUNET_TESTING_Peer *peer)
169 {
170   const char *keywords[] = {
171     "down_foo",
172     "down_bar",
173   };
174   char *buf;
175   struct GNUNET_CONTAINER_MetaData *meta;
176   struct GNUNET_FS_Uri *kuri;
177   struct GNUNET_FS_FileInformation *fi;
178   size_t i;
179   struct GNUNET_FS_BlockOptions bo;
180
181   fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
182   fs = GNUNET_FS_start (cfg, "test-fs-unindex", &progress_cb, NULL,
183                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
184   GNUNET_assert (NULL != fs);
185   buf = GNUNET_malloc (FILESIZE);
186   for (i = 0; i < FILESIZE; i++)
187     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
188   GNUNET_assert (FILESIZE ==
189                  GNUNET_DISK_fn_write (fn, buf, FILESIZE,
190                                        GNUNET_DISK_PERM_USER_READ |
191                                        GNUNET_DISK_PERM_USER_WRITE));
192   GNUNET_free (buf);
193   meta = GNUNET_CONTAINER_meta_data_create ();
194   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
195   bo.content_priority = 42;
196   bo.anonymity_level = 1;
197   bo.replication_level = 0;
198   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
199   fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
200                                                     kuri, meta, GNUNET_YES,
201                                                     &bo);
202   GNUNET_FS_uri_destroy (kuri);
203   GNUNET_CONTAINER_meta_data_destroy (meta);
204   GNUNET_assert (NULL != fi);
205   start = GNUNET_TIME_absolute_get ();
206   publish =
207       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
208                                GNUNET_FS_PUBLISH_OPTION_NONE);
209   GNUNET_assert (publish != NULL);
210 }
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   if (0 != GNUNET_TESTING_peer_run ("test-fs-unindex",
217                                     "test_fs_unindex_data.conf",
218                                     &run, NULL))
219     return 1;
220   return 0;
221 }
222
223 /* end of test_fs_unindex.c */