Branch data Line data Source code
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 <util/fs_helpers.h>
17 : : #include <util/threadinterrupt.h>
18 : :
19 : 1 : void initialize_i2p()
20 : : {
21 [ + - + - : 1 : static const auto testing_setup = MakeNoLogFileContext<>();
+ - ]
22 : 1 : }
23 : :
24 [ + - ]: 1233 : FUZZ_TARGET(i2p, .init = initialize_i2p)
25 : : {
26 : 779 : SeedRandomStateForTest(SeedRand::ZEROS);
27 : 779 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
28 : :
29 : 779 : SetMockTime(ConsumeTime(fuzzed_data_provider));
30 : :
31 : : // Mock CreateSock() to create FuzzedSock.
32 : 779 : auto CreateSockOrig = CreateSock;
33 : 2694 : CreateSock = [&fuzzed_data_provider](int, int, int) {
34 : 1915 : return std::make_unique<FuzzedSock>(fuzzed_data_provider);
35 : 779 : };
36 : :
37 [ + - + - ]: 1558 : const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
38 [ + - ]: 779 : const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), 7656};
39 : 779 : const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false};
40 [ + - ]: 779 : auto interrupt{ConsumeThreadInterrupt(fuzzed_data_provider)};
41 : :
42 [ + - + - ]: 1558 : i2p::sam::Session session{private_key_path, sam_proxy, interrupt};
43 [ + - ]: 779 : i2p::Connection conn;
44 : :
45 [ + - + + ]: 779 : if (session.Listen(conn)) {
46 [ + - + + ]: 143 : if (session.Accept(conn)) {
47 : 39 : try {
48 [ + + ]: 70 : (void)conn.sock->RecvUntilTerminator('\n', 10ms, *interrupt, i2p::sam::MAX_MSG_SIZE);
49 [ - + ]: 8 : } catch (const std::runtime_error&) {
50 : 8 : }
51 : : }
52 : : }
53 : :
54 : 779 : bool proxy_error;
55 : :
56 [ + - + - : 779 : if (session.Connect(CService{}, conn, proxy_error)) {
+ + ]
57 : 27 : try {
58 [ + + ]: 27 : conn.sock->SendComplete("verack\n", 10ms, *interrupt);
59 [ - + ]: 15 : } catch (const std::runtime_error&) {
60 : 15 : }
61 : : }
62 : :
63 [ + - ]: 779 : fs::remove_all(private_key_path);
64 : :
65 [ + - ]: 779 : CreateSock = CreateSockOrig;
66 [ + - ]: 3116 : }
|