Coverage Report

Created: 2026-10-24 08:16

/work/auto_tests/scenarios/scenario_nospam_test.c
Line
Count
Source (jump to first uncovered line)
1
#include "framework/framework.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
6
typedef struct {
7
    bool nospam_changed;
8
} AliceState;
9
10
static void set_checksum(uint8_t *address)
11
1
{
12
1
    uint8_t checksum[2] = {0};
13
37
    for (int i = 0; i < 36; ++i) {
14
36
        checksum[i % 2] ^= address[i];
15
36
    }
16
1
    address[36] = checksum[0];
17
1
    address[37] = checksum[1];
18
1
}
19
20
static void alice_script(ToxNode *self, void *ctx)
21
2
{
22
2
    AliceState *state = (AliceState *)ctx;
23
2
    tox_node_wait_for_self_connected(self);
24
25
2
    Tox *tox = tox_node_get_tox(self);
26
2
    uint32_t old_nospam = tox_self_get_nospam(tox);
27
2
    uint32_t new_nospam = old_nospam + 1;
28
29
2
    tox_node_log(self, "Old nospam: %u, setting new nospam: %u", old_nospam, new_nospam);
30
2
    tox_self_set_nospam(tox, new_nospam);
31
2
    ck_assert(tox_self_get_nospam(tox) == new_nospam);
32
33
    // Signal to Bob that we changed nospam
34
2
    state->nospam_changed = true;
35
36
2
    tox_node_log(self, "Waiting for friend request from Bob...");
37
2
    WAIT_FOR_EVENT(TOX_EVENT_FRIEND_REQUEST);
38
2
    tox_node_log(self, "Received friend request from Bob!");
39
2
}
40
41
static void bob_script(ToxNode *self, void *ctx)
42
1
{
43
1
    tox_node_wait_for_self_connected(self);
44
45
1
    ToxNode *alice = tox_scenario_get_node(tox_node_get_scenario(self), 0);
46
1
    const AliceState *alice_view = (const AliceState *)tox_node_get_peer_ctx(alice);
47
48
1
    tox_node_log(self, "Waiting for Alice to change nospam...");
49
1
    WAIT_UNTIL(alice_view->nospam_changed);
50
51
1
    uint8_t alice_addr[TOX_ADDRESS_SIZE];
52
1
    tox_node_get_address(alice, alice_addr);
53
54
    // Alice's address in alice_addr is already the NEW one.
55
    // Manually construct an old address.
56
1
    uint8_t old_alice_addr[TOX_ADDRESS_SIZE];
57
1
    memcpy(old_alice_addr, alice_addr, TOX_ADDRESS_SIZE);
58
59
1
    uint32_t nospam;
60
1
    memcpy(&nospam, old_alice_addr + 32, 4);
61
1
    nospam--; // Revert to old nospam
62
1
    memcpy(old_alice_addr + 32, &nospam, 4);
63
1
    set_checksum(old_alice_addr);
64
65
1
    tox_node_log(self, "Trying to add Alice with OLD nospam...");
66
1
    Tox_Err_Friend_Add err;
67
1
    uint32_t friend_num = tox_friend_add(tox_node_get_tox(self), old_alice_addr, (const uint8_t *)"Hi", 2, &err);
68
1
    ck_assert(err == TOX_ERR_FRIEND_ADD_OK);
69
70
    // Wait some time, but not too long.
71
101
    for (int i = 0; i < 100; ++i) {
72
100
        tox_scenario_yield(self);
73
100
    }
74
75
1
    tox_node_log(self, "Deleting Alice and trying with NEW nospam...");
76
1
    tox_friend_delete(tox_node_get_tox(self), friend_num, nullptr);
77
78
1
    tox_friend_add(tox_node_get_tox(self), alice_addr, (const uint8_t *)"Hi", 2, &err);
79
1
    ck_assert(err == TOX_ERR_FRIEND_ADD_OK);
80
1
    tox_node_log(self, "Friend request sent!");
81
1
}
82
83
int main(int argc, char *argv[])
84
31
{
85
31
    ToxScenario *s = tox_scenario_new(argc, argv, 60000);
86
87
31
    AliceState alice_state = {false};
88
31
    tox_scenario_add_node(s, "Alice", alice_script, &alice_state, sizeof(AliceState));
89
31
    tox_scenario_add_node(s, "Bob", bob_script, nullptr, 0);
90
91
31
    ToxNode *alice = tox_scenario_get_node(s, 0);
92
31
    ToxNode *bob = tox_scenario_get_node(s, 1);
93
94
31
    tox_node_bootstrap(alice, bob);
95
31
    tox_node_bootstrap(bob, alice);
96
97
31
    ToxScenarioStatus res = tox_scenario_run(s);
98
31
    if (res != TOX_SCENARIO_DONE) {
99
0
        fprintf(stderr, "Scenario failed with status %u\n", res);
100
0
        return 1;
101
0
    }
102
103
31
    tox_scenario_free(s);
104
31
    return 0;
105
31
}