Coverage Report

Created: 2025-10-08 19:34

/work/auto_tests/lossy_packet_test.c
Line
Count
Source
1
/* Tests that we can send lossy packets.
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/util.h"
11
#include "check_compat.h"
12
13
typedef struct State {
14
    bool custom_packet_received;
15
} State;
16
17
#include "auto_test_support.h"
18
19
15
#define LOSSY_PACKET_FILLER 200
20
21
static void handle_lossy_packet(const Tox_Event_Friend_Lossy_Packet *event, void *user_data)
22
5
{
23
    //const uint32_t friend_number = tox_event_friend_lossy_packet_get_friend_number(event);
24
5
    const uint8_t *data = tox_event_friend_lossy_packet_get_data(event);
25
5
    const uint32_t data_length = tox_event_friend_lossy_packet_get_data_length(event);
26
27
5
    uint8_t *cmp_packet = (uint8_t *)malloc(tox_max_custom_packet_size());
28
5
    ck_assert(cmp_packet != nullptr);
29
4
    memset(cmp_packet, LOSSY_PACKET_FILLER, tox_max_custom_packet_size());
30
31
4
    if (data_length == tox_max_custom_packet_size() && memcmp(data, cmp_packet, tox_max_custom_packet_size()) == 0) {
32
4
        const AutoTox *autotox = (AutoTox *)user_data;
33
4
        State *state = (State *)autotox->state;
34
4
        state->custom_packet_received = true;
35
4
    }
36
37
4
    free(cmp_packet);
38
4
}
39
40
static void test_lossy_packet(AutoTox *autotoxes)
41
12
{
42
12
    tox_events_callback_friend_lossy_packet(autotoxes[1].dispatch, &handle_lossy_packet);
43
12
    const size_t packet_size = tox_max_custom_packet_size() + 1;
44
12
    uint8_t *packet = (uint8_t *)malloc(packet_size);
45
12
    ck_assert(packet != nullptr);
46
11
    memset(packet, LOSSY_PACKET_FILLER, packet_size);
47
48
11
    bool ret = tox_friend_send_lossy_packet(autotoxes[0].tox, 0, packet, packet_size, nullptr);
49
11
    ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);
50
51
11
    ret = tox_friend_send_lossy_packet(autotoxes[0].tox, 0, packet, tox_max_custom_packet_size(), nullptr);
52
11
    ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
53
54
9
    free(packet);
55
56
85
    do {
57
85
        iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL);
58
85
    } while (!((State *)autotoxes[1].state)->custom_packet_received);
59
9
}
60
61
int main(void)
62
545
{
63
545
    setvbuf(stdout, nullptr, _IONBF, 0);
64
65
545
    Run_Auto_Options options = default_run_auto_options();
66
545
    options.graph = GRAPH_LINEAR;
67
68
545
    run_auto_test(nullptr, 2, test_lossy_packet, sizeof(State), &options);
69
70
545
    return 0;
71
545
}