/work/toxcore/net_crypto_fuzz_test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "net_crypto.h" |
2 | | |
3 | | #include <cassert> |
4 | | #include <cstring> |
5 | | #include <functional> |
6 | | #include <memory> |
7 | | #include <optional> |
8 | | |
9 | | #include "../testing/fuzzing/fuzz_support.hh" |
10 | | #include "../testing/fuzzing/fuzz_tox.hh" |
11 | | #include "DHT.h" |
12 | | #include "TCP_client.h" |
13 | | #include "net_profile.h" |
14 | | #include "network.h" |
15 | | |
16 | | namespace { |
17 | | |
18 | | std::optional<std::tuple<IP_Port, uint8_t>> prepare(Fuzz_Data &input) |
19 | 19 | { |
20 | 19 | IP_Port ipp; |
21 | 19 | ip_init(&ipp.ip, true); |
22 | 19 | ipp.port = 33445; |
23 | | |
24 | 19 | CONSUME_OR_RETURN_VAL(const uint8_t *iterations_packed, input, 1, std::nullopt); |
25 | 18 | uint8_t iterations = *iterations_packed; |
26 | | |
27 | 18 | return {{ipp, iterations}}; |
28 | 19 | } |
29 | | |
30 | | void TestNetCrypto(Fuzz_Data &input) |
31 | 19 | { |
32 | 19 | const auto prep = prepare(input); |
33 | 19 | if (!prep.has_value()) { |
34 | 1 | return; |
35 | 1 | } |
36 | 18 | const auto [ipp, iterations] = prep.value(); |
37 | | |
38 | | // rest of the fuzz data is input for malloc and network |
39 | 18 | Fuzz_System sys(input); |
40 | | |
41 | 18 | const Ptr<Logger> logger(logger_new(sys.mem.get()), logger_kill); |
42 | 18 | if (logger == nullptr) { |
43 | 1 | return; |
44 | 1 | } |
45 | | |
46 | 17 | const Ptr<Networking_Core> net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), |
47 | 17 | &ipp.ip, ipp.port, ipp.port + 100, nullptr), |
48 | 17 | kill_networking); |
49 | 17 | if (net == nullptr) { |
50 | 2 | return; |
51 | 2 | } |
52 | | |
53 | 15 | const std::unique_ptr<Mono_Time, std::function<void(Mono_Time *)>> mono_time( |
54 | 15 | mono_time_new( |
55 | 15 | sys.mem.get(), [](void *user_data) { return *static_cast<uint64_t *>(user_data); }, |
56 | 15 | &sys.clock), |
57 | 15 | [mem = sys.mem.get()](Mono_Time *ptr) { mono_time_free(mem, ptr); }); |
58 | 15 | if (mono_time == nullptr) { |
59 | 2 | return; |
60 | 2 | } |
61 | | |
62 | 13 | const Ptr<DHT> dht(new_dht(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), |
63 | 13 | mono_time.get(), net.get(), false, false), |
64 | 13 | kill_dht); |
65 | 13 | if (dht == nullptr) { |
66 | 13 | return; |
67 | 13 | } |
68 | | |
69 | 0 | Net_Profile *tcp_np = netprof_new(logger.get(), sys.mem.get()); |
70 | |
|
71 | 0 | if (tcp_np == nullptr) { |
72 | 0 | return; |
73 | 0 | } |
74 | | |
75 | 0 | const TCP_Proxy_Info proxy_info = {0}; |
76 | |
|
77 | 0 | const Ptr<Net_Crypto> net_crypto( |
78 | 0 | new_net_crypto(logger.get(), sys.mem.get(), sys.rng.get(), sys.ns.get(), mono_time.get(), |
79 | 0 | dht.get(), &proxy_info, tcp_np), |
80 | 0 | kill_net_crypto); |
81 | 0 | if (net_crypto == nullptr) { |
82 | 0 | netprof_kill(sys.mem.get(), tcp_np); |
83 | 0 | return; |
84 | 0 | } |
85 | | |
86 | 0 | for (uint8_t i = 0; i < iterations; ++i) { |
87 | 0 | networking_poll(net.get(), nullptr); |
88 | 0 | do_dht(dht.get()); |
89 | 0 | do_net_crypto(net_crypto.get(), nullptr); |
90 | | // "Sleep" |
91 | 0 | sys.clock += System::BOOTSTRAP_ITERATION_INTERVAL; |
92 | 0 | } |
93 | |
|
94 | 0 | netprof_kill(sys.mem.get(), tcp_np); |
95 | 0 | } |
96 | | |
97 | | } // namespace |
98 | | |
99 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
100 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
101 | 2.57k | { |
102 | 2.57k | fuzz_select_target<TestNetCrypto>(data, size); |
103 | 2.57k | return 0; |
104 | 2.57k | } |