More API function tests...
[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                 enum GNUNET_SET_Status status)
60 {
61   static int count;
62
63   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
64               "Processing result set 1 (%d)\n",
65               status);
66   switch (status)
67   {
68   case GNUNET_SET_STATUS_OK:
69     count++;
70     break;
71   case GNUNET_SET_STATUS_FAILURE:
72     oh1 = NULL;
73     ret = 1;
74     break;
75   case GNUNET_SET_STATUS_DONE:
76     oh1 = NULL;
77     GNUNET_assert (1 == count);
78     GNUNET_SET_destroy (set1);
79     set1 = NULL;
80     if (NULL == set2)
81       GNUNET_SCHEDULER_shutdown ();
82     break;
83   default:
84     GNUNET_assert (0);
85   }
86 }
87
88
89 static void
90 result_cb_set2 (void *cls,
91                 const struct GNUNET_SET_Element *element,
92                 enum GNUNET_SET_Status status)
93 {
94   static int count;
95
96   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
97               "Processing result set 2 (%d)\n",
98               status);
99   switch (status)
100   {
101   case GNUNET_SET_STATUS_OK:
102     count++;
103     break;
104   case GNUNET_SET_STATUS_FAILURE:
105     oh2 = NULL;
106     ret = 1;
107     break;
108   case GNUNET_SET_STATUS_DONE:
109     oh2 = NULL;
110     GNUNET_assert (1 == count);
111     GNUNET_SET_destroy (set2);
112     set2 = NULL;
113     if (NULL == set1)
114       GNUNET_SCHEDULER_shutdown ();
115     break;
116   default:
117     GNUNET_assert (0);
118   }
119 }
120
121
122 static void
123 listen_cb (void *cls,
124            const struct GNUNET_PeerIdentity *other_peer,
125            const struct GNUNET_MessageHeader *context_msg,
126            struct GNUNET_SET_Request *request)
127 {
128   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
129               "starting intersection by accepting and committing\n");
130   GNUNET_assert (NULL != context_msg);
131   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
132   GNUNET_SET_listen_cancel (listen_handle);
133   listen_handle = NULL;
134   oh2 = GNUNET_SET_accept (request,
135                            GNUNET_SET_RESULT_FULL,
136                            &result_cb_set2,
137                            NULL);
138   GNUNET_SET_commit (oh2,
139                      set2);
140 }
141
142
143 /**
144  * Start the set operation.
145  *
146  * @param cls closure, unused
147  */
148 static void
149 start (void *cls)
150 {
151   struct GNUNET_MessageHeader context_msg;
152
153   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154               "starting listener\n");
155   context_msg.size = htons (sizeof context_msg);
156   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
157   listen_handle = GNUNET_SET_listen (config,
158                                      GNUNET_SET_OPERATION_INTERSECTION,
159                                      &app_id,
160                                      &listen_cb,
161                                      NULL);
162   oh1 = GNUNET_SET_prepare (&local_id,
163                             &app_id,
164                             &context_msg,
165                             GNUNET_SET_RESULT_FULL,
166                             &result_cb_set1,
167                             NULL);
168   GNUNET_SET_commit (oh1,
169                      set1);
170 }
171
172
173 /**
174  * Initialize the second set, continue
175  *
176  * @param cls closure, unused
177  */
178 static void
179 init_set2 (void *cls)
180 {
181   struct GNUNET_SET_Element element;
182
183   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184               "initializing set 2\n");
185   element.element_type = 0;
186   element.data = "hello";
187   element.size = strlen(element.data);
188   GNUNET_SET_add_element (set2,
189                           &element,
190                           NULL,
191                           NULL);
192   element.data = "quux";
193   element.size = strlen(element.data);
194   GNUNET_SET_add_element (set2,
195                           &element,
196                           NULL,
197                           NULL);
198   element.data = "baz";
199   element.size = strlen(element.data);
200   GNUNET_SET_add_element (set2,
201                           &element,
202                           &start,
203                           NULL);
204 }
205
206
207 /**
208  * Initialize the first set, continue.
209  */
210 static void
211 init_set1 (void)
212 {
213   struct GNUNET_SET_Element element;
214
215   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
216               "initializing set 1\n");
217   element.element_type = 0;
218   element.data = "hello";
219   element.size = strlen(element.data);
220   GNUNET_SET_add_element (set1,
221                           &element,
222                           NULL,
223                           NULL);
224   element.data = "bar";
225   element.size = strlen(element.data);
226   GNUNET_SET_add_element (set1,
227                           &element,
228                           &init_set2,
229                           NULL);
230 }
231
232
233 static int
234 iter_cb (void *cls,
235          const struct GNUNET_SET_Element *element)
236 {
237   if (NULL == element)
238   {
239     GNUNET_assert (iter_count == 3);
240     GNUNET_SET_destroy (cls);
241     return GNUNET_YES;
242   }
243   iter_count++;
244   return GNUNET_YES;
245 }
246
247
248 static void
249 test_iter ()
250 {
251   struct GNUNET_SET_Element element;
252   struct GNUNET_SET_Handle *iter_set;
253
254   iter_set = GNUNET_SET_create (config,
255                                 GNUNET_SET_OPERATION_INTERSECTION);
256   element.element_type = 0;
257   element.data = "hello";
258   element.size = strlen(element.data);
259   GNUNET_SET_add_element (iter_set,
260                           &element,
261                           NULL,
262                           NULL);
263   element.data = "bar";
264   element.size = strlen(element.data);
265   GNUNET_SET_add_element (iter_set,
266                           &element,
267                           NULL,
268                           NULL);
269   element.data = "quux";
270   element.size = strlen(element.data);
271   GNUNET_SET_add_element (iter_set,
272                           &element,
273                           NULL,
274                           NULL);
275   GNUNET_SET_iterate (iter_set,
276                       &iter_cb,
277                       iter_set);
278 }
279
280
281 /**
282  * Function run on shutdown.
283  *
284  * @param cls closure
285  */
286 static void
287 do_shutdown (void *cls)
288 {
289   if (NULL != tt)
290   {
291     GNUNET_SCHEDULER_cancel (tt);
292     tt = NULL;
293   }
294   if (NULL != oh1)
295   {
296     GNUNET_SET_operation_cancel (oh1);
297     oh1 = NULL;
298   }
299   if (NULL != oh2)
300   {
301     GNUNET_SET_operation_cancel (oh2);
302     oh2 = NULL;
303   }
304   if (NULL != set1)
305   {
306     GNUNET_SET_destroy (set1);
307     set1 = NULL;
308   }
309   if (NULL != set2)
310   {
311     GNUNET_SET_destroy (set2);
312     set2 = NULL;
313   }
314   if (NULL != listen_handle)
315   {
316     GNUNET_SET_listen_cancel (listen_handle);
317     listen_handle = NULL;
318   }
319 }
320
321
322 /**
323  * Function run on timeout.
324  *
325  * @param cls closure
326  */
327 static void
328 timeout_fail (void *cls)
329 {
330   tt = NULL;
331   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
332               "Testcase failed with timeout\n");
333   GNUNET_SCHEDULER_shutdown ();
334   ret = 1;
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   config = cfg;
352   GNUNET_TESTING_peer_get_identity (peer,
353                                     &local_id);
354   if (0)
355     test_iter ();
356
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   set1 = GNUNET_SET_create (cfg,
364                             GNUNET_SET_OPERATION_INTERSECTION);
365   set2 = GNUNET_SET_create (cfg,
366                             GNUNET_SET_OPERATION_INTERSECTION);
367   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
368                                     &app_id);
369
370   /* test the real set reconciliation */
371   init_set1 ();
372 }
373
374
375 int
376 main (int argc,
377       char **argv)
378 {
379   if (0 != GNUNET_TESTING_peer_run ("test_set_intersection_result_full",
380                                     "test_set.conf",
381                                     &run, NULL))
382     return 1;
383   return ret;
384 }