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 a1bc442
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
socks5:
image: "xkuma/socks5"
ports:
- 1080:1080
- 8080:1080
env:
PROXY_USER: user
PROXY_PASSWORD: password
Expand Down
18 changes: 18 additions & 0 deletions core-tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.4'
services:
httpbin:
container_name: "httpbin"
image: "kennethreitz/httpbin"
tinyproxy:
container_name: "tinyproxy"
image: "vimagick/tinyproxy"
socks5:
container_name: "socks5"
image: "xkuma/socks5"
ports:
- "8080:1080"
environment:
- PROXY_USER=user
- PROXY_PASSWORD=password
- PROXY_SERVER=0.0.0.0:1080

42 changes: 34 additions & 8 deletions core-tests/include/test_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,47 @@
#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_HTTP_PROXY_PORT 8888
#define TEST_JPG_MD5SUM "64a42b4c0f3c65a14c23b60d3880a917"

#define TEST_HTTP_PROXY_HOST "127.0.0.1"
#define TEST_HTTP_PROXY_PORT 8888
#define TEST_HTTP_PROXY_USER "user"
#define TEST_HTTP_PROXY_PASSWORD "password"

#define TEST_SOCKS5_PROXY_HOST "127.0.0.1"
#define TEST_SOCKS5_PROXY_PORT 8080
#define TEST_SOCKS5_PROXY_USER "user"
#define TEST_SOCKS5_PROXY_PASSWORD "password"

#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_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
8 changes: 4 additions & 4 deletions core-tests/src/coroutine/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ TEST(coroutine_http_server, get) {

thread t1([&lock]() {
lock.lock();
Client cli(TEST_HOST, 8080);
Client cli(TEST_HOST, TEST_PORT);
auto resp1 = cli.Get("/hi");
EXPECT_EQ(resp1->status, 200);
EXPECT_EQ(resp1->body, string("Hello World!"));
Expand All @@ -53,7 +53,7 @@ TEST(coroutine_http_server, get) {

svr.BeforeListen([&lock]() { lock.unlock(); });

ASSERT_TRUE(svr.listen(TEST_HOST, 8080));
ASSERT_TRUE(svr.listen(TEST_HOST, TEST_PORT));
});

t1.join();
Expand All @@ -67,7 +67,7 @@ TEST(coroutine_http_server, post) {
std::thread t1([&lock]() {
lock.lock();

Client cli(TEST_HOST, 8080);
Client cli(TEST_HOST, TEST_PORT);

httplib::Params params;
params.emplace("name", "john");
Expand All @@ -92,7 +92,7 @@ TEST(coroutine_http_server, post) {

svr.BeforeListen([&lock]() { lock.unlock(); });

svr.listen(TEST_HOST, 8080);
svr.listen(TEST_HOST, TEST_PORT);
});

t1.join();
Expand Down
80 changes: 44 additions & 36 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 @@ -921,22 +923,22 @@ void test_sendto_recvfrom(enum swSocketType sock_type) {

Socket sock_server(sock_type);
Socket sock_client(sock_type);
sock_server.bind(ip, 8080);
sock_client.bind(ip, 8081);
sock_server.bind(ip, TEST_PORT);
sock_client.bind(ip, TEST_PORT + 1);

ON_SCOPE_EXIT {
sock_server.close();
sock_client.close();
};

sock_server.sendto(ip, 8081, (const void *) server_text.c_str(), server_length);
sock_server.sendto(ip, TEST_PORT + 1, (const void *) server_text.c_str(), server_length);

char data_from_server[128] = {};
struct sockaddr_in serveraddr;
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(ip);
serveraddr.sin_port = htons(8080);
serveraddr.sin_port = htons(TEST_PORT);
socklen_t addr_length = sizeof(serveraddr);

// receive data from server
Expand All @@ -948,7 +950,7 @@ void test_sendto_recvfrom(enum swSocketType sock_type) {

// receive data from client
char data_from_client[128] = {};
sock_client.sendto(ip, 8080, (const void *) client_text.c_str(), client_length);
sock_client.sendto(ip, TEST_PORT, (const void *) client_text.c_str(), client_length);
result = sock_server.recvfrom(data_from_client, client_length);
data_from_client[client_length] = '\0';
ASSERT_EQ(result, client_length);
Expand All @@ -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(TEST_SOCKS5_PROXY_HOST);
socks5_proxy->port = TEST_SOCKS5_PROXY_PORT;
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
Loading

0 comments on commit a1bc442

Please sign in to comment.