|
9 | 9 | #include <test/util/logging.h>
|
10 | 10 | #include <test/util/net.h>
|
11 | 11 | #include <test/util/setup_common.h>
|
| 12 | +#include <util/readwritefile.h> |
12 | 13 | #include <util/threadinterrupt.h>
|
13 | 14 |
|
14 | 15 | #include <boost/test/unit_test.hpp>
|
@@ -125,4 +126,47 @@ BOOST_AUTO_TEST_CASE(listen_ok_accept_fail)
|
125 | 126 | }
|
126 | 127 | }
|
127 | 128 |
|
| 129 | +BOOST_AUTO_TEST_CASE(damaged_private_key) |
| 130 | +{ |
| 131 | + const auto CreateSockOrig = CreateSock; |
| 132 | + |
| 133 | + CreateSock = [](const CService&) { |
| 134 | + return std::make_unique<StaticContentsSock>("HELLO REPLY RESULT=OK VERSION=3.1\n" |
| 135 | + "SESSION STATUS RESULT=OK DESTINATION=\n"); |
| 136 | + }; |
| 137 | + |
| 138 | + const auto i2p_private_key_file = m_args.GetDataDirNet() / "test_i2p_private_key_damaged"; |
| 139 | + |
| 140 | + for (const auto& [file_contents, expected_error] : std::vector<std::tuple<std::string, std::string>>{ |
| 141 | + {"", "The private key is too short (0 < 387)"}, |
| 142 | + |
| 143 | + {"abcd", "The private key is too short (4 < 387)"}, |
| 144 | + |
| 145 | + {std::string(386, '\0'), "The private key is too short (386 < 387)"}, |
| 146 | + |
| 147 | + {std::string(385, '\0') + '\0' + '\1', |
| 148 | + "Certificate length (1) designates that the private key should be 388 bytes, but it is only " |
| 149 | + "387 bytes"}, |
| 150 | + |
| 151 | + {std::string(385, '\0') + '\0' + '\5' + "abcd", |
| 152 | + "Certificate length (5) designates that the private key should be 392 bytes, but it is only " |
| 153 | + "391 bytes"}}) { |
| 154 | + BOOST_REQUIRE(WriteBinaryFile(i2p_private_key_file, file_contents)); |
| 155 | + |
| 156 | + CThreadInterrupt interrupt; |
| 157 | + i2p::sam::Session session(i2p_private_key_file, CService{}, &interrupt); |
| 158 | + |
| 159 | + { |
| 160 | + ASSERT_DEBUG_LOG("Creating persistent SAM session"); |
| 161 | + ASSERT_DEBUG_LOG(expected_error); |
| 162 | + |
| 163 | + i2p::Connection conn; |
| 164 | + bool proxy_error; |
| 165 | + BOOST_CHECK(!session.Connect(CService{}, conn, proxy_error)); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + CreateSock = CreateSockOrig; |
| 170 | +} |
| 171 | + |
128 | 172 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments