Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/friend_request_test.c
Line
Count
Source
1
/* Tests that we can add friends.
2
 */
3
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <time.h>
8
9
#include "../toxcore/ccompat.h"
10
#include "../toxcore/tox.h"
11
#include "../toxcore/util.h"
12
#include "../testing/misc_tools.h"
13
#include "auto_test_support.h"
14
#include "check_compat.h"
15
16
2
#define FR_MESSAGE "Gentoo"
17
18
static void accept_friend_request(Tox *tox, const Tox_Event_Friend_Request *event,
19
                                  void *userdata)
20
32
{
21
32
    const uint8_t *public_key = tox_event_friend_request_get_public_key(event);
22
32
    const uint8_t *data = tox_event_friend_request_get_message(event);
23
32
    const size_t length = tox_event_friend_request_get_message_length(event);
24
32
    ck_assert_msg(length == sizeof(FR_MESSAGE) && memcmp(FR_MESSAGE, data, sizeof(FR_MESSAGE)) == 0,
25
32
                  "unexpected friend request message");
26
32
    tox_friend_add_norequest(tox, public_key, nullptr);
27
32
}
28
29
static void iterate2_wait(const Tox_Dispatch *dispatch, Tox *tox1, Tox *tox2)
30
60
{
31
60
    Tox_Err_Events_Iterate err;
32
60
    Tox_Events *events;
33
34
60
    events = tox_events_iterate(tox1, true, &err);
35
60
    ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
36
60
    tox_dispatch_invoke(dispatch, events, tox1, nullptr);
37
60
    tox_events_free(events);
38
39
60
    events = tox_events_iterate(tox2, true, &err);
40
60
    ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
41
60
    tox_dispatch_invoke(dispatch, events, tox2, nullptr);
42
60
    tox_events_free(events);
43
44
60
    c_sleep(ITERATION_INTERVAL);
45
60
}
46
47
static void test_friend_request(void)
48
1
{
49
1
    printf("Initialising 2 toxes.\n");
50
1
    uint32_t index[] = { 1, 2 };
51
1
    const time_t cur_time = time(nullptr);
52
1
    Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
53
1
    Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
54
55
1
    ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
56
57
1
    tox_events_init(tox1);
58
1
    tox_events_init(tox2);
59
60
1
    printf("Bootstrapping tox2 off tox1.\n");
61
1
    uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
62
1
    tox_self_get_dht_id(tox1, dht_key);
63
1
    const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
64
65
1
    tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
66
67
1
    Tox_Dispatch *dispatch = tox_dispatch_new(nullptr);
68
1
    ck_assert(dispatch != nullptr);
69
70
41
    do {
71
41
        iterate2_wait(dispatch, tox1, tox2);
72
41
    } while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
73
41
             tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);
74
75
1
    printf("Toxes are online, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
76
1
    const time_t con_time = time(nullptr);
77
78
1
    printf("Tox1 adds tox2 as friend, tox2 accepts.\n");
79
1
    tox_events_callback_friend_request(dispatch, accept_friend_request);
80
81
1
    uint8_t address[TOX_ADDRESS_SIZE];
82
1
    tox_self_get_address(tox2, address);
83
84
1
    const uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)FR_MESSAGE, sizeof(FR_MESSAGE), nullptr);
85
1
    ck_assert_msg(test == 0, "failed to add friend error code: %u", test);
86
87
19
    do {
88
19
        iterate2_wait(dispatch, tox1, tox2);
89
19
    } while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
90
19
             tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP);
91
92
1
    printf("Tox clients connected took %lu seconds.\n", (unsigned long)(time(nullptr) - con_time));
93
1
    printf("friend_request_test succeeded, took %lu seconds.\n", (unsigned long)(time(nullptr) - cur_time));
94
95
1
    tox_dispatch_free(dispatch);
96
1
    tox_kill(tox1);
97
1
    tox_kill(tox2);
98
1
}
99
100
int main(void)
101
721
{
102
721
    setvbuf(stdout, nullptr, _IONBF, 0);
103
104
721
    test_friend_request();
105
721
    return 0;
106
721
}