Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_group_by_id_test.c
Line
Count
Source (jump to first uncovered line)
1
#include "framework/framework.h"
2
#include <stdio.h>
3
#include <string.h>
4
#include <stdlib.h>
5
6
2
#define GROUP_NAME "NASA Headquarters"
7
1
#define GROUP_NAME_LEN (sizeof(GROUP_NAME) - 1)
8
4
#define PEER_NICK "Test Nick"
9
2
#define PEER_NICK_LEN (sizeof(PEER_NICK) - 1)
10
11
typedef struct {
12
    Tox_Group_Number group_number;
13
    uint8_t chat_id[TOX_GROUP_CHAT_ID_SIZE];
14
} GroupState;
15
16
static void script(ToxNode *self, void *ctx)
17
1
{
18
1
    GroupState *state = (GroupState *)ctx;
19
1
    Tox *tox = tox_node_get_tox(self);
20
21
1
    Tox_Err_Group_New err_new;
22
1
    state->group_number = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)GROUP_NAME, GROUP_NAME_LEN, (const uint8_t *)PEER_NICK, PEER_NICK_LEN, &err_new);
23
1
    ck_assert_int_eq(err_new, TOX_ERR_GROUP_NEW_OK);
24
1
    tox_node_log(self, "Group created: %u", state->group_number);
25
26
1
    Tox_Err_Group_State_Query err_query;
27
1
    ck_assert(tox_group_get_chat_id(tox, state->group_number, state->chat_id, &err_query));
28
1
    ck_assert_int_eq(err_query, TOX_ERR_GROUP_STATE_QUERY_OK);
29
30
1
    {
31
        // Test tox_group_get_group_list
32
1
        uint32_t num_groups = tox_group_get_group_list_size(tox);
33
1
        ck_assert_uint_eq(num_groups, 1);
34
35
1
        Tox_Group_Number group_list[1];
36
1
        tox_group_get_group_list(tox, group_list);
37
1
        ck_assert_uint_eq(group_list[0], state->group_number);
38
39
        // Test with NULL (should not crash)
40
1
        tox_group_get_group_list(tox, NULL);
41
1
    }
42
43
0
    {
44
        // Test tox_group_by_id
45
1
        Tox_Err_Group_By_Id err_by_id;
46
1
        uint32_t group_number = tox_group_by_id(tox, state->chat_id, &err_by_id);
47
1
        ck_assert_int_eq(err_by_id, TOX_ERR_GROUP_BY_ID_OK);
48
1
        ck_assert_uint_eq(group_number, state->group_number);
49
50
        // Test tox_group_by_id with invalid ID
51
1
        uint8_t invalid_chat_id[TOX_GROUP_CHAT_ID_SIZE];
52
1
        memset(invalid_chat_id, 0xAA, TOX_GROUP_CHAT_ID_SIZE);
53
1
        group_number = tox_group_by_id(tox, invalid_chat_id, &err_by_id);
54
1
        ck_assert_int_eq(err_by_id, TOX_ERR_GROUP_BY_ID_NOT_FOUND);
55
1
        ck_assert_uint_eq(group_number, UINT32_MAX);
56
1
    }
57
58
    // Create another group to ensure it works with multiple groups
59
1
    Tox_Group_Number group2 = tox_group_new(tox, TOX_GROUP_PRIVACY_STATE_PUBLIC, (const uint8_t *)"Group 2", 7, (const uint8_t *)PEER_NICK, PEER_NICK_LEN, &err_new);
60
1
    ck_assert_int_eq(err_new, TOX_ERR_GROUP_NEW_OK);
61
1
    uint8_t chat_id2[TOX_GROUP_CHAT_ID_SIZE];
62
1
    ck_assert(tox_group_get_chat_id(tox, group2, chat_id2, &err_query));
63
1
    ck_assert_int_eq(err_query, TOX_ERR_GROUP_STATE_QUERY_OK);
64
65
1
    {
66
1
        uint32_t num_groups = tox_group_get_group_list_size(tox);
67
1
        ck_assert_uint_eq(num_groups, 2);
68
69
1
        Tox_Group_Number group_list[2];
70
1
        tox_group_get_group_list(tox, group_list);
71
        // Groups are added in order of their index in the array
72
1
        ck_assert_uint_eq(group_list[0], state->group_number);
73
1
        ck_assert_uint_eq(group_list[1], group2);
74
1
    }
75
76
1
    {
77
1
        Tox_Err_Group_By_Id err_by_id;
78
1
        ck_assert_uint_eq(tox_group_by_id(tox, state->chat_id, &err_by_id), state->group_number);
79
1
        ck_assert_int_eq(err_by_id, TOX_ERR_GROUP_BY_ID_OK);
80
81
1
        ck_assert_uint_eq(tox_group_by_id(tox, chat_id2, &err_by_id), group2);
82
1
        ck_assert_int_eq(err_by_id, TOX_ERR_GROUP_BY_ID_OK);
83
1
    }
84
85
1
    Tox_Err_Group_Leave err_leave;
86
1
    ck_assert(tox_group_leave(tox, state->group_number, nullptr, 0, &err_leave));
87
1
    ck_assert_int_eq(err_leave, TOX_ERR_GROUP_LEAVE_OK);
88
89
    // Yield to allow the group to be actually deleted
90
1
    tox_scenario_yield(self);
91
92
1
    {
93
1
        uint32_t num_groups = tox_group_get_group_list_size(tox);
94
1
        ck_assert_uint_eq(num_groups, 1);
95
96
1
        Tox_Group_Number group_list[1];
97
1
        tox_group_get_group_list(tox, group_list);
98
1
        ck_assert_uint_eq(group_list[0], group2);
99
1
    }
100
101
1
    {
102
        // After leaving, it should not be found
103
1
        Tox_Err_Group_By_Id err_by_id;
104
1
        uint32_t group_number = tox_group_by_id(tox, state->chat_id, &err_by_id);
105
1
        ck_assert_int_eq(err_by_id, TOX_ERR_GROUP_BY_ID_NOT_FOUND);
106
1
        ck_assert_uint_eq(group_number, UINT32_MAX);
107
1
    }
108
1
}
109
110
int main(int argc, char *argv[])
111
31
{
112
31
    ToxScenario *s = tox_scenario_new(argc, argv, 60000);
113
31
    GroupState state = {0};
114
115
31
    tox_scenario_add_node(s, "Node", script, &state, sizeof(GroupState));
116
117
31
    ToxScenarioStatus res = tox_scenario_run(s);
118
31
    if (res != TOX_SCENARIO_DONE) {
119
0
        return 1;
120
0
    }
121
122
31
    tox_scenario_free(s);
123
31
    return 0;
124
31
}