2024-11-11 22:55:26 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <larra/client/client.hpp>
|
|
|
|
#include <larra/impl/mock_socket.hpp>
|
|
|
|
#include <larra/proxy.hpp>
|
|
|
|
|
|
|
|
using namespace larra::xmpp;
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
namespace asio = boost::asio;
|
|
|
|
|
|
|
|
class ProxyTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
boost::asio::io_context io_context;
|
|
|
|
larra::xmpp::impl::MockSocket mock_socket{io_context.get_executor()};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test 1: Connect via HTTP proxy with successful server response
|
|
|
|
TEST_F(ProxyTest, ConnectViaHttpProxy_SuccessfulResponse) {
|
|
|
|
HttpProxy proxy{"proxy_host", 8080};
|
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string targetHost = "target_host";
|
|
|
|
uint16_t targetPort = 80;
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string expectedRequest =
|
|
|
|
std::format("CONNECT {}:{} HTTP/1.1\r\nHost: {}:{}\r\n\r\n", targetHost, targetPort, targetHost, targetPort);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string proxyResponse = "HTTP/1.1 200 Connection established\r\n\r\n";
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
mock_socket.AddReceivedData(proxyResponse);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
bool connectSuccessful = false;
|
2024-11-11 22:55:26 +00:00
|
|
|
|
|
|
|
asio::co_spawn(
|
|
|
|
io_context,
|
|
|
|
[&]() -> asio::awaitable<void> {
|
|
|
|
try {
|
2024-11-18 21:07:30 +00:00
|
|
|
co_await client::impl::ConnectViaProxy(mock_socket, proxy, targetHost, targetPort);
|
|
|
|
connectSuccessful = true;
|
2024-11-11 22:55:26 +00:00
|
|
|
} catch(...) {
|
2024-11-18 21:07:30 +00:00
|
|
|
connectSuccessful = false;
|
2024-11-11 22:55:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
asio::detached);
|
|
|
|
|
|
|
|
io_context.run();
|
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string sentData = mock_socket.GetSentData();
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
EXPECT_EQ(sentData, expectedRequest);
|
|
|
|
EXPECT_TRUE(connectSuccessful);
|
2024-11-11 22:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test 2: Connect via SOCKS proxy
|
|
|
|
TEST(Socks5ProxyTest, ConnectViaProxy) {
|
2024-11-21 15:46:25 +00:00
|
|
|
constexpr std::uint16_t kSocksPort = 1080;
|
|
|
|
constexpr std::uint16_t kAvailableUdpBufferSpaceForSocks = 262;
|
|
|
|
|
2024-11-11 22:55:26 +00:00
|
|
|
boost::asio::io_context io;
|
|
|
|
auto executor = io.get_executor();
|
|
|
|
|
|
|
|
larra::xmpp::impl::MockSocket socket{executor};
|
|
|
|
|
2024-11-21 15:46:25 +00:00
|
|
|
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)
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-21 15:46:25 +00:00
|
|
|
socket.AddReceivedData(expectedServerResponse);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-21 15:46:25 +00:00
|
|
|
Socks5Proxy proxy{.hostname = "proxy.example.com", .port = kSocksPort};
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string targetHostname = "target.example.com";
|
|
|
|
std::uint16_t targetPort = 80;
|
2024-11-11 22:55:26 +00:00
|
|
|
|
|
|
|
boost::asio::co_spawn(
|
|
|
|
executor,
|
|
|
|
[&]() -> boost::asio::awaitable<void> {
|
2024-11-18 21:07:30 +00:00
|
|
|
co_await client::impl::ConnectViaProxy(socket, proxy, targetHostname, targetPort);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
auto sentData = socket.GetSentData();
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string expectedGreeting = "\x05\x01\x00";
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-21 15:46:25 +00:00
|
|
|
std::array<std::uint8_t, kAvailableUdpBufferSpaceForSocks> expectedRequest{};
|
2024-11-11 22:55:26 +00:00
|
|
|
std::size_t req_len = 0;
|
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
expectedRequest[req_len++] = 0x05; // VER
|
|
|
|
expectedRequest[req_len++] = 0x01; // CMD: CONNECT
|
|
|
|
expectedRequest[req_len++] = 0x00; // RSV
|
|
|
|
expectedRequest[req_len++] = 0x03; // ATYP: DOMAINNAME
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
expectedRequest[req_len++] = static_cast<std::uint8_t>(targetHostname.size()); // domain length
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::memcpy(&expectedRequest[req_len], targetHostname.data(), targetHostname.size());
|
|
|
|
req_len += targetHostname.size();
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::uint16_t networkOrderPort = htons(targetPort);
|
|
|
|
expectedRequest[req_len++] = static_cast<std::uint8_t>((networkOrderPort >> 8) & 0xFF);
|
|
|
|
expectedRequest[req_len++] = static_cast<std::uint8_t>(networkOrderPort & 0xFF);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
std::string expectedData = expectedGreeting;
|
|
|
|
auto transformed_view = expectedRequest | std::views::take(req_len) | std::views::transform([](std::uint8_t byte) {
|
2024-11-12 23:03:30 +00:00
|
|
|
return static_cast<char>(byte);
|
|
|
|
});
|
2024-11-11 22:55:26 +00:00
|
|
|
|
2024-11-18 21:07:30 +00:00
|
|
|
expectedData.append(std::ranges::to<std::string>(transformed_view));
|
|
|
|
EXPECT_EQ(sentData, expectedData);
|
2024-11-11 22:55:26 +00:00
|
|
|
|
|
|
|
co_return;
|
|
|
|
},
|
|
|
|
boost::asio::detached);
|
|
|
|
|
|
|
|
io.run();
|
2024-11-21 15:46:25 +00:00
|
|
|
}
|