Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <compat/compat.h>
7#include <i2p.h>
8#include <netaddress.h>
9#include <netbase.h>
11#include <test/fuzz/fuzz.h>
12#include <test/fuzz/util.h>
13#include <test/fuzz/util/net.h>
16#include <util/fs_helpers.h>
18
20{
21 static const auto testing_setup = MakeNoLogFileContext<>();
22}
23
25{
27 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
28
29 SetMockTime(ConsumeTime(fuzzed_data_provider));
30
31 // Mock CreateSock() to create FuzzedSock.
32 auto CreateSockOrig = CreateSock;
33 CreateSock = [&fuzzed_data_provider](int, int, int) {
34 return std::make_unique<FuzzedSock>(fuzzed_data_provider);
35 };
36
37 const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
38 const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), 7656};
39 const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false};
40 auto interrupt{ConsumeThreadInterrupt(fuzzed_data_provider)};
41
42 i2p::sam::Session session{private_key_path, sam_proxy, interrupt};
43 i2p::Connection conn;
44
45 if (session.Listen(conn)) {
46 if (session.Accept(conn)) {
47 try {
48 (void)conn.sock->RecvUntilTerminator('\n', 10ms, *interrupt, i2p::sam::MAX_MSG_SIZE);
49 } catch (const std::runtime_error&) {
50 }
51 }
52 }
53
54 bool proxy_error;
55
56 if (session.Connect(CService{}, conn, proxy_error)) {
57 try {
58 conn.sock->SendComplete("verack\n", 10ms, *interrupt);
59 } catch (const std::runtime_error&) {
60 }
61 }
62
63 fs::remove_all(private_key_path);
64
65 CreateSock = CreateSockOrig;
66}
ArgsManager gArgs
Definition args.cpp:40
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:530
I2P SAM session.
Definition i2p.h:57
bool Listen(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Start listening for an incoming connection.
Definition i2p.cpp:143
bool Connect(const CService &to, Connection &conn, bool &proxy_error) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Connect to an I2P peer.
Definition i2p.cpp:222
bool Accept(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Wait for and accept a new incoming connection.
Definition i2p.cpp:158
#define COMPAT_IN6ADDR_LOOPBACK_INIT
Definition compat.h:50
#define FUZZ_TARGET(...)
Definition fuzz.h:35
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:577
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
An established connection with another peer.
Definition i2p.h:32
std::unique_ptr< Sock > sock
Connected socket.
Definition i2p.h:34
void initialize_i2p()
Definition i2p.cpp:19
std::shared_ptr< CThreadInterrupt > ConsumeThreadInterrupt(FuzzedDataProvider &fuzzed_data_provider)
NodeSeconds 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.
Definition random.h:19
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:44