- refactor kx sending, unify under send_kx
[oweals/gnunet.git] / src / set / test_set_union_result_full.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file set/test_set_union_result_full.c
23  * @brief testcase for full result mode of the union set operation
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_set_service.h"
29
30
31 /**
32  * Value to return from #main().
33  */
34 static int ret;
35
36 static struct GNUNET_PeerIdentity local_id;
37
38 static struct GNUNET_HashCode app_id;
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 /**
50  * Are we testing correctness for the empty set union?
51  */
52 static int empty;
53
54 /**
55  * Number of elements found in set 1
56  */
57 static unsigned int count_set1;
58
59 /**
60  * Number of elements found in set 2
61  */
62 static unsigned int count_set2;
63
64
65 static void
66 result_cb_set1 (void *cls,
67                 const struct GNUNET_SET_Element *element,
68                 enum GNUNET_SET_Status status)
69 {
70   switch (status)
71   {
72     case GNUNET_SET_STATUS_OK:
73       count_set1++;
74       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75                   "set 1: got element\n");
76       break;
77     case GNUNET_SET_STATUS_FAILURE:
78       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
79                   "set 1: failure\n");
80       ret = 1;
81       GNUNET_SCHEDULER_shutdown ();
82       break;
83     case GNUNET_SET_STATUS_DONE:
84       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85                   "set 1: done\n");
86       GNUNET_SET_destroy (set1);
87       set1 = NULL;
88       if (NULL == set2)
89         GNUNET_SCHEDULER_shutdown ();
90       break;
91     default:
92       GNUNET_assert (0);
93   }
94 }
95
96
97 static void
98 result_cb_set2 (void *cls,
99                 const struct GNUNET_SET_Element *element,
100                 enum GNUNET_SET_Status status)
101 {
102   switch (status)
103   {
104     case GNUNET_SET_STATUS_OK:
105       count_set2++;
106       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107                   "set 2: got element\n");
108       break;
109     case GNUNET_SET_STATUS_FAILURE:
110       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111                   "set 2: failure\n");
112       ret = 1;
113       GNUNET_SCHEDULER_shutdown ();
114       break;
115     case GNUNET_SET_STATUS_DONE:
116       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117                   "set 2: done\n");
118       GNUNET_SET_destroy (set2);
119       set2 = NULL;
120       if (NULL == set1)
121         GNUNET_SCHEDULER_shutdown ();
122       break;
123     default:
124       GNUNET_assert (0);
125   }
126 }
127
128
129 static void
130 listen_cb (void *cls,
131            const struct GNUNET_PeerIdentity *other_peer,
132            const struct GNUNET_MessageHeader *context_msg,
133            struct GNUNET_SET_Request *request)
134 {
135   struct GNUNET_SET_OperationHandle *oh;
136
137   GNUNET_assert (NULL != context_msg);
138   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140               "listen cb called\n");
141   GNUNET_SET_listen_cancel (listen_handle);
142   oh = GNUNET_SET_accept (request,
143                           GNUNET_SET_RESULT_FULL,
144                           &result_cb_set2,
145                           NULL);
146   GNUNET_SET_commit (oh, set2);
147 }
148
149
150 /**
151  * Start the set operation.
152  *
153  * @param cls closure, unused
154  */
155 static void
156 start (void *cls)
157 {
158   struct GNUNET_SET_OperationHandle *oh;
159   struct GNUNET_MessageHeader context_msg;
160
161   context_msg.size = htons (sizeof context_msg);
162   context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
163
164   listen_handle = GNUNET_SET_listen (config,
165                                      GNUNET_SET_OPERATION_UNION,
166                                      &app_id,
167                                      &listen_cb, NULL);
168   oh = GNUNET_SET_prepare (&local_id,
169                            &app_id,
170                            &context_msg,
171                            GNUNET_SET_RESULT_FULL,
172                            &result_cb_set1, NULL);
173   GNUNET_SET_commit (oh, 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_DEBUG,
188               "initializing set 2\n");
189   if (empty)
190   {
191     start (NULL);
192     return;
193   }
194   element.element_type = 0;
195   element.data = "hello";
196   element.size = strlen(element.data);
197   GNUNET_SET_add_element (set2,
198                           &element,
199                           NULL,
200                           NULL);
201   element.data = "quux";
202   element.size = strlen(element.data);
203   GNUNET_SET_add_element (set2,
204                           &element,
205                           NULL,
206                           NULL);
207   element.data = "baz";
208   element.size = strlen(element.data);
209   GNUNET_SET_add_element (set2,
210                           &element,
211                           &start, NULL);
212 }
213
214
215 /**
216  * Initialize the first set, continue.
217  */
218 static void
219 init_set1 (void)
220 {
221   struct GNUNET_SET_Element element;
222
223   if (empty)
224   {
225     init_set2 (NULL);
226     return;
227   }
228   element.element_type = 0;
229   element.data = "hello";
230   element.size = strlen(element.data);
231   GNUNET_SET_add_element (set1,
232                           &element,
233                           NULL,
234                           NULL);
235   element.data = "bar";
236   element.size = strlen(element.data);
237   GNUNET_SET_add_element (set1,
238                           &element,
239                           &init_set2,
240                           NULL);
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242               "initialized set 1\n");
243 }
244
245
246 static int
247 iter_cb (void *cls,
248          const struct GNUNET_SET_Element *element)
249 {
250   if (NULL == element)
251   {
252     GNUNET_assert (iter_count == 3);
253     GNUNET_SET_destroy (cls);
254     return GNUNET_YES;
255   }
256   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257               "iter: got element\n");
258   iter_count++;
259   return GNUNET_YES;
260 }
261
262
263 static void
264 test_iter ()
265 {
266   struct GNUNET_SET_Element element;
267   struct GNUNET_SET_Handle *iter_set;
268
269   iter_count = 0;
270   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
271   element.element_type = 0;
272   element.data = "hello";
273   element.size = strlen(element.data);
274   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
275   element.data = "bar";
276   element.size = strlen(element.data);
277   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
278   element.data = "quux";
279   element.size = strlen(element.data);
280   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
281
282   GNUNET_SET_iterate (iter_set,
283                       &iter_cb,
284                       iter_set);
285 }
286
287
288 /**
289  * Signature of the main function of a task.
290  *
291  * @param cls closure
292  * @param tc context information (why was this task triggered now)
293  */
294 static void
295 timeout_fail (void *cls,
296               const struct GNUNET_SCHEDULER_TaskContext *tc)
297 {
298   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
299     return;
300   GNUNET_SCHEDULER_shutdown ();
301   ret = 1;
302 }
303
304
305 /**
306  * Signature of the 'main' function for a (single-peer) testcase that
307  * is run using 'GNUNET_TESTING_peer_run'.
308  *
309  * @param cls closure
310  * @param cfg configuration of the peer that was started
311  * @param peer identity of the peer that was created
312  */
313 static void
314 run (void *cls,
315      const struct GNUNET_CONFIGURATION_Handle *cfg,
316      struct GNUNET_TESTING_Peer *peer)
317 {
318   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
319                                 &timeout_fail,
320                                 NULL);
321
322   config = cfg;
323   GNUNET_TESTING_peer_get_identity (peer,
324                                     &local_id);
325
326   test_iter ();
327
328   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
329   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
330   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id);
331
332   /* test the real set reconciliation */
333   init_set1 ();
334 }
335
336
337 int
338 main (int argc, char **argv)
339 {
340   empty = 1;
341   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
342                                     "test_set.conf",
343                                     &run, NULL))
344   {
345     return 1;
346   }
347   GNUNET_assert (0 == count_set1);
348   GNUNET_assert (0 == count_set2);
349   empty = 0;
350   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
351                                     "test_set.conf",
352                                     &run, NULL))
353   {
354     return 1;
355   }
356   GNUNET_assert (4 == count_set1);
357   GNUNET_assert (4 == count_set2);
358   return ret;
359 }