Coverage Report

Created: 2024-01-26 01:52

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