fuzz: Improve torcontrol fuzz test

Gets rid of the Dummy class and adds coverage of get_socks_cb.

Also explicitly handles an exception case within Torcontrol rather than
relying on a guard in the fuzz test.
This commit is contained in:
Fabian Jahr
2025-12-28 15:07:51 +01:00
parent b1869e9a2d
commit 4117b92e67
2 changed files with 16 additions and 33 deletions

View File

@@ -12,30 +12,6 @@
#include <string>
#include <vector>
class DummyTorControlConnection : public TorControlConnection
{
CThreadInterrupt m_dummy_interrupt;
public:
DummyTorControlConnection() : TorControlConnection{m_dummy_interrupt}
{
}
bool Connect(const std::string&)
{
return true;
}
void Disconnect()
{
}
bool Command(const std::string&, const ReplyHandlerCB&)
{
return true;
}
};
void initialize_torcontrol()
{
static const auto testing_setup = MakeNoLogFileContext<>();
@@ -46,6 +22,9 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
TorController tor_controller;
CThreadInterrupt interrupt;
TorControlConnection conn{interrupt};
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
TorControlReply tor_control_reply;
CallOneOf(
@@ -63,26 +42,26 @@ FUZZ_TARGET(torcontrol, .init = initialize_torcontrol)
tor_control_reply.code = fuzzed_data_provider.ConsumeIntegral<int>();
});
tor_control_reply.lines = ConsumeRandomLengthStringVector(fuzzed_data_provider);
if (tor_control_reply.lines.empty()) {
break;
}
DummyTorControlConnection dummy_tor_control_connection;
CallOneOf(
fuzzed_data_provider,
[&] {
tor_controller.add_onion_cb(dummy_tor_control_connection, tor_control_reply, /*pow_was_enabled=*/true);
tor_controller.add_onion_cb(conn, tor_control_reply, /*pow_was_enabled=*/true);
},
[&] {
tor_controller.add_onion_cb(dummy_tor_control_connection, tor_control_reply, /*pow_was_enabled=*/false);
tor_controller.add_onion_cb(conn, tor_control_reply, /*pow_was_enabled=*/false);
},
[&] {
tor_controller.auth_cb(dummy_tor_control_connection, tor_control_reply);
tor_controller.auth_cb(conn, tor_control_reply);
},
[&] {
tor_controller.authchallenge_cb(dummy_tor_control_connection, tor_control_reply);
tor_controller.authchallenge_cb(conn, tor_control_reply);
},
[&] {
tor_controller.protocolinfo_cb(dummy_tor_control_connection, tor_control_reply);
tor_controller.protocolinfo_cb(conn, tor_control_reply);
},
[&] {
tor_controller.get_socks_cb(conn, tor_control_reply);
});
}
}

View File

@@ -602,6 +602,10 @@ void TorController::authchallenge_cb(TorControlConnection& _conn, const TorContr
{
if (reply.code == TOR_REPLY_OK) {
LogDebug(BCLog::TOR, "SAFECOOKIE authentication challenge successful");
if (reply.lines.empty()) {
LogWarning("tor: AUTHCHALLENGE reply was empty");
return;
}
std::pair<std::string,std::string> l = SplitTorReplyLine(reply.lines[0]);
if (l.first == "AUTHCHALLENGE") {
std::map<std::string,std::string> m = ParseTorReplyMapping(l.second);