// Copyright (c) 2021-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include BOOST_FIXTURE_TEST_SUITE(i2p_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(unlimited_recv) { const auto prev_log_level{LogInstance().LogLevel()}; LogInstance().SetLogLevel(BCLog::Level::Trace); auto CreateSockOrig = CreateSock; // Mock CreateSock() to create MockSock. CreateSock = [](const CService&) { return std::make_unique(std::string(i2p::sam::MAX_MSG_SIZE + 1, 'a')); }; CThreadInterrupt interrupt; i2p::sam::Session session(gArgs.GetDataDirNet() / "test_i2p_private_key", CService{}, &interrupt); { ASSERT_DEBUG_LOG("Creating persistent SAM session"); ASSERT_DEBUG_LOG("too many bytes without a terminator"); i2p::Connection conn; bool proxy_error; BOOST_REQUIRE(!session.Connect(CService{}, conn, proxy_error)); } CreateSock = CreateSockOrig; LogInstance().SetLogLevel(prev_log_level); } BOOST_AUTO_TEST_CASE(damaged_private_key) { const auto CreateSockOrig = CreateSock; CreateSock = [](const CService&) { return std::make_unique("HELLO REPLY RESULT=OK VERSION=3.1\n" "SESSION STATUS RESULT=OK DESTINATION=\n"); }; const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged"; for (const auto& [file_contents, expected_error] : std::vector>{ {"", "The private key is too short (0 < 387)"}, {"abcd", "The private key is too short (4 < 387)"}, {std::string(386, '\0'), "The private key is too short (386 < 387)"}, {std::string(385, '\0') + '\0' + '\1', "Certificate length (1) designates that the private key should be 388 bytes, but it is only " "387 bytes"}, {std::string(385, '\0') + '\0' + '\5' + "abcd", "Certificate length (5) designates that the private key should be 392 bytes, but it is only " "391 bytes"}}) { BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents)); CThreadInterrupt interrupt; i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt); { ASSERT_DEBUG_LOG("Creating persistent SAM session"); ASSERT_DEBUG_LOG(expected_error); i2p::Connection conn; bool proxy_error; BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error)); } } CreateSock = CreateSockOrig; } BOOST_AUTO_TEST_SUITE_END()