-comments: the world ain't all male
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file set/test_set_intersection_result_full.c
21  * @brief testcase for full result mode of the intersection set operation
22  * @author Christian Fuchs
23  * @author Christian Grothoff
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 static int ret;
32
33 static struct GNUNET_PeerIdentity local_id;
34
35 static struct GNUNET_HashCode app_id;
36
37 static struct GNUNET_SET_Handle *set1;
38
39 static struct GNUNET_SET_Handle *set2;
40
41 static struct GNUNET_SET_ListenHandle *listen_handle;
42
43 static const struct GNUNET_CONFIGURATION_Handle *config;
44
45 static int iter_count;
46
47 static struct GNUNET_SCHEDULER_Task *tt;
48
49 static struct GNUNET_SET_OperationHandle *oh1;
50
51 static struct GNUNET_SET_OperationHandle *oh2;
52
53
54 static void
55 result_cb_set1 (void *cls,
56                 const struct GNUNET_SET_Element *element,
57                 uint64_t current_size,
58                 enum GNUNET_SET_Status status)
59 {
60   static int count;
61
62   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
63               "Processing result set 1 (%d)\n",
64               status);
65   switch (status)
66   {
67   case GNUNET_SET_STATUS_OK:
68     count++;
69     break;
70   case GNUNET_SET_STATUS_FAILURE:
71     oh1 = NULL;
72     ret = 1;
73     break;
74   case GNUNET_SET_STATUS_DONE:
75     oh1 = NULL;
76     GNUNET_assert (1 == count);
77     GNUNET_SET_destroy (set1);
78     set1 = NULL;
79     if (NULL == set2)
80       GNUNET_SCHEDULER_shutdown ();
81     break;
82   default:
83     GNUNET_assert (0);
84   }
85 }
86
87
88 static void
89 result_cb_set2 (void *cls,
90                 const struct GNUNET_SET_Element *element,
91                 uint64_t current_size,
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   oh2 = GNUNET_SET_accept (request,
133                            GNUNET_SET_RESULT_FULL,
134                            (struct GNUNET_SET_Option[]) { 0 },
135                            &result_cb_set2,
136                            NULL);
137   GNUNET_SET_commit (oh2,
138                      set2);
139 }
140
141
142 /**
143  * Start the set operation.
144  *
145  * @param cls closure, unused
146  */
147 static void
148 start (void *cls)
149 {
150   struct GNUNET_MessageHeader context_msg;
151
152   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
153               "starting listener\n");
154   context_msg.size = htons (sizeof context_msg);
155   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
156   listen_handle = GNUNET_SET_listen (config,
157                                      GNUNET_SET_OPERATION_INTERSECTION,
158                                      &app_id,
159                                      &listen_cb,
160                                      NULL);
161   oh1 = GNUNET_SET_prepare (&local_id,
162                             &app_id,
163                             &context_msg,
164                             GNUNET_SET_RESULT_FULL,
165                             (struct GNUNET_SET_Option[]) { 0 },
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 }