Skip to content

Commit

Permalink
Optimize http_proxy/socks5_proxy core tests --filter=[core]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Nov 4, 2024
1 parent 72886f2 commit 8098b79
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 73 deletions.
38 changes: 31 additions & 7 deletions core-tests/include/test_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,45 @@
#include <unordered_map>
#include <fstream>


#define TEST_HOST "127.0.0.1"
#define TEST_PORT 9501
#define TEST_TMP_FILE "/tmp/swoole_core_test_file"
#define TEST_TMP_DIR "/tmp/swoole_core_test_dir"
#define TEST_TMP_DIR "/tmp/swoole_core_test_dir"
#define TEST_JPG_FILE "/examples/test.jpg"
#define TEST_JPG_MD5SUM "64a42b4c0f3c65a14c23b60d3880a917"
#define TEST_JPG_MD5SUM "64a42b4c0f3c65a14c23b60d3880a917"
#define TEST_HTTP_PROXY_PORT 8888
#define TEST_HTTP_PROXY_HOST "127.0.0.1"

#define ASSERT_MEMEQ(x,y,n) ASSERT_EQ(memcmp((x), (y), n), 0)
#define EXPECT_MEMEQ(x,y,n) EXPECT_EQ(memcmp((x), (y), n), 0)
#define TEST_SOCKS5_PROXY_USER "user"
#define TEST_SOCKS5_PROXY_PASSWORD "password"

#define TEST_HTTP_PROXY_USER "user"
#define TEST_HTTP_PROXY_PASSWORD "password"

#define TEST_DOMAIN_BAIDU "www.baidu.com"

#define TEST_REQUEST_BAIDU \
"GET / HTTP/1.1\r\n" \
"Host: www.baidu.com\r\n" \
"Connection: close\r\n" \
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/51.0.2704.106 Safari/537.36" \
"\r\n\r\n"

#define ASSERT_MEMEQ(x, y, n) ASSERT_EQ(memcmp((x), (y), n), 0)
#define EXPECT_MEMEQ(x, y, n) EXPECT_EQ(memcmp((x), (y), n), 0)

namespace swoole {
struct HttpProxy;
struct Socks5Proxy;
namespace test {

namespace swoole { namespace test {
const std::string &get_root_path();
std::string get_jpg_file();
bool is_github_ci();
}};

Socks5Proxy *create_socks5_proxy();
HttpProxy *create_http_proxy();

} // namespace test
}; // namespace swoole
70 changes: 39 additions & 31 deletions core-tests/src/coroutine/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using swoole::coroutine::System;
using swoole::network::Address;
using swoole::network::IOVector;
using swoole::test::Server;
using swoole::test::create_http_proxy;
using swoole::test::create_socks5_proxy;

const std::string host = "www.baidu.com";

Expand Down Expand Up @@ -961,62 +963,68 @@ TEST(coroutine_socket, sendto_recvfrom_udp) {
test_sendto_recvfrom(SW_SOCK_UDP6);
}

void socket_send(Socket &sock, int port) {
bool retval = sock.connect(host, port);
void proxy_test(Socket &sock, bool https) {
if (https) {
sock.enable_ssl_encrypt();
}

bool retval = sock.connect(host, https ? 443 : 80);
ON_SCOPE_EXIT {
sock.close();
};
ASSERT_EQ(retval, true);

if (443 == port) {
if (https) {
ASSERT_NE(sock.ssl_get_peer_cert(), "");
}

sock.send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows NT "
"10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36\r\n\r\n");
ASSERT_GT(sock.send(SW_STRL(TEST_REQUEST_BAIDU)), 0);

char buf[65536];
ssize_t result = 0;
ssize_t recv_total = 0;
String buf(65536);
while (true) {
result = sock.recv(buf + recv_total, 65536 - recv_total);
if (0 == result) {
char rbuf[4096];
ssize_t nr = sock.recv(rbuf, sizeof(rbuf));
if (nr <= 0) {
break;
}
recv_total += result;
buf.append(rbuf, nr);
}
std::string content(buf);
ASSERT_NE(content.find("baidu"), std::string::npos);
ASSERT_TRUE(buf.contains("www.baidu.com"));
}

TEST(coroutine_socket, socks5_proxy) {
TEST(coroutine_socket, http_get_with_socks5_proxy) {
coroutine::run([](void *arg) {
Socket sock(SW_SOCK_TCP);
sock.socks5_proxy = new Socks5Proxy();
sock.socks5_proxy->host = std::string("127.0.0.1");
sock.socks5_proxy->port = 1080;
sock.socks5_proxy->dns_tunnel = 1;
sock.socks5_proxy->method = 0x02;
sock.socks5_proxy->username = std::string("user");
sock.socks5_proxy->password = std::string("password");

socket_send(sock, 80);
sock.socks5_proxy = create_socks5_proxy();
proxy_test(sock, false);
});
}

TEST(coroutine_socket, http_proxy) {
TEST(coroutine_socket, http_get_with_http_proxy) {
coroutine::run([&](void *arg) {
Socket sock(SW_SOCK_TCP);
sock.http_proxy = new HttpProxy();
sock.http_proxy->proxy_host = std::string("127.0.0.1");
sock.http_proxy->proxy_port = 8888;
sock.http_proxy->username = std::string("user");
sock.http_proxy->password = std::string("password");
sock.http_proxy = create_http_proxy();
proxy_test(sock, false);
});
}

socket_send(sock, 80);
TEST(coroutine_socket, https_get_with_socks5_proxy) {
coroutine::run([](void *arg) {
Socket sock(SW_SOCK_TCP);
sock.socks5_proxy = create_socks5_proxy();
proxy_test(sock, true);
});
}

TEST(coroutine_socket, https_get_with_http_proxy) {
coroutine::run([&](void *arg) {
Socket sock(SW_SOCK_TCP);
sock.http_proxy = create_http_proxy();
proxy_test(sock, true);
});
}


#ifdef SW_USE_OPENSSL
TEST(coroutine_socket, ssl) {
coroutine::run([&](void *arg) {
Expand All @@ -1029,7 +1037,7 @@ TEST(coroutine_socket, ssl) {
sock.get_ssl_context()->allow_self_signed = true;
sock.get_ssl_context()->cafile = swoole::test::get_root_path() + "/tests/include/ssl_certs/ca.crt";

socket_send(sock, 443);
proxy_test(sock, 443);
});
}
#endif
Expand Down
26 changes: 26 additions & 0 deletions core-tests/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "test_core.h"

#include "swoole_proxy.h"

using namespace swoole;
using namespace std;

Expand Down Expand Up @@ -56,5 +58,29 @@ bool is_github_ci() {
return getenv("GITHUB_ACTIONS") != nullptr;
}

Socks5Proxy *create_socks5_proxy() {
auto socks5_proxy = new Socks5Proxy();
socks5_proxy->host = std::string("127.0.0.1");
socks5_proxy->port = 1080;
socks5_proxy->dns_tunnel = 1;
if (is_github_ci()) {
socks5_proxy->method = SW_SOCKS5_METHOD_AUTH;
socks5_proxy->username = std::string(TEST_SOCKS5_PROXY_USER);
socks5_proxy->password = std::string(TEST_SOCKS5_PROXY_PASSWORD);
}
return socks5_proxy;
}

HttpProxy *create_http_proxy() {
auto http_proxy = new HttpProxy();
http_proxy->proxy_host = std::string(TEST_HTTP_PROXY_HOST);
http_proxy->proxy_port = TEST_HTTP_PROXY_PORT;
if (is_github_ci()) {
http_proxy->username = std::string(TEST_HTTP_PROXY_USER);
http_proxy->password = std::string(TEST_HTTP_PROXY_PASSWORD);
}
return http_proxy;
}

} // namespace test
} // namespace swoole
50 changes: 15 additions & 35 deletions core-tests/src/network/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
using swoole::HttpProxy;
using swoole::Pipe;
using swoole::Socks5Proxy;
using swoole::String;
using swoole::network::AsyncClient;
using swoole::network::Client;
using swoole::test::create_http_proxy;
using swoole::test::create_socks5_proxy;
using swoole::test::Process;
using swoole::test::Server;

Expand Down Expand Up @@ -218,36 +221,25 @@ TEST(client, shutdown_all) {

#ifdef SW_USE_OPENSSL

static const char *request_baidu = "GET / HTTP/1.1\r\n"
"Host: www.baidu.com\r\n"
"Connection: close\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/51.0.2704.106 Safari/537.36"
"\r\n\r\n";

static const char *domain_baidu = "www.baidu.com";

#define SOCKS5_WITH_AUTH 1

TEST(client, ssl_1) {
bool connected = false;
bool closed = false;
swoole::String buf(65536);
String buf(65536);

swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);

Client client(SW_SOCK_TCP, true);
client.enable_ssl_encrypt();
client.onConnect = [&connected](Client *cli) {
connected = true;
cli->send(cli, request_baidu, strlen(request_baidu), 0);
cli->send(cli, SW_STRL(TEST_REQUEST_BAIDU), 0);
};

client.onError = [](Client *cli) {};
client.onClose = [&closed](Client *cli) { closed = true; };
client.onReceive = [&buf](Client *cli, const char *data, size_t length) { buf.append(data, length); };

ASSERT_EQ(client.connect(&client, domain_baidu, 443, -1, 0), 0);
ASSERT_EQ(client.connect(&client, TEST_DOMAIN_BAIDU, 443, -1, 0), 0);

swoole_event_wait();

Expand All @@ -257,28 +249,26 @@ TEST(client, ssl_1) {
}

static void proxy_async_test(Client &client, bool https) {
int ret;

swoole_event_init(SW_EVENTLOOP_WAIT_EXIT);

bool connected = false;
bool closed = false;
swoole::String buf(65536);
String buf(65536);

if (https) {
client.enable_ssl_encrypt();
}

client.onConnect = [&connected](Client *cli) {
connected = true;
cli->send(cli, request_baidu, strlen(request_baidu), 0);
cli->send(cli, SW_STRL(TEST_REQUEST_BAIDU), 0);
};

client.onError = [](Client *cli) {};
client.onClose = [&closed](Client *cli) { closed = true; };
client.onReceive = [&buf](Client *cli, const char *data, size_t length) { buf.append(data, length); };

ASSERT_EQ(client.connect(&client, domain_baidu, https ? 443 : 80, -1, 0), 0);
ASSERT_EQ(client.connect(&client, TEST_DOMAIN_BAIDU, https ? 443 : 80, -1, 0), 0);

swoole_event_wait();

Expand All @@ -288,15 +278,15 @@ static void proxy_async_test(Client &client, bool https) {
}

static void proxy_sync_test(Client &client, bool https) {
swoole::String buf(65536);
String buf(65536);
if (https) {
client.enable_ssl_encrypt();
}

ASSERT_EQ(client.connect(&client, domain_baidu, https ? 443 : 80, -1, 0), 0);
ASSERT_GT(client.send(&client, request_baidu, strlen(request_baidu), 0), 0);
ASSERT_EQ(client.connect(&client, TEST_DOMAIN_BAIDU, https ? 443 : 80, -1, 0), 0);
ASSERT_GT(client.send(&client, SW_STRL(TEST_REQUEST_BAIDU), 0), 0);

while(true) {
while (true) {
char rbuf[4096];
auto nr = client.recv(&client, rbuf, sizeof(rbuf), 0);
if (nr <= 0) {
Expand All @@ -309,21 +299,11 @@ static void proxy_sync_test(Client &client, bool https) {
}

static void proxy_set_socks5_proxy(Client &client) {
client.socks5_proxy = new Socks5Proxy();
client.socks5_proxy->host = std::string("127.0.0.1");
client.socks5_proxy->port = 1080;
client.socks5_proxy->dns_tunnel = 1;
#if SOCKS5_WITH_AUTH
client.socks5_proxy->method = SW_SOCKS5_METHOD_AUTH;
client.socks5_proxy->username = std::string("user");
client.socks5_proxy->password = std::string("password");
#endif
client.socks5_proxy = create_socks5_proxy();
}

static void proxy_set_http_proxy(Client &client) {
client.http_proxy = new HttpProxy();
client.http_proxy->proxy_host = std::string(TEST_HTTP_PROXY_HOST);
client.http_proxy->proxy_port = TEST_HTTP_PROXY_PORT;
client.http_proxy = create_http_proxy();
}

TEST(client, https_get_async_with_http_proxy) {
Expand Down

0 comments on commit 8098b79

Please sign in to comment.