Skip to content

Commit

Permalink
Optimize core tests (#5555)
Browse files Browse the repository at this point in the history
* Optimize http_proxy/socks5_proxy core tests --filter=[core]

* debug --filter=[core]

* debug 2 --filter=[core]

* debug 3 --filter=[core]

* debug 4 --filter=[core]

* debug 5 --filter=[core]

* swoole::test::get_random_port() --filter=[core]
  • Loading branch information
matyhtf authored Nov 4, 2024
1 parent 72886f2 commit 683aa7c
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 136 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

44 changes: 36 additions & 8 deletions core-tests/include/test_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,49 @@
#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 TEST_DOMAIN_BAIDU "www.baidu.com"

#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_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();
}};

int get_random_port();

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

} // namespace test
}; // namespace swoole
2 changes: 1 addition & 1 deletion core-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ for task in $tasks; do
if [ $task = "log." ]; then
$execute_command --gtest_filter=$task*
else
sudo $execute_command --gtest_filter=$task*
sudo -E $execute_command --gtest_filter=$task*
fi

if [ $? -ne 0 ] && [ "${GITHUB_ACTIONS}" = true ]; then
Expand Down
3 changes: 1 addition & 2 deletions core-tests/src/core/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ TEST(log, rotation) {
}

TEST(log, redirect) {
char *p = getenv("GITHUB_ACTIONS");
if (p) {
if (test::is_github_ci()) {
return;
}
sw_logger()->reset();
Expand Down
18 changes: 10 additions & 8 deletions core-tests/src/coroutine/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ using namespace std;
TEST(coroutine_http_server, get) {
Server svr;
mutex lock;
int port = swoole::test::get_random_port();
lock.lock();

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

coroutine::run([&lock, &svr](void *arg) {
coroutine::run([&lock, &svr, port](void *arg) {
svr.Get("/hi", [](const Request &req, Response &res) { res.set_content("Hello World!", "text/plain"); });

svr.Get("/stop", [&svr](const Request &req, Response &res) {
Expand All @@ -53,7 +54,7 @@ TEST(coroutine_http_server, get) {

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

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

t1.join();
Expand All @@ -62,12 +63,13 @@ TEST(coroutine_http_server, get) {
TEST(coroutine_http_server, post) {
Server svr;
mutex lock;
int port = swoole::test::get_random_port();
lock.lock();

std::thread t1([&lock]() {
std::thread t1([&lock, port]() {
lock.lock();

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

httplib::Params params;
params.emplace("name", "john");
Expand All @@ -82,7 +84,7 @@ TEST(coroutine_http_server, post) {
EXPECT_EQ(resp2->body, string("Stop Server!"));
});

coroutine::run([&lock, &svr](void *arg) {
coroutine::run([&lock, &svr, port](void *arg) {
svr.Get("/stop", [&svr](const Request &req, Response &res) {
res.set_content("Stop Server!", "text/plain");
svr.stop();
Expand All @@ -92,7 +94,7 @@ TEST(coroutine_http_server, post) {

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

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

t1.join();
Expand Down
100 changes: 56 additions & 44 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 @@ -66,13 +68,14 @@ TEST(coroutine_socket, connect_with_dns) {

TEST(coroutine_socket, recv_success) {
pid_t pid;
int port = swoole::test::get_random_port();

Process proc([](Process *proc) {
Process proc([port](Process *proc) {
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) {
SERVER_THIS->send(req->info.fd, req->data, req->info.len);
};

Server serv(TEST_HOST, TEST_PORT, swoole::Server::MODE_BASE, SW_SOCK_TCP);
Server serv(TEST_HOST, port, swoole::Server::MODE_BASE, SW_SOCK_TCP);
serv.on("onReceive", (void *) receive_fn);
serv.start();
});
Expand All @@ -81,9 +84,9 @@ TEST(coroutine_socket, recv_success) {

sleep(1); // wait for the test server to start

coroutine::run([](void *arg) {
coroutine::run([port](void *arg) {
Socket sock(SW_SOCK_TCP);
bool retval = sock.connect(TEST_HOST, TEST_PORT, -1);
bool retval = sock.connect(TEST_HOST, port, -1);
ASSERT_EQ(retval, true);
ASSERT_EQ(sock.errCode, 0);
sock.send(SW_STRS("hello world\n"));
Expand All @@ -100,11 +103,12 @@ TEST(coroutine_socket, recv_success) {

TEST(coroutine_socket, recv_fail) {
pid_t pid;
int port = swoole::test::get_random_port();

Process proc([](Process *proc) {
Process proc([port](Process *proc) {
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) { SERVER_THIS->close(req->info.fd, 0); };

Server serv(TEST_HOST, TEST_PORT, swoole::Server::MODE_BASE, SW_SOCK_TCP);
Server serv(TEST_HOST, port, swoole::Server::MODE_BASE, SW_SOCK_TCP);
serv.on("onReceive", (void *) receive_fn);
serv.start();
});
Expand All @@ -113,9 +117,9 @@ TEST(coroutine_socket, recv_fail) {

sleep(1); // wait for the test server to start

coroutine::run([](void *arg) {
coroutine::run([port](void *arg) {
Socket sock(SW_SOCK_TCP);
bool retval = sock.connect(TEST_HOST, TEST_PORT, -1);
bool retval = sock.connect(TEST_HOST, port, -1);
ASSERT_EQ(retval, true);
ASSERT_EQ(sock.errCode, 0);
sock.send("close", 6);
Expand Down Expand Up @@ -919,24 +923,26 @@ void test_sendto_recvfrom(enum swSocketType sock_type) {

const char *ip = sock_type == SW_SOCK_UDP ? "127.0.0.1" : "::1";

int port = swoole::test::get_random_port();

Socket sock_server(sock_type);
Socket sock_client(sock_type);
sock_server.bind(ip, 8080);
sock_client.bind(ip, 8081);
sock_server.bind(ip, port);
sock_client.bind(ip, 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, 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(port);
socklen_t addr_length = sizeof(serveraddr);

// receive data from server
Expand All @@ -948,7 +954,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, 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 +967,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);
});
}

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);
});
}

socket_send(sock, 80);
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 +1041,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
Loading

0 comments on commit 683aa7c

Please sign in to comment.