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