Skip to content

Commit 2d8ac77

Browse files
committed
fuzz: add tests for the I2P Session public interface
1 parent 9947e44 commit 2d8ac77

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ test_fuzz_fuzz_SOURCES = \
239239
test/fuzz/golomb_rice.cpp \
240240
test/fuzz/hex.cpp \
241241
test/fuzz/http_request.cpp \
242+
test/fuzz/i2p.cpp \
242243
test/fuzz/integer.cpp \
243244
test/fuzz/key.cpp \
244245
test/fuzz/key_io.cpp \

src/test/fuzz/i2p.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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>
8+
#include <test/fuzz/FuzzedDataProvider.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+
15+
void initialize_i2p()
16+
{
17+
static const auto testing_setup = MakeNoLogFileContext<>();
18+
}
19+
20+
FUZZ_TARGET_INIT(i2p, initialize_i2p)
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{GetDataDir() / "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+
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+
}

0 commit comments

Comments
 (0)