fix state when requesting full IBF
[oweals/gnunet.git] / src / set / test_set_api.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
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.
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      General Public License for more details.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file set/test_set_api.c
23  * @brief testcase for set_api.c
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_set_service.h"
30
31
32 static struct GNUNET_PeerIdentity local_id;
33
34 static struct GNUNET_HashCode app_id;
35
36 static struct GNUNET_SET_Handle *set1;
37
38 static struct GNUNET_SET_Handle *set2;
39
40 static struct GNUNET_SET_ListenHandle *listen_handle;
41
42 static struct GNUNET_SET_OperationHandle *oh1;
43
44 static struct GNUNET_SET_OperationHandle *oh2;
45
46 static const struct GNUNET_CONFIGURATION_Handle *config;
47
48 static unsigned int iter_count;
49
50 static int ret;
51
52 static struct GNUNET_SCHEDULER_Task *tt;
53
54
55 static void
56 result_cb_set1 (void *cls,
57                 const struct GNUNET_SET_Element *element,
58                 enum GNUNET_SET_Status status)
59 {
60   switch (status)
61   {
62   case GNUNET_SET_STATUS_OK:
63     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
64                 "set 1: got element\n");
65     break;
66   case GNUNET_SET_STATUS_FAILURE:
67     GNUNET_break (0);
68     oh1 = NULL;
69     fprintf (stderr,
70              "set 1: received failure status!\n");
71     ret = 1;
72     if (NULL != tt)
73     {
74       GNUNET_SCHEDULER_cancel (tt);
75       tt = NULL;
76     }
77     GNUNET_SCHEDULER_shutdown ();
78     break;
79   case GNUNET_SET_STATUS_DONE:
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81                 "set 1: done\n");
82     oh1 = NULL;
83     if (NULL != set1)
84     {
85       GNUNET_SET_destroy (set1);
86       set1 = NULL;
87     }
88     if (NULL == set2)
89     {
90       GNUNET_SCHEDULER_cancel (tt);
91       tt = NULL;
92       GNUNET_SCHEDULER_shutdown ();
93     }
94     break;
95   default:
96     GNUNET_assert (0);
97   }
98 }
99
100
101 static void
102 result_cb_set2 (void *cls,
103                 const struct GNUNET_SET_Element *element,
104                 enum GNUNET_SET_Status status)
105 {
106   switch (status)
107   {
108   case GNUNET_SET_STATUS_OK:
109     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110                 "set 2: got element\n");
111     break;
112   case GNUNET_SET_STATUS_FAILURE:
113     GNUNET_break (0);
114     oh2 = NULL;
115     fprintf (stderr,
116              "set 2: received failure status\n");
117     ret = 1;
118     break;
119   case GNUNET_SET_STATUS_DONE:
120     oh2 = NULL;
121     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122                 "set 2: done\n");
123     GNUNET_SET_destroy (set2);
124     set2 = NULL;
125     if (NULL == set1)
126     {
127       GNUNET_SCHEDULER_cancel (tt);
128       tt = NULL;
129       GNUNET_SCHEDULER_shutdown ();
130     }
131     break;
132   default:
133     GNUNET_assert (0);
134   }
135 }
136
137
138 static void
139 listen_cb (void *cls,
140            const struct GNUNET_PeerIdentity *other_peer,
141            const struct GNUNET_MessageHeader *context_msg,
142            struct GNUNET_SET_Request *request)
143 {
144   GNUNET_assert (NULL != context_msg);
145   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "listen cb called\n");
148   GNUNET_SET_listen_cancel (listen_handle);
149   listen_handle = NULL;
150   oh2 = GNUNET_SET_accept (request,
151                            GNUNET_SET_RESULT_ADDED,
152                            (struct GNUNET_SET_Option[]) { 0 },
153                            &result_cb_set2,
154                            NULL);
155   GNUNET_SET_commit (oh2,
156                      set2);
157 }
158
159
160 /**
161  * Start the set operation.
162  *
163  * @param cls closure, unused
164  */
165 static void
166 start (void *cls)
167 {
168   struct GNUNET_MessageHeader context_msg;
169
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171               "Starting reconciliation\n");
172   context_msg.size = htons (sizeof context_msg);
173   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
174   listen_handle = GNUNET_SET_listen (config,
175                                      GNUNET_SET_OPERATION_UNION,
176                                      &app_id,
177                                      &listen_cb,
178                                      NULL);
179   oh1 = GNUNET_SET_prepare (&local_id,
180                             &app_id,
181                             &context_msg,
182                             GNUNET_SET_RESULT_ADDED,
183                             (struct GNUNET_SET_Option[]) { 0 },
184                             &result_cb_set1,
185                             NULL);
186   GNUNET_SET_commit (oh1,
187                      set1);
188 }
189
190
191 /**
192  * Initialize the second set, continue
193  *
194  * @param cls closure, unused
195  */
196 static void
197 init_set2 (void *cls)
198 {
199   struct GNUNET_SET_Element element;
200
201   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
202
203   element.element_type = 0;
204
205   element.data = "hello";
206   element.size = strlen(element.data);
207   GNUNET_SET_add_element (set2, &element, NULL, NULL);
208   element.data = "quux";
209   element.size = strlen(element.data);
210   GNUNET_SET_add_element (set2, &element, NULL, NULL);
211   element.data = "baz";
212   element.size = strlen(element.data);
213   GNUNET_SET_add_element (set2, &element, &start, NULL);
214 }
215
216
217 /**
218  * Initialize the first set, continue.
219  */
220 static void
221 init_set1 (void)
222 {
223   struct GNUNET_SET_Element element;
224
225   element.element_type = 0;
226
227   element.data = "hello";
228   element.size = strlen(element.data);
229   GNUNET_SET_add_element (set1, &element, NULL, NULL);
230   element.data = "bar";
231   element.size = strlen(element.data);
232   GNUNET_SET_add_element (set1, &element, init_set2, NULL);
233
234   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235               "initialized set 1\n");
236 }
237
238
239 static int
240 iter_cb (void *cls,
241          const struct GNUNET_SET_Element *element)
242 {
243   if (NULL == element)
244   {
245     GNUNET_assert (3 == iter_count);
246     GNUNET_SET_destroy (cls);
247     return GNUNET_YES;
248   }
249   iter_count++;
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251               "iter: got element %u\n",
252               iter_count);
253   return GNUNET_YES;
254 }
255
256
257 static void
258 test_iter ()
259 {
260   struct GNUNET_SET_Element element;
261   struct GNUNET_SET_Handle *iter_set;
262
263   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
264
265   element.element_type = 0;
266
267   element.data = "hello";
268   element.size = strlen(element.data);
269   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
270   element.data = "bar";
271   element.size = strlen(element.data);
272   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
273   element.data = "quux";
274   element.size = strlen(element.data);
275   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
276
277   GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
278 }
279
280
281 /**
282  * Function run on timeout.
283  *
284  * @param cls closure
285  */
286 static void
287 timeout_fail (void *cls)
288 {
289   tt = NULL;
290   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
291               "Testcase failed with timeout\n");
292   GNUNET_SCHEDULER_shutdown ();
293   ret = 1;
294 }
295
296
297 /**
298  * Function run on shutdown.
299  *
300  * @param cls closure
301  */
302 static void
303 do_shutdown (void *cls)
304 {
305   if (NULL != tt)
306   {
307     GNUNET_SCHEDULER_cancel (tt);
308     tt = NULL;
309   }
310   if (NULL != oh1)
311   {
312     GNUNET_SET_operation_cancel (oh1);
313     oh1 = NULL;
314   }
315   if (NULL != oh2)
316   {
317     GNUNET_SET_operation_cancel (oh2);
318     oh2 = NULL;
319   }
320   if (NULL != set1)
321   {
322     GNUNET_SET_destroy (set1);
323     set1 = NULL;
324   }
325   if (NULL != set2)
326   {
327     GNUNET_SET_destroy (set2);
328     set2 = NULL;
329   }
330   if (NULL != listen_handle)
331   {
332     GNUNET_SET_listen_cancel (listen_handle);
333     listen_handle = NULL;
334   }
335 }
336
337
338 /**
339  * Signature of the 'main' function for a (single-peer) testcase that
340  * is run using 'GNUNET_TESTING_peer_run'.
341  *
342  * @param cls closure
343  * @param cfg configuration of the peer that was started
344  * @param peer identity of the peer that was created
345  */
346 static void
347 run (void *cls,
348      const struct GNUNET_CONFIGURATION_Handle *cfg,
349      struct GNUNET_TESTING_Peer *peer)
350 {
351
352   struct GNUNET_SET_OperationHandle *my_oh;
353
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355               "Running preparatory tests\n");
356   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
357                                      &timeout_fail,
358                                      NULL);
359   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
360                                  NULL);
361
362   config = cfg;
363   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
364   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365               "my id (from CRYPTO): %s\n",
366               GNUNET_i2s (&local_id));
367   GNUNET_TESTING_peer_get_identity (peer, &local_id);
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369               "my id (from TESTING): %s\n",
370               GNUNET_i2s (&local_id));
371   test_iter ();
372
373   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
374   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
375   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
376                                     &app_id);
377
378   ///* test if canceling an uncommited request works! */
379   my_oh = GNUNET_SET_prepare (&local_id,
380                               &app_id,
381                               NULL,
382                               GNUNET_SET_RESULT_ADDED,
383                               (struct GNUNET_SET_Option[]) { 0 },
384                               NULL,
385                               NULL);
386
387   GNUNET_SET_operation_cancel (my_oh);
388
389   /* test the real set reconciliation */
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391               "Running real set-reconciliation\n");
392   init_set1 ();
393 }
394
395
396 int
397 main (int argc, char **argv)
398 {
399   GNUNET_log_setup ("test_set_api",
400                     "WARNING",
401                     NULL);
402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403               "Launching peer\n");
404   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
405                                     "test_set.conf",
406                                     &run, NULL))
407   {
408     return 1;
409   }
410   return ret;
411 }