Skip to content

Commit d7d7abd

Browse files
authored
Fix php tests, revert alpn code, added more core tests (#5775)
* Fix tests [1] --filter=[core] --verbose * Fix tests [2] --filter=[core][unit] --verbose * Fix tests [3] --filter=[core][unit] --verbose * Fix tests [4] --filter=[core][unit] --verbose * Optimize tests [14] --filter=[core] --verbose
1 parent 2d9c878 commit d7d7abd

File tree

14 files changed

+484
-153
lines changed

14 files changed

+484
-153
lines changed

core-tests/include/test_core.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#define TEST_LOG_FILE "/tmp/swoole.log"
4545
#define TEST_SOCK_FILE "/tmp/swoole-core-tests.sock"
4646

47+
#define TEST_COUNTER_NUM 32
48+
4749
#define TEST_REQUEST_BAIDU \
4850
"GET / HTTP/1.1\r\n" \
4951
"Host: www.baidu.com\r\n" \
@@ -93,6 +95,13 @@ int get_random_port();
9395
pid_t spawn_exec(const std::function<void(void)> &fn);
9496
int spawn_exec_and_wait(const std::function<void(void)> &fn);
9597

98+
void counter_init();
99+
int *counter_ptr();
100+
int counter_incr(int index, int add = 1);
101+
int counter_get(int index);
102+
void counter_set(int index, int value);
103+
void counter_incr_and_put_log(int index, const char *msg);
104+
96105
int dump_cert_info(const char *data, size_t len);
97106

98107
static inline int dump_cert_info(const String *str) {

core-tests/src/main.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "test_core.h"
2-
2+
#include "swoole_memory.h"
33
#include "swoole_proxy.h"
44

55
using namespace swoole;
66
using namespace std;
77

88
static string root_path;
9+
static int *test_counter;
910

1011
static void init_root_path(const char *);
1112

@@ -22,6 +23,12 @@ int main(int argc, char **argv) {
2223
swoole_set_trace_flags(SW_TRACE_ALL);
2324
#endif
2425

26+
if (getenv("VERBOSE") != nullptr && std::string(getenv("VERBOSE")) == "0") {
27+
swoole_set_log_level(SW_LOG_INFO);
28+
}
29+
30+
test_counter = (int *) sw_mem_pool()->alloc(sizeof(int) * TEST_COUNTER_NUM);
31+
2532
::testing::InitGoogleTest(&argc, argv);
2633
int retval = RUN_ALL_TESTS();
2734

@@ -51,6 +58,29 @@ static void init_root_path(const char *_exec_file) {
5158
namespace swoole {
5259
namespace test {
5360
NullStream null_stream;
61+
void counter_init() {
62+
sw_memset_zero(test_counter, sizeof(int) * TEST_COUNTER_NUM);
63+
}
64+
65+
int *counter_ptr() {
66+
return test_counter;
67+
}
68+
69+
int counter_incr(int index, int add) {
70+
return sw_atomic_add_fetch(&test_counter[index], add);
71+
}
72+
73+
int counter_get(int index) {
74+
return test_counter[index];
75+
}
76+
77+
void counter_set(int index, int value) {
78+
test_counter[index] = value;
79+
}
80+
81+
void counter_incr_and_put_log(int index, const char *msg) {
82+
DEBUG() << "PID: " << getpid() << ", VALUE: " << counter_incr(index) << "; " << msg << std::endl;
83+
}
5484

5585
const string &get_root_path() {
5686
return root_path;

core-tests/src/network/socket.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,130 @@ TEST(socket, make_server_socket) {
654654
ASSERT_NE(sock, nullptr);
655655
sock->free();
656656
}
657+
658+
TEST(socket, ssl_get_error_reason) {
659+
swoole_ssl_init();
660+
{
661+
int reason = -1;
662+
const char *error_str = network::Socket::ssl_get_error_reason(&reason);
663+
664+
EXPECT_EQ(error_str, nullptr);
665+
EXPECT_EQ(reason, 0);
666+
}
667+
// 测试单个错误的情况
668+
{
669+
// 生成一个 OpenSSL 错误
670+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SET_SESSION, SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED, __FILE__, __LINE__);
671+
672+
int reason = -1;
673+
const char *error_str = network::Socket::ssl_get_error_reason(&reason);
674+
675+
// 验证错误原因代码
676+
EXPECT_EQ(reason, SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED);
677+
678+
// 验证错误字符串
679+
EXPECT_NE(error_str, nullptr);
680+
EXPECT_TRUE(strstr(error_str, "certificate expired") != nullptr ||
681+
strstr(error_str, "CERTIFICATE_EXPIRED") != nullptr);
682+
683+
// 验证错误队列现在应该为空(因为 ERR_get_error 会移除错误)
684+
EXPECT_EQ(ERR_peek_error(), 0);
685+
}
686+
687+
// 测试多个错误的情况(只返回第一个)
688+
{
689+
// 生成多个 OpenSSL 错误
690+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SET_SESSION, SSL_R_SSLV3_ALERT_BAD_CERTIFICATE, __FILE__, __LINE__);
691+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SHUTDOWN, SSL_R_PROTOCOL_IS_SHUTDOWN, __FILE__, __LINE__);
692+
693+
int reason = -1;
694+
const char *error_str = network::Socket::ssl_get_error_reason(&reason);
695+
696+
// 验证返回的是第一个错误的原因代码
697+
EXPECT_EQ(reason, SSL_R_SSLV3_ALERT_BAD_CERTIFICATE);
698+
699+
// 验证错误字符串
700+
EXPECT_NE(error_str, nullptr);
701+
EXPECT_TRUE(strstr(error_str, "bad certificate") != nullptr || strstr(error_str, "BAD_CERTIFICATE") != nullptr);
702+
703+
// 验证错误队列中还有一个错误
704+
EXPECT_NE(ERR_peek_error(), 0);
705+
706+
ERR_get_error();
707+
}
708+
709+
// 测试不同库的错误
710+
{
711+
// 生成一个 BIO 库错误
712+
ERR_put_error(ERR_LIB_BIO, BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE, __FILE__, __LINE__);
713+
714+
int reason = -1;
715+
const char *error_str = network::Socket::ssl_get_error_reason(&reason);
716+
717+
// 验证错误原因代码
718+
EXPECT_EQ(reason, BIO_R_BROKEN_PIPE);
719+
720+
// 验证错误字符串
721+
EXPECT_NE(error_str, nullptr);
722+
EXPECT_TRUE(strstr(error_str, "broken pipe") != nullptr || strstr(error_str, "BROKEN_PIPE") != nullptr);
723+
}
724+
725+
// 测试 reason 参数为 nullptr 的情况(如果函数支持)
726+
{
727+
// 生成一个 OpenSSL 错误
728+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_READ, SSL_R_SSL_HANDSHAKE_FAILURE, __FILE__, __LINE__);
729+
730+
// 调用函数,传入 nullptr 作为 reason 参数
731+
// 注意:如果函数不支持 nullptr 参数,这个测试会导致段错误
732+
// 在这种情况下,应该跳过这个测试或修改函数以支持 nullptr
733+
const char *error_str = network::Socket::ssl_get_error_reason(nullptr);
734+
735+
// 验证错误字符串
736+
EXPECT_NE(error_str, nullptr);
737+
EXPECT_TRUE(strstr(error_str, "handshake failure") != nullptr ||
738+
strstr(error_str, "HANDSHAKE_FAILURE") != nullptr);
739+
}
740+
741+
// 测试错误队列中有错误但 ERR_reason_error_string 返回 nullptr 的情况
742+
{
743+
// 使用一个不常见的错误代码,可能没有对应的错误字符串
744+
// 注意:这个测试可能不稳定,因为 OpenSSL 可能为所有错误代码都提供字符串
745+
ERR_put_error(ERR_LIB_USER, 0, 12345, __FILE__, __LINE__);
746+
747+
int reason = -1;
748+
const char *error_str = network::Socket::ssl_get_error_reason(&reason);
749+
750+
// 验证错误原因代码
751+
EXPECT_EQ(reason, 12345);
752+
753+
// 错误字符串可能为 nullptr 或包含通用错误信息
754+
// 这个验证可能需要根据实际情况调整
755+
if (error_str != nullptr) {
756+
EXPECT_TRUE(true); // 如果有字符串,测试通过
757+
} else {
758+
EXPECT_EQ(error_str, nullptr); // 如果没有字符串,也测试通过
759+
}
760+
}
761+
762+
// 测试函数在多次调用后的行为
763+
{
764+
// 生成一个 OpenSSL 错误
765+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS, __FILE__, __LINE__);
766+
767+
// 第一次调用
768+
int reason1 = -1;
769+
const char *error_str1 = network::Socket::ssl_get_error_reason(&reason1);
770+
771+
// 验证第一次调用的结果
772+
EXPECT_EQ(reason1, SSL_R_LIBRARY_HAS_NO_CIPHERS);
773+
EXPECT_NE(error_str1, nullptr);
774+
775+
// 第二次调用,应该没有错误了
776+
int reason2 = -1;
777+
const char *error_str2 = network::Socket::ssl_get_error_reason(&reason2);
778+
779+
// 验证第二次调用的结果
780+
EXPECT_EQ(reason2, 0);
781+
EXPECT_EQ(error_str2, nullptr);
782+
}
783+
}

core-tests/src/os/process_pool.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,6 @@ static void test_func_stream_protocol(ProcessPool &pool) {
7171
test_func(pool);
7272
}
7373

74-
static int test_incr_shm_value(ProcessPool *pool) {
75-
auto shm_value = static_cast<int *>(pool->ptr);
76-
return sw_atomic_add_fetch(shm_value, 1);
77-
}
78-
79-
static MAYBE_UNUSED int test_get_shm_value(ProcessPool *pool) {
80-
auto shm_value = static_cast<int *>(pool->ptr);
81-
return *shm_value;
82-
}
83-
84-
static void test_set_shm_value(ProcessPool *pool, int value) {
85-
auto shm_value = static_cast<int *>(pool->ptr);
86-
*shm_value = value;
87-
}
88-
8974
TEST(process_pool, tcp) {
9075
ProcessPool pool{};
9176
int svr_port = swoole::test::get_random_port();
@@ -210,16 +195,15 @@ TEST(process_pool, shutdown) {
210195

211196
TEST(process_pool, reload) {
212197
ProcessPool pool{};
213-
int *shm_value = (int *) sw_mem_pool()->alloc(sizeof(int));
198+
test::counter_init();
214199
ASSERT_EQ(pool.create(2), SW_OK);
215200

216201
// init
217202
pool.set_max_packet_size(8192);
218-
pool.ptr = shm_value;
219203
pool.max_wait_time = 1;
220204

221205
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
222-
test_incr_shm_value(pool);
206+
test::counter_incr(0);
223207

224208
sysv_signal(SIGTERM, SIG_IGN);
225209

@@ -245,7 +229,7 @@ TEST(process_pool, reload) {
245229

246230
pool.destroy();
247231

248-
ASSERT_EQ(*shm_value, 4);
232+
ASSERT_EQ(test::counter_get(0), 4);
249233

250234
sysv_signal(SIGTERM, SIG_DFL);
251235
}
@@ -257,23 +241,22 @@ static void test_async_pool() {
257241
// init
258242
pool.set_max_packet_size(8192);
259243
pool.set_protocol(SW_PROTOCOL_TASK);
260-
int *shm_value = (int *) sw_mem_pool()->alloc(sizeof(int));
261-
pool.ptr = shm_value;
262244
pool.async = true;
245+
test::counter_init();
263246

264247
pool.onStart = [](ProcessPool *pool) {
265248
current_pool = pool;
266249
sysv_signal(SIGTERM, [](int sig) { current_pool->running = false; });
267250
};
268251

269252
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
270-
test_set_shm_value(pool, magic_number);
253+
test::counter_set(0, magic_number);
271254
current_worker = worker;
272255
current_pool = pool;
273256
sysv_signal(SIGTERM, [](int sig) { current_pool->running = false; });
274257

275258
swoole_signal_set(SIGTERM, [](int sig) {
276-
DEBUG() << "value: " << test_incr_shm_value(current_pool) << "; "
259+
DEBUG() << "value: " << test::counter_incr(0) << "; "
277260
<< "SIGTERM, stop worker\n";
278261
current_pool->stop(current_worker);
279262
});
@@ -282,7 +265,7 @@ static void test_async_pool() {
282265
};
283266

284267
pool.onMessage = [](ProcessPool *pool, RecvData *msg) {
285-
DEBUG() << "value: " << test_incr_shm_value(current_pool) << "; "
268+
DEBUG() << "value: " << test::counter_incr(0) << "; "
286269
<< "onMessage, kill\n";
287270
kill(pool->master_pid, SIGTERM);
288271
};
@@ -301,7 +284,7 @@ static void test_async_pool() {
301284

302285
pool.destroy();
303286

304-
ASSERT_EQ(*shm_value, magic_number + 2);
287+
ASSERT_EQ(test::counter_get(0), magic_number + 2);
305288

306289
swoole_signal_clear();
307290
sysv_signal(SIGTERM, SIG_DFL);
@@ -312,10 +295,6 @@ TEST(process_pool, async) {
312295
// ASSERT_EQ(test::spawn_exec_and_wait([]() { test_async_pool(); }), 0);
313296
}
314297

315-
static void test_shm_value_incr_and_put_log(ProcessPool *pool, const char *msg) {
316-
DEBUG() << "PID: " << getpid() << ", VALUE: " << test_incr_shm_value(pool) << "; " << msg << std::endl;
317-
}
318-
319298
static void test_async_pool_with_mb() {
320299
ProcessPool pool{};
321300
ASSERT_EQ(pool.create(1, 0, SW_IPC_UNIXSOCK), SW_OK);
@@ -329,19 +308,17 @@ static void test_async_pool_with_mb() {
329308
// init
330309
pool.set_max_packet_size(8192);
331310
pool.set_protocol(SW_PROTOCOL_TASK);
332-
auto shm_value = (int *) sw_mem_pool()->alloc(sizeof(int));
333-
*shm_value = 0;
334-
pool.ptr = shm_value;
311+
test::counter_init();
335312
pool.async = true;
336313

337314
pool.onWorkerStart = [](ProcessPool *pool, Worker *worker) {
338315
current_worker = worker;
339316
current_pool = pool;
340317

341-
test_shm_value_incr_and_put_log(pool, "onWorkerStart");
318+
test::counter_incr_and_put_log(0, "onWorkerStart");
342319

343320
swoole_signal_set(SIGTERM, [](int sig) {
344-
test_shm_value_incr_and_put_log(current_pool, "SIGTERM, stop worker");
321+
test::counter_incr_and_put_log(0, "SIGTERM, stop worker");
345322
current_pool->stop(sw_worker());
346323
});
347324

@@ -352,19 +329,17 @@ static void test_async_pool_with_mb() {
352329
current_worker = worker;
353330
current_pool = pool;
354331

355-
test_shm_value_incr_and_put_log(pool, "onWorkerStop");
332+
test::counter_incr_and_put_log(0, "onWorkerStop");
356333
};
357334

358-
pool.onWorkerExit = [](ProcessPool *pool, Worker *worker) {
359-
test_shm_value_incr_and_put_log(pool, "onWorkerExit");
360-
};
335+
pool.onWorkerExit = [](ProcessPool *pool, Worker *worker) { test::counter_incr_and_put_log(0, "onWorkerExit"); };
361336

362337
pool.onStart = [](ProcessPool *pool) {
363338
current_pool = pool;
364339
swoole_signal_set(SIGTERM, [](int sig) { current_pool->running = false; });
365340
swoole_signal_set(SIGIO, [](int sig) { current_pool->read_message = true; });
366341

367-
test_shm_value_incr_and_put_log(pool, "onStart");
342+
test::counter_incr_and_put_log(0, "onStart");
368343

369344
swoole_timer_after(100, [pool](TIMER_PARAMS) {
370345
pool->send_message(0, SW_STRL("detach"));
@@ -373,16 +348,16 @@ static void test_async_pool_with_mb() {
373348
});
374349
};
375350

376-
pool.onShutdown = [](ProcessPool *pool) { test_shm_value_incr_and_put_log(pool, "onShutdown"); };
351+
pool.onShutdown = [](ProcessPool *pool) { test::counter_incr_and_put_log(0, "onShutdown"); };
377352

378353
pool.onMessage = [](ProcessPool *pool, RecvData *msg) {
379354
auto req = std::string(msg->data, msg->info.len);
380355

381356
if (req == "detach") {
382-
test_shm_value_incr_and_put_log(pool, "onMessage, detach");
357+
test::counter_incr_and_put_log(0, "onMessage, detach");
383358
ASSERT_TRUE(pool->detach());
384359
} else if ((req == "shutdown")) {
385-
test_shm_value_incr_and_put_log(pool, "onMessage, shutdown");
360+
test::counter_incr_and_put_log(0, "onMessage, shutdown");
386361
pool->shutdown();
387362
}
388363
};
@@ -394,7 +369,7 @@ static void test_async_pool_with_mb() {
394369

395370
pool.destroy();
396371

397-
ASSERT_GE(*shm_value, 8);
372+
ASSERT_GE(test::counter_get(0), 8);
398373

399374
swoole_signal_clear();
400375
sysv_signal(SIGTERM, SIG_DFL);

0 commit comments

Comments
 (0)