diff options
author | Elias Summermatter <elias.summermatter@seccom.ch> | 2021-03-31 11:16:38 +0200 |
---|---|---|
committer | Elias Summermatter <elias.summermatter@seccom.ch> | 2021-03-31 11:16:38 +0200 |
commit | b5660e0a33f4c7a819de0b5056172f78f912e352 (patch) | |
tree | 58e952f6ade105dffdb7c1456ec8ce27d7100498 | |
parent | 7d85c27e9b61ac900932337c9ec6a72c6326f6fc (diff) | |
download | gnunet-b5660e0a33f4c7a819de0b5056172f78f912e352.tar.gz gnunet-b5660e0a33f4c7a819de0b5056172f78f912e352.zip |
SETU imporved tests
-rw-r--r-- | src/setu/perf_setu_api.c | 418 | ||||
-rw-r--r-- | src/setu/test_setu_api.c | 10 |
2 files changed, 423 insertions, 5 deletions
diff --git a/src/setu/perf_setu_api.c b/src/setu/perf_setu_api.c new file mode 100644 index 000000000..710e75381 --- /dev/null +++ b/src/setu/perf_setu_api.c | |||
@@ -0,0 +1,418 @@ | |||
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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file set/test_setu_api.c | ||
23 | * @brief testcase for setu_api.c | ||
24 | * @author Florian Dold | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_lib.h" | ||
29 | #include "gnunet_setu_service.h" | ||
30 | |||
31 | |||
32 | static struct GNUNET_PeerIdentity local_id; | ||
33 | |||
34 | static struct GNUNET_HashCode app_id; | ||
35 | |||
36 | static struct GNUNET_SETU_Handle *set1; | ||
37 | |||
38 | static struct GNUNET_SETU_Handle *set2; | ||
39 | |||
40 | static struct GNUNET_SETU_ListenHandle *listen_handle; | ||
41 | |||
42 | static struct GNUNET_SETU_OperationHandle *oh1; | ||
43 | |||
44 | static struct GNUNET_SETU_OperationHandle *oh2; | ||
45 | |||
46 | static const struct GNUNET_CONFIGURATION_Handle *config; | ||
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_SETU_Element *element, | ||
56 | uint64_t size, | ||
57 | enum GNUNET_SETU_Status status) | ||
58 | { | ||
59 | switch (status) | ||
60 | { | ||
61 | case GNUNET_SETU_STATUS_ADD_LOCAL: | ||
62 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: got element\n"); | ||
63 | break; | ||
64 | |||
65 | case GNUNET_SETU_STATUS_FAILURE: | ||
66 | GNUNET_break (0); | ||
67 | oh1 = NULL; | ||
68 | fprintf (stderr, "set 1: received failure status!\n"); | ||
69 | ret = 1; | ||
70 | if (NULL != tt) | ||
71 | { | ||
72 | GNUNET_SCHEDULER_cancel (tt); | ||
73 | tt = NULL; | ||
74 | } | ||
75 | GNUNET_SCHEDULER_shutdown (); | ||
76 | break; | ||
77 | |||
78 | case GNUNET_SETU_STATUS_DONE: | ||
79 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 1: done\n"); | ||
80 | oh1 = NULL; | ||
81 | if (NULL != set1) | ||
82 | { | ||
83 | GNUNET_SETU_destroy (set1); | ||
84 | set1 = NULL; | ||
85 | } | ||
86 | if (NULL == set2) | ||
87 | { | ||
88 | GNUNET_SCHEDULER_cancel (tt); | ||
89 | tt = NULL; | ||
90 | GNUNET_SCHEDULER_shutdown (); | ||
91 | } | ||
92 | break; | ||
93 | |||
94 | default: | ||
95 | GNUNET_assert (0); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | result_cb_set2 (void *cls, | ||
102 | const struct GNUNET_SETU_Element *element, | ||
103 | uint64_t size, | ||
104 | enum GNUNET_SETU_Status status) | ||
105 | { | ||
106 | switch (status) | ||
107 | { | ||
108 | case GNUNET_SETU_STATUS_ADD_LOCAL: | ||
109 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: got element\n"); | ||
110 | break; | ||
111 | |||
112 | case GNUNET_SETU_STATUS_FAILURE: | ||
113 | GNUNET_break (0); | ||
114 | oh2 = NULL; | ||
115 | fprintf (stderr, "set 2: received failure status\n"); | ||
116 | GNUNET_SCHEDULER_shutdown (); | ||
117 | ret = 1; | ||
118 | break; | ||
119 | |||
120 | case GNUNET_SETU_STATUS_DONE: | ||
121 | oh2 = NULL; | ||
122 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "set 2: done\n"); | ||
123 | GNUNET_SETU_destroy (set2); | ||
124 | set2 = NULL; | ||
125 | if (NULL == set1) | ||
126 | { | ||
127 | GNUNET_SCHEDULER_cancel (tt); | ||
128 | tt = NULL; | ||
129 | GNUNET_SCHEDULER_shutdown (); | ||
130 | } | ||
131 | break; | ||
132 | |||
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_SETU_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, "listen cb called\n"); | ||
148 | oh2 = GNUNET_SETU_accept (request, | ||
149 | (struct GNUNET_SETU_Option[]){ 0 }, | ||
150 | &result_cb_set2, | ||
151 | NULL); | ||
152 | GNUNET_SETU_commit (oh2, set2); | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * Start the set operation. | ||
158 | * | ||
159 | * @param cls closure, unused | ||
160 | */ | ||
161 | static void | ||
162 | start (void *cls) | ||
163 | { | ||
164 | struct GNUNET_MessageHeader context_msg; | ||
165 | |||
166 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting reconciliation\n"); | ||
167 | context_msg.size = htons (sizeof context_msg); | ||
168 | context_msg.type = htons (GNUNET_MESSAGE_TYPE_DUMMY); | ||
169 | listen_handle = GNUNET_SETU_listen (config, | ||
170 | &app_id, | ||
171 | &listen_cb, | ||
172 | NULL); | ||
173 | oh1 = GNUNET_SETU_prepare (&local_id, | ||
174 | &app_id, | ||
175 | &context_msg, | ||
176 | (struct GNUNET_SETU_Option[]){ 0 }, | ||
177 | &result_cb_set1, | ||
178 | NULL); | ||
179 | GNUNET_SETU_commit (oh1, set1); | ||
180 | } | ||
181 | |||
182 | |||
183 | /** | ||
184 | * Initialize the second set, continue | ||
185 | * | ||
186 | * @param cls closure, unused | ||
187 | */ | ||
188 | static void | ||
189 | init_set2 (void *cls) | ||
190 | { | ||
191 | struct GNUNET_SETU_Element element; | ||
192 | |||
193 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initializing set 2\n"); | ||
194 | |||
195 | element.element_type = 0; | ||
196 | element.data = "hello1"; | ||
197 | element.size = strlen (element.data); | ||
198 | GNUNET_SETU_add_element (set2, &element, NULL, NULL); | ||
199 | element.data = "quux"; | ||
200 | element.size = strlen (element.data); | ||
201 | GNUNET_SETU_add_element (set2, &element, NULL, NULL); | ||
202 | element.data = "baz"; | ||
203 | element.size = strlen (element.data); | ||
204 | GNUNET_SETU_add_element (set2, &element, &start, NULL); | ||
205 | } | ||
206 | |||
207 | /** | ||
208 | * Generate random byte stream | ||
209 | */ | ||
210 | |||
211 | unsigned char *gen_rdm_bytestream (size_t num_bytes) | ||
212 | { | ||
213 | unsigned char *stream = GNUNET_malloc (num_bytes); | ||
214 | GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, stream, sizeof(stream)); | ||
215 | return stream; | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * Generate random sets | ||
220 | */ | ||
221 | |||
222 | static void | ||
223 | initRandomSets(int overlap, int set1_size, int set2_size, int element_size_in_bytes) | ||
224 | { | ||
225 | struct GNUNET_SETU_Element element; | ||
226 | element.element_type = 0; | ||
227 | |||
228 | // Add elements to both sets | ||
229 | for (int i = 0; i < overlap; i++) { | ||
230 | element.data = gen_rdm_bytestream(element_size_in_bytes); | ||
231 | element.size = strlen (element.data); | ||
232 | GNUNET_SETU_add_element (set1, &element, NULL, NULL); | ||
233 | GNUNET_SETU_add_element (set2, &element, NULL, NULL); | ||
234 | set1_size--; | ||
235 | set2_size--; | ||
236 | } | ||
237 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized elements in both sets\n"); | ||
238 | |||
239 | // Add other elements to set 1 | ||
240 | while(set1_size>0) { | ||
241 | element.data = gen_rdm_bytestream(element_size_in_bytes); | ||
242 | element.size = strlen (element.data); | ||
243 | GNUNET_SETU_add_element (set1, &element, NULL, NULL); | ||
244 | set1_size--; | ||
245 | } | ||
246 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized elements in set1\n"); | ||
247 | |||
248 | // Add other elements to set 2 | ||
249 | while(set2_size > 0) { | ||
250 | element.data = gen_rdm_bytestream(element_size_in_bytes); | ||
251 | element.size = strlen (element.data); | ||
252 | |||
253 | if(set2_size != 1) { | ||
254 | GNUNET_SETU_add_element (set2, &element,NULL, NULL); | ||
255 | } else { | ||
256 | GNUNET_SETU_add_element (set2, &element,&start, NULL); | ||
257 | } | ||
258 | |||
259 | set2_size--; | ||
260 | } | ||
261 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized elements in set2\n"); | ||
262 | } | ||
263 | |||
264 | /** | ||
265 | * Initialize the first set, continue. | ||
266 | */ | ||
267 | static void | ||
268 | init_set1 (void) | ||
269 | { | ||
270 | struct GNUNET_SETU_Element element; | ||
271 | |||
272 | element.element_type = 0; | ||
273 | element.data = "hello"; | ||
274 | element.size = strlen (element.data); | ||
275 | GNUNET_SETU_add_element (set1, &element, NULL, NULL); | ||
276 | element.data = "bar"; | ||
277 | element.size = strlen (element.data); | ||
278 | GNUNET_SETU_add_element (set1, &element, &init_set2, NULL); | ||
279 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "initialized set 1\n"); | ||
280 | } | ||
281 | |||
282 | |||
283 | /** | ||
284 | * Function run on timeout. | ||
285 | * | ||
286 | * @param cls closure | ||
287 | */ | ||
288 | static void | ||
289 | timeout_fail (void *cls) | ||
290 | { | ||
291 | tt = NULL; | ||
292 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Testcase failed with timeout\n"); | ||
293 | GNUNET_SCHEDULER_shutdown (); | ||
294 | ret = 1; | ||
295 | } | ||
296 | |||
297 | |||
298 | /** | ||
299 | * Function run on shutdown. | ||
300 | * | ||
301 | * @param cls closure | ||
302 | */ | ||
303 | static void | ||
304 | do_shutdown (void *cls) | ||
305 | { | ||
306 | if (NULL != tt) | ||
307 | { | ||
308 | GNUNET_SCHEDULER_cancel (tt); | ||
309 | tt = NULL; | ||
310 | } | ||
311 | if (NULL != oh1) | ||
312 | { | ||
313 | GNUNET_SETU_operation_cancel (oh1); | ||
314 | oh1 = NULL; | ||
315 | } | ||
316 | if (NULL != oh2) | ||
317 | { | ||
318 | GNUNET_SETU_operation_cancel (oh2); | ||
319 | oh2 = NULL; | ||
320 | } | ||
321 | if (NULL != set1) | ||
322 | { | ||
323 | GNUNET_SETU_destroy (set1); | ||
324 | set1 = NULL; | ||
325 | } | ||
326 | if (NULL != set2) | ||
327 | { | ||
328 | GNUNET_SETU_destroy (set2); | ||
329 | set2 = NULL; | ||
330 | } | ||
331 | if (NULL != listen_handle) | ||
332 | { | ||
333 | GNUNET_SETU_listen_cancel (listen_handle); | ||
334 | listen_handle = NULL; | ||
335 | } | ||
336 | } | ||
337 | |||
338 | |||
339 | /** | ||
340 | * Signature of the 'main' function for a (single-peer) testcase that | ||
341 | * is run using 'GNUNET_TESTING_peer_run'. | ||
342 | * | ||
343 | * @param cls closure | ||
344 | * @param cfg configuration of the peer that was started | ||
345 | * @param peer identity of the peer that was created | ||
346 | */ | ||
347 | static void | ||
348 | run (void *cls, | ||
349 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
350 | struct GNUNET_TESTING_Peer *peer) | ||
351 | { | ||
352 | struct GNUNET_SETU_OperationHandle *my_oh; | ||
353 | |||
354 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
355 | "Running preparatory tests\n"); | ||
356 | tt = GNUNET_SCHEDULER_add_delayed ( | ||
357 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), | ||
358 | &timeout_fail, | ||
359 | NULL); | ||
360 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
361 | |||
362 | config = cfg; | ||
363 | GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_get_peer_identity (cfg, | ||
364 | &local_id)); | ||
365 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
366 | "my id (from CRYPTO): %s\n", | ||
367 | GNUNET_i2s (&local_id)); | ||
368 | GNUNET_TESTING_peer_get_identity (peer, | ||
369 | &local_id); | ||
370 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
371 | "my id (from TESTING): %s\n", | ||
372 | GNUNET_i2s (&local_id)); | ||
373 | set1 = GNUNET_SETU_create (cfg); | ||
374 | set2 = GNUNET_SETU_create (cfg); | ||
375 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
376 | "Created sets %p and %p for union operation\n", | ||
377 | set1, | ||
378 | set2); | ||
379 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &app_id); | ||
380 | |||
381 | /* test if canceling an uncommited request works! */ | ||
382 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
383 | "Launching and instantly stopping set operation\n"); | ||
384 | my_oh = GNUNET_SETU_prepare (&local_id, | ||
385 | &app_id, | ||
386 | NULL, | ||
387 | (struct GNUNET_SETU_Option[]){ 0 }, | ||
388 | NULL, | ||
389 | NULL); | ||
390 | GNUNET_SETU_operation_cancel (my_oh); | ||
391 | |||
392 | /* test the real set reconciliation */ | ||
393 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
394 | "Running real set-reconciliation\n"); | ||
395 | //init_set1 (); | ||
396 | // limit ~23800 element total | ||
397 | initRandomSets(19500,20000,20000,32); | ||
398 | } | ||
399 | |||
400 | |||
401 | int | ||
402 | main (int argc, char **argv) | ||
403 | { | ||
404 | GNUNET_log_setup ("test_setu_api", | ||
405 | "WARNING", | ||
406 | NULL); | ||
407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
408 | "Launching peer\n"); | ||
409 | if (0 != | ||
410 | GNUNET_TESTING_peer_run ("test_setu_api", | ||
411 | "test_setu.conf", | ||
412 | &run, | ||
413 | NULL)) | ||
414 | { | ||
415 | return 1; | ||
416 | } | ||
417 | return ret; | ||
418 | } | ||
diff --git a/src/setu/test_setu_api.c b/src/setu/test_setu_api.c index 797e3f534..d573cfe3c 100644 --- a/src/setu/test_setu_api.c +++ b/src/setu/test_setu_api.c | |||
@@ -210,8 +210,8 @@ init_set2 (void *cls) | |||
210 | 210 | ||
211 | unsigned char *gen_rdm_bytestream (size_t num_bytes) | 211 | unsigned char *gen_rdm_bytestream (size_t num_bytes) |
212 | { | 212 | { |
213 | unsigned char *stream = GNUNET_malloc (num_bytes); | 213 | unsigned char *stream = GNUNET_malloc (num_bytes + 1); |
214 | GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, stream, sizeof(stream)); | 214 | GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, stream, num_bytes - 1); |
215 | return stream; | 215 | return stream; |
216 | } | 216 | } |
217 | 217 | ||
@@ -392,9 +392,9 @@ run (void *cls, | |||
392 | /* test the real set reconciliation */ | 392 | /* test the real set reconciliation */ |
393 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 393 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
394 | "Running real set-reconciliation\n"); | 394 | "Running real set-reconciliation\n"); |
395 | init_set1 (); | 395 | //init_set1 (); |
396 | // limit ~23800 element total | 396 | initRandomSets(19500,20000,20000,4096); |
397 | //initRandomSets(9990,9997,9997,32); | 397 | //initRandomSets(19500,20000,20000,32); |
398 | } | 398 | } |
399 | 399 | ||
400 | 400 | ||