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