From 683aa7c65b4b9eaa7cdffc77fede34acdfb86e34 Mon Sep 17 00:00:00 2001 From: "Tianfeng.Han" Date: Mon, 4 Nov 2024 19:27:43 +0800 Subject: [PATCH] Optimize core tests (#5555) * 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] --- .github/workflows/core.yml | 2 +- core-tests/docker-compose.yml | 18 ++++ core-tests/include/test_core.h | 44 ++++++++-- core-tests/run.sh | 2 +- core-tests/src/core/log.cpp | 3 +- core-tests/src/coroutine/http_server.cpp | 18 ++-- core-tests/src/coroutine/socket.cpp | 100 +++++++++++++---------- core-tests/src/main.cpp | 30 +++++++ core-tests/src/network/client.cpp | 77 +++++++---------- core-tests/src/network/dns.cpp | 5 +- core-tests/src/network/socket.cpp | 36 ++++---- core-tests/src/network/stream.cpp | 5 +- core-tests/src/os/process_pool.cpp | 6 +- core-tests/src/server/server.cpp | 5 +- tests/ubuntu.sh | 1 + 15 files changed, 216 insertions(+), 136 deletions(-) create mode 100755 core-tests/docker-compose.yml create mode 100644 tests/ubuntu.sh diff --git a/.github/workflows/core.yml b/.github/workflows/core.yml index 04d55f2d9d8..8561b3c0bf5 100644 --- a/.github/workflows/core.yml +++ b/.github/workflows/core.yml @@ -17,7 +17,7 @@ jobs: socks5: image: "xkuma/socks5" ports: - - 1080:1080 + - 8080:1080 env: PROXY_USER: user PROXY_PASSWORD: password diff --git a/core-tests/docker-compose.yml b/core-tests/docker-compose.yml new file mode 100755 index 00000000000..4188b9de5f6 --- /dev/null +++ b/core-tests/docker-compose.yml @@ -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 + diff --git a/core-tests/include/test_core.h b/core-tests/include/test_core.h index c56deb9ee55..21feb2da8b6 100644 --- a/core-tests/include/test_core.h +++ b/core-tests/include/test_core.h @@ -16,21 +16,49 @@ #include #include - #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 diff --git a/core-tests/run.sh b/core-tests/run.sh index a6acf64e104..eac533dc4d8 100755 --- a/core-tests/run.sh +++ b/core-tests/run.sh @@ -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 diff --git a/core-tests/src/core/log.cpp b/core-tests/src/core/log.cpp index 63a7d472464..af76a7483cb 100644 --- a/core-tests/src/core/log.cpp +++ b/core-tests/src/core/log.cpp @@ -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(); diff --git a/core-tests/src/coroutine/http_server.cpp b/core-tests/src/coroutine/http_server.cpp index 68ddae7642b..373d2d8afb1 100644 --- a/core-tests/src/coroutine/http_server.cpp +++ b/core-tests/src/coroutine/http_server.cpp @@ -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!")); @@ -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) { @@ -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(); @@ -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"); @@ -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(); @@ -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(); diff --git a/core-tests/src/coroutine/socket.cpp b/core-tests/src/coroutine/socket.cpp index 173e7fa709d..a66f18780e9 100644 --- a/core-tests/src/coroutine/socket.cpp +++ b/core-tests/src/coroutine/socket.cpp @@ -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"; @@ -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(); }); @@ -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")); @@ -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(); }); @@ -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); @@ -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 @@ -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); @@ -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) { @@ -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 diff --git a/core-tests/src/main.cpp b/core-tests/src/main.cpp index 4cd1757cdc9..05f8a5c6b36 100644 --- a/core-tests/src/main.cpp +++ b/core-tests/src/main.cpp @@ -1,5 +1,7 @@ #include "test_core.h" +#include "swoole_proxy.h" + using namespace swoole; using namespace std; @@ -56,5 +58,33 @@ 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; +} + +int get_random_port() { + return TEST_PORT + swoole_system_random(1, 10000); +} + } // namespace test } // namespace swoole diff --git a/core-tests/src/network/client.cpp b/core-tests/src/network/client.cpp index 121720a426f..acbee375417 100644 --- a/core-tests/src/network/client.cpp +++ b/core-tests/src/network/client.cpp @@ -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; @@ -18,13 +21,14 @@ TEST(client, tcp) { char buf[128]; 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(); }); @@ -35,7 +39,7 @@ TEST(client, tcp) { Client cli(SW_SOCK_TCP, false); ASSERT_NE(cli.socket, nullptr); - ret = cli.connect(&cli, TEST_HOST, TEST_PORT, -1, 0); + ret = cli.connect(&cli, TEST_HOST, port, -1, 0); ASSERT_EQ(ret, 0); ret = cli.send(&cli, SW_STRS(GREETER), 0); ASSERT_GT(ret, 0); @@ -51,16 +55,16 @@ TEST(client, tcp) { TEST(client, udp) { int ret; char buf[128]; - + int port = swoole::test::get_random_port(); pid_t pid; - Process proc([](Process *proc) { + Process proc([port](Process *proc) { on_packet_lambda_type packet_fn = [](ON_PACKET_PARAMS) { swoole::DgramPacket *packet = (swoole::DgramPacket *) req->data; SERVER_THIS->sendto(packet->socket_addr, packet->data, packet->length, req->info.server_fd); }; - Server serv(TEST_HOST, TEST_PORT, swoole::Server::MODE_BASE, SW_SOCK_UDP); + Server serv(TEST_HOST, port, swoole::Server::MODE_BASE, SW_SOCK_UDP); serv.on("onPacket", (void *) packet_fn); serv.start(); }); @@ -71,7 +75,7 @@ TEST(client, udp) { Client cli(SW_SOCK_UDP, false); ASSERT_NE(cli.socket, nullptr); - ret = cli.connect(&cli, TEST_HOST, TEST_PORT, -1, 0); + ret = cli.connect(&cli, TEST_HOST, port, -1, 0); ASSERT_EQ(ret, 0); ret = cli.send(&cli, SW_STRS(GREETER), 0); ASSERT_GT(ret, 0); @@ -90,12 +94,12 @@ static void test_async_client_tcp(const char *host, int port) { Pipe p(true); ASSERT_TRUE(p.ready()); - Process proc([&p](Process *proc) { + Process proc([&p, 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.set_private_data("pipe", &p); @@ -147,17 +151,17 @@ static void test_async_client_tcp(const char *host, int port) { } TEST(client, async_tcp) { - test_async_client_tcp(TEST_HOST, TEST_PORT); + test_async_client_tcp(TEST_HOST, swoole::test::get_random_port()); } TEST(client, async_tcp_dns) { - test_async_client_tcp("localhost", TEST_PORT); + test_async_client_tcp("localhost", swoole::test::get_random_port()); } TEST(client, connect_refuse) { int ret; Client cli(SW_SOCK_TCP, false); - ret = cli.connect(&cli, TEST_HOST, TEST_PORT + 10001, -1, 0); + ret = cli.connect(&cli, TEST_HOST, swoole::test::get_random_port(), -1, 0); ASSERT_EQ(ret, -1); ASSERT_EQ(swoole_get_last_error(), ECONNREFUSED); } @@ -165,7 +169,7 @@ TEST(client, connect_refuse) { TEST(client, connect_timeout) { int ret; Client cli(SW_SOCK_TCP, false); - ret = cli.connect(&cli, "19.168.0.99", TEST_PORT + 10001, 0.2, 0); + ret = cli.connect(&cli, "19.168.0.99", swoole::test::get_random_port(), 0.2, 0); ASSERT_EQ(ret, -1); ASSERT_EQ(swoole_get_last_error(), ETIMEDOUT); } @@ -218,21 +222,10 @@ 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); @@ -240,14 +233,14 @@ TEST(client, ssl_1) { 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(); @@ -257,13 +250,11 @@ 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(); @@ -271,14 +262,14 @@ static void proxy_async_test(Client &client, bool https) { 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(); @@ -288,15 +279,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) { @@ -309,21 +300,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) { diff --git a/core-tests/src/network/dns.cpp b/core-tests/src/network/dns.cpp index cd4256c6d1b..f89746600c8 100644 --- a/core-tests/src/network/dns.cpp +++ b/core-tests/src/network/dns.cpp @@ -90,17 +90,18 @@ TEST(dns, load_resolv_conf) { // reset SwooleG.dns_server_host = ""; SwooleG.dns_server_port = 0; + int port = swoole::test::get_random_port(); auto dns_server = swoole_get_dns_server(); ASSERT_TRUE(dns_server.first.empty()); ASSERT_EQ(dns_server.second, 0); // with port - std::string test_server = "127.0.0.1:8080"; // fake dns server + std::string test_server = "127.0.0.1:" + std::to_string(port); // fake dns server swoole_set_dns_server(test_server); dns_server = swoole_get_dns_server(); ASSERT_STREQ(dns_server.first.c_str(), "127.0.0.1"); - ASSERT_EQ(dns_server.second, 8080); + ASSERT_EQ(dns_server.second, port); // invalid port test_server = "127.0.0.1:808088"; diff --git a/core-tests/src/network/socket.cpp b/core-tests/src/network/socket.cpp index 333c2d69c97..f4b656eb96e 100644 --- a/core-tests/src/network/socket.cpp +++ b/core-tests/src/network/socket.cpp @@ -89,9 +89,10 @@ TEST(socket, sendto_ipv6) { TEST(socket, recv) { mutex m; m.lock(); + int port = swoole::test::get_random_port(); - thread t1([&m]() { - auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + thread t1([&m, port]() { + auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, port); char buf[1024] = {}; svr->set_block(); m.unlock(); @@ -103,10 +104,10 @@ TEST(socket, recv) { svr->free(); }); - thread t2([&m]() { + thread t2([&m, port]() { m.lock(); auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0); - ASSERT_EQ(cli->connect(TEST_HOST, TEST_PORT), SW_OK); + ASSERT_EQ(cli->connect(TEST_HOST, port), SW_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); cli->send(test_data, sizeof(test_data), 0); cli->free(); @@ -119,9 +120,10 @@ TEST(socket, recv) { TEST(socket, recvfrom_blocking) { mutex m; m.lock(); + int port = swoole::test::get_random_port(); - thread t1([&m]() { - auto svr = make_server_socket(SW_SOCK_UDP, TEST_HOST, TEST_PORT); + thread t1([&m, port]() { + auto svr = make_server_socket(SW_SOCK_UDP, TEST_HOST, port); network::Address addr; char buf[1024] = {}; svr->set_nonblock(); @@ -131,11 +133,11 @@ TEST(socket, recvfrom_blocking) { svr->free(); }); - thread t2([&m]() { + thread t2([&m, port]() { m.lock(); auto cli = make_socket(SW_SOCK_UDP, SW_FD_STREAM_CLIENT, 0); network::Address addr; - addr.assign(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + addr.assign(SW_SOCK_TCP, TEST_HOST, port); ASSERT_EQ(cli->connect(addr), SW_OK); std::this_thread::sleep_for(std::chrono::milliseconds(100)); cli->send(test_data, sizeof(test_data), 0); @@ -149,12 +151,13 @@ TEST(socket, recvfrom_blocking) { TEST(socket, sendfile_blocking) { string file = test::get_root_path() + "/examples/test.jpg"; mutex m; + int port = swoole::test::get_random_port(); m.lock(); auto str = file_get_contents(file); - thread t1([&m, &str]() { - auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + thread t1([&m, &str, port]() { + auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, port); m.unlock(); auto cli = svr->accept(); int len; @@ -169,11 +172,11 @@ TEST(socket, sendfile_blocking) { svr->free(); }); - thread t2([&m, &file, &str]() { + thread t2([&m, &file, &str, port]() { m.lock(); auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0); network::Address addr; - addr.assign(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + addr.assign(SW_SOCK_TCP, TEST_HOST, port); ASSERT_EQ(cli->connect(addr), SW_OK); int len = htonl(str->get_length()); cli->send(&len, sizeof(len), 0); @@ -291,10 +294,11 @@ TEST(socket, clean) { TEST(socket, check_liveness) { mutex m; + int svr_port = swoole::test::get_random_port(); m.lock(); - thread t1([&m]() { - auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + thread t1([&m, svr_port]() { + auto svr = make_server_socket(SW_SOCK_TCP, TEST_HOST, svr_port); m.unlock(); auto cli = svr->accept(); @@ -313,11 +317,11 @@ TEST(socket, check_liveness) { svr->free(); }); - thread t2([&m]() { + thread t2([&m, svr_port]() { m.lock(); auto cli = make_socket(SW_SOCK_TCP, SW_FD_STREAM_CLIENT, 0); - ASSERT_EQ(cli->connect(TEST_HOST, TEST_PORT), SW_OK); + ASSERT_EQ(cli->connect(TEST_HOST, svr_port), SW_OK); cli->send(test_data, sizeof(test_data), 0); std::this_thread::sleep_for(std::chrono::milliseconds(1)); diff --git a/core-tests/src/network/stream.cpp b/core-tests/src/network/stream.cpp index 2389fa38abd..5dd870eefa0 100644 --- a/core-tests/src/network/stream.cpp +++ b/core-tests/src/network/stream.cpp @@ -27,10 +27,11 @@ using namespace swoole::network; TEST(stream, send) { Server serv(Server::MODE_BASE); serv.worker_num = 1; + int svr_port = swoole::test::get_random_port(); int ori_log_level = sw_logger()->get_level(); sw_logger()->set_level(SW_LOG_ERROR); - ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, TEST_PORT); + ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, svr_port); if (!port) { swoole_warning("listen failed, [error=%d]", swoole_get_last_error()); exit(2); @@ -68,7 +69,7 @@ TEST(stream, send) { ASSERT_EQ(stream1->send(buf, sizeof(buf)), SW_OK); // success requset - auto stream2 = Stream::create(TEST_HOST, TEST_PORT, SW_SOCK_TCP); + auto stream2 = Stream::create(TEST_HOST, svr_port, SW_SOCK_TCP); ASSERT_TRUE(stream2); stream2->private_data = new string(buf, sizeof(buf)); stream2->response = [](Stream *stream, const char *data, uint32_t length) { diff --git a/core-tests/src/os/process_pool.cpp b/core-tests/src/os/process_pool.cpp index b1b43dbb604..607b3a714bc 100644 --- a/core-tests/src/os/process_pool.cpp +++ b/core-tests/src/os/process_pool.cpp @@ -65,8 +65,9 @@ static void test_func_stream_protocol(ProcessPool &pool) { TEST(process_pool, tcp) { ProcessPool pool{}; + int svr_port = swoole::test::get_random_port(); ASSERT_EQ(pool.create(1, 0, SW_IPC_SOCKET), SW_OK); - ASSERT_EQ(pool.listen(TEST_HOST, TEST_PORT, 128), SW_OK); + ASSERT_EQ(pool.listen(TEST_HOST, svr_port, 128), SW_OK); test_func_task_protocol(pool); } @@ -82,8 +83,9 @@ TEST(process_pool, unix_sock) { TEST(process_pool, tcp_raw) { ProcessPool pool{}; constexpr int size = 2 * 1024 * 1024; + int svr_port = swoole::test::get_random_port(); ASSERT_EQ(pool.create(1, 0, SW_IPC_SOCKET), SW_OK); - ASSERT_EQ(pool.listen(TEST_HOST, TEST_PORT, 128), SW_OK); + ASSERT_EQ(pool.listen(TEST_HOST, svr_port, 128), SW_OK); pool.set_max_packet_size(size); pool.set_protocol(SW_PROTOCOL_STREAM); diff --git a/core-tests/src/server/server.cpp b/core-tests/src/server/server.cpp index 1dc2121ca2f..7d75e54d4aa 100644 --- a/core-tests/src/server/server.cpp +++ b/core-tests/src/server/server.cpp @@ -1021,10 +1021,11 @@ TEST(server, system) { lock.lock(); int fd = socket(AF_INET, SOCK_STREAM, 0); + int svr_port = swoole::test::get_random_port(); struct sockaddr_in serv_addr; bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_addr.s_addr = inet_addr(TEST_HOST); - serv_addr.sin_port = htons(8080); + serv_addr.sin_port = htons(svr_port); serv_addr.sin_family = AF_INET; bind(fd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)); listen(fd, 1024); @@ -1041,7 +1042,7 @@ TEST(server, system) { lock.lock(); network::SyncClient c(SW_SOCK_TCP); - c.connect(TEST_HOST, 8080); + c.connect(TEST_HOST, svr_port); c.send(packet, strlen(packet)); char buf[1024]; c.recv(buf, sizeof(buf)); diff --git a/tests/ubuntu.sh b/tests/ubuntu.sh new file mode 100644 index 00000000000..55da7f2ef77 --- /dev/null +++ b/tests/ubuntu.sh @@ -0,0 +1 @@ +sudo apt install libcurl4-openssl-dev libc-ares-dev libssl-dev libbrotli-dev libpq-dev libsqlite3-dev unixodbc-dev