Skip to content

Commit 10d4477

Browse files
tests: Add fuzzing harness for TorController
1 parent 64219c0 commit 10d4477

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ test_fuzz_fuzz_SOURCES = \
291291
test/fuzz/strprintf.cpp \
292292
test/fuzz/system.cpp \
293293
test/fuzz/timedata.cpp \
294+
test/fuzz/torcontrol.cpp \
294295
test/fuzz/transaction.cpp \
295296
test/fuzz/tx_in.cpp \
296297
test/fuzz/tx_out.cpp \

src/test/fuzz/torcontrol.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2020 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 <test/fuzz/FuzzedDataProvider.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <test/fuzz/util.h>
8+
#include <torcontrol.h>
9+
10+
#include <cstdint>
11+
#include <string>
12+
#include <vector>
13+
14+
class DummyTorControlConnection : public TorControlConnection
15+
{
16+
public:
17+
DummyTorControlConnection() : TorControlConnection{nullptr}
18+
{
19+
}
20+
21+
bool Connect(const std::string&, const ConnectionCB&, const ConnectionCB&)
22+
{
23+
return true;
24+
}
25+
26+
void Disconnect()
27+
{
28+
}
29+
30+
bool Command(const std::string&, const ReplyHandlerCB&)
31+
{
32+
return true;
33+
}
34+
};
35+
36+
void initialize_torcontrol()
37+
{
38+
static const auto testing_setup = MakeNoLogFileContext<>();
39+
}
40+
41+
FUZZ_TARGET_INIT(torcontrol, initialize_torcontrol)
42+
{
43+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
44+
45+
TorController tor_controller;
46+
while (fuzzed_data_provider.ConsumeBool()) {
47+
TorControlReply tor_control_reply;
48+
CallOneOf(
49+
fuzzed_data_provider,
50+
[&] {
51+
tor_control_reply.code = 250;
52+
},
53+
[&] {
54+
tor_control_reply.code = 510;
55+
},
56+
[&] {
57+
tor_control_reply.code = fuzzed_data_provider.ConsumeIntegral<int>();
58+
});
59+
tor_control_reply.lines = ConsumeRandomLengthStringVector(fuzzed_data_provider);
60+
if (tor_control_reply.lines.empty()) {
61+
break;
62+
}
63+
DummyTorControlConnection dummy_tor_control_connection;
64+
CallOneOf(
65+
fuzzed_data_provider,
66+
[&] {
67+
tor_controller.add_onion_cb(dummy_tor_control_connection, tor_control_reply);
68+
},
69+
[&] {
70+
tor_controller.auth_cb(dummy_tor_control_connection, tor_control_reply);
71+
},
72+
[&] {
73+
tor_controller.authchallenge_cb(dummy_tor_control_connection, tor_control_reply);
74+
},
75+
[&] {
76+
tor_controller.protocolinfo_cb(dummy_tor_control_connection, tor_control_reply);
77+
});
78+
}
79+
}

src/torcontrol.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class TorController
113113
{
114114
public:
115115
TorController(struct event_base* base, const std::string& tor_control_center, const CService& target);
116+
TorController() : conn{nullptr} {
117+
// Used for testing only.
118+
}
116119
~TorController();
117120

118121
/** Get name of file to store private key in */
@@ -127,7 +130,7 @@ class TorController
127130
std::string private_key;
128131
std::string service_id;
129132
bool reconnect;
130-
struct event *reconnect_ev;
133+
struct event *reconnect_ev = nullptr;
131134
float reconnect_timeout;
132135
CService service;
133136
const CService m_target;
@@ -136,6 +139,7 @@ class TorController
136139
/** ClientNonce for SAFECOOKIE auth */
137140
std::vector<uint8_t> clientNonce;
138141

142+
public:
139143
/** Callback for ADD_ONION result */
140144
void add_onion_cb(TorControlConnection& conn, const TorControlReply& reply);
141145
/** Callback for AUTHENTICATE result */

0 commit comments

Comments
 (0)