/work/auto_tests/send_message_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Tests that we can send messages to friends. |
2 | | */ |
3 | | |
4 | | #include <stdbool.h> |
5 | | #include <stdint.h> |
6 | | #include <string.h> |
7 | | |
8 | | typedef struct State { |
9 | | bool message_received; |
10 | | } State; |
11 | | |
12 | | #include "auto_test_support.h" |
13 | | |
14 | 224 | #define MESSAGE_FILLER 'G' |
15 | | |
16 | | static void message_callback( |
17 | | Tox *m, const Tox_Event_Friend_Message *event, void *user_data) |
18 | 105 | { |
19 | 105 | const AutoTox *autotox = (AutoTox *)user_data; |
20 | 105 | State *state = (State *)autotox->state; |
21 | | |
22 | 105 | const Tox_Message_Type type = tox_event_friend_message_get_type(event); |
23 | 105 | const uint8_t *string = tox_event_friend_message_get_message(event); |
24 | 105 | const uint32_t length = tox_event_friend_message_get_message_length(event); |
25 | | |
26 | 105 | if (type != TOX_MESSAGE_TYPE_NORMAL) { |
27 | 0 | ck_abort_msg("Bad type"); |
28 | 0 | } |
29 | | |
30 | 105 | const size_t cmp_msg_len = tox_max_message_length(); |
31 | 105 | uint8_t *cmp_msg = (uint8_t *)malloc(cmp_msg_len); |
32 | 105 | ck_assert(cmp_msg != nullptr); |
33 | 103 | memset(cmp_msg, MESSAGE_FILLER, cmp_msg_len); |
34 | | |
35 | 103 | if (length == tox_max_message_length() && memcmp(string, cmp_msg, cmp_msg_len) == 0) { |
36 | 103 | state->message_received = true; |
37 | 103 | } |
38 | | |
39 | 103 | free(cmp_msg); |
40 | 103 | } |
41 | | |
42 | | static void send_message_test(AutoTox *autotoxes) |
43 | 123 | { |
44 | 123 | tox_events_callback_friend_message(autotoxes[1].dispatch, &message_callback); |
45 | | |
46 | 123 | const size_t msgs_len = tox_max_message_length() + 1; |
47 | 123 | uint8_t *msgs = (uint8_t *)malloc(msgs_len); |
48 | 123 | ck_assert(msgs != nullptr); |
49 | 121 | memset(msgs, MESSAGE_FILLER, msgs_len); |
50 | | |
51 | 121 | Tox_Err_Friend_Send_Message errm; |
52 | 121 | tox_friend_send_message(autotoxes[0].tox, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, msgs_len, &errm); |
53 | 121 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "tox_max_message_length() is too small? error=%d", errm); |
54 | | |
55 | 121 | tox_friend_send_message(autotoxes[0].tox, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, tox_max_message_length(), &errm); |
56 | 121 | ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "tox_max_message_length() is too big? error=%d", errm); |
57 | | |
58 | 119 | free(msgs); |
59 | | |
60 | 179 | do { |
61 | 179 | iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL); |
62 | 179 | } while (!((State *)autotoxes[1].state)->message_received); |
63 | 119 | } |
64 | | |
65 | | int main(void) |
66 | 194 | { |
67 | 194 | setvbuf(stdout, nullptr, _IONBF, 0); |
68 | | |
69 | 194 | struct Tox_Options *tox_options = tox_options_new(nullptr); |
70 | 194 | ck_assert(tox_options != nullptr); |
71 | | |
72 | 194 | Run_Auto_Options options = default_run_auto_options(); |
73 | 194 | options.graph = GRAPH_LINEAR; |
74 | 194 | tox_options_set_ipv6_enabled(tox_options, true); |
75 | 194 | run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options); |
76 | | |
77 | 194 | tox_options_set_ipv6_enabled(tox_options, false); |
78 | 194 | run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options); |
79 | | |
80 | 194 | tox_options_free(tox_options); |
81 | | |
82 | 194 | return 0; |
83 | 194 | } |