diff options
Diffstat (limited to 'src/psyc/test_psyc.c')
-rw-r--r-- | src/psyc/test_psyc.c | 1018 |
1 files changed, 0 insertions, 1018 deletions
diff --git a/src/psyc/test_psyc.c b/src/psyc/test_psyc.c deleted file mode 100644 index b6e27bbab..000000000 --- a/src/psyc/test_psyc.c +++ /dev/null | |||
@@ -1,1018 +0,0 @@ | |||
1 | /* | ||
2 | * This file is part of GNUnet | ||
3 | * Copyright (C) 2013 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 psyc/test_psyc.c | ||
23 | * @brief Tests for the PSYC API. | ||
24 | * @author Gabor X Toth | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | |||
28 | #include <inttypes.h> | ||
29 | |||
30 | #include "platform.h" | ||
31 | #include "gnunet_crypto_lib.h" | ||
32 | #include "gnunet_common.h" | ||
33 | #include "gnunet_util_lib.h" | ||
34 | #include "gnunet_testing_lib.h" | ||
35 | #include "gnunet_psyc_util_lib.h" | ||
36 | #include "gnunet_psyc_service.h" | ||
37 | |||
38 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) | ||
39 | |||
40 | /** | ||
41 | * Return value from 'main'. | ||
42 | */ | ||
43 | static int res; | ||
44 | |||
45 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
46 | |||
47 | static struct GNUNET_PeerIdentity this_peer; | ||
48 | |||
49 | /** | ||
50 | * Handle for task for timeout termination. | ||
51 | */ | ||
52 | static struct GNUNET_SCHEDULER_Task * end_badly_task; | ||
53 | |||
54 | static struct GNUNET_PSYC_Master *mst; | ||
55 | static struct GNUNET_PSYC_Slave *slv; | ||
56 | |||
57 | static struct GNUNET_PSYC_Channel *mst_chn, *slv_chn; | ||
58 | |||
59 | static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key; | ||
60 | static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key; | ||
61 | |||
62 | static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key; | ||
63 | static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key; | ||
64 | |||
65 | struct TransmitClosure | ||
66 | { | ||
67 | struct GNUNET_PSYC_MasterTransmitHandle *mst_tmit; | ||
68 | struct GNUNET_PSYC_SlaveTransmitHandle *slv_tmit; | ||
69 | struct GNUNET_PSYC_Environment *env; | ||
70 | struct GNUNET_PSYC_Modifier *mod; | ||
71 | char *data[16]; | ||
72 | const char *mod_value; | ||
73 | size_t mod_value_size; | ||
74 | uint8_t data_delay[16]; | ||
75 | uint8_t data_count; | ||
76 | uint8_t paused; | ||
77 | uint8_t n; | ||
78 | }; | ||
79 | |||
80 | static struct TransmitClosure *tmit; | ||
81 | |||
82 | static uint8_t join_req_count, end_count; | ||
83 | |||
84 | enum | ||
85 | { | ||
86 | TEST_NONE = 0, | ||
87 | TEST_MASTER_START = 1, | ||
88 | TEST_SLAVE_JOIN_REJECT = 2, | ||
89 | TEST_SLAVE_JOIN_ACCEPT = 3, | ||
90 | TEST_SLAVE_ADD = 4, | ||
91 | TEST_SLAVE_REMOVE = 5, | ||
92 | TEST_SLAVE_TRANSMIT = 6, | ||
93 | TEST_MASTER_TRANSMIT = 7, | ||
94 | TEST_MASTER_HISTORY_REPLAY_LATEST = 8, | ||
95 | TEST_SLAVE_HISTORY_REPLAY_LATEST = 9, | ||
96 | TEST_MASTER_HISTORY_REPLAY = 10, | ||
97 | TEST_SLAVE_HISTORY_REPLAY = 11, | ||
98 | TEST_MASTER_STATE_GET = 12, | ||
99 | TEST_SLAVE_STATE_GET = 13, | ||
100 | TEST_MASTER_STATE_GET_PREFIX = 14, | ||
101 | TEST_SLAVE_STATE_GET_PREFIX = 15, | ||
102 | } test; | ||
103 | |||
104 | |||
105 | static void | ||
106 | master_transmit (); | ||
107 | |||
108 | static void | ||
109 | master_history_replay_latest (); | ||
110 | |||
111 | |||
112 | static void | ||
113 | master_stopped (void *cls) | ||
114 | { | ||
115 | if (NULL != tmit) | ||
116 | { | ||
117 | GNUNET_PSYC_env_destroy (tmit->env); | ||
118 | GNUNET_free (tmit); | ||
119 | tmit = NULL; | ||
120 | } | ||
121 | GNUNET_SCHEDULER_shutdown (); | ||
122 | } | ||
123 | |||
124 | |||
125 | static void | ||
126 | slave_parted (void *cls) | ||
127 | { | ||
128 | if (NULL != mst) | ||
129 | { | ||
130 | GNUNET_PSYC_master_stop (mst, GNUNET_NO, &master_stopped, NULL); | ||
131 | mst = NULL; | ||
132 | } | ||
133 | else | ||
134 | master_stopped (NULL); | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Clean up all resources used. | ||
140 | */ | ||
141 | static void | ||
142 | cleanup () | ||
143 | { | ||
144 | if (NULL != slv) | ||
145 | { | ||
146 | GNUNET_PSYC_slave_part (slv, GNUNET_NO, &slave_parted, NULL); | ||
147 | slv = NULL; | ||
148 | } | ||
149 | else | ||
150 | slave_parted (NULL); | ||
151 | } | ||
152 | |||
153 | |||
154 | /** | ||
155 | * Terminate the test case (failure). | ||
156 | * | ||
157 | * @param cls NULL | ||
158 | */ | ||
159 | static void | ||
160 | end_badly (void *cls) | ||
161 | { | ||
162 | res = 1; | ||
163 | cleanup (); | ||
164 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n"); | ||
165 | } | ||
166 | |||
167 | |||
168 | /** | ||
169 | * Terminate the test case (success). | ||
170 | * | ||
171 | * @param cls NULL | ||
172 | */ | ||
173 | static void | ||
174 | end_normally (void *cls) | ||
175 | { | ||
176 | res = 0; | ||
177 | cleanup (); | ||
178 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n"); | ||
179 | } | ||
180 | |||
181 | |||
182 | /** | ||
183 | * Finish the test case (successfully). | ||
184 | */ | ||
185 | static void | ||
186 | end () | ||
187 | { | ||
188 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending tests.\n"); | ||
189 | |||
190 | if (end_badly_task != NULL) | ||
191 | { | ||
192 | GNUNET_SCHEDULER_cancel (end_badly_task); | ||
193 | end_badly_task = NULL; | ||
194 | } | ||
195 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, | ||
196 | &end_normally, NULL); | ||
197 | } | ||
198 | |||
199 | |||
200 | static void | ||
201 | master_message_cb (void *cls, const struct GNUNET_PSYC_MessageHeader *msg) | ||
202 | { | ||
203 | GNUNET_assert (NULL != msg); | ||
204 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
205 | "Test #%d: Master got PSYC message fragment of size %u " | ||
206 | "belonging to message ID %" PRIu64 " with flags %x\n", | ||
207 | test, ntohs (msg->header.size), | ||
208 | GNUNET_ntohll (msg->message_id), ntohl (msg->flags)); | ||
209 | // FIXME | ||
210 | } | ||
211 | |||
212 | |||
213 | static void | ||
214 | master_message_part_cb (void *cls, const struct GNUNET_PSYC_MessageHeader *msg, | ||
215 | const struct GNUNET_MessageHeader *pmsg) | ||
216 | { | ||
217 | GNUNET_assert (NULL != msg && NULL != pmsg); | ||
218 | |||
219 | uint64_t message_id = GNUNET_ntohll (msg->message_id); | ||
220 | uint32_t flags = ntohl (msg->flags); | ||
221 | |||
222 | uint16_t type = ntohs (pmsg->type); | ||
223 | uint16_t size = ntohs (pmsg->size); | ||
224 | |||
225 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
226 | "Test #%d: Master got message part of type %u and size %u " | ||
227 | "belonging to message ID %" PRIu64 " with flags %x\n", | ||
228 | test, type, size, message_id, flags); | ||
229 | |||
230 | switch (test) | ||
231 | { | ||
232 | case TEST_SLAVE_TRANSMIT: | ||
233 | if (GNUNET_PSYC_MESSAGE_REQUEST != flags) | ||
234 | { | ||
235 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
236 | "Test #%d: Unexpected request flags: %x" PRIu32 "\n", | ||
237 | test, flags); | ||
238 | GNUNET_assert (0); | ||
239 | return; | ||
240 | } | ||
241 | // FIXME: check rest of message | ||
242 | |||
243 | if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type) | ||
244 | master_transmit (); | ||
245 | break; | ||
246 | |||
247 | case TEST_MASTER_TRANSMIT: | ||
248 | if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type && 2 == ++end_count) | ||
249 | master_history_replay_latest (); | ||
250 | break; | ||
251 | |||
252 | case TEST_MASTER_HISTORY_REPLAY: | ||
253 | case TEST_MASTER_HISTORY_REPLAY_LATEST: | ||
254 | if (GNUNET_PSYC_MESSAGE_HISTORIC != flags) | ||
255 | { | ||
256 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
257 | "Test #%d: Unexpected flags for historic message: %x" PRIu32 "\n", | ||
258 | test, flags); | ||
259 | GNUNET_assert (0); | ||
260 | return; | ||
261 | } | ||
262 | break; | ||
263 | |||
264 | default: | ||
265 | GNUNET_assert (0); | ||
266 | } | ||
267 | } | ||
268 | |||
269 | |||
270 | static void | ||
271 | slave_message_cb (void *cls, const struct GNUNET_PSYC_MessageHeader *msg) | ||
272 | { | ||
273 | GNUNET_assert (NULL != msg); | ||
274 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
275 | "Test #%d: Slave got PSYC message fragment of size %u " | ||
276 | "belonging to message ID %" PRIu64 " with flags %x\n", | ||
277 | test, ntohs (msg->header.size), | ||
278 | GNUNET_ntohll (msg->message_id), ntohl (msg->flags)); | ||
279 | // FIXME | ||
280 | } | ||
281 | |||
282 | |||
283 | static void | ||
284 | slave_message_part_cb (void *cls, | ||
285 | const struct GNUNET_PSYC_MessageHeader *msg, | ||
286 | const struct GNUNET_MessageHeader *pmsg) | ||
287 | { | ||
288 | GNUNET_assert (NULL != msg && NULL != pmsg); | ||
289 | |||
290 | uint64_t message_id = GNUNET_ntohll (msg->message_id); | ||
291 | uint32_t flags = ntohl (msg->flags); | ||
292 | |||
293 | uint16_t type = ntohs (pmsg->type); | ||
294 | uint16_t size = ntohs (pmsg->size); | ||
295 | |||
296 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
297 | "Test #%d: Slave got message part of type %u and size %u " | ||
298 | "belonging to message ID %" PRIu64 " with flags %x\n", | ||
299 | test, type, size, message_id, flags); | ||
300 | |||
301 | switch (test) | ||
302 | { | ||
303 | case TEST_MASTER_TRANSMIT: | ||
304 | if (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END == type && 2 == ++end_count) | ||
305 | master_history_replay_latest (); | ||
306 | break; | ||
307 | |||
308 | case TEST_SLAVE_HISTORY_REPLAY: | ||
309 | case TEST_SLAVE_HISTORY_REPLAY_LATEST: | ||
310 | if (GNUNET_PSYC_MESSAGE_HISTORIC != flags) | ||
311 | { | ||
312 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
313 | "Test #%d: Unexpected flags for historic message: %x" PRIu32 "\n", | ||
314 | test, flags); | ||
315 | GNUNET_assert (0); | ||
316 | return; | ||
317 | } | ||
318 | break; | ||
319 | |||
320 | default: | ||
321 | GNUNET_assert (0); | ||
322 | } | ||
323 | } | ||
324 | |||
325 | |||
326 | static void | ||
327 | state_get_var (void *cls, const struct GNUNET_MessageHeader *mod, | ||
328 | const char *name, const void *value, | ||
329 | uint32_t value_size, uint32_t full_value_size) | ||
330 | { | ||
331 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
332 | "Got state var: %s\n%.*s\n", | ||
333 | name, | ||
334 | (int) value_size, | ||
335 | (const char *) value); | ||
336 | } | ||
337 | |||
338 | |||
339 | /*** Slave state_get_prefix() ***/ | ||
340 | |||
341 | static void | ||
342 | slave_state_get_prefix_result (void *cls, int64_t result, | ||
343 | const void *err_msg, uint16_t err_msg_size) | ||
344 | { | ||
345 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
346 | "Test #%d: slave_state_get_prefix:\t%" PRId64 " (%.*s)\n", | ||
347 | test, result, | ||
348 | (int) err_msg_size, | ||
349 | (const char *) err_msg); | ||
350 | // FIXME: GNUNET_assert (2 == result); | ||
351 | end (); | ||
352 | } | ||
353 | |||
354 | |||
355 | static void | ||
356 | slave_state_get_prefix () | ||
357 | { | ||
358 | test = TEST_SLAVE_STATE_GET_PREFIX; | ||
359 | GNUNET_PSYC_channel_state_get_prefix (slv_chn, "_foo", state_get_var, | ||
360 | slave_state_get_prefix_result, NULL); | ||
361 | } | ||
362 | |||
363 | |||
364 | /*** Master state_get_prefix() ***/ | ||
365 | |||
366 | |||
367 | static void | ||
368 | master_state_get_prefix_result (void *cls, int64_t result, | ||
369 | const void *err_msg, uint16_t err_msg_size) | ||
370 | { | ||
371 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
372 | "Test #%d: master_state_get_prefix:\t%" PRId64 " (%s)\n", | ||
373 | test, result, (char *) err_msg); | ||
374 | // FIXME: GNUNET_assert (2 == result); | ||
375 | slave_state_get_prefix (); | ||
376 | } | ||
377 | |||
378 | |||
379 | static void | ||
380 | master_state_get_prefix () | ||
381 | { | ||
382 | test = TEST_MASTER_STATE_GET_PREFIX; | ||
383 | GNUNET_PSYC_channel_state_get_prefix (mst_chn, "_foo", state_get_var, | ||
384 | master_state_get_prefix_result, NULL); | ||
385 | } | ||
386 | |||
387 | |||
388 | /*** Slave state_get() ***/ | ||
389 | |||
390 | |||
391 | static void | ||
392 | slave_state_get_result (void *cls, int64_t result, | ||
393 | const void *err_msg, uint16_t err_msg_size) | ||
394 | { | ||
395 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
396 | "Test #%d: slave_state_get:\t%" PRId64 " (%.*s)\n", | ||
397 | test, result, err_msg_size, (char *) err_msg); | ||
398 | // FIXME: GNUNET_assert (2 == result); | ||
399 | master_state_get_prefix (); | ||
400 | } | ||
401 | |||
402 | |||
403 | static void | ||
404 | slave_state_get () | ||
405 | { | ||
406 | test = TEST_SLAVE_STATE_GET; | ||
407 | GNUNET_PSYC_channel_state_get (slv_chn, "_foo_bar_baz", state_get_var, | ||
408 | slave_state_get_result, NULL); | ||
409 | } | ||
410 | |||
411 | |||
412 | /*** Master state_get() ***/ | ||
413 | |||
414 | |||
415 | static void | ||
416 | master_state_get_result (void *cls, int64_t result, | ||
417 | const void *err_msg, uint16_t err_msg_size) | ||
418 | { | ||
419 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
420 | "Test #%d: master_state_get:\t%" PRId64 " (%.*s)\n", | ||
421 | test, result, err_msg_size, (char *) err_msg); | ||
422 | // FIXME: GNUNET_assert (1 == result); | ||
423 | slave_state_get (); | ||
424 | } | ||
425 | |||
426 | |||
427 | static void | ||
428 | master_state_get () | ||
429 | { | ||
430 | test = TEST_MASTER_STATE_GET; | ||
431 | GNUNET_PSYC_channel_state_get (mst_chn, "_foo_bar_baz", state_get_var, | ||
432 | master_state_get_result, NULL); | ||
433 | } | ||
434 | |||
435 | |||
436 | /*** Slave history_replay() ***/ | ||
437 | |||
438 | static void | ||
439 | slave_history_replay_result (void *cls, int64_t result, | ||
440 | const void *err_msg, uint16_t err_msg_size) | ||
441 | { | ||
442 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
443 | "Test #%d: slave_history_replay:\t%" PRId64 " (%.*s)\n", | ||
444 | test, result, | ||
445 | (int) err_msg_size, | ||
446 | (const char *) err_msg); | ||
447 | GNUNET_assert (9 == result); | ||
448 | |||
449 | master_state_get (); | ||
450 | } | ||
451 | |||
452 | |||
453 | static void | ||
454 | slave_history_replay () | ||
455 | { | ||
456 | test = TEST_SLAVE_HISTORY_REPLAY; | ||
457 | GNUNET_PSYC_channel_history_replay (slv_chn, 1, 1, "", | ||
458 | GNUNET_PSYC_HISTORY_REPLAY_LOCAL, | ||
459 | slave_message_cb, | ||
460 | slave_message_part_cb, | ||
461 | slave_history_replay_result, NULL); | ||
462 | } | ||
463 | |||
464 | |||
465 | /*** Master history_replay() ***/ | ||
466 | |||
467 | |||
468 | static void | ||
469 | master_history_replay_result (void *cls, int64_t result, | ||
470 | const void *err_msg, uint16_t err_msg_size) | ||
471 | { | ||
472 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
473 | "Test #%d: master_history_replay:\t%" PRId64 " (%.*s)\n", | ||
474 | test, result, | ||
475 | (int) err_msg_size, | ||
476 | (const char *) err_msg); | ||
477 | GNUNET_assert (9 == result); | ||
478 | |||
479 | slave_history_replay (); | ||
480 | } | ||
481 | |||
482 | |||
483 | static void | ||
484 | master_history_replay () | ||
485 | { | ||
486 | test = TEST_MASTER_HISTORY_REPLAY; | ||
487 | GNUNET_PSYC_channel_history_replay (mst_chn, 1, 1, "", | ||
488 | GNUNET_PSYC_HISTORY_REPLAY_LOCAL, | ||
489 | master_message_cb, | ||
490 | master_message_part_cb, | ||
491 | master_history_replay_result, NULL); | ||
492 | } | ||
493 | |||
494 | |||
495 | /*** Slave history_replay_latest() ***/ | ||
496 | |||
497 | |||
498 | static void | ||
499 | slave_history_replay_latest_result (void *cls, int64_t result, | ||
500 | const void *err_msg, uint16_t err_msg_size) | ||
501 | { | ||
502 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
503 | "Test #%d: slave_history_replay_latest:\t%" PRId64 " (%.*s)\n", | ||
504 | test, result, | ||
505 | (int) err_msg_size, | ||
506 | (const char *) err_msg); | ||
507 | GNUNET_assert (9 == result); | ||
508 | |||
509 | master_history_replay (); | ||
510 | } | ||
511 | |||
512 | |||
513 | static void | ||
514 | slave_history_replay_latest () | ||
515 | { | ||
516 | test = TEST_SLAVE_HISTORY_REPLAY_LATEST; | ||
517 | GNUNET_PSYC_channel_history_replay_latest (slv_chn, 1, "", | ||
518 | GNUNET_PSYC_HISTORY_REPLAY_LOCAL, | ||
519 | &slave_message_cb, | ||
520 | &slave_message_part_cb, | ||
521 | &slave_history_replay_latest_result, | ||
522 | NULL); | ||
523 | } | ||
524 | |||
525 | |||
526 | /*** Master history_replay_latest() ***/ | ||
527 | |||
528 | |||
529 | static void | ||
530 | master_history_replay_latest_result (void *cls, int64_t result, | ||
531 | const void *err_msg, uint16_t err_msg_size) | ||
532 | { | ||
533 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
534 | "Test #%d: master_history_replay_latest:\t%" PRId64 " (%.*s)\n", | ||
535 | test, result, err_msg_size, (char *) err_msg); | ||
536 | GNUNET_assert (9 == result); | ||
537 | |||
538 | slave_history_replay_latest (); | ||
539 | } | ||
540 | |||
541 | |||
542 | static void | ||
543 | master_history_replay_latest () | ||
544 | { | ||
545 | test = TEST_MASTER_HISTORY_REPLAY_LATEST; | ||
546 | GNUNET_PSYC_channel_history_replay_latest (mst_chn, 1, "", | ||
547 | GNUNET_PSYC_HISTORY_REPLAY_LOCAL, | ||
548 | &master_message_cb, | ||
549 | &master_message_part_cb, | ||
550 | &master_history_replay_latest_result, | ||
551 | NULL); | ||
552 | } | ||
553 | |||
554 | |||
555 | static void | ||
556 | transmit_resume (void *cls) | ||
557 | { | ||
558 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
559 | "Test #%d: Transmission resumed.\n", test); | ||
560 | struct TransmitClosure *tmit = cls; | ||
561 | if (NULL != tmit->mst_tmit) | ||
562 | GNUNET_PSYC_master_transmit_resume (tmit->mst_tmit); | ||
563 | else | ||
564 | GNUNET_PSYC_slave_transmit_resume (tmit->slv_tmit); | ||
565 | } | ||
566 | |||
567 | |||
568 | static int | ||
569 | tmit_notify_data (void *cls, uint16_t *data_size, void *data) | ||
570 | { | ||
571 | struct TransmitClosure *tmit = cls; | ||
572 | if (0 == tmit->data_count) | ||
573 | { | ||
574 | *data_size = 0; | ||
575 | return GNUNET_YES; | ||
576 | } | ||
577 | |||
578 | uint16_t size = strlen (tmit->data[tmit->n]); | ||
579 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
580 | "Test #%d: Transmit notify data: %u bytes available, " | ||
581 | "processing fragment %u/%u (size %u).\n", | ||
582 | test, *data_size, tmit->n + 1, tmit->data_count, size); | ||
583 | if (*data_size < size) | ||
584 | { | ||
585 | *data_size = 0; | ||
586 | GNUNET_assert (0); | ||
587 | return GNUNET_SYSERR; | ||
588 | } | ||
589 | |||
590 | if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n]) | ||
591 | { | ||
592 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
593 | "Test #%d: Transmission paused.\n", test); | ||
594 | tmit->paused = GNUNET_YES; | ||
595 | GNUNET_SCHEDULER_add_delayed ( | ||
596 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, | ||
597 | tmit->data_delay[tmit->n]), | ||
598 | &transmit_resume, tmit); | ||
599 | *data_size = 0; | ||
600 | return GNUNET_NO; | ||
601 | } | ||
602 | tmit->paused = GNUNET_NO; | ||
603 | |||
604 | *data_size = size; | ||
605 | GNUNET_memcpy (data, tmit->data[tmit->n], size); | ||
606 | |||
607 | return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES; | ||
608 | } | ||
609 | |||
610 | |||
611 | static int | ||
612 | tmit_notify_mod (void *cls, uint16_t *data_size, void *data, uint8_t *oper, | ||
613 | uint32_t *full_value_size) | ||
614 | { | ||
615 | struct TransmitClosure *tmit = cls; | ||
616 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
617 | "Test #%d: Transmit notify modifier: %u bytes available, " | ||
618 | "%u modifiers left to process.\n", | ||
619 | test, *data_size, (unsigned int) GNUNET_PSYC_env_get_count (tmit->env)); | ||
620 | |||
621 | uint16_t name_size = 0; | ||
622 | size_t value_size = 0; | ||
623 | const char *value = NULL; | ||
624 | |||
625 | if (NULL != oper && NULL != tmit->mod) | ||
626 | { /* New modifier */ | ||
627 | tmit->mod = tmit->mod->next; | ||
628 | if (NULL == tmit->mod) | ||
629 | { /* No more modifiers, continue with data */ | ||
630 | *data_size = 0; | ||
631 | return GNUNET_YES; | ||
632 | } | ||
633 | |||
634 | GNUNET_assert (tmit->mod->value_size < UINT32_MAX); | ||
635 | *full_value_size = tmit->mod->value_size; | ||
636 | *oper = tmit->mod->oper; | ||
637 | name_size = strlen (tmit->mod->name); | ||
638 | |||
639 | if (name_size + 1 + tmit->mod->value_size <= *data_size) | ||
640 | { | ||
641 | *data_size = name_size + 1 + tmit->mod->value_size; | ||
642 | } | ||
643 | else | ||
644 | { | ||
645 | tmit->mod_value_size = tmit->mod->value_size; | ||
646 | value_size = *data_size - name_size - 1; | ||
647 | tmit->mod_value_size -= value_size; | ||
648 | tmit->mod_value = tmit->mod->value + value_size; | ||
649 | } | ||
650 | |||
651 | GNUNET_memcpy (data, tmit->mod->name, name_size); | ||
652 | ((char *)data)[name_size] = '\0'; | ||
653 | GNUNET_memcpy ((char *)data + name_size + 1, tmit->mod->value, value_size); | ||
654 | } | ||
655 | else if (NULL != tmit->mod_value && 0 < tmit->mod_value_size) | ||
656 | { /* Modifier continuation */ | ||
657 | value = tmit->mod_value; | ||
658 | if (tmit->mod_value_size <= *data_size) | ||
659 | { | ||
660 | value_size = tmit->mod_value_size; | ||
661 | tmit->mod_value = NULL; | ||
662 | } | ||
663 | else | ||
664 | { | ||
665 | value_size = *data_size; | ||
666 | tmit->mod_value += value_size; | ||
667 | } | ||
668 | tmit->mod_value_size -= value_size; | ||
669 | |||
670 | if (*data_size < value_size) | ||
671 | { | ||
672 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
673 | "value larger than buffer: %u < %zu\n", | ||
674 | *data_size, value_size); | ||
675 | *data_size = 0; | ||
676 | return GNUNET_NO; | ||
677 | } | ||
678 | |||
679 | *data_size = value_size; | ||
680 | GNUNET_memcpy (data, value, value_size); | ||
681 | } | ||
682 | |||
683 | return GNUNET_NO; | ||
684 | } | ||
685 | |||
686 | |||
687 | static void | ||
688 | slave_join (); | ||
689 | |||
690 | |||
691 | static void | ||
692 | slave_transmit () | ||
693 | { | ||
694 | test = TEST_SLAVE_TRANSMIT; | ||
695 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
696 | "Test #%d: Slave sending request to master.\n", test); | ||
697 | |||
698 | tmit = GNUNET_new (struct TransmitClosure); | ||
699 | tmit->env = GNUNET_PSYC_env_create (); | ||
700 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
701 | "_abc", "abc def", 7); | ||
702 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
703 | "_abc_def", "abc def ghi", 11); | ||
704 | tmit->mod = GNUNET_PSYC_env_head (tmit->env); | ||
705 | tmit->n = 0; | ||
706 | tmit->data[0] = "slave test"; | ||
707 | tmit->data_count = 1; | ||
708 | tmit->slv_tmit | ||
709 | = GNUNET_PSYC_slave_transmit (slv, "_request_test", &tmit_notify_mod, | ||
710 | &tmit_notify_data, tmit, | ||
711 | GNUNET_PSYC_SLAVE_TRANSMIT_NONE); | ||
712 | } | ||
713 | |||
714 | |||
715 | static void | ||
716 | slave_remove_cb (void *cls, int64_t result, | ||
717 | const void *err_msg, uint16_t err_msg_size) | ||
718 | { | ||
719 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
720 | "Test #%d: slave_remove:\t%" PRId64 " (%.*s)\n", | ||
721 | test, result, err_msg_size, (char *) err_msg); | ||
722 | |||
723 | slave_transmit (); | ||
724 | } | ||
725 | |||
726 | |||
727 | static void | ||
728 | slave_remove () | ||
729 | { | ||
730 | test = TEST_SLAVE_REMOVE; | ||
731 | struct GNUNET_PSYC_Channel *chn = GNUNET_PSYC_master_get_channel (mst); | ||
732 | GNUNET_PSYC_channel_slave_remove (chn, &slave_pub_key, 2, | ||
733 | &slave_remove_cb, chn); | ||
734 | } | ||
735 | |||
736 | |||
737 | static void | ||
738 | slave_add_cb (void *cls, int64_t result, | ||
739 | const void *err_msg, uint16_t err_msg_size) | ||
740 | { | ||
741 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
742 | "Test #%d: slave_add:\t%" PRId64 " (%.*s)\n", | ||
743 | test, result, err_msg_size, (char *) err_msg); | ||
744 | slave_remove (); | ||
745 | } | ||
746 | |||
747 | |||
748 | static void | ||
749 | slave_add () | ||
750 | { | ||
751 | test = TEST_SLAVE_ADD; | ||
752 | struct GNUNET_PSYC_Channel *chn = GNUNET_PSYC_master_get_channel (mst); | ||
753 | GNUNET_PSYC_channel_slave_add (chn, &slave_pub_key, 2, 2, &slave_add_cb, chn); | ||
754 | } | ||
755 | |||
756 | |||
757 | static void | ||
758 | schedule_second_slave_join (void *cls) | ||
759 | { | ||
760 | slave_join (TEST_SLAVE_JOIN_ACCEPT); | ||
761 | } | ||
762 | |||
763 | |||
764 | static void | ||
765 | first_slave_parted (void *cls) | ||
766 | { | ||
767 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "First slave parted.\n"); | ||
768 | GNUNET_SCHEDULER_add_now (&schedule_second_slave_join, NULL); | ||
769 | } | ||
770 | |||
771 | |||
772 | static void | ||
773 | schedule_first_slave_part (void *cls) | ||
774 | { | ||
775 | GNUNET_PSYC_slave_part (slv, GNUNET_NO, &first_slave_parted, NULL); | ||
776 | } | ||
777 | |||
778 | |||
779 | static void | ||
780 | join_decision_cb (void *cls, | ||
781 | const struct GNUNET_PSYC_JoinDecisionMessage *dcsn, | ||
782 | int is_admitted, | ||
783 | const struct GNUNET_PSYC_Message *join_msg) | ||
784 | { | ||
785 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
786 | "Test #%d: Slave got join decision: %d\n", test, is_admitted); | ||
787 | |||
788 | switch (test) | ||
789 | { | ||
790 | case TEST_SLAVE_JOIN_REJECT: | ||
791 | GNUNET_assert (0 == is_admitted); | ||
792 | GNUNET_assert (1 == join_req_count); | ||
793 | GNUNET_SCHEDULER_add_now (&schedule_first_slave_part, NULL); | ||
794 | break; | ||
795 | |||
796 | case TEST_SLAVE_JOIN_ACCEPT: | ||
797 | GNUNET_assert (1 == is_admitted); | ||
798 | GNUNET_assert (2 == join_req_count); | ||
799 | slave_add (); | ||
800 | break; | ||
801 | |||
802 | default: | ||
803 | GNUNET_break (0); | ||
804 | } | ||
805 | } | ||
806 | |||
807 | |||
808 | static void | ||
809 | join_request_cb (void *cls, | ||
810 | const struct GNUNET_PSYC_JoinRequestMessage *req, | ||
811 | const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key, | ||
812 | const struct GNUNET_PSYC_Message *join_msg, | ||
813 | struct GNUNET_PSYC_JoinHandle *jh) | ||
814 | { | ||
815 | struct GNUNET_HashCode slave_key_hash; | ||
816 | GNUNET_CRYPTO_hash (slave_key, sizeof (*slave_key), &slave_key_hash); | ||
817 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
818 | "Test #%d: Got join request #%u from %s.\n", | ||
819 | test, join_req_count, GNUNET_h2s (&slave_key_hash)); | ||
820 | |||
821 | /* Reject first request */ | ||
822 | int is_admitted = (0 < join_req_count++) ? GNUNET_YES : GNUNET_NO; | ||
823 | GNUNET_PSYC_join_decision (jh, is_admitted, 0, NULL, NULL); | ||
824 | } | ||
825 | |||
826 | |||
827 | static void | ||
828 | slave_connect_cb (void *cls, int result, uint64_t max_message_id) | ||
829 | { | ||
830 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
831 | "Test #%d: Slave connected: %d, max_message_id: %" PRIu64 "\n", | ||
832 | test, result, max_message_id); | ||
833 | GNUNET_assert (TEST_SLAVE_JOIN_REJECT == test || TEST_SLAVE_JOIN_ACCEPT == test); | ||
834 | GNUNET_assert (GNUNET_OK == result || GNUNET_NO == result); | ||
835 | } | ||
836 | |||
837 | |||
838 | static void | ||
839 | slave_join (int t) | ||
840 | { | ||
841 | test = t; | ||
842 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
843 | "Test #%d: Joining slave.\n", t); | ||
844 | |||
845 | struct GNUNET_PeerIdentity origin = this_peer; | ||
846 | struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create (); | ||
847 | GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, | ||
848 | "_foo", "bar baz", 7); | ||
849 | GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN, | ||
850 | "_foo_bar", "foo bar baz", 11); | ||
851 | struct GNUNET_PSYC_Message * | ||
852 | join_msg = GNUNET_PSYC_message_create ("_request_join", env, "some data", 9); | ||
853 | |||
854 | slv = GNUNET_PSYC_slave_join (cfg, | ||
855 | &channel_pub_key, | ||
856 | slave_key, | ||
857 | GNUNET_PSYC_SLAVE_JOIN_NONE, | ||
858 | &origin, | ||
859 | 0, | ||
860 | NULL, | ||
861 | &slave_message_cb, | ||
862 | &slave_message_part_cb, | ||
863 | &slave_connect_cb, | ||
864 | &join_decision_cb, | ||
865 | NULL, | ||
866 | join_msg); | ||
867 | GNUNET_free (join_msg); | ||
868 | slv_chn = GNUNET_PSYC_slave_get_channel (slv); | ||
869 | GNUNET_PSYC_env_destroy (env); | ||
870 | } | ||
871 | |||
872 | |||
873 | static void | ||
874 | master_transmit () | ||
875 | { | ||
876 | test = TEST_MASTER_TRANSMIT; | ||
877 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
878 | "Test #%d: Master sending message to all.\n", test); | ||
879 | end_count = 0; | ||
880 | |||
881 | uint32_t i, j; | ||
882 | |||
883 | char *name_max = "_test_max"; | ||
884 | uint8_t name_max_size = sizeof ("_test_max"); | ||
885 | char *val_max = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD); | ||
886 | for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD; i++) | ||
887 | val_max[i] = (0 == i % 10000) ? '0' + i / 10000 : '.'; | ||
888 | |||
889 | char *name_cont = "_test_cont"; | ||
890 | uint8_t name_cont_size = sizeof ("_test_cont"); | ||
891 | char *val_cont = GNUNET_malloc (GNUNET_PSYC_MODIFIER_MAX_PAYLOAD | ||
892 | + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD); | ||
893 | for (i = 0; i < GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size; i++) | ||
894 | val_cont[i] = (0 == i % 10000) ? '0' + i / 10000 : ':'; | ||
895 | for (j = 0; j < GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD; j++, i++) | ||
896 | val_cont[i] = (0 == j % 10000) ? '0' + j / 10000 : '!'; | ||
897 | |||
898 | tmit = GNUNET_new (struct TransmitClosure); | ||
899 | tmit->env = GNUNET_PSYC_env_create (); | ||
900 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
901 | "_foo", "bar baz", 7); | ||
902 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
903 | name_max, val_max, | ||
904 | GNUNET_PSYC_MODIFIER_MAX_PAYLOAD | ||
905 | - name_max_size); | ||
906 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
907 | "_foo_bar", "foo bar baz", 11); | ||
908 | GNUNET_PSYC_env_add (tmit->env, GNUNET_PSYC_OP_ASSIGN, | ||
909 | name_cont, val_cont, | ||
910 | GNUNET_PSYC_MODIFIER_MAX_PAYLOAD - name_cont_size | ||
911 | + GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD); | ||
912 | tmit->mod = GNUNET_PSYC_env_head (tmit->env); | ||
913 | tmit->data[0] = "foo"; | ||
914 | tmit->data[1] = GNUNET_malloc (GNUNET_PSYC_DATA_MAX_PAYLOAD + 1); | ||
915 | for (i = 0; i < GNUNET_PSYC_DATA_MAX_PAYLOAD; i++) | ||
916 | tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_'; | ||
917 | tmit->data[2] = "foo bar"; | ||
918 | tmit->data[3] = "foo bar baz"; | ||
919 | tmit->data_delay[1] = 3; | ||
920 | tmit->data_count = 4; | ||
921 | tmit->mst_tmit | ||
922 | = GNUNET_PSYC_master_transmit (mst, "_notice_test", &tmit_notify_mod, | ||
923 | &tmit_notify_data, tmit, | ||
924 | GNUNET_PSYC_MASTER_TRANSMIT_INC_GROUP_GEN); | ||
925 | } | ||
926 | |||
927 | |||
928 | static void | ||
929 | master_start_cb (void *cls, int result, uint64_t max_message_id) | ||
930 | { | ||
931 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
932 | "Test #%d: Master started: %d, max_message_id: %" PRIu64 "\n", | ||
933 | test, result, max_message_id); | ||
934 | GNUNET_assert (TEST_MASTER_START == test); | ||
935 | GNUNET_assert (GNUNET_OK == result || GNUNET_NO == result); | ||
936 | slave_join (TEST_SLAVE_JOIN_REJECT); | ||
937 | } | ||
938 | |||
939 | |||
940 | static void | ||
941 | master_start () | ||
942 | { | ||
943 | test = TEST_MASTER_START; | ||
944 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
945 | "Test #%d: Starting master.\n", test); | ||
946 | mst = GNUNET_PSYC_master_start (cfg, channel_key, GNUNET_PSYC_CHANNEL_PRIVATE, | ||
947 | &master_start_cb, &join_request_cb, | ||
948 | &master_message_cb, &master_message_part_cb, | ||
949 | NULL); | ||
950 | mst_chn = GNUNET_PSYC_master_get_channel (mst); | ||
951 | } | ||
952 | |||
953 | |||
954 | static void | ||
955 | schedule_master_start (void *cls) | ||
956 | { | ||
957 | master_start (); | ||
958 | } | ||
959 | |||
960 | |||
961 | /** | ||
962 | * Main function of the test, run from scheduler. | ||
963 | * | ||
964 | * @param cls NULL | ||
965 | * @param cfg configuration we use (also to connect to PSYC service) | ||
966 | * @param peer handle to access more of the peer (not used) | ||
967 | */ | ||
968 | static void | ||
969 | #if DEBUG_TEST_PSYC | ||
970 | run (void *cls, char *const *args, const char *cfgfile, | ||
971 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
972 | #else | ||
973 | run (void *cls, | ||
974 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
975 | struct GNUNET_TESTING_Peer *peer) | ||
976 | #endif | ||
977 | { | ||
978 | cfg = c; | ||
979 | end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
980 | |||
981 | GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer); | ||
982 | |||
983 | channel_key = GNUNET_CRYPTO_eddsa_key_create (); | ||
984 | slave_key = GNUNET_CRYPTO_ecdsa_key_create (); | ||
985 | |||
986 | GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key); | ||
987 | GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key); | ||
988 | |||
989 | #if DEBUG_TEST_PSYC | ||
990 | master_start (); | ||
991 | #else | ||
992 | /* Allow some time for the services to initialize. */ | ||
993 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
994 | &schedule_master_start, NULL); | ||
995 | #endif | ||
996 | } | ||
997 | |||
998 | |||
999 | int | ||
1000 | main (int argc, char *argv[]) | ||
1001 | { | ||
1002 | res = 1; | ||
1003 | #if DEBUG_TEST_PSYC | ||
1004 | const struct GNUNET_GETOPT_CommandLineOption opts[] = { | ||
1005 | GNUNET_GETOPT_OPTION_END | ||
1006 | }; | ||
1007 | if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc", | ||
1008 | "test-psyc [options]", | ||
1009 | opts, &run, NULL)) | ||
1010 | return 1; | ||
1011 | #else | ||
1012 | if (0 != GNUNET_TESTING_peer_run ("test-psyc", "test_psyc.conf", &run, NULL)) | ||
1013 | return 1; | ||
1014 | #endif | ||
1015 | return res; | ||
1016 | } | ||
1017 | |||
1018 | /* end of test_psyc.c */ | ||