Bitcoin Core 30.99.0
P2P Digital Currency
i2p_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present 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 <logging.h>
9#include <netaddress.h>
10#include <netbase.h>
11#include <test/util/logging.h>
12#include <test/util/net.h>
14#include <util/readwritefile.h>
15#include <util/threadinterrupt.h>
16
17#include <boost/test/unit_test.hpp>
18
19#include <memory>
20#include <string>
21
24{
25public:
26 explicit EnvTestingSetup(const ChainType chainType = ChainType::MAIN,
27 TestOpts opts = {})
28 : BasicTestingSetup{chainType, opts},
31 {
33 }
34
36 {
39 }
40
41private:
44};
45
47
48BOOST_AUTO_TEST_CASE(unlimited_recv)
49{
50 CreateSock = [](int, int, int) {
51 return std::make_unique<StaticContentsSock>(std::string(i2p::sam::MAX_MSG_SIZE + 1, 'a'));
52 };
53
54 auto interrupt{std::make_shared<CThreadInterrupt>()};
55 const std::optional<CService> addr{Lookup("127.0.0.1", 9000, false)};
56 const Proxy sam_proxy(addr.value(), /*tor_stream_isolation=*/false);
57 i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", sam_proxy, interrupt);
58
59 {
60 ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
61 ASSERT_DEBUG_LOG("too many bytes without a terminator");
62
63 i2p::Connection conn;
64 bool proxy_error;
65 BOOST_REQUIRE(!session.Connect(CService{}, conn, proxy_error));
66 }
67}
68
69BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
70{
71 size_t num_sockets{0};
72 CreateSock = [&num_sockets](int, int, int) {
73 // clang-format off
74 ++num_sockets;
75 // First socket is the control socket for creating the session.
76 if (num_sockets == 1) {
77 return std::make_unique<StaticContentsSock>(
78 // reply to HELLO
79 "HELLO REPLY RESULT=OK VERSION=3.1\n"
80 // reply to DEST GENERATE
81 "DEST REPLY PUB=WnGOL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yGPtd1p0AeoADrq1ccZggLPQ4ZLUsGK-HVw373rcTfvxrcuwenqVjiN4tbbYLWtP7xXGWj6fM6HyORhU63GphrjEePpMUHDHXd3o7pWGM-ieVVQSK~1MzF9P93pQWI3Do52EeNAayz4HbpPjNhVBzG1hUEFwznfPmUZBPuaOR4-uBm1NEWEuONlNOCctE4-U0Ukh94z-Qb55U5vXjR5G4apmBblr68t6Wm1TKlzpgFHzSqLryh3stWqrOKY1H0z9eZ2z1EkHFOpD5LyF6nf51e-lV7HLMl44TYzoEHK8RRVodtLcW9lacVdBpv~tOzlZERIiDziZODPETENZMz5oy9DQ7UUw==\n"
82 // reply to SESSION CREATE
83 "SESSION STATUS RESULT=OK\n"
84 // dummy to avoid reporting EOF on the socket
85 "a"
86 );
87 }
88 // Subsequent sockets are for recreating the session or for listening and accepting incoming connections.
89 if (num_sockets % 2 == 0) {
90 // Replies to Listen() and Accept()
91 return std::make_unique<StaticContentsSock>(
92 // reply to HELLO
93 "HELLO REPLY RESULT=OK VERSION=3.1\n"
94 // reply to STREAM ACCEPT
95 "STREAM STATUS RESULT=OK\n"
96 // continued reply to STREAM ACCEPT, violating the protocol described at
97 // https://geti2p.net/en/docs/api/samv3#Accept%20Response
98 // should be base64, something like
99 // "IchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLiHIVetPG2g6F26s0CkqhQ5k1z1YKA2zwIWSbzUV18YuIchV608baDoXbqzQKSqFDmTXPVgoDbPAhZJvNRXXxi4hyFXrTxtoOhdurNApKoUOZNc9WCgNs8CFkm81FdfGLlSreVaCuCS5sdb-8ToWULWP7kt~lRPDeUNxQMq3cRSBBQAEAAcAAA==\n"
100 "STREAM STATUS RESULT=I2P_ERROR MESSAGE=\"Session was closed\"\n"
101 );
102 } else {
103 // Another control socket, but without creating a destination (it is cached in the session).
104 return std::make_unique<StaticContentsSock>(
105 // reply to HELLO
106 "HELLO REPLY RESULT=OK VERSION=3.1\n"
107 // reply to SESSION CREATE
108 "SESSION STATUS RESULT=OK\n"
109 // dummy to avoid reporting EOF on the socket
110 "a"
111 );
112 }
113 // clang-format on
114 };
115
116 auto interrupt{std::make_shared<CThreadInterrupt>()};
117 const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), /*port=*/7656};
118 const Proxy sam_proxy(addr, /*tor_stream_isolation=*/false);
119 i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key",
120 sam_proxy,
121 interrupt);
122
123 i2p::Connection conn;
124 for (size_t i = 0; i < 5; ++i) {
125 ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
126 ASSERT_DEBUG_LOG("Persistent I2P SAM session" /* ... created */);
127 ASSERT_DEBUG_LOG("Error accepting");
128 ASSERT_DEBUG_LOG("Destroying I2P SAM session");
129 BOOST_REQUIRE(session.Listen(conn));
130 BOOST_REQUIRE(!session.Accept(conn));
131 }
132}
133
134BOOST_AUTO_TEST_CASE(damaged_private_key)
135{
136 CreateSock = [](int, int, int) {
137 return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n"
138 "SESSION STATUS RESULT=OK DESTINATION=\n");
139 };
140
141 const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged";
142
143 for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{
144 {"", "The private key is too short (0 < 387)"},
145
146 {"abcd", "The private key is too short (4 < 387)"},
147
148 {std::string(386, '\0'), "The private key is too short (386 < 387)"},
149
150 {std::string(385, '\0') + '\0' + '\1',
151 "Certificate length (1) designates that the private key should be 388 bytes, but it is only "
152 "387 bytes"},
153
154 {std::string(385, '\0') + '\0' + '\5' + "abcd",
155 "Certificate length (5) designates that the private key should be 392 bytes, but it is only "
156 "391 bytes"}}) {
157 BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));
158
159 auto interrupt{std::make_shared<CThreadInterrupt>()};
160 const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), /*port=*/7656};
161 const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false};
162 i2p::sam::Session session(i2p_private_key_file, sam_proxy, interrupt);
163
164 {
165 ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");
166 ASSERT_DEBUG_LOG(expected_error);
167
168 i2p::Connection conn;
169 bool proxy_error;
170 BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error));
171 }
172 }
173}
174
ArgsManager gArgs
Definition: args.cpp:40
ChainType
Definition: chaintype.h:11
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:235
void SetLogLevel(Level level)
Definition: logging.h:327
Level LogLevel() const
Definition: logging.h:326
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Save the log level and the value of CreateSock and restore them when the test ends.
Definition: i2p_tests.cpp:24
EnvTestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts opts={})
Definition: i2p_tests.cpp:26
const decltype(CreateSock) m_create_sock_orig
Definition: i2p_tests.cpp:43
const BCLog::Level m_prev_log_level
Definition: i2p_tests.cpp:42
Definition: netbase.h:59
I2P SAM session.
Definition: i2p.h:57
bool Listen(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Start listening for an incoming connection.
Definition: i2p.cpp:143
bool Connect(const CService &to, Connection &conn, bool &proxy_error) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Connect to an I2P peer.
Definition: i2p.cpp:222
bool Accept(Connection &conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Wait for and accept a new incoming connection.
Definition: i2p.cpp:158
#define COMPAT_IN6ADDR_LOOPBACK_INIT
Definition: compat.h:50
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(unlimited_recv)
Definition: i2p_tests.cpp:48
BCLog::Logger & LogInstance()
Definition: logging.cpp:26
Level
Definition: logging.h:122
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
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:191
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:577
#define BOOST_CHECK(expr)
Definition: object.cpp:16
bool WriteBinaryFile(const fs::path &filename, const std::string &data)
Write contents of std::string to a file.
Basic testing setup.
Definition: setup_common.h:64
An established connection with another peer.
Definition: i2p.h:32
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:42