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