Bitcoin Core  25.99.0
P2P Digital Currency
i2p.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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>
13 #include <test/util/setup_common.h>
14 #include <util/threadinterrupt.h>
15 
17 {
18  static const auto testing_setup = MakeNoLogFileContext<>();
19 }
20 
22 {
23  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
24 
25  // Mock CreateSock() to create FuzzedSock.
26  auto CreateSockOrig = CreateSock;
27  CreateSock = [&fuzzed_data_provider](const CService&) {
28  return std::make_unique<FuzzedSock>(fuzzed_data_provider);
29  };
30 
31  const CService sam_proxy;
32  CThreadInterrupt interrupt;
33 
34  i2p::sam::Session sess{gArgs.GetDataDirNet() / "fuzzed_i2p_private_key", sam_proxy, &interrupt};
35 
36  i2p::Connection conn;
37 
38  if (sess.Listen(conn)) {
39  if (sess.Accept(conn)) {
40  try {
41  (void)conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
42  } catch (const std::runtime_error&) {
43  }
44  }
45  }
46 
47  const CService to;
48  bool proxy_error;
49 
50  if (sess.Connect(to, conn, proxy_error)) {
51  try {
52  conn.sock->SendComplete("verack\n", 10ms, interrupt);
53  } catch (const std::runtime_error&) {
54  }
55  }
56 
57  CreateSock = CreateSockOrig;
58 }
ArgsManager gArgs
Definition: args.cpp:42
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:231
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:535
A helper class for interruptible sleeps.
I2P SAM session.
Definition: i2p.h:56
static constexpr size_t MAX_MSG_SIZE
The maximum size of an incoming message from the I2P SAM proxy (in bytes).
Definition: i2p.h:50
Definition: i2p.cpp:28
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:502
An established connection with another peer.
Definition: i2p.h:31
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:33
FUZZ_TARGET(i2p,.init=initialize_i2p)
Definition: i2p.cpp:21
void initialize_i2p()
Definition: i2p.cpp:16