Bitcoin Core 28.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>
14#include <util/fs_helpers.h>
16
18{
19 static const auto testing_setup = MakeNoLogFileContext<>();
20}
21
23{
25 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
26
27 SetMockTime(ConsumeTime(fuzzed_data_provider));
28
29 // Mock CreateSock() to create FuzzedSock.
30 auto CreateSockOrig = CreateSock;
31 CreateSock = [&fuzzed_data_provider](int, int, int) {
32 return std::make_unique<FuzzedSock>(fuzzed_data_provider);
33 };
34
35 const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
36 const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), 7656};
37 const Proxy sam_proxy{addr, false};
38 CThreadInterrupt interrupt;
39
40 i2p::sam::Session session{private_key_path, sam_proxy, &interrupt};
41 i2p::Connection conn;
42
43 if (session.Listen(conn)) {
44 if (session.Accept(conn)) {
45 try {
46 (void)conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
47 } catch (const std::runtime_error&) {
48 }
49 }
50 }
51
52 bool proxy_error;
53
54 if (session.Connect(CService{}, conn, proxy_error)) {
55 try {
56 conn.sock->SendComplete("verack\n", 10ms, interrupt);
57 } catch (const std::runtime_error&) {
58 }
59 }
60
61 fs::remove_all(private_key_path);
62
63 CreateSock = CreateSockOrig;
64}
ArgsManager gArgs
Definition: args.cpp:42
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
A helper class for interruptible sleeps.
Definition: netbase.h:59
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
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:31
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:557
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:22
void initialize_i2p()
Definition: i2p.cpp:17
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:35