Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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>13#include <test/util/setup_common.h>14#include <util/readwritefile.h>15#include <util/threadinterrupt.h>17#include <boost/test/unit_test.hpp>19#include <memory>20#include <string>22/// Save the log level and the value of CreateSock and restore them when the test ends.23class EnvTestingSetup : public BasicTestingSetup24{25public:26explicit EnvTestingSetup(const ChainType chainType = ChainType::MAIN,27TestOpts opts = {})28: BasicTestingSetup{chainType, opts},29m_prev_log_level{LogInstance().LogLevel()},30m_create_sock_orig{CreateSock}31{32LogInstance().SetLogLevel(BCLog::Level::Trace);33}35~EnvTestingSetup()36{37CreateSock = m_create_sock_orig;38LogInstance().SetLogLevel(m_prev_log_level);39}41private:42const BCLog::Level m_prev_log_level;43const decltype(CreateSock) m_create_sock_orig;44};46BOOST_FIXTURE_TEST_SUITE(i2p_tests, EnvTestingSetup)48BOOST_AUTO_TEST_CASE(unlimited_recv)49{50CreateSock = [](int, int, int) {51return std::make_unique<StaticContentsSock>(std::string(i2p::sam::MAX_MSG_SIZE + 1, 'a'));52};54auto interrupt{std::make_shared<CThreadInterrupt>()};55const std::optional<CService> addr{Lookup("127.0.0.1", 9000, false)};56const Proxy sam_proxy(addr.value(), /*tor_stream_isolation=*/false);57i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", sam_proxy, interrupt);59{60ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");61ASSERT_DEBUG_LOG("too many bytes without a terminator");63i2p::Connection conn;64bool proxy_error;65BOOST_REQUIRE(!session.Connect(CService{}, conn, proxy_error));66}67}69BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)70{71size_t num_sockets{0};72CreateSock = [&num_sockets](int, int, int) {73// clang-format off74++num_sockets;75// First socket is the control socket for creating the session.76if (num_sockets == 1) {77return std::make_unique<StaticContentsSock>(78// reply to HELLO79"HELLO REPLY RESULT=OK VERSION=3.1\n"80// reply to DEST GENERATE81"DEST REPLY PUB=WnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqLE4SD-yjT48UNI7qiTUfIPiDitCoiTTz2cr4QGfw89rBQAEAAcAAA== PRIV=WnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqFpxji10Qah0IXVYxZVqkcScM~Yccf9v8BnNlaZbWtSoWnGOLXRBqHQhdVjFlWqRxJwz9hxx~2~wGc2Vplta1KhacY4tdEGodCF1WMWVapHEnDP2HHH~b~AZzZWmW1rUqLE4SD-yjT48UNI7qiTUfIPiDitCoiTTz2cr4QGfw89rBQAEAAcAAOvuCIKTyv5f~1QgGq7XQl-IqBULTB5WzB3gw5yGPtd1p0AeoADrq1ccZggLPQ4ZLUsGK-HVw373rcTfvxrcuwenqVjiN4tbbYLWtP7xXGWj6fM6HyORhU63GphrjEePpMUHDHXd3o7pWGM-ieVVQSK~1MzF9P93pQWI3Do52EeNAayz4HbpPjNhVBzG1hUEFwznfPmUZBPuaOR4-uBm1NEWEuONlNOCctE4-U0Ukh94z-Qb55U5vXjR5G4apmBblr68t6Wm1TKlzpgFHzSqLryh3stWqrOKY1H0z9eZ2z1EkHFOpD5LyF6nf51e-lV7HLMl44TYzoEHK8RRVodtLcW9lacVdBpv~tOzlZERIiDziZODPETENZMz5oy9DQ7UUw==\n"82// reply to SESSION CREATE83"SESSION STATUS RESULT=OK\n"84// dummy to avoid reporting EOF on the socket85"a"86);87}88// Subsequent sockets are for recreating the session or for listening and accepting incoming connections.89if (num_sockets % 2 == 0) {90// Replies to Listen() and Accept()91return std::make_unique<StaticContentsSock>(92// reply to HELLO93"HELLO REPLY RESULT=OK VERSION=3.1\n"94// reply to STREAM ACCEPT95"STREAM STATUS RESULT=OK\n"96// continued reply to STREAM ACCEPT, violating the protocol described at97// https://i2p.net/en/docs/api/samv3/#accept-response98// should be base64, something like99// "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).104return std::make_unique<StaticContentsSock>(105// reply to HELLO106"HELLO REPLY RESULT=OK VERSION=3.1\n"107// reply to SESSION CREATE108"SESSION STATUS RESULT=OK\n"109// dummy to avoid reporting EOF on the socket110"a"111);112}113// clang-format on114};116auto interrupt{std::make_shared<CThreadInterrupt>()};117const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), /*port=*/7656};118const Proxy sam_proxy(addr, /*tor_stream_isolation=*/false);119i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key",120sam_proxy,121interrupt);123i2p::Connection conn;124for (size_t i = 0; i < 5; ++i) {125ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");126ASSERT_DEBUG_LOG("Persistent I2P SAM session" /* ... created */);127ASSERT_DEBUG_LOG("Error accepting");128ASSERT_DEBUG_LOG("Destroying I2P SAM session");129BOOST_REQUIRE(session.Listen(conn));130BOOST_REQUIRE(!session.Accept(conn));131}132}134BOOST_AUTO_TEST_CASE(damaged_private_key)135{136CreateSock = [](int, int, int) {137return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n"138"SESSION STATUS RESULT=OK DESTINATION=\n");139};141const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged";143for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{144{"", "The private key is too short (0 < 387)"},146{"abcd", "The private key is too short (4 < 387)"},148{std::string(386, '\0'), "The private key is too short (386 < 387)"},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"},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"}}) {157BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents));159auto interrupt{std::make_shared<CThreadInterrupt>()};160const CService addr{in6_addr(COMPAT_IN6ADDR_LOOPBACK_INIT), /*port=*/7656};161const Proxy sam_proxy{addr, /*tor_stream_isolation=*/false};162i2p::sam::Session session(i2p_private_key_file, sam_proxy, interrupt);164{165ASSERT_DEBUG_LOG("Creating persistent I2P SAM session");166ASSERT_DEBUG_LOG(expected_error);168i2p::Connection conn;169bool proxy_error;170BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error));171}172}173}175BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.