Bitcoin Core
31.99.0
P2P Digital Currency
src
test
fuzz
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
>
10
#include <
test/fuzz/FuzzedDataProvider.h
>
11
#include <
test/fuzz/fuzz.h
>
12
#include <
test/fuzz/util.h
>
13
#include <
test/fuzz/util/net.h
>
14
#include <
test/fuzz/util/threadinterrupt.h
>
15
#include <
test/util/setup_common.h
>
16
#include <
test/util/time.h
>
17
#include <
util/fs_helpers.h
>
18
#include <util/threadinterrupt.h>
19
20
void
initialize_i2p
()
21
{
22
static
const
auto
testing_setup = MakeNoLogFileContext<>();
23
}
24
25
FUZZ_TARGET
(
i2p
, .
init
=
initialize_i2p
)
26
{
27
SeedRandomStateForTest
(
SeedRand::ZEROS
);
28
FuzzedDataProvider
fuzzed_data_provider
{buffer.data(), buffer.size()};
29
30
NodeClockContext
clock_ctx{
ConsumeTime
(
fuzzed_data_provider
)};
31
32
// Mock CreateSock() to create FuzzedSock.
33
auto
CreateSockOrig =
CreateSock
;
34
CreateSock
= [&
fuzzed_data_provider
](int, int, int) {
35
return
std::make_unique<FuzzedSock>(
fuzzed_data_provider
);
36
};
37
38
const
fs::path private_key_path =
gArgs
.
GetDataDirNet
() /
"fuzzed_i2p_private_key"
;
39
const
CService
addr{in6_addr(
COMPAT_IN6ADDR_LOOPBACK_INIT
), 7656};
40
const
Proxy
sam_proxy{addr,
/*tor_stream_isolation=*/
false
};
41
auto
interrupt{
ConsumeThreadInterrupt
(
fuzzed_data_provider
)};
42
43
i2p::sam::Session
session{private_key_path, sam_proxy, interrupt};
44
i2p::Connection
conn;
45
46
if
(session.Listen(conn)) {
47
if
(session.Accept(conn)) {
48
try
{
49
(void)conn.
sock
->RecvUntilTerminator(
'\n'
, 10ms, *interrupt,
i2p::sam::MAX_MSG_SIZE
);
50
}
catch
(
const
std::runtime_error&) {
51
}
52
}
53
}
54
55
bool
proxy_error;
56
57
if
(session.Connect(
CService
{}, conn, proxy_error)) {
58
try
{
59
conn.
sock
->SendComplete(
"verack\n"
, 10ms, *interrupt);
60
}
catch
(
const
std::runtime_error&) {
61
}
62
}
63
64
fs::remove(private_key_path);
65
66
CreateSock
= CreateSockOrig;
67
}
FuzzedDataProvider.h
gArgs
ArgsManager gArgs
Definition:
args.cpp:40
args.h
ArgsManager::GetDataDirNet
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Definition:
args.cpp:330
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition:
netaddress.h:530
FuzzedDataProvider
Definition:
FuzzedDataProvider.h:32
NodeClockContext
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition:
time.h:40
Proxy
Definition:
netbase.h:60
i2p::sam::Session
I2P SAM session.
Definition:
i2p.h:57
compat.h
COMPAT_IN6ADDR_LOOPBACK_INIT
#define COMPAT_IN6ADDR_LOOPBACK_INIT
Definition:
compat.h:50
fs_helpers.h
fuzz.h
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:51
i2p
Definition:
i2p.cpp:32
init
Definition:
basic.cpp:8
netaddress.h
CreateSock
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition:
netbase.cpp:577
netbase.h
setup_common.h
i2p::Connection
An established connection with another peer.
Definition:
i2p.h:32
i2p::Connection::sock
std::unique_ptr< Sock > sock
Connected socket.
Definition:
i2p.h:34
FUZZ_TARGET
FUZZ_TARGET(i2p,.init=initialize_i2p)
Definition:
i2p.cpp:25
initialize_i2p
void initialize_i2p()
Definition:
i2p.cpp:20
net.h
threadinterrupt.h
ConsumeThreadInterrupt
std::shared_ptr< CThreadInterrupt > ConsumeThreadInterrupt(FuzzedDataProvider &fuzzed_data_provider)
Definition:
threadinterrupt.h:28
ConsumeTime
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition:
util.cpp:34
util.h
SeedRandomStateForTest
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition:
random.cpp:19
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
time.h
fuzzed_data_provider
FuzzedDataProvider & fuzzed_data_provider
Definition:
fees.cpp:39
Generated on Mon Apr 6 2026 20:00:29 for Bitcoin Core by
1.9.4