Coverage Report

Created: 2024-01-26 01:52

/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.h"
8
9
namespace {
10
11
void TestSaveDataLoading(Fuzz_Data &input)
12
1.16k
{
13
1.16k
    Tox_Err_Options_New error_options;
14
15
1.16k
    struct Tox_Options *tox_options = tox_options_new(&error_options);
16
17
1.16k
    assert(tox_options != nullptr);
18
1.16k
    assert(error_options == TOX_ERR_OPTIONS_NEW_OK);
19
20
1.16k
    const size_t savedata_size = input.size();
21
1.16k
    CONSUME_OR_RETURN(const uint8_t *savedata, input, savedata_size);
22
23
1.16k
    Null_System sys;
24
1.16k
    tox_options_set_operating_system(tox_options, sys.sys.get());
25
26
    // pass test data to Tox
27
1.16k
    tox_options_set_savedata_data(tox_options, savedata, savedata_size);
28
1.16k
    tox_options_set_savedata_type(tox_options, TOX_SAVEDATA_TYPE_TOX_SAVE);
29
30
1.16k
    Tox *tox = tox_new(tox_options, nullptr);
31
1.16k
    tox_options_free(tox_options);
32
1.16k
    if (tox == nullptr) {
33
        // Tox save was invalid, we're finished here
34
300
        return;
35
300
    }
36
37
    // verify that the file can be saved again
38
861
    std::vector<uint8_t> new_savedata(tox_get_savedata_size(tox));
39
861
    tox_get_savedata(tox, new_savedata.data());
40
41
861
    tox_kill(tox);
42
861
}
43
44
}
45
46
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
47
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48
2.99k
{
49
2.99k
    Fuzz_Data input{data, size};
50
2.99k
    TestSaveDataLoading(input);
51
2.99k
    return 0;  // Non-zero return values are reserved for future use.
52
2.99k
}