WIP: proxy_support #3
1 changed files with 12 additions and 96 deletions
106
tests/proxy.cpp
106
tests/proxy.cpp
|
@ -52,21 +52,23 @@ TEST_F(ProxyTest, ConnectViaHttpProxy_SuccessfulResponse) {
|
|||
|
||||
// Test 2: Connect via SOCKS proxy
|
||||
TEST(Socks5ProxyTest, ConnectViaProxy) {
|
||||
constexpr std::uint16_t kSocksPort = 1080;
|
||||
constexpr std::uint16_t kAvailableUdpBufferSpaceForSocks = 262;
|
||||
|
||||
boost::asio::io_context io;
|
||||
auto executor = io.get_executor();
|
||||
|
||||
larra::xmpp::impl::MockSocket socket{executor};
|
||||
|
||||
// expected server responses
|
||||
std::string serverResponse;
|
||||
serverResponse += "\x05\x00"; // VER, METHOD
|
||||
serverResponse += "\x05\x00\x00\x01"; // VER, REP, RSV, ATYP (IPv4)
|
||||
serverResponse += "\x7F\x00\x00\x01"; // BND.ADDR (127.0.0.1)
|
||||
serverResponse += "\x1F\x90"; // BND.PORT (8080)
|
||||
std::string expectedServerResponse;
|
||||
expectedServerResponse += "\x05\x00"; // VER, METHOD
|
||||
expectedServerResponse += "\x05\x00\x00\x01"; // VER, REP, RSV, ATYP (IPv4)
|
||||
expectedServerResponse += "\x7F\x00\x00\x01"; // BND.ADDR (127.0.0.1)
|
||||
expectedServerResponse += "\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::uint16_t targetPort = 80;
|
||||
|
||||
|
@ -77,11 +79,9 @@ TEST(Socks5ProxyTest, ConnectViaProxy) {
|
|||
|
||||
auto sentData = socket.GetSentData();
|
||||
|
||||
// Expected client greeting
|
||||
std::string expectedGreeting = "\x05\x01\x00";
|
||||
|
||||
// Expected CONNECT request
|
||||
std::array<std::uint8_t, 262> expectedRequest{};
|
||||
std::array<std::uint8_t, kAvailableUdpBufferSpaceForSocks> expectedRequest{};
|
||||
std::size_t req_len = 0;
|
||||
|
||||
expectedRequest[req_len++] = 0x05; // VER
|
||||
|
@ -112,87 +112,3 @@ TEST(Socks5ProxyTest, ConnectViaProxy) {
|
|||
|
||||
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());
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue