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