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