Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/proxy_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
#include "auto_test_support.h"
9
10
static void *proxy_routine(void *arg)
11
0
{
12
0
    const char *proxy_bin = (const char *)arg;
13
0
    ck_assert(proxy_bin != nullptr);
14
0
    printf("starting http/sock5 proxy: %s\n", proxy_bin);
15
0
    ck_assert(system(proxy_bin) == 0);
16
0
    return nullptr;
17
0
}
18
19
static bool try_bootstrap(Tox *tox1, Tox *tox2, Tox *tox3, Tox *tox4)
20
1
{
21
161
    for (uint32_t i = 0; i < 400; ++i) {
22
161
        if (tox_self_get_connection_status(tox1) != TOX_CONNECTION_NONE &&
23
161
                tox_self_get_connection_status(tox2) != TOX_CONNECTION_NONE &&
24
161
                tox_self_get_connection_status(tox3) != TOX_CONNECTION_NONE &&
25
161
                tox_self_get_connection_status(tox4) != TOX_CONNECTION_NONE) {
26
1
            printf("%d %d %d %d\n",
27
1
                    tox_self_get_connection_status(tox1),
28
1
                    tox_self_get_connection_status(tox2),
29
1
                    tox_self_get_connection_status(tox3),
30
1
                    tox_self_get_connection_status(tox4));
31
1
            return true;
32
1
        }
33
34
160
        tox_iterate(tox1, nullptr);
35
160
        tox_iterate(tox2, nullptr);
36
160
        tox_iterate(tox3, nullptr);
37
160
        tox_iterate(tox4, nullptr);
38
39
160
        if (i % 10 == 0) {
40
16
            printf("%d %d %d %d\n",
41
16
                    tox_self_get_connection_status(tox1),
42
16
                    tox_self_get_connection_status(tox2),
43
16
                    tox_self_get_connection_status(tox3),
44
16
                    tox_self_get_connection_status(tox4));
45
16
        }
46
47
160
        c_sleep(tox_iteration_interval(tox1));
48
160
    }
49
50
0
    return false;
51
1
}
52
53
int main(int argc, char **argv)
54
1
{
55
1
    setvbuf(stdout, nullptr, _IONBF, 0);
56
1
    if (argc >= 3) {
57
0
        char *proxy_bin = argv[2];
58
0
        pthread_t proxy_thread;
59
0
        pthread_create(&proxy_thread, nullptr, proxy_routine, proxy_bin);
60
0
        c_sleep(100);
61
0
    }
62
63
1
    const uint16_t tcp_port = 8082;
64
1
    uint32_t index[] = { 1, 2, 3, 4 };
65
66
1
    struct Tox_Options *tox_options = tox_options_new(nullptr);
67
1
    ck_assert(tox_options != nullptr);
68
69
    // tox1 is a TCP server and has UDP enabled.
70
1
    tox_options_set_udp_enabled(tox_options, true);
71
1
    tox_options_set_tcp_port(tox_options, tcp_port);
72
73
1
    Tox *tox1 = tox_new_log(tox_options, nullptr, &index[0]);
74
1
    ck_assert(tox1 != nullptr);
75
76
    // Get tox1's DHT key and port.
77
1
    uint8_t dht_pk[TOX_PUBLIC_KEY_SIZE];
78
1
    tox_self_get_dht_id(tox1, dht_pk);
79
1
    uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
80
1
    ck_assert(dht_port != 0);
81
82
    // tox2 is a regular DHT node bootstrapping against tox1.
83
1
    tox_options_set_udp_enabled(tox_options, true);
84
1
    tox_options_set_tcp_port(tox_options, 0);
85
86
1
    Tox *tox2 = tox_new_log(tox_options, nullptr, &index[1]);
87
1
    ck_assert(tox2 != nullptr);
88
89
    // tox2 bootstraps against tox1 with UDP.
90
1
    ck_assert(tox_bootstrap(tox2, "127.0.0.1", dht_port, dht_pk, nullptr));
91
92
    // tox3 has UDP disabled and connects to tox1 via an HTTP proxy
93
1
    tox_options_set_udp_enabled(tox_options, false);
94
1
    tox_options_set_proxy_host(tox_options, "127.0.0.1");
95
1
    tox_options_set_proxy_port(tox_options, 8080);
96
1
    tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_HTTP);
97
98
1
    Tox *tox3 = tox_new_log(tox_options, nullptr, &index[2]);
99
1
    ck_assert(tox3 != nullptr);
100
101
    // tox4 has UDP disabled and connects to tox1 via a SOCKS5 proxy
102
1
    tox_options_set_udp_enabled(tox_options, false);
103
1
    tox_options_set_proxy_host(tox_options, "127.0.0.1");
104
1
    tox_options_set_proxy_port(tox_options, 8081);
105
1
    tox_options_set_proxy_type(tox_options, TOX_PROXY_TYPE_SOCKS5);
106
107
1
    Tox *tox4 = tox_new_log(tox_options, nullptr, &index[3]);
108
1
    ck_assert(tox4 != nullptr);
109
110
    // tox3 and tox4 bootstrap against tox1 and add it as a TCP relay
111
1
    ck_assert(tox_bootstrap(tox3, "127.0.0.1", dht_port, dht_pk, nullptr));
112
1
    ck_assert(tox_add_tcp_relay(tox3, "127.0.0.1", tcp_port, dht_pk, nullptr));
113
114
1
    ck_assert(tox_bootstrap(tox4, "127.0.0.1", dht_port, dht_pk, nullptr));
115
1
    ck_assert(tox_add_tcp_relay(tox4, "127.0.0.1", tcp_port, dht_pk, nullptr));
116
117
1
    int ret = 1;
118
1
    if (try_bootstrap(tox1, tox2, tox3, tox4)) {
119
1
        ret = 0;
120
1
    }
121
122
1
    tox_options_free(tox_options);
123
1
    tox_kill(tox4);
124
1
    tox_kill(tox3);
125
1
    tox_kill(tox2);
126
1
    tox_kill(tox1);
127
128
1
    return ret;
129
1
}