/root/bitcoin/src/test/fuzz/i2p.cpp
Line | Count | Source |
1 | | // Copyright (c) The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <common/args.h> |
6 | | #include <compat/compat.h> |
7 | | #include <i2p.h> |
8 | | #include <netaddress.h> |
9 | | #include <netbase.h> |
10 | | #include <test/fuzz/FuzzedDataProvider.h> |
11 | | #include <test/fuzz/fuzz.h> |
12 | | #include <test/fuzz/util.h> |
13 | | #include <test/fuzz/util/net.h> |
14 | | #include <test/fuzz/util/threadinterrupt.h> |
15 | | #include <test/util/setup_common.h> |
16 | | #include <test/util/time.h> |
17 | | #include <util/fs_helpers.h> |
18 | | #include <util/threadinterrupt.h> |
19 | | |
20 | | void initialize_i2p() |
21 | 0 | { |
22 | 0 | static const auto testing_setup = MakeNoLogFileContext<>(); |
23 | 0 | } |
24 | | |
25 | | FUZZ_TARGET(i2p, .init = initialize_i2p) |
26 | 667 | { |
27 | 667 | SeedRandomStateForTest(SeedRand::ZEROS); |
28 | 667 | FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
29 | | |
30 | 667 | FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)}; |
31 | 667 | FakeSteadyClock steady_clock; |
32 | | |
33 | | // Mock CreateSock() to create FuzzedSock. |
34 | 667 | auto CreateSockOrig = CreateSock; |
35 | 1.70k | CreateSock = [&fuzzed_data_provider, &steady_clock](int, int, int) { |
36 | 1.70k | return std::make_unique<FuzzedSock>(fuzzed_data_provider, steady_clock); |
37 | 1.70k | }; |
38 | | |
39 | 667 | const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key"; |
40 | 667 | const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), 7656}; |
41 | 667 | const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false}; |
42 | 667 | auto interrupt{ConsumeThreadInterrupt(fuzzed_data_provider)}; |
43 | | |
44 | 667 | i2p::sam::Session session{private_key_path, sam_proxy, interrupt}; |
45 | 667 | i2p::Connection conn; |
46 | | |
47 | 667 | if (session.Listen(conn)) { Branch (47:9): [True: 152, False: 515]
|
48 | 152 | if (session.Accept(conn)) { Branch (48:13): [True: 40, False: 112]
|
49 | 40 | try { |
50 | 40 | (void)conn.sock->RecvUntilTerminator('\n', 10ms, *interrupt, i2p::sam::MAX_MSG_SIZE); |
51 | 40 | } catch (const std::runtime_error&) { |
52 | 15 | } |
53 | 40 | } |
54 | 152 | } |
55 | | |
56 | 667 | bool proxy_error; |
57 | | |
58 | 667 | if (session.Connect(CService{}, conn, proxy_error)) { Branch (58:9): [True: 33, False: 634]
|
59 | 33 | try { |
60 | 33 | conn.sock->SendComplete("verack\n", 10ms, *interrupt); |
61 | 33 | } catch (const std::runtime_error&) { |
62 | 17 | } |
63 | 33 | } |
64 | | |
65 | 667 | fs::remove(private_key_path); |
66 | | |
67 | 667 | CreateSock = CreateSockOrig; |
68 | 667 | } |