Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/DHT_fuzz_test.cc
Line
Count
Source
1
#include "DHT.h"
2
3
#include <cassert>
4
#include <cstddef>
5
#include <cstdlib>
6
#include <cstring>
7
#include <vector>
8
9
#include "../testing/support/public/fuzz_data.hh"
10
#include "../testing/support/public/simulated_environment.hh"
11
12
namespace {
13
14
using tox::test::Fuzz_Data;
15
using tox::test::SimulatedEnvironment;
16
17
void TestHandleRequest(Fuzz_Data &input)
18
6
{
19
6
    SimulatedEnvironment env{12345};
20
6
    auto c_mem = env.fake_memory().c_memory();
21
22
6
    CONSUME_OR_RETURN(const std::uint8_t *self_public_key, input, CRYPTO_PUBLIC_KEY_SIZE);
23
5
    CONSUME_OR_RETURN(const std::uint8_t *self_secret_key, input, CRYPTO_SECRET_KEY_SIZE);
24
25
4
    std::uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
26
4
    std::uint8_t request[MAX_CRYPTO_REQUEST_SIZE];
27
4
    std::uint8_t request_id;
28
4
    handle_request(&c_mem, self_public_key, self_secret_key, public_key, request, &request_id,
29
4
        input.data(), input.size());
30
4
}
31
32
void TestUnpackNodes(Fuzz_Data &input)
33
34
{
34
34
    CONSUME1_OR_RETURN(const bool, tcp_enabled, input);
35
36
33
    const std::uint16_t node_count = 5;
37
33
    Node_format nodes[node_count];
38
33
    std::uint16_t processed_data_len;
39
33
    const int packed_count = unpack_nodes(
40
33
        nodes, node_count, &processed_data_len, input.data(), input.size(), tcp_enabled);
41
33
    if (packed_count > 0) {
42
17
        SimulatedEnvironment env{12345};
43
17
        auto c_mem = env.fake_memory().c_memory();
44
17
        Logger *logger = logger_new(&c_mem);
45
17
        std::vector<std::uint8_t> packed(packed_count * PACKED_NODE_SIZE_IP6);
46
17
        const int packed_size
47
17
            = pack_nodes(logger, packed.data(), packed.size(), nodes, packed_count);
48
17
        LOGGER_ASSERT(logger, packed_size == processed_data_len,
49
17
            "packed size (%d) != unpacked size (%d)", packed_size, processed_data_len);
50
17
        logger_kill(logger);
51
52
        // Check that packed nodes can be unpacked again and result in the
53
        // original unpacked nodes.
54
17
        Node_format nodes2[node_count];
55
17
        std::uint16_t processed_data_len2;
56
17
        const int packed_count2 = unpack_nodes(
57
17
            nodes2, node_count, &processed_data_len2, packed.data(), packed.size(), tcp_enabled);
58
17
        (void)packed_count2;
59
#if 0
60
        assert(processed_data_len2 == processed_data_len);
61
        assert(packed_count2 == packed_count);
62
#endif
63
17
        assert(std::memcmp(nodes, nodes2, sizeof(Node_format) * packed_count) == 0);
64
17
    }
65
33
}
66
67
}  // namespace
68
69
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size);
70
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size)
71
2.69k
{
72
2.69k
    tox::test::fuzz_select_target<TestHandleRequest, TestUnpackNodes>(data, size);
73
2.69k
    return 0;
74
2.69k
}