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