glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / fs / test_fs_search_probes.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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_search_probes.c
18  * @brief simple testcase for publish + search operation with probes
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 /**
28  * File-size we use for testing.
29  */
30 #define FILESIZE 1024
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_SearchContext *search;
48
49 static struct GNUNET_FS_PublishContext *publish;
50
51 static struct GNUNET_SCHEDULER_Task * timeout_task;
52
53 static int err;
54
55
56 static void
57 abort_error (void *cls)
58 {
59   timeout_task = NULL;
60   fprintf (stderr,
61            "Timeout\n");
62   if (NULL != search)
63   {
64     GNUNET_FS_search_stop (search);
65     search = NULL;
66   }
67   if (NULL != publish)
68   {
69     GNUNET_FS_publish_stop (publish);
70     publish = NULL;
71   }
72   err = 1;
73 }
74
75
76 static void
77 abort_publish_task (void *cls)
78 {
79   if (NULL != publish)
80   {
81     GNUNET_FS_publish_stop (publish);
82     publish = NULL;
83   }
84   if (NULL != timeout_task)
85   {
86     GNUNET_SCHEDULER_cancel (timeout_task);
87     timeout_task = NULL;
88   }
89 }
90
91
92 static void
93 abort_search_task (void *cls)
94 {
95   if (search != NULL)
96     GNUNET_FS_search_stop (search);
97   search = NULL;
98 }
99
100
101 static void *
102 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
103 {
104   const char *keywords[] = {
105     "down_foo"
106   };
107   struct GNUNET_FS_Uri *kuri;
108
109   switch (event->status)
110   {
111   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
112     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113                 "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
114                 (unsigned long long) event->value.publish.completed,
115                 (unsigned long long) event->value.publish.size,
116                 event->value.publish.specifics.progress.depth,
117                 (unsigned long long) event->value.publish.specifics.
118                 progress.offset);
119     break;
120   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
121     break;
122   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
123     kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
124     start = GNUNET_TIME_absolute_get ();
125     search =
126         GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
127                                 "search");
128     GNUNET_FS_uri_destroy (kuri);
129     GNUNET_assert (search != NULL);
130     break;
131   case GNUNET_FS_STATUS_SEARCH_RESULT:
132     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Search complete.\n");
133     break;
134   case GNUNET_FS_STATUS_PUBLISH_ERROR:
135     FPRINTF (stderr, "Error publishing file: %s\n",
136              event->value.publish.specifics.error.message);
137     GNUNET_break (0);
138     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
139     break;
140   case GNUNET_FS_STATUS_SEARCH_ERROR:
141     FPRINTF (stderr, "Error searching file: %s\n",
142              event->value.search.specifics.error.message);
143     GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
144     break;
145   case GNUNET_FS_STATUS_PUBLISH_START:
146     GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
147     GNUNET_assert (NULL == event->value.publish.pctx);
148     GNUNET_assert (FILESIZE == event->value.publish.size);
149     GNUNET_assert (0 == event->value.publish.completed);
150     GNUNET_assert (1 == event->value.publish.anonymity);
151     break;
152   case GNUNET_FS_STATUS_PUBLISH_STOPPED:
153     GNUNET_assert (publish == event->value.publish.pc);
154     GNUNET_assert (FILESIZE == event->value.publish.size);
155     GNUNET_assert (1 == event->value.publish.anonymity);
156     GNUNET_FS_stop (fs);
157     fs = NULL;
158     break;
159   case GNUNET_FS_STATUS_SEARCH_UPDATE:
160     if ( (0 < event->value.search.specifics.update.availability_rank) &&
161          (0 < event->value.search.specifics.update.availability_certainty) )
162       GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
163     break;
164   case GNUNET_FS_STATUS_SEARCH_START:
165     GNUNET_assert (search == NULL);
166     GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
167     GNUNET_assert (1 == event->value.search.anonymity);
168     break;
169   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
170     break;
171   case GNUNET_FS_STATUS_SEARCH_STOPPED:
172     GNUNET_assert (search == event->value.search.sc);
173     GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
174     break;
175   default:
176     FPRINTF (stderr,
177              "Unexpected event: %d\n",
178              event->status);
179     break;
180   }
181   return NULL;
182 }
183
184
185 static void
186 run (void *cls,
187      const struct GNUNET_CONFIGURATION_Handle *cfg,
188      struct GNUNET_TESTING_Peer *peer)
189 {
190   const char *keywords[] = {
191     "down_foo",
192     "down_bar"
193   };
194   char *buf;
195   struct GNUNET_CONTAINER_MetaData *meta;
196   struct GNUNET_FS_Uri *kuri;
197   struct GNUNET_FS_BlockOptions bo;
198   struct GNUNET_FS_FileInformation *fi;
199   size_t i;
200
201   fs = GNUNET_FS_start (cfg, "test-fs-search", &progress_cb, NULL,
202                         GNUNET_FS_FLAGS_DO_PROBES,
203                         GNUNET_FS_OPTIONS_END);
204   GNUNET_assert (NULL != fs);
205   buf = GNUNET_malloc (FILESIZE);
206   for (i = 0; i < FILESIZE; i++)
207     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
208   meta = GNUNET_CONTAINER_meta_data_create ();
209   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
210   bo.content_priority = 42;
211   bo.anonymity_level = 1;
212   bo.replication_level = 0;
213   bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
214   fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
215                                                     FILESIZE, buf, kuri, meta,
216                                                     GNUNET_NO, &bo);
217   GNUNET_FS_uri_destroy (kuri);
218   GNUNET_CONTAINER_meta_data_destroy (meta);
219   GNUNET_assert (NULL != fi);
220   start = GNUNET_TIME_absolute_get ();
221   publish =
222       GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
223                                GNUNET_FS_PUBLISH_OPTION_NONE);
224   GNUNET_assert (publish != NULL);
225   timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
226                                                &abort_error, NULL);
227 }
228
229
230 int
231 main (int argc, char *argv[])
232 {
233   if (0 != GNUNET_TESTING_peer_run ("test-fs-search-probes",
234                                     "test_fs_search_data.conf",
235                                     &run, NULL))
236     return 1;
237   return err;
238 }
239
240 /* end of test_fs_search_probes.c */