|
| 1 | +// Copyright (c) 2020-2021 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 <i2p.h> |
| 6 | +#include <netaddress.h> |
| 7 | +#include <netbase.h> |
| 8 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 9 | +#include <test/fuzz/fuzz.h> |
| 10 | +#include <test/fuzz/util.h> |
| 11 | +#include <test/util/setup_common.h> |
| 12 | +#include <threadinterrupt.h> |
| 13 | +#include <util/system.h> |
| 14 | + |
| 15 | +void initialize_i2p() |
| 16 | +{ |
| 17 | + static const auto testing_setup = MakeNoLogFileContext<>(); |
| 18 | +} |
| 19 | + |
| 20 | +FUZZ_TARGET_INIT(i2p, initialize_i2p) |
| 21 | +{ |
| 22 | + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
| 23 | + |
| 24 | + // Mock CreateSock() to create FuzzedSock. |
| 25 | + auto CreateSockOrig = CreateSock; |
| 26 | + CreateSock = [&fuzzed_data_provider](const CService&) { |
| 27 | + return std::make_unique<FuzzedSock>(fuzzed_data_provider); |
| 28 | + }; |
| 29 | + |
| 30 | + const CService sam_proxy; |
| 31 | + CThreadInterrupt interrupt; |
| 32 | + |
| 33 | + i2p::sam::Session sess{GetDataDir() / "fuzzed_i2p_private_key", sam_proxy, &interrupt}; |
| 34 | + |
| 35 | + i2p::Connection conn; |
| 36 | + |
| 37 | + if (sess.Listen(conn)) { |
| 38 | + if (sess.Accept(conn)) { |
| 39 | + try { |
| 40 | + conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE); |
| 41 | + } catch (const std::runtime_error&) { |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const CService to; |
| 47 | + bool proxy_error; |
| 48 | + |
| 49 | + if (sess.Connect(to, conn, proxy_error)) { |
| 50 | + try { |
| 51 | + conn.sock->SendComplete("verack\n", 10ms, interrupt); |
| 52 | + } catch (const std::runtime_error&) { |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + CreateSock = CreateSockOrig; |
| 57 | +} |
0 commit comments