Coverage Report

Created: 2025-10-08 19:34

/work/testing/fuzzing/toxsave_fuzz_test.cc
Line
Count
Source
1
#include <cassert>
2
#include <cstdint>
3
#include <vector>
4
5
#include "../../toxcore/tox.h"
6
#include "../../toxcore/tox_private.h"
7
#include "fuzz_support.hh"
8
9
namespace {
10
11
void TestSaveDataLoading(Fuzz_Data &input)
12
1.37k
{
13
1.37k
    Tox_Err_Options_New error_options;
14
15
1.37k
    struct Tox_Options *tox_options = tox_options_new(&error_options);
16
17
1.37k
    assert(tox_options != nullptr);
18
1.37k
    assert(error_options == TOX_ERR_OPTIONS_NEW_OK);
19
20
1.37k
    const size_t savedata_size = input.size();
21
1.37k
    CONSUME_OR_RETURN(const uint8_t *savedata, input, savedata_size);
22
23
1.37k
    tox_options_set_experimental_groups_persistence(tox_options, true);
24
25
    // pass test data to Tox
26
1.37k
    tox_options_set_savedata_data(tox_options, savedata, savedata_size);
27
1.37k
    tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
28
29
1.37k
    Tox_Options_Testing tox_options_testing;
30
1.37k
    Null_System sys;
31
1.37k
    tox_options_testing.operating_system = sys.sys.get();
32
33
1.37k
    Tox *tox = tox_new_testing(tox_options, nullptr, &tox_options_testing, nullptr);
34
1.37k
    tox_options_free(tox_options);
35
1.37k
    if (tox == nullptr) {
36
        // Tox save was invalid, we're finished here
37
369
        return;
38
369
    }
39
40
    // verify that the file can be saved again
41
1.01k
    std::vector<uint8_t> new_savedata(tox_get_savedata_size(tox));
42
1.01k
    tox_get_savedata(tox, new_savedata.data());
43
44
1.01k
    tox_kill(tox);
45
1.01k
}
46
47
}
48
49
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
50
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
51
2.57k
{
52
2.57k
    Fuzz_Data input{data, size};
53
2.57k
    TestSaveDataLoading(input);
54
2.57k
    return 0;  // Non-zero return values are reserved for future use.
55
2.57k
}