Bitcoin Core  22.99.0
P2P Digital Currency
i2p.cpp
Go to the documentation of this file.
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>
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 
16 {
17  static const auto testing_setup = MakeNoLogFileContext<>();
18 }
19 
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{gArgs.GetDataDirNet() / "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  (void)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 }
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
threadinterrupt.h
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:282
setup_common.h
util.h
i2p
Definition: i2p.cpp:27
CreateSock
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:526
i2p::sam::Session
I2P SAM session.
Definition: i2p.h:55
initialize_i2p
void initialize_i2p()
Definition: i2p.cpp:15
netaddress.h
FuzzedDataProvider.h
gArgs
ArgsManager gArgs
Definition: system.cpp:84
system.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
i2p.h
i2p::sam::MAX_MSG_SIZE
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
i2p::Connection
An established connection with another peer.
Definition: i2p.h:31
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(i2p, initialize_i2p)
Definition: i2p.cpp:20
netbase.h
CThreadInterrupt
Definition: threadinterrupt.h:19
i2p::Connection::sock
std::unique_ptr< Sock > sock
Connected socket.
Definition: i2p.h:33