Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/auto_test_support.c
Line
Count
Source (jump to first uncovered line)
1
#include <assert.h>  // assert
2
#include <stdlib.h>  // calloc, free
3
4
#include "check_compat.h"
5
#include "../testing/misc_tools.h"
6
#include "../toxcore/Messenger.h"
7
#include "../toxcore/mono_time.h"
8
#include "../toxcore/tox_dispatch.h"
9
#include "../toxcore/tox_events.h"
10
#include "../toxcore/tox_struct.h"
11
#include "../toxcore/net_crypto.h"
12
#include "../toxcore/DHT.h"
13
14
#include "auto_test_support.h"
15
16
#ifndef ABORT_ON_LOG_ERROR
17
#define ABORT_ON_LOG_ERROR true
18
#endif
19
20
static const uint8_t *auto_test_nc_dht_get_shared_key_sent_wrapper(void *dht, const uint8_t *public_key)
21
0
{
22
0
    return dht_get_shared_key_sent((DHT *)dht, public_key);
23
0
}
24
25
static const uint8_t *auto_test_nc_dht_get_self_public_key_wrapper(const void *dht)
26
0
{
27
0
    return dht_get_self_public_key((const DHT *)dht);
28
0
}
29
30
static const uint8_t *auto_test_nc_dht_get_self_secret_key_wrapper(const void *dht)
31
70
{
32
70
    return dht_get_self_secret_key((const DHT *)dht);
33
70
}
34
35
const Net_Crypto_DHT_Funcs auto_test_dht_funcs = {
36
    auto_test_nc_dht_get_shared_key_sent_wrapper,
37
    auto_test_nc_dht_get_self_public_key_wrapper,
38
    auto_test_nc_dht_get_self_secret_key_wrapper,
39
};
40
41
#ifndef USE_IPV6
42
#define USE_IPV6 1
43
#endif
44
45
// List of live bootstrap nodes. These nodes should have TCP server enabled.
46
static const struct BootstrapNodes {
47
    const char   *ip;
48
    uint16_t      port;
49
    const uint8_t key[32];
50
} bootstrap_nodes[] = {
51
    {
52
        "144.217.167.73", 33445,
53
        0x7E, 0x56, 0x68, 0xE0, 0xEE, 0x09, 0xE1, 0x9F,
54
        0x32, 0x0A, 0xD4, 0x79, 0x02, 0x41, 0x93, 0x31,
55
        0xFF, 0xEE, 0x14, 0x7B, 0xB3, 0x60, 0x67, 0x69,
56
        0xCF, 0xBE, 0x92, 0x1A, 0x2A, 0x2F, 0xD3, 0x4C,
57
    },
58
    {
59
        "205.185.115.131", 33445,
60
        0x30, 0x91, 0xC6, 0xBE, 0xB2, 0xA9, 0x93, 0xF1,
61
        0xC6, 0x30, 0x0C, 0x16, 0x54, 0x9F, 0xAB, 0xA6,
62
        0x70, 0x98, 0xFF, 0x3D, 0x62, 0xC6, 0xD2, 0x53,
63
        0x82, 0x8B, 0x53, 0x14, 0x70, 0xB5, 0x3D, 0x68,
64
    },
65
    {
66
        "tox1.mf-net.eu", 33445,
67
        0xB3, 0xE5, 0xFA, 0x80, 0xDC, 0x8E, 0xBD, 0x11,
68
        0x49, 0xAD, 0x2A, 0xB3, 0x5E, 0xD8, 0xB8, 0x5B,
69
        0xD5, 0x46, 0xDE, 0xDE, 0x26, 0x1C, 0xA5, 0x93,
70
        0x23, 0x4C, 0x61, 0x92, 0x49, 0x41, 0x95, 0x06,
71
    },
72
    {
73
        "3.0.24.15", 33445,
74
        0xE2, 0x0A, 0xBC, 0xF3, 0x8C, 0xDB, 0xFF, 0xD7,
75
        0xD0, 0x4B, 0x29, 0xC9, 0x56, 0xB3, 0x3F, 0x7B,
76
        0x27, 0xA3, 0xBB, 0x7A, 0xF0, 0x61, 0x81, 0x01,
77
        0x61, 0x7B, 0x03, 0x6E, 0x4A, 0xEA, 0x40, 0x2D,
78
    },
79
    {
80
        "139.162.110.188", 33445,
81
        0xF7, 0x6A, 0x11, 0x28, 0x45, 0x47, 0x16, 0x38,
82
        0x89, 0xDD, 0xC8, 0x9A, 0x77, 0x38, 0xCF, 0x27,
83
        0x17, 0x97, 0xBF, 0x5E, 0x5E, 0x22, 0x06, 0x43,
84
        0xE9, 0x7A, 0xD3, 0xC7, 0xE7, 0x90, 0x3D, 0x55,
85
    },
86
    {
87
        "172.105.109.31", 33445,
88
        0xD4, 0x6E, 0x97, 0xCF, 0x99, 0x5D, 0xC1, 0x82,
89
        0x0B, 0x92, 0xB7, 0xD8, 0x99, 0xE1, 0x52, 0xA2,
90
        0x17, 0xD3, 0x6A, 0xBE, 0x22, 0x73, 0x0F, 0xEA,
91
        0x4B, 0x6B, 0xF1, 0xBF, 0xC0, 0x6C, 0x61, 0x7C,
92
    },
93
    { nullptr, 0, 0 },
94
};
95
96
void bootstrap_tox_live_network(Tox *tox, bool enable_tcp)
97
2
{
98
2
    ck_assert(tox != nullptr);
99
100
14
    for (size_t j = 0; bootstrap_nodes[j].ip != nullptr; ++j) {
101
12
        const char *ip = bootstrap_nodes[j].ip;
102
12
        uint16_t port = bootstrap_nodes[j].port;
103
12
        const uint8_t *key = bootstrap_nodes[j].key;
104
105
12
        Tox_Err_Bootstrap err;
106
12
        tox_bootstrap(tox, ip, port, key, &err);
107
108
12
        if (err != TOX_ERR_BOOTSTRAP_OK) {
109
0
            fprintf(stderr, "Failed to bootstrap node %zu (%s): error %u\n", j, ip, err);
110
0
        }
111
112
12
        if (enable_tcp) {
113
12
            tox_add_tcp_relay(tox, ip, port, key, &err);
114
115
12
            if (err != TOX_ERR_BOOTSTRAP_OK) {
116
0
                fprintf(stderr, "Failed to add TCP relay %zu (%s): error %u\n", j, ip, err);
117
0
            }
118
12
        }
119
12
    }
120
2
}
121
122
typedef void autotox_test_cb(AutoTox *autotoxes);
123
124
static const char *tox_log_level_name(Tox_Log_Level level)
125
1.60k
{
126
1.60k
    switch (level) {
127
0
        case TOX_LOG_LEVEL_TRACE:
128
0
            return "TRACE";
129
130
1.57k
        case TOX_LOG_LEVEL_DEBUG:
131
1.57k
            return "DEBUG";
132
133
1
        case TOX_LOG_LEVEL_INFO:
134
1
            return "INFO";
135
136
2
        case TOX_LOG_LEVEL_WARNING:
137
2
            return "WARNING";
138
139
23
        case TOX_LOG_LEVEL_ERROR:
140
23
            return "ERROR";
141
1.60k
    }
142
143
0
    return "<unknown>";
144
1.60k
}
145
146
void print_debug_log(Tox *m, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
147
                     const char *message, void *user_data)
148
119k
{
149
119k
    if (level == TOX_LOG_LEVEL_TRACE) {
150
118k
        return;
151
118k
    }
152
153
1.60k
    const uint32_t index = user_data ? *(uint32_t *)user_data : 0;
154
1.60k
    fprintf(stderr, "[#%u] %s %s:%u\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
155
156
1.60k
    if (level == TOX_LOG_LEVEL_ERROR && ABORT_ON_LOG_ERROR) {
157
0
        fputs("Aborting test program\n", stderr);
158
0
        abort();
159
0
    }
160
1.60k
}
161
162
void print_debug_logger(void *context, Logger_Level level, const char *file, uint32_t line, const char *func, const char *message, void *userdata)
163
115k
{
164
115k
    print_debug_log(nullptr, (Tox_Log_Level) level, file, line, func, message, userdata);
165
115k
}
166
167
Tox *tox_new_log_lan(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data, bool lan_discovery)
168
17
{
169
17
    struct Tox_Options *log_options = options;
170
171
17
    if (log_options == nullptr) {
172
4
        log_options = tox_options_new(nullptr);
173
4
    }
174
175
17
    assert(log_options != nullptr);
176
177
17
    tox_options_set_ipv6_enabled(log_options, USE_IPV6);
178
17
    tox_options_set_local_discovery_enabled(log_options, lan_discovery);
179
    // Use a higher start port for non-LAN-discovery tests so it's more likely for the LAN discovery
180
    // test to get the default port 33445.
181
17
    const uint16_t start_port = lan_discovery ? 33445 : 33545;
182
17
    tox_options_set_start_port(log_options, start_port);
183
17
    tox_options_set_end_port(log_options, start_port + 2000);
184
17
    tox_options_set_log_callback(log_options, &print_debug_log);
185
17
    tox_options_set_log_user_data(log_options, log_user_data);
186
17
    Tox *tox = tox_new(log_options, err);
187
188
17
    if (options == nullptr) {
189
4
        tox_options_free(log_options);
190
4
    }
191
192
17
    return tox;
193
17
}
194
195
Tox *tox_new_log(struct Tox_Options *options, Tox_Err_New *err, void *log_user_data)
196
17
{
197
17
    return tox_new_log_lan(options, err, log_user_data, false);
198
17
}