|
| 1 | +#include "vdf_client_session.h" |
| 2 | + |
| 3 | +#include <boost/asio.hpp> |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | +#include <exception> |
| 8 | +#include <string> |
| 9 | +#include <thread> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +std::string run_init_session_with_payload(const std::vector<uint8_t>& payload) { |
| 15 | + boost::asio::io_context io; |
| 16 | + using boost::asio::ip::tcp; |
| 17 | + |
| 18 | + tcp::acceptor acceptor(io, tcp::endpoint(tcp::v4(), 0)); |
| 19 | + const uint16_t port = acceptor.local_endpoint().port(); |
| 20 | + |
| 21 | + std::thread server([&]() { |
| 22 | + boost::system::error_code server_error; |
| 23 | + tcp::socket peer(io); |
| 24 | + acceptor.accept(peer, server_error); |
| 25 | + if (server_error) { |
| 26 | + return; |
| 27 | + } |
| 28 | + if (!payload.empty()) { |
| 29 | + boost::asio::write(peer, boost::asio::buffer(payload), server_error); |
| 30 | + } |
| 31 | + peer.shutdown(tcp::socket::shutdown_send, server_error); |
| 32 | + peer.close(server_error); |
| 33 | + }); |
| 34 | + |
| 35 | + std::string error_message; |
| 36 | + try { |
| 37 | + tcp::socket client(io); |
| 38 | + client.connect(tcp::endpoint(boost::asio::ip::address_v4::loopback(), port)); |
| 39 | + InitSession(client); |
| 40 | + } catch (const std::exception& e) { |
| 41 | + error_message = e.what(); |
| 42 | + } |
| 43 | + |
| 44 | + server.join(); |
| 45 | + return error_message; |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
| 49 | + |
| 50 | +TEST(VdfClientSessionRegressionTest, TruncatedDiscriminantSizeFailsBeforeParse) { |
| 51 | + const std::vector<uint8_t> payload = {'0', '3'}; |
| 52 | + const std::string error = run_init_session_with_payload(payload); |
| 53 | + EXPECT_NE(error.find("Connection closed while reading discriminant size"), std::string::npos); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(VdfClientSessionRegressionTest, ZeroDiscriminantSizeIsRejected) { |
| 57 | + const std::vector<uint8_t> payload = {'0', '0', '0'}; |
| 58 | + const std::string error = run_init_session_with_payload(payload); |
| 59 | + EXPECT_NE(error.find("Invalid discriminant size"), std::string::npos); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(VdfClientSessionRegressionTest, OversizedDiscriminantSizeIsRejected) { |
| 63 | + const std::vector<uint8_t> payload = {'3', '5', '0'}; |
| 64 | + const std::string error = run_init_session_with_payload(payload); |
| 65 | + EXPECT_NE(error.find("Invalid discriminant size"), std::string::npos); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(VdfClientSessionRegressionTest, TruncatedFormSizeFailsBeforeValidation) { |
| 69 | + const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c'}; |
| 70 | + const std::string error = run_init_session_with_payload(payload); |
| 71 | + EXPECT_NE(error.find("Connection closed while reading form size"), std::string::npos); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(VdfClientSessionRegressionTest, ZeroFormSizeIsRejected) { |
| 75 | + const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c', 0x00}; |
| 76 | + const std::string error = run_init_session_with_payload(payload); |
| 77 | + EXPECT_NE(error.find("Invalid form size"), std::string::npos); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(VdfClientSessionRegressionTest, WrappedNegativeFormSizeIsRejected) { |
| 81 | + // 0xFF arrives as -1 in signed char and must be rejected by the <= 0 check. |
| 82 | + const std::vector<uint8_t> payload = {'0', '0', '3', 'a', 'b', 'c', 0xFF}; |
| 83 | + const std::string error = run_init_session_with_payload(payload); |
| 84 | + EXPECT_NE(error.find("Invalid form size"), std::string::npos); |
| 85 | +} |
0 commit comments