HomePhabricator

p2p: Refactor sock to add I2P unit test

Description

p2p: Refactor sock to add I2P unit test

Summary:

net: add connect() and getsockopt() wrappers to Sock

Extend the Sock class with wrappers to connect() and getsockopt().

This will make it possible to mock code which uses those.

https://github.com/bitcoin/bitcoin/pull/21387/commits/b5861100f85fef77b00f55dcdf01ffb4a2a112d8

net: change ConnectSocketDirectly() to take a Sock argument

Change ConnectSocketDirectly() to take a Sock argument instead of a
bare SOCKET. With this, use the Sock's (possibly mocked) methods
Connect(), Wait() and GetSockOpt() instead of calling the OS
functions directly.

https://github.com/bitcoin/bitcoin/pull/21387/commits/82d360b5a88d9057b6c09b61cd69e426c7a2412d

i2p: use pointers to Sock to accommodate mocking

Change the types of i2p::Connection::sock and
i2p::sam::Session::m_control_sock from Sock to
std::unique_ptr<Sock>.

Using pointers would allow us to sneak FuzzedSock instead of Sock
and have the methods of the former called.

After this change a test only needs to replace CreateSock() with
a function that returns FuzzedSock.

https://github.com/bitcoin/bitcoin/pull/21387/commits/9947e44de0cbd79e99d883443a9ac441d8c69713

test: add I2P test for a runaway SAM proxy

Add a regression test for https://github.com/bitcoin/bitcoin/pull/21407.

The test creates a socket that, upon read, returns some data, but never
the expected terminator \n, injects that socket into the I2P code and
expects i2p::sam::Session::Connect() to fail, printing a specific
error message to the log.

https://github.com/bitcoin/bitcoin/pull/21387/commits/40316a37cb02cf8a9a8b2cbd4d7153ffa57e7ec5

This is a partial backport of core#21387, without the fuzzer changes.
Depends on D11037

Test Plan: ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11042

Details

Provenance
PiRKAuthored on Feb 11 2022, 08:40
PiRKPushed on Feb 15 2022, 18:07
Reviewer
Restricted Project
Differential Revision
D11042: p2p: Refactor sock to add I2P unit test
Parents
rABC24433c09e9d6: doc: add basic I2P documentation
Branches
Unknown
Tags
Unknown