Coverage Report

Created: 2025-10-08 19:34

/work/auto_tests/tox_events_test.c
Line
Count
Source (jump to first uncovered line)
1
/* Auto Tests: Many clients.
2
 */
3
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <time.h>
8
9
#include "../testing/misc_tools.h"
10
#include "../toxcore/tox.h"
11
#include "../toxcore/tox_events.h"
12
#include "../toxcore/tox_struct.h"
13
#include "auto_test_support.h"
14
#include "check_compat.h"
15
16
static bool await_message(Tox **toxes)
17
7
{
18
7
    for (uint32_t i = 0; i < 100; ++i) {
19
        // Ignore events on tox 1.
20
7
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
21
        // Check if tox 2 got the message from tox 1.
22
7
        Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);
23
24
7
        if (events != nullptr) {
25
7
            uint32_t events_size = tox_events_get_size(events);
26
7
            ck_assert(events_size == 1);
27
28
5
            const Tox_Event_Friend_Message *msg_event = nullptr;
29
10
            for (uint32_t j = 0; j < events_size; ++j) {
30
5
                const Tox_Event *ev = tox_events_get(events, j);
31
5
                if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) {
32
5
                    msg_event = tox_event_get_friend_message(ev);
33
5
                }
34
5
            }
35
36
5
            ck_assert(msg_event != nullptr);
37
5
            ck_assert(tox_event_friend_message_get_message_length(msg_event) == sizeof("hello"));
38
4
            const uint8_t *msg = tox_event_friend_message_get_message(msg_event);
39
4
            ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0,
40
4
                          "message was not expected 'hello' but '%s'", (const char *)msg);
41
42
4
            tox_events_free(events);
43
4
            return true;
44
4
        }
45
46
0
        c_sleep(tox_iteration_interval(toxes[0]));
47
0
    }
48
49
0
    return false;
50
7
}
51
52
static uint64_t get_state_clock_callback(void *user_data)
53
442k
{
54
442k
    const uint64_t *clock = (const uint64_t *)user_data;
55
442k
    return *clock;
56
442k
}
57
58
static void test_tox_events(void)
59
8
{
60
8
    uint8_t message[sizeof("hello")];
61
8
    memcpy(message, "hello", sizeof(message));
62
63
8
    Tox *toxes[2];
64
8
    uint32_t index[2];
65
66
24
    for (uint32_t i = 0; i < 2; ++i) {
67
16
        index[i] = i + 1;
68
16
        toxes[i] = tox_new_log(nullptr, nullptr, &index[i]);
69
16
        tox_events_init(toxes[i]);
70
16
        ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
71
16
    }
72
73
8
    uint64_t clock = current_time_monotonic(toxes[0]->mono_time);
74
8
    Mono_Time *mono_time;
75
76
8
    mono_time = toxes[0]->mono_time;
77
8
    mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
78
8
    mono_time = toxes[1]->mono_time;
79
8
    mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
80
81
8
    uint8_t pk[TOX_PUBLIC_KEY_SIZE];
82
8
    tox_self_get_dht_id(toxes[0], pk);
83
8
    tox_bootstrap(toxes[1], "localhost", tox_self_get_udp_port(toxes[0], nullptr), pk, nullptr);
84
85
8
    tox_self_get_public_key(toxes[0], pk);
86
8
    tox_friend_add_norequest(toxes[1], pk, nullptr);
87
88
8
    tox_self_get_public_key(toxes[1], pk);
89
8
    tox_friend_add_norequest(toxes[0], pk, nullptr);
90
91
8
    printf("bootstrapping and connecting 2 toxes\n");
92
93
656
    while (tox_self_get_connection_status(toxes[0]) == TOX_CONNECTION_NONE ||
94
656
            tox_self_get_connection_status(toxes[1]) == TOX_CONNECTION_NONE) {
95
        // Ignore connection events for now.
96
648
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
97
648
        tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
98
99
648
        clock += 100;
100
648
        c_sleep(5);
101
648
    }
102
103
8
    printf("toxes online, waiting for friend connection\n");
104
105
404
    while (tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_NONE ||
106
404
            tox_friend_get_connection_status(toxes[1], 0, nullptr) == TOX_CONNECTION_NONE) {
107
        // Ignore connection events for now.
108
396
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
109
396
        tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
110
111
396
        clock += 100;
112
396
        c_sleep(5);
113
396
    }
114
115
8
    printf("friends are connected via %s, now sending message\n",
116
8
           tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_TCP ? "TCP" : "UDP");
117
118
8
    Tox_Err_Friend_Send_Message err;
119
8
    tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof(message), &err);
120
8
    ck_assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK);
121
122
7
    ck_assert(await_message(toxes));
123
124
15
    for (uint32_t i = 0; i < 2; ++i) {
125
8
        tox_kill(toxes[i]);
126
8
    }
127
7
}
128
129
int main(void)
130
545
{
131
545
    setvbuf(stdout, nullptr, _IONBF, 0);
132
545
    test_tox_events();
133
545
    return 0;
134
545
}