Coverage Report

Created: 2024-01-26 01:52

/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
6
{
18
6
    for (uint32_t i = 0; i < 100; ++i) {
19
        // Ignore events on tox 1.
20
6
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
21
        // Check if tox 2 got the message from tox 1.
22
6
        Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);
23
24
6
        if (events != nullptr) {
25
6
            uint32_t events_size = tox_events_get_size(events);
26
6
            ck_assert(events_size == 1);
27
28
4
            const Tox_Event_Friend_Message *msg_event = nullptr;
29
8
            for (uint32_t j = 0; j < events_size; ++j) {
30
4
                const Tox_Event *ev = tox_events_get(events, j);
31
4
                if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) {
32
4
                    msg_event = tox_event_get_friend_message(ev);
33
4
                }
34
4
            }
35
36
4
            ck_assert(msg_event != nullptr);
37
4
            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
6
}
51
52
static uint64_t get_state_clock_callback(void *user_data)
53
463k
{
54
463k
    const uint64_t *clock = (const uint64_t *)user_data;
55
463k
    return *clock;
56
463k
}
57
58
static void test_tox_events(void)
59
7
{
60
7
    uint8_t message[sizeof("hello")];
61
7
    memcpy(message, "hello", sizeof(message));
62
63
7
    Tox *toxes[2];
64
7
    uint32_t index[2];
65
66
21
    for (uint32_t i = 0; i < 2; ++i) {
67
14
        index[i] = i + 1;
68
14
        toxes[i] = tox_new_log(nullptr, nullptr, &index[i]);
69
14
        tox_events_init(toxes[i]);
70
14
        ck_assert_msg(toxes[i] != nullptr, "failed to create tox instances %u", i);
71
14
    }
72
73
7
    uint64_t clock = current_time_monotonic(toxes[0]->mono_time);
74
7
    Mono_Time *mono_time;
75
76
7
    mono_time = toxes[0]->mono_time;
77
7
    mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
78
7
    mono_time = toxes[1]->mono_time;
79
7
    mono_time_set_current_time_callback(mono_time, get_state_clock_callback, &clock);
80
81
7
    uint8_t pk[TOX_PUBLIC_KEY_SIZE];
82
7
    tox_self_get_dht_id(toxes[0], pk);
83
7
    tox_bootstrap(toxes[1], "localhost", tox_self_get_udp_port(toxes[0], nullptr), pk, nullptr);
84
85
7
    tox_self_get_public_key(toxes[0], pk);
86
7
    tox_friend_add_norequest(toxes[1], pk, nullptr);
87
88
7
    tox_self_get_public_key(toxes[1], pk);
89
7
    tox_friend_add_norequest(toxes[0], pk, nullptr);
90
91
7
    printf("bootstrapping and connecting 2 toxes\n");
92
93
574
    while (tox_self_get_connection_status(toxes[0]) == TOX_CONNECTION_NONE ||
94
574
            tox_self_get_connection_status(toxes[1]) == TOX_CONNECTION_NONE) {
95
        // Ignore connection events for now.
96
567
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
97
567
        tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
98
99
567
        clock += 100;
100
567
        c_sleep(5);
101
567
    }
102
103
7
    printf("toxes online, waiting for friend connection\n");
104
105
495
    while (tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_NONE ||
106
495
            tox_friend_get_connection_status(toxes[1], 0, nullptr) == TOX_CONNECTION_NONE) {
107
        // Ignore connection events for now.
108
488
        tox_events_free(tox_events_iterate(toxes[0], false, nullptr));
109
488
        tox_events_free(tox_events_iterate(toxes[1], false, nullptr));
110
111
488
        clock += 100;
112
488
        c_sleep(5);
113
488
    }
114
115
7
    printf("friends are connected via %s, now sending message\n",
116
7
           tox_friend_get_connection_status(toxes[0], 0, nullptr) == TOX_CONNECTION_TCP ? "TCP" : "UDP");
117
118
7
    Tox_Err_Friend_Send_Message err;
119
7
    tox_friend_send_message(toxes[0], 0, TOX_MESSAGE_TYPE_NORMAL, message, sizeof(message), &err);
120
7
    ck_assert(err == TOX_ERR_FRIEND_SEND_MESSAGE_OK);
121
122
6
    ck_assert(await_message(toxes));
123
124
14
    for (uint32_t i = 0; i < 2; ++i) {
125
8
        tox_kill(toxes[i]);
126
8
    }
127
6
}
128
129
int main(void)
130
721
{
131
721
    setvbuf(stdout, nullptr, _IONBF, 0);
132
721
    test_tox_events();
133
721
    return 0;
134
721
}