/work/auto_tests/overflow_recvq_test.c
Line | Count | Source |
1 | | /* Try to overflow the net_crypto packet buffer. |
2 | | */ |
3 | | |
4 | | #include <stdint.h> |
5 | | |
6 | | typedef struct State { |
7 | | uint32_t recv_count; |
8 | | } State; |
9 | | |
10 | | #include "auto_test_support.h" |
11 | | |
12 | 65.5k | #define NUM_MSGS 40000 |
13 | | |
14 | | static void handle_friend_message(Tox *tox, const Tox_Event_Friend_Message *event, void *user_data) |
15 | 8.71k | { |
16 | | //const uint32_t friend_number = tox_event_friend_message_get_friend_number(event); |
17 | | //const Tox_Message_Type type = tox_event_friend_message_get_type(event); |
18 | | //const uint8_t *message = tox_event_friend_message_get_message(event); |
19 | | //const uint32_t message_length = tox_event_friend_message_get_message_length(event); |
20 | | |
21 | 8.71k | const AutoTox *autotox = (AutoTox *)user_data; |
22 | 8.71k | State *state = (State *)autotox->state; |
23 | 8.71k | state->recv_count++; |
24 | 8.71k | } |
25 | | |
26 | | static void net_crypto_overflow_test(AutoTox *autotoxes) |
27 | 1 | { |
28 | 1 | tox_events_callback_friend_message(autotoxes[0].dispatch, handle_friend_message); |
29 | | |
30 | 1 | printf("sending many messages to tox0\n"); |
31 | | |
32 | 3 | for (uint32_t tox_index = 1; tox_index < 3; tox_index++) { |
33 | 65.5k | for (uint32_t i = 0; i < NUM_MSGS; i++) { |
34 | 65.5k | uint8_t message[128] = {0}; |
35 | 65.5k | snprintf((char *)message, sizeof(message), "%u-%u", tox_index, i); |
36 | | |
37 | 65.5k | Tox_Err_Friend_Send_Message err; |
38 | 65.5k | tox_friend_send_message(autotoxes[tox_index].tox, 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof message, &err); |
39 | | |
40 | 65.5k | if (err == TOX_ERR_FRIEND_SEND_MESSAGE_SENDQ) { |
41 | 2 | printf("tox%u sent %u messages to friend 0\n", tox_index, i); |
42 | 2 | break; |
43 | 2 | } |
44 | | |
45 | 65.5k | ck_assert_msg(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK, |
46 | 65.5k | "tox%u failed to send message number %u: %d", tox_index, i, err); |
47 | 65.5k | } |
48 | 2 | } |
49 | | |
50 | | // TODO(iphydf): Wait until all messages have arrived. Currently, not all |
51 | | // messages arrive, so this test would always fail. |
52 | 201 | for (uint32_t i = 0; i < 200; i++) { |
53 | 200 | iterate_all_wait(autotoxes, 3, ITERATION_INTERVAL); |
54 | 200 | } |
55 | | |
56 | 1 | printf("tox%u received %u messages\n", autotoxes[0].index, ((State *)autotoxes[0].state)->recv_count); |
57 | 1 | } |
58 | | |
59 | | int main(void) |
60 | 721 | { |
61 | 721 | setvbuf(stdout, nullptr, _IONBF, 0); |
62 | | |
63 | 721 | Run_Auto_Options options = default_run_auto_options(); |
64 | 721 | options.graph = GRAPH_LINEAR; |
65 | 721 | run_auto_test(nullptr, 3, net_crypto_overflow_test, sizeof(State), &options); |
66 | | |
67 | 721 | return 0; |
68 | 721 | } |