log error when timed out
[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
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_api.c
23  * @brief testcase for set_api.c
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 struct GNUNET_PeerIdentity local_id;
32
33 static struct GNUNET_HashCode app_id;
34
35 static struct GNUNET_SET_Handle *set1;
36
37 static struct GNUNET_SET_Handle *set2;
38
39 static struct GNUNET_SET_ListenHandle *listen_handle;
40
41 static const struct GNUNET_CONFIGURATION_Handle *config;
42
43 static unsigned int iter_count;
44
45 static int ret;
46
47 static struct GNUNET_SCHEDULER_Task *tt;
48
49
50 static void
51 result_cb_set1 (void *cls,
52                 const struct GNUNET_SET_Element *element,
53                 enum GNUNET_SET_Status status)
54 {
55   switch (status)
56   {
57   case GNUNET_SET_STATUS_OK:
58     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
59                 "set 1: got element\n");
60     break;
61   case GNUNET_SET_STATUS_FAILURE:
62     GNUNET_break (0);
63     fprintf (stderr,
64              "set 1: received failure status!\n");
65     ret = 1;
66     GNUNET_SCHEDULER_cancel (tt);
67     tt = NULL;
68     GNUNET_SCHEDULER_shutdown ();
69     break;
70   case GNUNET_SET_STATUS_DONE:
71     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72                 "set 1: done\n");
73     GNUNET_SET_destroy (set1);
74     set1 = NULL;
75     if (NULL == set2)
76     {
77       GNUNET_SCHEDULER_cancel (tt);
78       tt = NULL;
79       GNUNET_SCHEDULER_shutdown ();
80     }
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                 enum GNUNET_SET_Status status)
92 {
93   switch (status)
94   {
95   case GNUNET_SET_STATUS_OK:
96     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97                 "set 2: got element\n");
98     break;
99   case GNUNET_SET_STATUS_FAILURE:
100     GNUNET_break (0);
101     fprintf (stderr,
102              "set 2: received failure status\n");
103     ret = 1;
104     break;
105   case GNUNET_SET_STATUS_DONE:
106     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107                 "set 2: done\n");
108     GNUNET_SET_destroy (set2);
109     set2 = NULL;
110     if (NULL == set1)
111     {
112       GNUNET_SCHEDULER_cancel (tt);
113       tt = NULL;
114       GNUNET_SCHEDULER_shutdown ();
115     }
116     break;
117   default:
118     GNUNET_assert (0);
119   }
120 }
121
122
123 static void
124 listen_cb (void *cls,
125            const struct GNUNET_PeerIdentity *other_peer,
126            const struct GNUNET_MessageHeader *context_msg,
127            struct GNUNET_SET_Request *request)
128 {
129   struct GNUNET_SET_OperationHandle *oh;
130
131   GNUNET_assert (NULL != context_msg);
132
133   GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_TEST);
134
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136               "listen cb called\n");
137   GNUNET_SET_listen_cancel (listen_handle);
138   oh = GNUNET_SET_accept (request,
139                           GNUNET_SET_RESULT_ADDED,
140                           &result_cb_set2, NULL);
141   GNUNET_SET_commit (oh, set2);
142 }
143
144
145 /**
146  * Start the set operation.
147  *
148  * @param cls closure, unused
149  */
150 static void
151 start (void *cls)
152 {
153   struct GNUNET_SET_OperationHandle *oh;
154   struct GNUNET_MessageHeader context_msg;
155
156   context_msg.size = htons (sizeof context_msg);
157   context_msg.type = htons (GNUNET_MESSAGE_TYPE_TEST);
158
159   listen_handle = GNUNET_SET_listen (config, GNUNET_SET_OPERATION_UNION,
160                                      &app_id, listen_cb, NULL);
161   oh = GNUNET_SET_prepare (&local_id, &app_id, &context_msg,
162                            GNUNET_SET_RESULT_ADDED,
163                            result_cb_set1, NULL);
164   GNUNET_SET_commit (oh, set1);
165 }
166
167
168 /**
169  * Initialize the second set, continue
170  *
171  * @param cls closure, unused
172  */
173 static void
174 init_set2 (void *cls)
175 {
176   struct GNUNET_SET_Element element;
177
178   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n");
179
180   element.element_type = 0;
181
182   element.data = "hello";
183   element.size = strlen(element.data);
184   GNUNET_SET_add_element (set2, &element, NULL, NULL);
185   element.data = "quux";
186   element.size = strlen(element.data);
187   GNUNET_SET_add_element (set2, &element, NULL, NULL);
188   element.data = "baz";
189   element.size = strlen(element.data);
190   GNUNET_SET_add_element (set2, &element, start, NULL);
191 }
192
193
194 /**
195  * Initialize the first set, continue.
196  */
197 static void
198 init_set1 (void)
199 {
200   struct GNUNET_SET_Element element;
201
202   element.element_type = 0;
203
204   element.data = "hello";
205   element.size = strlen(element.data);
206   GNUNET_SET_add_element (set1, &element, NULL, NULL);
207   element.data = "bar";
208   element.size = strlen(element.data);
209   GNUNET_SET_add_element (set1, &element, init_set2, NULL);
210
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n");
212 }
213
214
215 static int
216 iter_cb (void *cls,
217          const struct GNUNET_SET_Element *element)
218 {
219   if (NULL == element)
220   {
221     GNUNET_assert (3 == iter_count);
222     GNUNET_SET_destroy (cls);
223     return GNUNET_YES;
224   }
225   iter_count++;
226   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227               "iter: got element %u\n",
228               iter_count);
229   return GNUNET_YES;
230 }
231
232
233 static void
234 test_iter ()
235 {
236   struct GNUNET_SET_Element element;
237   struct GNUNET_SET_Handle *iter_set;
238
239   iter_set = GNUNET_SET_create (config, GNUNET_SET_OPERATION_UNION);
240
241   element.element_type = 0;
242
243   element.data = "hello";
244   element.size = strlen(element.data);
245   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
246   element.data = "bar";
247   element.size = strlen(element.data);
248   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
249   element.data = "quux";
250   element.size = strlen(element.data);
251   GNUNET_SET_add_element (iter_set, &element, NULL, NULL);
252
253   GNUNET_SET_iterate (iter_set, iter_cb, iter_set);
254 }
255
256
257 /**
258  * Signature of the main function of a task.
259  *
260  * @param cls closure
261  */
262 static void
263 timeout_fail (void *cls)
264 {
265   tt = NULL;
266   GNUNET_SCHEDULER_shutdown ();
267   ret = 1;
268 }
269
270
271 /**
272  * Signature of the 'main' function for a (single-peer) testcase that
273  * is run using 'GNUNET_TESTING_peer_run'.
274  *
275  * @param cls closure
276  * @param cfg configuration of the peer that was started
277  * @param peer identity of the peer that was created
278  */
279 static void
280 run (void *cls,
281      const struct GNUNET_CONFIGURATION_Handle *cfg,
282      struct GNUNET_TESTING_Peer *peer)
283 {
284
285   struct GNUNET_SET_OperationHandle *my_oh;
286
287   tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
288                                      &timeout_fail, NULL);
289
290   config = cfg;
291   GNUNET_CRYPTO_get_peer_identity (cfg, &local_id);
292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293               "my id (from CRYPTO): %s\n",
294               GNUNET_i2s (&local_id));
295   GNUNET_TESTING_peer_get_identity (peer, &local_id);
296   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297               "my id (from TESTING): %s\n",
298               GNUNET_i2s (&local_id));
299   test_iter ();
300
301   set1 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
302   set2 = GNUNET_SET_create (cfg, GNUNET_SET_OPERATION_UNION);
303   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
304                                     &app_id);
305
306   ///* test if canceling an uncommited request works! */
307   my_oh = GNUNET_SET_prepare (&local_id, &app_id, NULL,
308                               GNUNET_SET_RESULT_ADDED, NULL, NULL);
309
310   GNUNET_SET_operation_cancel (my_oh);
311
312   /* test the real set reconciliation */
313   init_set1 ();
314 }
315
316
317 int
318 main (int argc, char **argv)
319 {
320   if (0 != GNUNET_TESTING_peer_run ("test_set_api",
321                                     "test_set.conf",
322                                     &run, NULL))
323   {
324     return 1;
325   }
326   return ret;
327 }