Coverage Report

Created: 2024-01-26 01:52

/work/auto_tests/set_status_message_test.c
Line
Count
Source
1
/* Tests that we can set our status message
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
2
#define STATUS_MESSAGE "Installing Gentoo"
17
18
static void status_callback(Tox *tox, const Tox_Event_Friend_Status_Message *event, void *user_data)
19
1
{
20
    //uint32_t friend_number = tox_event_friend_status_message_get_friend_number(event);
21
1
    const uint8_t *message = tox_event_friend_status_message_get_message(event);
22
1
    uint32_t message_length = tox_event_friend_status_message_get_message_length(event);
23
24
1
    ck_assert_msg(message_length == sizeof(STATUS_MESSAGE) &&
25
1
                  memcmp(message, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0,
26
1
                  "incorrect data in status callback");
27
1
    bool *status_updated = (bool *)user_data;
28
1
    *status_updated = true;
29
1
}
30
31
static void test_set_status_message(void)
32
1
{
33
1
    printf("initialising 2 toxes\n");
34
1
    uint32_t index[] = { 1, 2 };
35
1
    const time_t cur_time = time(nullptr);
36
1
    Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
37
1
    Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
38
39
1
    ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
40
41
    // we only run events on tox2 in this test case
42
1
    tox_events_init(tox2);
43
44
1
    Tox_Dispatch *dispatch2 = tox_dispatch_new(nullptr);
45
1
    ck_assert(dispatch2 != nullptr);
46
47
1
    printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
48
1
    uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
49
1
    tox_self_get_public_key(tox2, public_key);
50
1
    tox_friend_add_norequest(tox1, public_key, nullptr);
51
1
    tox_self_get_public_key(tox1, public_key);
52
1
    tox_friend_add_norequest(tox2, public_key, nullptr);
53
54
1
    printf("bootstrapping tox2 off tox1\n");
55
1
    uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
56
1
    tox_self_get_dht_id(tox1, dht_key);
57
1
    const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
58
59
1
    tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
60
61
41
    do {
62
41
        tox_iterate(tox1, nullptr);
63
64
41
        Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK;
65
41
        Tox_Events *events = tox_events_iterate(tox2, true, &err);
66
41
        ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
67
        //tox_dispatch_invoke(dispatch2, events, tox2, nullptr);
68
41
        tox_events_free(events);
69
70
41
        c_sleep(ITERATION_INTERVAL);
71
41
    } while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
72
41
             tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE);
73
74
1
    printf("toxes are online, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
75
1
    const time_t con_time = time(nullptr);
76
77
14
    do {
78
14
        tox_iterate(tox1, nullptr);
79
80
14
        Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK;
81
14
        Tox_Events *events = tox_events_iterate(tox2, true, &err);
82
14
        ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
83
        //tox_dispatch_invoke(dispatch2, events, tox2, nullptr);
84
14
        tox_events_free(events);
85
86
14
        c_sleep(ITERATION_INTERVAL);
87
14
    } while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
88
14
             tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP);
89
90
1
    printf("tox clients connected took %lu seconds\n", (unsigned long)(time(nullptr) - con_time));
91
92
1
    tox_events_callback_friend_status_message(dispatch2, status_callback);
93
1
    Tox_Err_Set_Info err_n;
94
1
    bool ret = tox_self_set_status_message(tox1, (const uint8_t *)STATUS_MESSAGE, sizeof(STATUS_MESSAGE),
95
1
                                           &err_n);
96
1
    ck_assert_msg(ret && err_n == TOX_ERR_SET_INFO_OK, "tox_self_set_status_message failed because %d\n", err_n);
97
98
1
    bool status_updated = false;
99
100
1
    do {
101
1
        tox_iterate(tox1, nullptr);
102
103
1
        Tox_Err_Events_Iterate err = TOX_ERR_EVENTS_ITERATE_OK;
104
1
        Tox_Events *events = tox_events_iterate(tox2, true, &err);
105
1
        ck_assert(err == TOX_ERR_EVENTS_ITERATE_OK);
106
1
        tox_dispatch_invoke(dispatch2, events, tox2, &status_updated);
107
1
        tox_events_free(events);
108
109
1
        c_sleep(ITERATION_INTERVAL);
110
1
    } while (!status_updated);
111
112
1
    ck_assert_msg(tox_friend_get_status_message_size(tox2, 0, nullptr) == sizeof(STATUS_MESSAGE),
113
1
                  "status message length not correct");
114
1
    uint8_t cmp_status[sizeof(STATUS_MESSAGE)];
115
1
    tox_friend_get_status_message(tox2, 0, cmp_status, nullptr);
116
1
    ck_assert_msg(memcmp(cmp_status, STATUS_MESSAGE, sizeof(STATUS_MESSAGE)) == 0,
117
1
                  "status message not correct");
118
119
1
    printf("test_set_status_message succeeded, took %lu seconds\n", (unsigned long)(time(nullptr) - cur_time));
120
121
1
    tox_dispatch_free(dispatch2);
122
123
1
    tox_kill(tox1);
124
1
    tox_kill(tox2);
125
1
}
126
127
int main(void)
128
721
{
129
721
    setvbuf(stdout, nullptr, _IONBF, 0);
130
131
721
    test_set_status_message();
132
721
    return 0;
133
721
}