Refactoring
All checks were successful
PR Check / on-push-commit-check (push) Successful in 12m7s

This commit is contained in:
sectapunterx 2024-11-21 18:46:25 +03:00
parent 760bc2ab68
commit 665ceb9604

View file

@ -52,21 +52,23 @@ TEST_F(ProxyTest, ConnectViaHttpProxy_SuccessfulResponse) {
// Test 2: Connect via SOCKS proxy // Test 2: Connect via SOCKS proxy
TEST(Socks5ProxyTest, ConnectViaProxy) { TEST(Socks5ProxyTest, ConnectViaProxy) {
constexpr std::uint16_t kSocksPort = 1080;
constexpr std::uint16_t kAvailableUdpBufferSpaceForSocks = 262;
boost::asio::io_context io; boost::asio::io_context io;
auto executor = io.get_executor(); auto executor = io.get_executor();
larra::xmpp::impl::MockSocket socket{executor}; larra::xmpp::impl::MockSocket socket{executor};
// expected server responses std::string expectedServerResponse;
std::string serverResponse; expectedServerResponse += "\x05\x00"; // VER, METHOD
serverResponse += "\x05\x00"; // VER, METHOD expectedServerResponse += "\x05\x00\x00\x01"; // VER, REP, RSV, ATYP (IPv4)
serverResponse += "\x05\x00\x00\x01"; // VER, REP, RSV, ATYP (IPv4) expectedServerResponse += "\x7F\x00\x00\x01"; // BND.ADDR (127.0.0.1)
serverResponse += "\x7F\x00\x00\x01"; // BND.ADDR (127.0.0.1) expectedServerResponse += "\x1F\x90"; // BND.PORT (8080)
serverResponse += "\x1F\x90"; // BND.PORT (8080)
socket.AddReceivedData(serverResponse); socket.AddReceivedData(expectedServerResponse);
Socks5Proxy proxy{"proxy.example.com", 1080}; Socks5Proxy proxy{.hostname = "proxy.example.com", .port = kSocksPort};
std::string targetHostname = "target.example.com"; std::string targetHostname = "target.example.com";
std::uint16_t targetPort = 80; std::uint16_t targetPort = 80;
@ -77,11 +79,9 @@ TEST(Socks5ProxyTest, ConnectViaProxy) {
auto sentData = socket.GetSentData(); auto sentData = socket.GetSentData();
// Expected client greeting
std::string expectedGreeting = "\x05\x01\x00"; std::string expectedGreeting = "\x05\x01\x00";
// Expected CONNECT request std::array<std::uint8_t, kAvailableUdpBufferSpaceForSocks> expectedRequest{};
std::array<std::uint8_t, 262> expectedRequest{};
std::size_t req_len = 0; std::size_t req_len = 0;
expectedRequest[req_len++] = 0x05; // VER expectedRequest[req_len++] = 0x05; // VER
@ -111,88 +111,4 @@ TEST(Socks5ProxyTest, ConnectViaProxy) {
boost::asio::detached); boost::asio::detached);
io.run(); io.run();
} }
/*
// Test 3: Connect via system configured proxy
TEST_F(ProxyTest, ConnectViaSystemProxy_HttpProxy) {
// Set the environment variable for the system proxy
const char* original_http_proxy = std::getenv("http_proxy");
setenv("http_proxy", "http://proxy_host:8080", 1);
std::string target_host = "target_host";
uint16_t target_port = 80;
std::string expected_request = std::format(
"CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\n\r\n",
target_host, target_port, target_host, target_port);
std::string proxy_response = "HTTP/1.1 200 Connection established\r\n\r\n";
mock_socket.AddReceivedData(proxy_response);
bool connect_successful = false;
asio::co_spawn(io_context, [&]() -> asio::awaitable<void> {
try {
SystemConfiguredProxy proxy;
co_await client::impl::ConnectToServer(mock_socket, proxy, target_host, target_port);
connect_successful = true;
} catch (...) {
connect_successful = false;
}
}, asio::detached);
io_context.run();
std::string sent_data = mock_socket.GetSentData();
EXPECT_EQ(sent_data, expected_request);
EXPECT_TRUE(connect_successful);
// Restore the original environment variable
if (original_http_proxy) {
setenv("http_proxy", original_http_proxy, 1);
} else {
unsetenv("http_proxy");
}
}
// Test 4: Incorrect proxy data
TEST_F(ProxyTest, ConnectViaHttpProxy_IncorrectData) {
HttpProxy proxy{"proxy_host", 8080};
std::string target_host = "target_host";
uint16_t target_port = 80;
std::string expected_request = std::format(
"CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\n\r\n",
target_host, target_port, target_host, target_port);
// Simulate an incorrect response from the proxy server
std::string proxy_response = "HTTP/1.1 400 Bad Request\r\n\r\n";
mock_socket.AddReceivedData(proxy_response);
bool connect_successful = false;
std::string error_message;
asio::co_spawn(io_context, [&]() -> asio::awaitable<void> {
try {
co_await client::impl::ConnectToServer(mock_socket, proxy, target_host, target_port);
connect_successful = true;
} catch (const std::runtime_error& e) {
connect_successful = false;
error_message = e.what();
}
}, asio::detached);
io_context.run();
std::string sent_data = mock_socket.GetSentData();
EXPECT_EQ(sent_data, expected_request);
EXPECT_FALSE(connect_successful);
EXPECT_FALSE(error_message.empty());
}
*/