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