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