diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-06-25 12:37:46 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-06-29 23:24:20 +0000 |
commit | 706fad1ce88c2104009a3835ee343ff9d8ec8b79 (patch) | |
tree | b2c0d8ccb434515cbbd4e0c9e86ce21f099c11a7 /auto_tests/overflow_recvq_test.c | |
parent | 52f21e32518244f3008efbef073f9d3ac9e68b43 (diff) |
Add a test to try and overflow the send queue in net_crypto.
Diffstat (limited to 'auto_tests/overflow_recvq_test.c')
-rw-r--r-- | auto_tests/overflow_recvq_test.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/auto_tests/overflow_recvq_test.c b/auto_tests/overflow_recvq_test.c new file mode 100644 index 00000000..3227c5f4 --- /dev/null +++ b/auto_tests/overflow_recvq_test.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* Try to overflow the net_crypto packet buffer. | ||
2 | */ | ||
3 | |||
4 | #ifndef _XOPEN_SOURCE | ||
5 | #define _XOPEN_SOURCE 600 | ||
6 | #endif | ||
7 | |||
8 | #include "check_compat.h" | ||
9 | |||
10 | #include "../toxcore/tox.h" | ||
11 | |||
12 | typedef struct State { | ||
13 | uint32_t index; | ||
14 | uint32_t recv_count; | ||
15 | } State; | ||
16 | |||
17 | #include "run_auto_test.h" | ||
18 | |||
19 | #define NUM_MSGS 40000 | ||
20 | |||
21 | static void handle_friend_message(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, | ||
22 | const uint8_t *message, size_t length, void *user_data) | ||
23 | { | ||
24 | State *state = (State *)user_data; | ||
25 | state->recv_count++; | ||
26 | } | ||
27 | |||
28 | static void net_crypto_overflow_test(Tox **toxes, State *state) | ||
29 | { | ||
30 | tox_callback_friend_message(toxes[0], handle_friend_message); | ||
31 | |||
32 | printf("sending many messages to tox0\n"); | ||
33 | |||
34 | for (uint32_t tox_index = 1; tox_index < 3; tox_index++) { | ||
35 | for (uint32_t i = 0; i < NUM_MSGS; i++) { | ||
36 | uint8_t message[128] = {0}; | ||
37 | snprintf((char *)message, sizeof(message), "%u-%u", tox_index, i); | ||
38 | |||
39 | TOX_ERR_FRIEND_SEND_MESSAGE err; | ||
40 | tox_friend_send_message(toxes[tox_index], 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof message, &err); | ||
41 | |||
42 | if (err == TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ) { | ||
43 | printf("tox%u sent %u messages to friend 0\n", tox_index, i); | ||
44 | break; | ||
45 | } | ||
46 | |||
47 | ck_assert_msg(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK, | ||
48 | "tox%u failed to send message number %u: %d", tox_index, i, err); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | // TODO(iphydf): Wait until all messages have arrived. Currently, not all | ||
53 | // messages arrive, so this test would always fail. | ||
54 | for (uint32_t i = 0; i < 200; i++) { | ||
55 | iterate_all(3, toxes, state); | ||
56 | c_sleep(ITERATION_INTERVAL); | ||
57 | } | ||
58 | |||
59 | printf("tox%u received %u messages\n", state[0].index, state[0].recv_count); | ||
60 | } | ||
61 | |||
62 | int main(void) | ||
63 | { | ||
64 | setvbuf(stdout, nullptr, _IONBF, 0); | ||
65 | |||
66 | run_auto_test(3, net_crypto_overflow_test); | ||
67 | return 0; | ||
68 | } | ||