Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/group_tcp_test.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Does a basic functionality test for TCP connections.
3
 */
4
5
#include <stdbool.h>
6
#include <stdint.h>
7
#include <string.h>
8
9
#include "auto_test_support.h"
10
11
0
#define NUM_GROUP_TOXES 2
12
0
#define CODEWORD "RONALD MCDONALD"
13
0
#define CODEWORD_LEN (sizeof(CODEWORD) - 1)
14
15
typedef struct State {
16
    size_t   num_peers;
17
    bool     got_code;
18
    bool     got_second_code;
19
    uint32_t peer_id[NUM_GROUP_TOXES - 1];
20
} State;
21
22
static void group_invite_handler(Tox *tox, const Tox_Event_Group_Invite *event, void *user_data)
23
95
{
24
95
    const uint32_t friend_number = tox_event_group_invite_get_friend_number(event);
25
95
    const uint8_t *invite_data = tox_event_group_invite_get_invite_data(event);
26
95
    const size_t length = tox_event_group_invite_get_invite_data_length(event);
27
28
95
    printf("Accepting friend invite\n");
29
30
95
    Tox_Err_Group_Invite_Accept err_accept;
31
95
    tox_group_invite_accept(tox, friend_number, invite_data, length, (const uint8_t *)"test", 4,
32
95
                            nullptr, 0, &err_accept);
33
95
    ck_assert(err_accept == TOX_ERR_GROUP_INVITE_ACCEPT_OK);
34
95
}
35
36
static void group_peer_join_handler(Tox *tox, const Tox_Event_Group_Peer_Join *event, void *user_data)
37
40
{
38
40
    AutoTox *autotox = (AutoTox *)user_data;
39
40
    ck_assert(autotox != nullptr);
40
41
40
    State *state = (State *)autotox->state;
42
43
40
    const uint32_t peer_id = tox_event_group_peer_join_get_peer_id(event);
44
45
40
    fprintf(stderr, "joined: %zu, %u\n", state->num_peers, peer_id);
46
40
    ck_assert_msg(state->num_peers < NUM_GROUP_TOXES - 1, "%zu", state->num_peers);
47
48
40
    state->peer_id[state->num_peers++] = peer_id;
49
40
}
50
51
static void group_private_message_handler(Tox *tox, const Tox_Event_Group_Private_Message *event, void *user_data)
52
0
{
53
0
    const uint8_t *message = tox_event_group_private_message_get_message(event);
54
0
    const size_t length = tox_event_group_private_message_get_message_length(event);
55
56
0
    AutoTox *autotox = (AutoTox *)user_data;
57
0
    ck_assert(autotox != nullptr);
58
59
0
    State *state = (State *)autotox->state;
60
61
0
    ck_assert(length == CODEWORD_LEN);
62
0
    ck_assert(memcmp(CODEWORD, message, length) == 0);
63
64
0
    printf("Codeword: %s\n", CODEWORD);
65
66
0
    state->got_code = true;
67
0
}
68
69
static void group_message_handler(Tox *tox, const Tox_Event_Group_Message *event, void *user_data)
70
0
{
71
0
    const uint8_t *message = tox_event_group_message_get_message(event);
72
0
    const size_t length = tox_event_group_message_get_message_length(event);
73
74
0
    AutoTox *autotox = (AutoTox *)user_data;
75
0
    ck_assert(autotox != nullptr);
76
77
0
    State *state = (State *)autotox->state;
78
79
0
    ck_assert(length == CODEWORD_LEN);
80
0
    ck_assert(memcmp(CODEWORD, message, length) == 0);
81
82
0
    printf("Codeword: %s\n", CODEWORD);
83
84
0
    state->got_second_code = true;
85
0
}
86
87
/*
88
 * We need different constants to make TCP run smoothly. TODO(Jfreegman): is this because of the group
89
 * implementation or just an autotest quirk?
90
 */
91
0
#define GROUP_ITERATION_INTERVAL 100
92
static void iterate_group(AutoTox *autotoxes, uint32_t num_toxes, size_t interval)
93
0
{
94
0
    for (uint32_t i = 0; i < num_toxes; i++) {
95
0
        if (autotoxes[i].alive) {
96
0
            tox_iterate(autotoxes[i].tox, &autotoxes[i]);
97
0
            autotoxes[i].clock += interval;
98
0
        }
99
0
    }
100
101
0
    c_sleep(50);
102
0
}
103
104
static bool all_peers_connected(AutoTox *autotoxes)
105
0
{
106
0
    iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
107
108
0
    size_t count = 0;
109
110
0
    for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) {
111
0
        const State *state = (const State *)autotoxes[i].state;
112
113
0
        if (state->num_peers == NUM_GROUP_TOXES - 1) {
114
0
            ++count;
115
0
        }
116
0
    }
117
118
0
    return count == NUM_GROUP_TOXES;
119
0
}
120
121
static bool all_peers_got_code(AutoTox *autotoxes)
122
0
{
123
0
    iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
124
125
0
    size_t count = 0;
126
127
0
    for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) {
128
0
        const State *state = (const State *)autotoxes[i].state;
129
130
0
        if (state->got_code) {
131
0
            ++count;
132
0
        }
133
0
    }
134
135
0
    return count == NUM_GROUP_TOXES - 1;
136
0
}
137
138
static void group_tcp_test(AutoTox *autotoxes)
139
0
{
140
0
    ck_assert(NUM_GROUP_TOXES >= 2);
141
142
0
    State *state0 = (State *)autotoxes[0].state;
143
0
    State *state1 = (State *)autotoxes[1].state;
144
145
0
    for (size_t i = 0; i < NUM_GROUP_TOXES; ++i) {
146
0
        tox_events_callback_group_peer_join(autotoxes[i].dispatch, group_peer_join_handler);
147
0
        tox_events_callback_group_private_message(autotoxes[i].dispatch, group_private_message_handler);
148
0
    }
149
150
0
    tox_events_callback_group_message(autotoxes[1].dispatch, group_message_handler);
151
0
    tox_events_callback_group_invite(autotoxes[1].dispatch, group_invite_handler);
152
153
0
    Tox_Err_Group_New new_err;
154
0
    uint32_t groupnumber = tox_group_new(autotoxes[0].tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)"test", 4,
155
0
                                         (const uint8_t *)"test", 4, &new_err);
156
0
    ck_assert_msg(new_err == TOX_ERR_GROUP_NEW_OK, "tox_group_new failed: %d", new_err);
157
158
0
    iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
159
160
0
    Tox_Err_Group_State_Queries id_err;
161
0
    uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE];
162
163
0
    tox_group_get_chat_id(autotoxes[0].tox, groupnumber, chat_id, &id_err);
164
0
    ck_assert_msg(id_err == TOX_ERR_GROUP_STATE_QUERIES_OK, "%d", id_err);
165
166
0
    printf("Tox 0 created new group...\n");
167
168
0
    for (size_t i = 1; i < NUM_GROUP_TOXES; ++i) {
169
0
        Tox_Err_Group_Join jerr;
170
0
        tox_group_join(autotoxes[i].tox, chat_id, (const uint8_t *)"test", 4, nullptr, 0, &jerr);
171
0
        ck_assert_msg(jerr == TOX_ERR_GROUP_JOIN_OK, "%d", jerr);
172
0
        iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL * 10);
173
0
    }
174
175
0
    while (!all_peers_connected(autotoxes))
176
0
        ;
177
178
0
    printf("%d peers successfully joined. Waiting for code...\n", NUM_GROUP_TOXES);
179
0
    printf("Tox 0 sending secret code to all peers\n");
180
181
182
0
    for (size_t i = 0; i < NUM_GROUP_TOXES - 1; ++i) {
183
184
0
        Tox_Err_Group_Send_Private_Message perr;
185
0
        tox_group_send_private_message(autotoxes[0].tox, groupnumber, state0->peer_id[i],
186
0
                                       TOX_MESSAGE_TYPE_NORMAL,
187
0
                                       (const uint8_t *)CODEWORD, CODEWORD_LEN, &perr);
188
0
        ck_assert_msg(perr == TOX_ERR_GROUP_SEND_PRIVATE_MESSAGE_OK, "%d", perr);
189
0
    }
190
191
0
    while (!all_peers_got_code(autotoxes))
192
0
        ;
193
194
0
    Tox_Err_Group_Leave err_exit;
195
0
    tox_group_leave(autotoxes[1].tox, groupnumber, nullptr, 0, &err_exit);
196
0
    ck_assert(err_exit == TOX_ERR_GROUP_LEAVE_OK);
197
198
0
    iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
199
200
0
    state0->num_peers = 0;
201
0
    state1->num_peers = 0;
202
203
    // now do a friend invite to make sure the TCP-specific logic for friend invites is okay
204
205
0
    printf("Tox1 leaves group and Tox0 does a friend group invite for tox1\n");
206
207
0
    Tox_Err_Group_Invite_Friend err_invite;
208
0
    tox_group_invite_friend(autotoxes[0].tox, groupnumber, 0, &err_invite);
209
0
    ck_assert(err_invite == TOX_ERR_GROUP_INVITE_FRIEND_OK);
210
211
0
    while (state0->num_peers == 0 && state1->num_peers == 0) {
212
0
        iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
213
0
    }
214
215
0
    printf("Tox 1 successfully joined. Waiting for code...\n");
216
217
0
    Tox_Err_Group_Send_Message merr;
218
0
    tox_group_send_message(autotoxes[0].tox, groupnumber, TOX_MESSAGE_TYPE_NORMAL,
219
0
                           (const uint8_t *)CODEWORD, CODEWORD_LEN, &merr);
220
0
    ck_assert(merr == TOX_ERR_GROUP_SEND_MESSAGE_OK);
221
222
0
    while (!state1->got_second_code) {
223
0
        iterate_group(autotoxes, NUM_GROUP_TOXES, GROUP_ITERATION_INTERVAL);
224
0
    }
225
226
0
    for (size_t i = 0; i < NUM_GROUP_TOXES; i++) {
227
0
        tox_group_leave(autotoxes[i].tox, groupnumber, nullptr, 0, &err_exit);
228
0
        ck_assert(err_exit == TOX_ERR_GROUP_LEAVE_OK);
229
0
    }
230
231
0
    printf("Test passed!\n");
232
0
}
233
234
int main(int argc, char **argv)
235
3
{
236
3
    setvbuf(stdout, nullptr, _IONBF, 0);
237
238
3
    struct Tox_Options *options = tox_options_new(nullptr);
239
3
    ck_assert(options != nullptr);
240
241
3
    tox_options_set_udp_enabled(options, false);
242
243
3
    Run_Auto_Options autotest_opts = default_run_auto_options();
244
3
    autotest_opts.graph = GRAPH_COMPLETE;
245
246
    // TODO(JFreegman): Fix this test and remove the "if".
247
3
    if (argc > 2) {
248
0
        run_auto_test(options, NUM_GROUP_TOXES, group_tcp_test, sizeof(State), &autotest_opts);
249
0
    }
250
251
3
    tox_options_free(options);
252
3
    return 0;
253
3
}
254
255
#undef CODEWORD_LEN
256
#undef CODEWORD
257
#undef NUM_GROUP_TOXES