-comments: the world ain't all male
[oweals/gnunet.git] / src / set / test_set_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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_api.c
21  * @brief testcase for set_api.c
22  * @author Florian Dold
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_set_service.h"
28
29
30 static struct GNUNET_PeerIdentity local_id;
31
32 static struct GNUNET_HashCode app_id;
33
34 static struct GNUNET_SET_Handle *set1;
35
36 static struct GNUNET_SET_Handle *set2;
37
38 static struct GNUNET_SET_ListenHandle *listen_handle;
39
40 static struct GNUNET_SET_OperationHandle *oh1;
41
42 static struct GNUNET_SET_OperationHandle *oh2;
43
44 static const struct GNUNET_CONFIGURATION_Handle *config;
45
46 static unsigned int iter_count;
47
48 static int ret;
49
50 static struct GNUNET_SCHEDULER_Task *tt;
51
52
53 static void
54 result_cb_set1 (void *cls,
55                 const struct GNUNET_SET_Element *element,
56                 uint64_t size,
57                 enum GNUNET_SET_Status status)
58 {
59   switch (status)
60   {
61   case GNUNET_SET_STATUS_OK:
62     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
63                 "set 1: got element\n");
64     break;
65   case GNUNET_SET_STATUS_FAILURE:
66     GNUNET_break (0);
67     oh1 = NULL;
68     fprintf (stderr,
69              "set 1: received failure status!\n");
70     ret = 1;
71     if (NULL != tt)
72     {
73       GNUNET_SCHEDULER_cancel (tt);
74       tt = NULL;
75     }
76     GNUNET_SCHEDULER_shutdown ();
77     break;
78   case GNUNET_SET_STATUS_DONE:
79     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
80                 "set 1: done\n");
81     oh1 = NULL;
82     if (NULL != set1)
83     {
84       GNUNET_SET_destroy (set1);
85       set1 = NULL;
86     }
87     if (NULL == set2)
88     {
89       GNUNET_SCHEDULER_cancel (tt);
90       tt = NULL;
91       GNUNET_SCHEDULER_shutdown ();
92     }
93     break;
94   default:
95     GNUNET_assert (0);
96   }
97 }
98
99
100 static void
101 result_cb_set2 (void *cls,
102                 const struct GNUNET_SET_Element *element,
103                 uint64_t size,
104                 enum GNUNET_SET_Status status)
105 {
106   switch (status)
107   {
108   case GNUNET_SET_STATUS_OK:
109     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110                 "set 2: got element\n");
111     break;
112   case GNUNET_SET_STATUS_FAILURE:
113     GNUNET_break (0);
114     oh2 = NULL;
115     fprintf (stderr,
116              "set 2: received failure status\n");
117     GNUNET_SCHEDULER_shutdown ();
118     ret = 1;
119     break;
120   case GNUNET_SET_STATUS_DONE:
121     oh2 = NULL;
122     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123                 "set 2: done\n");
124     GNUNET_SET_destroy (set2);
125     set2 = NULL;
126     if (NULL == set1)
127     {
128       GNUNET_SCHEDULER_cancel (tt);
129       tt = NULL;
130       GNUNET_SCHEDULER_shutdown ();
131     }
132     break;
133   default:
134     GNUNET_assert (0);
135   }
136 }
137
138
139 static void
140 listen_cb (void *cls,
141            const struct GNUNET_PeerIdentity *other_peer,
142            const struct GNUNET_MessageHeader *context_msg,
143            struct GNUNET_SET_Request *request)
144 {
145   GNUNET_assert (NULL != context_msg);
146   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY);
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148               "listen cb called\n");
149   oh2 = GNUNET_SET_accept (request,
150                            GNUNET_SET_RESULT_ADDED,
151                            (struct GNUNET_SET_Option[]) { 0 },
152                            &result_cb_set2,
153                            NULL);
154   GNUNET_SET_commit (oh2,
155                      set2);
156 }
157
158
159 /**
160  * Start the set operation.
161  *
162  * @param cls closure, unused
163  */
164 static void
165 start (void *cls)
166 {
167   struct GNUNET_MessageHeader context_msg;
168
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "Starting reconciliation\n");
171   context_msg.size = htons (sizeof context_msg);
172   context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY);
173   listen_handle = GNUNET_SET_listen (config,
174                                      GNUNET_SET_OPERATION_UNION,
175                                      &app_id,
176                                      &listen_cb,
177                                      NULL);
178   oh1 = GNUNET_SET_prepare (&local_id,
179                             &app_id,
180                             &context_msg,
181                             GNUNET_SET_RESULT_ADDED,
182                             (struct GNUNET_SET_Option[]) { 0 },
183                             &result_cb_set1,
184                             NULL);
185   GNUNET_SET_commit (oh1,
186                      set1);
187 }
188
189
190 /**
191  * Initialize the second set, continue
192  *
193  * @param cls closure, unused
194  */
195 static void
196 init_set2 (void *cls)
197 {
198   struct GNUNET_SET_Element element;
199
200   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
201               "initializing set 2\n");
202
203   element.element_type = 0;
204   element.data = "hello";
205   element.size = strlen(element.data);
206   GNUNET_SET_add_element (set2,
207                           &element,
208                           NULL, NULL);
209   element.data = "quux";
210   element.size = strlen(element.data);
211   GNUNET_SET_add_element (set2,
212                           &element,
213                           NULL, NULL);
214   element.data = "baz";
215   element.size = strlen(element.data);
216   GNUNET_SET_add_element (set2,
217                           &element,
218                           &start, NULL);
219 }
220
221
222 /**
223  * Initialize the first set, continue.
224  */
225 static void
226 init_set1 (void)
227 {
228   struct GNUNET_SET_Element element;
229
230   element.element_type = 0;
231   element.data = "hello";
232   element.size = strlen(element.data);
233   GNUNET_SET_add_element (set1,
234                           &element,
235                           NULL, NULL);
236   element.data = "bar";
237   element.size = strlen(element.data);
238   GNUNET_SET_add_element (set1,
239                           &element,
240                           &init_set2,
241                           NULL);
242   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243               "initialized set 1\n");
244 }
245
246
247 static int
248 iter_cb (void *cls,
249          const struct GNUNET_SET_Element *element)
250 {
251   struct GNUNET_SET_Handle *set = cls;
252
253   if (NULL == element)
254   {
255     GNUNET_assert (3 == iter_count);
256     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257                 "Iteration finished, destroying set %p\n",
258                 set);
259     GNUNET_SET_destroy (set);
260     return GNUNET_YES;
261   }
262   iter_count++;
263   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264               "iter: got element %u\n",
265               iter_count);
266   return GNUNET_YES;
267 }
268
269
270 static void
271 test_iter ()
272 {
273   struct GNUNET_SET_Element element;
274   struct GNUNET_SET_Handle *iter_set;
275
276   iter_set = GNUNET_SET_create (config,
277                                 GNUNET_SET_OPERATION_UNION);
278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279               "Testing iteration over 3 elements on set %p\n",
280               iter_set);
281   element.element_type = 0;
282
283   element.data = "hello";
284   element.size = strlen(element.data);
285   GNUNET_SET_add_element (iter_set,
286                           &element,
287                           NULL, NULL);
288   element.data = "bar";
289   element.size = strlen(element.data);
290   GNUNET_SET_add_element (iter_set,
291                           &element,
292                           NULL, NULL);
293   element.data = "quux";
294   element.size = strlen(element.data);
295   GNUNET_SET_add_element (iter_set,
296                           &element,
297                           NULL, NULL);
298   GNUNET_SET_iterate (iter_set,
299                       &iter_cb,
300                       iter_set);
301 }
302
303
304 /**
305  * Function run on timeout.
306  *
307  * @param cls closure
308  */
309 static void
310 timeout_fail (void *cls)
311 {
312   tt = NULL;
313   GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
314               "Testcase failed with timeout\n");
315   GNUNET_SCHEDULER_shutdown ();
316   ret = 1;
317 }
318
319
320 /**
321  * Function run on shutdown.
322  *
323  * @param cls closure
324  */
325 static void
326 do_shutdown (void *cls)
327 {
328   if (NULL != tt)
329   {
330     GNUNET_SCHEDULER_cancel (tt);
331     tt = NULL;
332   }
333   if (NULL != oh1)
334   {
335     GNUNET_SET_operation_cancel (oh1);
336     oh1 = NULL;
337   }
338   if (NULL != oh2)
339   {
340     GNUNET_SET_operation_cancel (oh2);
341     oh2 = NULL;
342   }
343   if (NULL != set1)
344   {
345     GNUNET_SET_destroy (set1);
346     set1 = NULL;
347   }
348   if (NULL != set2)
349   {
350     GNUNET_SET_destroy (set2);
351     set2 = NULL;
352   }
353   if (NULL != listen_handle)
354   {
355     GNUNET_SET_listen_cancel (listen_handle);
356     listen_handle = NULL;
357   }
358 }
359
360
361 /**
362  * Signature of the 'main' function for a (single-peer) testcase that
363  * is run using 'GNUNET_TESTING_peer_run'.
364  *
365  * @param cls closure
366  * @param cfg configuration of the peer that was started
367  * @param peer identity of the peer that was created
368  */
369 static void
370 run (void *cls,
371      const struct GNUNET_CONFIGURATION_Handle *cfg,
372      struct GNUNET_TESTING_Peer *peer)
373 {
374
375   struct GNUNET_SET_OperationHandle *my_oh;
376
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Running preparatory tests\n");
379   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
380                                      &timeout_fail,
381                                      NULL);
382   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
383                                  NULL);
384
385   config = cfg;
386   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
387   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388               "my id (from CRYPTO): %s\n",
389               GNUNET_i2s (&local_id));
390   GNUNET_TESTING_peer_get_identity (peer, &local_id);
391   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392               "my id (from TESTING): %s\n",
393               GNUNET_i2s (&local_id));
394   test_iter ();
395
396   set1 = GNUNET_SET_create (cfg,
397                             GNUNET_SET_OPERATION_UNION);
398   set2 = GNUNET_SET_create (cfg,
399                             GNUNET_SET_OPERATION_UNION);
400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401               "Created sets %p and %p for union operation\n",
402               set1,
403               set2);
404   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
405                                     &app_id);
406
407   /* test if canceling an uncommited request works! */
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409               "Launching and instantly stopping set operation\n");
410   my_oh = GNUNET_SET_prepare (&local_id,
411                               &app_id,
412                               NULL,
413                               GNUNET_SET_RESULT_ADDED,
414                               (struct GNUNET_SET_Option[]) { 0 },
415                               NULL,
416                               NULL);
417   GNUNET_SET_operation_cancel (my_oh);
418
419   /* test the real set reconciliation */
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421               "Running real set-reconciliation\n");
422   init_set1 ();
423 }
424
425
426 int
427 main (int argc, char **argv)
428 {
429   GNUNET_log_setup ("test_set_api",
430                     "WARNING",
431                     NULL);
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Launching peer\n");
434   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
435                                     "test_set.conf",
436                                     &run, NULL))
437   {
438     return 1;
439   }
440   return ret;
441 }