Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/lossless_packet_test.c
Line
Count
Source
1
/* Tests that we can send lossless 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
16
#define LOSSLESS_PACKET_FILLER 160
20
21
static void handle_lossless_packet(Tox *tox, const Tox_Event_Friend_Lossless_Packet *event, void *user_data)
22
5
{
23
    //const uint32_t friend_number = tox_event_friend_lossless_packet_get_friend_number(event);
24
5
    const uint8_t *data = tox_event_friend_lossless_packet_get_data(event);
25
5
    const uint32_t data_length = tox_event_friend_lossless_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, LOSSLESS_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_lossless_packet(AutoTox *autotoxes)
41
13
{
42
13
    tox_events_callback_friend_lossless_packet(autotoxes[1].dispatch, &handle_lossless_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, LOSSLESS_PACKET_FILLER, packet_size);
47
48
12
    bool ret = tox_friend_send_lossless_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_lossless_packet(autotoxes[0].tox, 0, packet, tox_max_custom_packet_size(), nullptr);
52
12
    ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);
53
54
11
    free(packet);
55
56
41
    do {
57
41
        iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL);
58
41
    } while (!((State *)autotoxes[1].state)->custom_packet_received);
59
11
}
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_lossless_packet, sizeof(State), &options);
69
70
721
    return 0;
71
721
}