Debian Codegraph @

testing/fuzzing/toxsave_fuzz_test.cc

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