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