Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/file_streaming_test.c
Line
Count
Source
1
/* File transfer test: streaming version (no known size).
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/ccompat.h"
11
#include "../toxcore/tox.h"
12
#include "../toxcore/util.h"
13
#include "auto_test_support.h"
14
#include "check_compat.h"
15
16
#ifndef USE_IPV6
17
#define USE_IPV6 1
18
#endif
19
20
#ifdef TOX_LOCALHOST
21
#undef TOX_LOCALHOST
22
#endif
23
#if USE_IPV6
24
#define TOX_LOCALHOST "::1"
25
#else
26
2
#define TOX_LOCALHOST "127.0.0.1"
27
#endif
28
29
static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
30
53
{
31
53
    if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
32
53
        tox_friend_add_norequest(m, public_key, nullptr);
33
53
    }
34
53
}
35
36
static uint64_t size_recv;
37
static uint64_t sending_pos;
38
39
static uint8_t file_cmp_id[TOX_FILE_ID_LENGTH];
40
static uint32_t file_accepted;
41
static uint64_t file_size;
42
static void tox_file_receive(Tox *tox, uint32_t friend_number, uint32_t file_number, uint32_t kind, uint64_t filesize,
43
                             const uint8_t *filename, size_t filename_length, void *userdata)
44
3
{
45
3
    ck_assert_msg(kind == TOX_FILE_KIND_DATA, "bad kind");
46
47
3
    ck_assert_msg(filename_length == sizeof("Gentoo.exe")
48
3
                  && memcmp(filename, "Gentoo.exe", sizeof("Gentoo.exe")) == 0, "bad filename");
49
50
3
    uint8_t file_id[TOX_FILE_ID_LENGTH];
51
52
3
    ck_assert_msg(tox_file_get_file_id(tox, friend_number, file_number, file_id, nullptr), "tox_file_get_file_id error");
53
54
3
    ck_assert_msg(memcmp(file_id, file_cmp_id, TOX_FILE_ID_LENGTH) == 0, "bad file_id");
55
56
3
    const uint8_t empty[TOX_FILE_ID_LENGTH] = {0};
57
58
3
    ck_assert_msg(memcmp(empty, file_cmp_id, TOX_FILE_ID_LENGTH) != 0, "empty file_id");
59
60
3
    file_size = filesize;
61
62
3
    if (filesize) {
63
2
        sending_pos = size_recv = 1337;
64
65
2
        Tox_Err_File_Seek err_s;
66
67
2
        ck_assert_msg(tox_file_seek(tox, friend_number, file_number, 1337, &err_s), "tox_file_seek error");
68
69
2
        ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_OK, "tox_file_seek wrong error");
70
71
2
    } else {
72
1
        sending_pos = size_recv = 0;
73
1
    }
74
75
3
    Tox_Err_File_Control error;
76
77
3
    ck_assert_msg(tox_file_control(tox, friend_number, file_number, TOX_FILE_CONTROL_RESUME, &error),
78
3
                  "tox_file_control failed. %i", error);
79
3
    ++file_accepted;
80
81
3
    Tox_Err_File_Seek err_s;
82
83
3
    ck_assert_msg(!tox_file_seek(tox, friend_number, file_number, 1234, &err_s), "tox_file_seek no error");
84
85
3
    ck_assert_msg(err_s == TOX_ERR_FILE_SEEK_DENIED, "tox_file_seek wrong error");
86
3
}
87
88
static uint32_t sendf_ok;
89
static void file_print_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control,
90
                               void *userdata)
91
3
{
92
    /* First send file num is 0.*/
93
3
    if (file_number == 0 && control == TOX_FILE_CONTROL_RESUME) {
94
3
        sendf_ok = 1;
95
3
    }
96
3
}
97
98
static uint64_t max_sending;
99
static bool m_send_reached;
100
static uint8_t sending_num;
101
static bool file_sending_done;
102
static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position,
103
                                   size_t length, void *user_data)
104
76.5k
{
105
76.5k
    ck_assert_msg(sendf_ok, "didn't get resume control");
106
107
76.5k
    ck_assert_msg(sending_pos == position, "bad position %lu", (unsigned long)position);
108
109
76.5k
    if (length == 0) {
110
3
        ck_assert_msg(!file_sending_done, "file sending already done");
111
112
3
        file_sending_done = 1;
113
3
        return;
114
3
    }
115
116
76.5k
    if (position + length > max_sending) {
117
1
        ck_assert_msg(!m_send_reached, "requested done file transfer");
118
119
1
        length = max_sending - position;
120
1
        m_send_reached = 1;
121
1
    }
122
123
76.5k
    VLA(uint8_t, f_data, length);
124
76.5k
    memset(f_data, sending_num, length);
125
126
76.5k
    Tox_Err_File_Send_Chunk error;
127
76.5k
    tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error);
128
129
130
76.5k
    ck_assert_msg(error == TOX_ERR_FILE_SEND_CHUNK_OK,
131
76.5k
                  "could not send chunk, error num=%d pos=%d len=%d", (int)error, (int)position, (int)length);
132
133
76.5k
    ++sending_num;
134
76.5k
    sending_pos += length;
135
76.5k
}
136
137
138
static uint8_t num;
139
static bool file_recv;
140
static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uint64_t position, const uint8_t *data,
141
                       size_t length, void *user_data)
142
76.5k
{
143
76.5k
    ck_assert_msg(size_recv == position, "bad position");
144
145
76.5k
    if (length == 0) {
146
3
        file_recv = 1;
147
3
        return;
148
3
    }
149
150
76.5k
    VLA(uint8_t, f_data, length);
151
76.5k
    memset(f_data, num, length);
152
76.5k
    ++num;
153
154
76.5k
    ck_assert_msg(memcmp(f_data, data, length) == 0, "FILE_CORRUPTED");
155
156
76.5k
    size_recv += length;
157
76.5k
}
158
159
static void file_transfer_test(void)
160
1
{
161
1
    printf("Starting test: few_clients\n");
162
1
    uint32_t index[] = { 1, 2, 3 };
163
1
    long long unsigned int cur_time = time(nullptr);
164
1
    Tox_Err_New t_n_error;
165
1
    Tox *tox1 = tox_new_log(nullptr, &t_n_error, &index[0]);
166
1
    ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
167
1
    Tox *tox2 = tox_new_log(nullptr, &t_n_error, &index[1]);
168
1
    ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
169
1
    Tox *tox3 = tox_new_log(nullptr, &t_n_error, &index[2]);
170
1
    ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
171
172
1
    ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances");
173
174
1
    tox_callback_friend_request(tox2, accept_friend_request);
175
1
    uint8_t address[TOX_ADDRESS_SIZE];
176
1
    tox_self_get_address(tox2, address);
177
1
    uint32_t test = tox_friend_add(tox3, address, (const uint8_t *)"Gentoo", 7, nullptr);
178
1
    ck_assert_msg(test == 0, "Failed to add friend error code: %u", test);
179
180
1
    uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
181
1
    tox_self_get_dht_id(tox1, dht_key);
182
1
    uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
183
184
1
    tox_bootstrap(tox2, TOX_LOCALHOST, dht_port, dht_key, nullptr);
185
1
    tox_bootstrap(tox3, TOX_LOCALHOST, dht_port, dht_key, nullptr);
186
187
1
    printf("Waiting for toxes to come online\n");
188
189
55
    do {
190
55
        tox_iterate(tox1, nullptr);
191
55
        tox_iterate(tox2, nullptr);
192
55
        tox_iterate(tox3, nullptr);
193
194
55
        printf("Connections: self (%d, %d, %d), friends (%d, %d)\n",
195
55
               tox_self_get_connection_status(tox1),
196
55
               tox_self_get_connection_status(tox2),
197
55
               tox_self_get_connection_status(tox3),
198
55
               tox_friend_get_connection_status(tox2, 0, nullptr),
199
55
               tox_friend_get_connection_status(tox3, 0, nullptr));
200
55
        c_sleep(ITERATION_INTERVAL);
201
55
    } while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
202
55
             tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE ||
203
55
             tox_self_get_connection_status(tox3) == TOX_CONNECTION_NONE ||
204
55
             tox_friend_get_connection_status(tox2, 0, nullptr) == TOX_CONNECTION_NONE ||
205
55
             tox_friend_get_connection_status(tox3, 0, nullptr) == TOX_CONNECTION_NONE);
206
207
1
    printf("Starting file transfer test: 100MiB file.\n");
208
209
1
    file_accepted = file_size = sendf_ok = size_recv = 0;
210
1
    file_recv = 0;
211
1
    max_sending = UINT64_MAX;
212
213
1
    printf("Starting file streaming transfer test.\n");
214
215
1
    file_sending_done = 0;
216
1
    file_accepted = 0;
217
1
    file_size = 0;
218
1
    sendf_ok = 0;
219
1
    size_recv = 0;
220
1
    file_recv = 0;
221
1
    tox_callback_file_recv_chunk(tox3, write_file);
222
1
    tox_callback_file_recv_control(tox2, file_print_control);
223
1
    tox_callback_file_chunk_request(tox2, tox_file_chunk_request);
224
1
    tox_callback_file_recv_control(tox3, file_print_control);
225
1
    tox_callback_file_recv(tox3, tox_file_receive);
226
1
    const uint64_t totalf_size = UINT64_MAX;
227
1
    Tox_File_Number fnum = tox_file_send(
228
1
        tox2, 0, TOX_FILE_KIND_DATA, totalf_size, nullptr,
229
1
        (const uint8_t *)"Gentoo.exe", sizeof("Gentoo.exe"), nullptr);
230
1
    ck_assert_msg(fnum != UINT32_MAX, "tox_new_file_sender fail");
231
232
1
    Tox_Err_File_Get gfierr;
233
1
    ck_assert_msg(!tox_file_get_file_id(tox2, 1, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail");
234
1
    ck_assert_msg(gfierr == TOX_ERR_FILE_GET_FRIEND_NOT_FOUND, "wrong error");
235
1
    ck_assert_msg(!tox_file_get_file_id(tox2, 0, fnum + 1, file_cmp_id, &gfierr), "tox_file_get_file_id didn't fail");
236
1
    ck_assert_msg(gfierr == TOX_ERR_FILE_GET_NOT_FOUND, "wrong error");
237
1
    ck_assert_msg(tox_file_get_file_id(tox2, 0, fnum, file_cmp_id, &gfierr), "tox_file_get_file_id failed");
238
1
    ck_assert_msg(gfierr == TOX_ERR_FILE_GET_OK, "wrong error");
239
240
1
    max_sending = 100 * 1024;
241
1
    m_send_reached = 0;
242
243
337
    do {
244
337
        tox_iterate(tox1, nullptr);
245
337
        tox_iterate(tox2, nullptr);
246
337
        tox_iterate(tox3, nullptr);
247
248
337
        uint32_t tox1_interval = tox_iteration_interval(tox1);
249
337
        uint32_t tox2_interval = tox_iteration_interval(tox2);
250
337
        uint32_t tox3_interval = tox_iteration_interval(tox3);
251
252
337
        c_sleep(min_u32(tox1_interval, min_u32(tox2_interval, tox3_interval)));
253
337
    } while (!file_sending_done);
254
255
1
    ck_assert_msg(sendf_ok && file_recv && m_send_reached && totalf_size == file_size && size_recv == max_sending
256
1
                  && sending_pos == size_recv && file_accepted == 1,
257
1
                  "something went wrong in file transfer %u %u %u %u %u %u %u %lu %lu %lu %lu", sendf_ok, file_recv,
258
1
                  m_send_reached, totalf_size == file_size, size_recv == max_sending, sending_pos == size_recv, file_accepted == 1,
259
1
                  (unsigned long)totalf_size, (unsigned long)file_size,
260
1
                  (unsigned long)size_recv, (unsigned long)sending_pos);
261
262
1
    printf("file_transfer_test succeeded, took %llu seconds\n", time(nullptr) - cur_time);
263
264
1
    tox_kill(tox1);
265
1
    tox_kill(tox2);
266
1
    tox_kill(tox3);
267
1
}
268
269
int main(void)
270
721
{
271
721
    setvbuf(stdout, nullptr, _IONBF, 0);
272
721
    file_transfer_test();
273
721
    return 0;
274
721
}