Bitcoin Core 30.99.0
P2P Digital Currency
i2p.cpp
Go to the documentation of this file.
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>
10#include <test/fuzz/fuzz.h>
11#include <test/fuzz/util.h>
12#include <test/fuzz/util/net.h>
15#include <util/fs_helpers.h>
16#include <util/threadinterrupt.h>
17
19{
20 static const auto testing_setup = MakeNoLogFileContext<>();
21}
22
24{
26 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
27
28 SetMockTime(ConsumeTime(fuzzed_data_provider));
29
30 // Mock CreateSock() to create FuzzedSock.
31 auto CreateSockOrig = CreateSock;
32 CreateSock = [&fuzzed_data_provider](int, int, int) {
33 return std::make_unique<FuzzedSock>(fuzzed_data_provider);
34 };
35
36 const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
37 const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), 7656};
38 const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false};
39 auto interrupt{ConsumeThreadInterrupt(fuzzed_data_provider)};
40
41 i2p::sam::Session session{private_key_path, sam_proxy, interrupt};
42 i2p::Connection conn;
43
44 if (session.Listen(conn)) {
45 if (session.Accept(conn)) {
46 try {
47 (void)conn.sock->RecvUntilTerminator('\n', 10ms, *interrupt, i2p::sam::MAX_MSG_SIZE);
48 } catch (const std::runtime_error&) {
49 }
50 }
51 }
52
53 bool proxy_error;
54
55 if (session.Connect(CService{}, conn, proxy_error)) {
56 try {
57 conn.sock->SendComplete("verack\n", 10ms, *interrupt);
58 } catch (const std::runtime_error&) {
59 }
60 }
61
62 fs::remove_all(private_key_path);
63
64 CreateSock = CreateSockOrig;
65}
ArgsManager gArgs
Definition: args.cpp:42
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:235
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Definition: netbase.h:59
I2P SAM session.
Definition: i2p.h:57
static constexpr size_t MAX_MSG_SIZE
The maximum size of an incoming message from the I2P SAM proxy (in bytes).
Definition: i2p.h:51
Definition: i2p.cpp:32
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:581
An established connection with another peer.
Definition: i2p.h:32
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:34
FUZZ_TARGET(i2p,.init=initialize_i2p)
Definition: i2p.cpp:23
void initialize_i2p()
Definition: i2p.cpp:18
std::shared_ptr< CThreadInterrupt > ConsumeThreadInterrupt(FuzzedDataProvider &fuzzed_data_provider)
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40