diff options
Diffstat (limited to 'src/seti/test_seti_api.c')
-rw-r--r-- | src/seti/test_seti_api.c | 345 |
1 files changed, 345 insertions, 0 deletions
diff --git a/src/seti/test_seti_api.c b/src/seti/test_seti_api.c new file mode 100644 index 000000000..9074fab41 --- /dev/null +++ b/src/seti/test_seti_api.c | |||
@@ -0,0 +1,345 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2014, 2020 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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file set/test_seti_api.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_seti_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_SETI_Handle *set1; | ||
40 | |||
41 | static struct GNUNET_SETI_Handle *set2; | ||
42 | |||
43 | static struct GNUNET_SETI_ListenHandle *listen_handle; | ||
44 | |||
45 | static const struct GNUNET_CONFIGURATION_Handle *config; | ||
46 | |||
47 | static struct GNUNET_SCHEDULER_Task *tt; | ||
48 | |||
49 | static struct GNUNET_SETI_OperationHandle *oh1; | ||
50 | |||
51 | static struct GNUNET_SETI_OperationHandle *oh2; | ||
52 | |||
53 | |||
54 | static void | ||
55 | result_cb_set1 (void *cls, | ||
56 | const struct GNUNET_SETI_Element *element, | ||
57 | uint64_t current_size, | ||
58 | enum GNUNET_SETI_Status status) | ||
59 | { | ||
60 | static int count; | ||
61 | |||
62 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
63 | "Processing result set 1 (%d)\n", | ||
64 | status); | ||
65 | switch (status) | ||
66 | { | ||
67 | case GNUNET_SETI_STATUS_ADD_LOCAL: | ||
68 | count++; | ||
69 | break; | ||
70 | case GNUNET_SETI_STATUS_FAILURE: | ||
71 | oh1 = NULL; | ||
72 | ret = 1; | ||
73 | break; | ||
74 | case GNUNET_SETI_STATUS_DONE: | ||
75 | oh1 = NULL; | ||
76 | GNUNET_assert (1 == count); | ||
77 | GNUNET_SETI_destroy (set1); | ||
78 | set1 = NULL; | ||
79 | if (NULL == set2) | ||
80 | GNUNET_SCHEDULER_shutdown (); | ||
81 | break; | ||
82 | |||
83 | default: | ||
84 | GNUNET_assert (0); | ||
85 | } | ||
86 | } | ||
87 | |||
88 | |||
89 | static void | ||
90 | result_cb_set2 (void *cls, | ||
91 | const struct GNUNET_SETI_Element *element, | ||
92 | uint64_t current_size, | ||
93 | enum GNUNET_SETI_Status status) | ||
94 | { | ||
95 | static int count; | ||
96 | |||
97 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
98 | "Processing result set 2 (%d)\n", | ||
99 | status); | ||
100 | switch (status) | ||
101 | { | ||
102 | case GNUNET_SETI_STATUS_ADD_LOCAL: | ||
103 | count++; | ||
104 | break; | ||
105 | case GNUNET_SETI_STATUS_FAILURE: | ||
106 | oh2 = NULL; | ||
107 | ret = 1; | ||
108 | break; | ||
109 | case GNUNET_SETI_STATUS_DONE: | ||
110 | oh2 = NULL; | ||
111 | GNUNET_break (1 == count); | ||
112 | if (1 != count) | ||
113 | ret |= 2; | ||
114 | GNUNET_SETI_destroy (set2); | ||
115 | set2 = NULL; | ||
116 | if (NULL == set1) | ||
117 | GNUNET_SCHEDULER_shutdown (); | ||
118 | break; | ||
119 | case GNUNET_SETI_STATUS_DEL_LOCAL: | ||
120 | /* unexpected! */ | ||
121 | ret = 1; | ||
122 | break; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | |||
127 | static void | ||
128 | listen_cb (void *cls, | ||
129 | const struct GNUNET_PeerIdentity *other_peer, | ||
130 | const struct GNUNET_MessageHeader *context_msg, | ||
131 | struct GNUNET_SETI_Request *request) | ||
132 | { | ||
133 | struct GNUNET_SETI_Option opts[] = { | ||
134 | { .type = GNUNET_SETI_OPTION_RETURN_INTERSECTION }, | ||
135 | { .type = GNUNET_SETI_OPTION_END } | ||
136 | }; | ||
137 | |||
138 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
139 | "starting intersection by accepting and committing\n"); | ||
140 | GNUNET_assert (NULL != context_msg); | ||
141 | GNUNET_assert (ntohs (context_msg->type) == GNUNET_MESSAGE_TYPE_DUMMY); | ||
142 | oh2 = GNUNET_SETI_accept (request, | ||
143 | opts, | ||
144 | &result_cb_set2, | ||
145 | NULL); | ||
146 | GNUNET_SETI_commit (oh2, | ||
147 | set2); | ||
148 | } | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Start the set operation. | ||
153 | * | ||
154 | * @param cls closure, unused | ||
155 | */ | ||
156 | static void | ||
157 | start (void *cls) | ||
158 | { | ||
159 | struct GNUNET_MessageHeader context_msg; | ||
160 | struct GNUNET_SETI_Option opts[] = { | ||
161 | { .type = GNUNET_SETI_OPTION_RETURN_INTERSECTION }, | ||
162 | { .type = GNUNET_SETI_OPTION_END } | ||
163 | }; | ||
164 | |||
165 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
166 | "starting listener\n"); | ||
167 | context_msg.size = htons (sizeof (context_msg)); | ||
168 | context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY); | ||
169 | listen_handle = GNUNET_SETI_listen (config, | ||
170 | &app_id, | ||
171 | &listen_cb, | ||
172 | NULL); | ||
173 | oh1 = GNUNET_SETI_prepare (&local_id, | ||
174 | &app_id, | ||
175 | &context_msg, | ||
176 | opts, | ||
177 | &result_cb_set1, | ||
178 | NULL); | ||
179 | GNUNET_SETI_commit (oh1, | ||
180 | set1); | ||
181 | } | ||
182 | |||
183 | |||
184 | /** | ||
185 | * Initialize the second set, continue | ||
186 | * | ||
187 | * @param cls closure, unused | ||
188 | */ | ||
189 | static void | ||
190 | init_set2 (void *cls) | ||
191 | { | ||
192 | struct GNUNET_SETI_Element element; | ||
193 | |||
194 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
195 | "initializing set 2\n"); | ||
196 | element.element_type = 0; | ||
197 | element.data = "hello"; | ||
198 | element.size = strlen (element.data); | ||
199 | GNUNET_SETI_add_element (set2, | ||
200 | &element, | ||
201 | NULL, | ||
202 | NULL); | ||
203 | element.data = "quux"; | ||
204 | element.size = strlen (element.data); | ||
205 | GNUNET_SETI_add_element (set2, | ||
206 | &element, | ||
207 | NULL, | ||
208 | NULL); | ||
209 | element.data = "baz"; | ||
210 | element.size = strlen (element.data); | ||
211 | GNUNET_SETI_add_element (set2, | ||
212 | &element, | ||
213 | &start, | ||
214 | NULL); | ||
215 | } | ||
216 | |||
217 | |||
218 | /** | ||
219 | * Initialize the first set, continue. | ||
220 | */ | ||
221 | static void | ||
222 | init_set1 (void) | ||
223 | { | ||
224 | struct GNUNET_SETI_Element element; | ||
225 | |||
226 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
227 | "initializing set 1\n"); | ||
228 | element.element_type = 0; | ||
229 | element.data = "hello"; | ||
230 | element.size = strlen (element.data); | ||
231 | GNUNET_SETI_add_element (set1, | ||
232 | &element, | ||
233 | NULL, | ||
234 | NULL); | ||
235 | element.data = "bar"; | ||
236 | element.size = strlen (element.data); | ||
237 | GNUNET_SETI_add_element (set1, | ||
238 | &element, | ||
239 | &init_set2, | ||
240 | NULL); | ||
241 | } | ||
242 | |||
243 | |||
244 | /** | ||
245 | * Function run on shutdown. | ||
246 | * | ||
247 | * @param cls closure | ||
248 | */ | ||
249 | static void | ||
250 | do_shutdown (void *cls) | ||
251 | { | ||
252 | if (NULL != tt) | ||
253 | { | ||
254 | GNUNET_SCHEDULER_cancel (tt); | ||
255 | tt = NULL; | ||
256 | } | ||
257 | if (NULL != oh1) | ||
258 | { | ||
259 | GNUNET_SETI_operation_cancel (oh1); | ||
260 | oh1 = NULL; | ||
261 | } | ||
262 | if (NULL != oh2) | ||
263 | { | ||
264 | GNUNET_SETI_operation_cancel (oh2); | ||
265 | oh2 = NULL; | ||
266 | } | ||
267 | if (NULL != set1) | ||
268 | { | ||
269 | GNUNET_SETI_destroy (set1); | ||
270 | set1 = NULL; | ||
271 | } | ||
272 | if (NULL != set2) | ||
273 | { | ||
274 | GNUNET_SETI_destroy (set2); | ||
275 | set2 = NULL; | ||
276 | } | ||
277 | if (NULL != listen_handle) | ||
278 | { | ||
279 | GNUNET_SETI_listen_cancel (listen_handle); | ||
280 | listen_handle = NULL; | ||
281 | } | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Function run on timeout. | ||
287 | * | ||
288 | * @param cls closure | ||
289 | */ | ||
290 | static void | ||
291 | timeout_fail (void *cls) | ||
292 | { | ||
293 | tt = NULL; | ||
294 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
295 | "Testcase failed with timeout\n"); | ||
296 | GNUNET_SCHEDULER_shutdown (); | ||
297 | ret = 1; | ||
298 | } | ||
299 | |||
300 | |||
301 | /** | ||
302 | * Signature of the 'main' function for a (single-peer) testcase that | ||
303 | * is run using 'GNUNET_TESTING_peer_run'. | ||
304 | * | ||
305 | * @param cls closure | ||
306 | * @param cfg configuration of the peer that was started | ||
307 | * @param peer identity of the peer that was created | ||
308 | */ | ||
309 | static void | ||
310 | run (void *cls, | ||
311 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
312 | struct GNUNET_TESTING_Peer *peer) | ||
313 | { | ||
314 | config = cfg; | ||
315 | GNUNET_TESTING_peer_get_identity (peer, | ||
316 | &local_id); | ||
317 | tt = GNUNET_SCHEDULER_add_delayed ( | ||
318 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, | ||
319 | 5), | ||
320 | &timeout_fail, | ||
321 | NULL); | ||
322 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, | ||
323 | NULL); | ||
324 | |||
325 | set1 = GNUNET_SETI_create (cfg); | ||
326 | set2 = GNUNET_SETI_create (cfg); | ||
327 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, | ||
328 | &app_id); | ||
329 | |||
330 | /* test the real set reconciliation */ | ||
331 | init_set1 (); | ||
332 | } | ||
333 | |||
334 | |||
335 | int | ||
336 | main (int argc, | ||
337 | char **argv) | ||
338 | { | ||
339 | if (0 != GNUNET_TESTING_peer_run ("test_seti_api", | ||
340 | "test_seti.conf", | ||
341 | &run, | ||
342 | NULL)) | ||
343 | return 1; | ||
344 | return ret; | ||
345 | } | ||