Skip to content

Commit 030b1e1

Browse files
committed
Added tests [3] --filter=[core] --verbose
1 parent 2ef2bd1 commit 030b1e1

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

core-tests/src/protocol/ssl.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <openssl/bio.h>
2424
#include <openssl/evp.h>
2525
#include <openssl/buffer.h>
26+
#include <openssl/err.h>
2627

2728
using swoole::SSLContext;
2829
using swoole::String;
@@ -34,12 +35,35 @@ TEST(ssl, destroy) {
3435
}
3536

3637
TEST(ssl, get_error) {
37-
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SET_SESSION, SSL_R_CERTIFICATE_VERIFY_FAILED, __FILE__, __LINE__);
38-
const char *error_str = swoole_ssl_get_error();
39-
EXPECT_NE(error_str, nullptr);
40-
String str(error_str);
41-
DEBUG() << str.to_std_string() << std::endl;
42-
ASSERT_TRUE(str.contains("reason(134)"));
38+
swoole_ssl_init();
39+
{
40+
ERR_clear_error();
41+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SET_SESSION, SSL_R_CERTIFICATE_VERIFY_FAILED, __FILE__, __LINE__);
42+
const char *error_str = swoole_ssl_get_error();
43+
EXPECT_NE(error_str, nullptr);
44+
String str(error_str);
45+
DEBUG() << str.to_std_string() << std::endl;
46+
ASSERT_TRUE(str.contains("certificate verify failed"));
47+
}
48+
{
49+
ERR_clear_error();
50+
51+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SET_SESSION, SSL_R_CERTIFICATE_VERIFY_FAILED, __FILE__, __LINE__);
52+
ERR_put_error(ERR_LIB_SSL, SSL_F_SSL_SHUTDOWN, SSL_R_PROTOCOL_IS_SHUTDOWN, __FILE__, __LINE__);
53+
54+
const char *error_str = swoole_ssl_get_error();
55+
EXPECT_NE(error_str, nullptr);
56+
57+
const char *error_str2 = swoole_ssl_get_error();
58+
EXPECT_NE(error_str2, nullptr);
59+
60+
String str(error_str2);
61+
DEBUG() << str.to_std_string() << std::endl;
62+
ASSERT_TRUE(str.contains("protocol is shutdown"));
63+
64+
const char *error_st3 = swoole_ssl_get_error();
65+
ASSERT_STREQ(error_st3, "");
66+
}
4367
}
4468

4569
TEST(ssl, password) {

core-tests/src/server/server.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,15 @@ TEST(server, reactor_thread_pipe_writable) {
19901990
EXPECT_TRUE(serv->send(req->info.fd, &len, sizeof(len)));
19911991
EXPECT_TRUE(serv->send(req->info.fd, rdata.str, rdata.length));
19921992
EXPECT_MEMEQ(req->data + 4, rdata.str, rdata.length);
1993-
EXPECT_NE(serv->get_worker_message_bus()->move_packet(), nullptr);
1993+
1994+
/**
1995+
* After using MessageBus::move_packet(), the data pointer will be out of the control of message_bus,
1996+
* and this part of the memory must be manually released; otherwise, a memory leak will occur.
1997+
*/
1998+
char *data = serv->get_worker_message_bus()->move_packet();
1999+
EXPECT_NE(data, nullptr);
2000+
sw_free(data);
2001+
19942002
return SW_OK;
19952003
};
19962004

@@ -2285,9 +2293,7 @@ static void test_clean_worker(Server::Mode mode) {
22852293
swoole_timer_after(100, [port, _serv, &ac](TIMER_PARAMS) {
22862294
ac.on_connect([&](AsyncClient *ac) { ac->send(SW_STRL(TEST_STR)); });
22872295

2288-
ac.on_close([_serv](AsyncClient *ac) {
2289-
DEBUG() << "client onClose\n";
2290-
});
2296+
ac.on_close([_serv](AsyncClient *ac) { DEBUG() << "client onClose\n"; });
22912297

22922298
ac.on_error([](AsyncClient *ac) { swoole_warning("connect failed, error=%d", swoole_get_last_error()); });
22932299

@@ -2303,10 +2309,10 @@ static void test_clean_worker(Server::Mode mode) {
23032309
};
23042310

23052311
ASSERT_EQ(serv.start(), SW_OK);
2306-
ASSERT_EQ(test::counter_get(0), 0); // Server on_receive
2307-
ASSERT_EQ(test::counter_get(1), 3); // worker start
2308-
ASSERT_EQ(test::counter_get(2), 1); // Server on_close
2309-
ASSERT_EQ(test::counter_get(3), 0); // Client on_receive
2312+
ASSERT_EQ(test::counter_get(0), 0); // Server on_receive
2313+
ASSERT_EQ(test::counter_get(1), 3); // worker start
2314+
ASSERT_EQ(test::counter_get(2), 1); // Server on_close
2315+
ASSERT_EQ(test::counter_get(3), 0); // Client on_receive
23102316
}
23112317

23122318
TEST(server, clean_worker_1) {

src/protocol/ssl.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17+
#include "php_swoole_cxx.h"
1718
#include "swoole.h"
1819
#include "swoole_string.h"
1920
#include "swoole_socket.h"
@@ -90,14 +91,17 @@ void swoole_ssl_destroy() {
9091
}
9192

9293
static int ssl_error_cb(const char *str, size_t len, void *buf) {
93-
memcpy(buf, str, len);
94-
94+
auto s = static_cast<swoole::String *>(buf);
95+
memcpy(s->str, str, len);
96+
s->length = len;
97+
s->set_null_terminated();
9598
return 0;
9699
}
97100

98101
const char *swoole_ssl_get_error() {
99-
ERR_print_errors_cb(ssl_error_cb, sw_tg_buffer()->str);
100-
102+
sw_tg_buffer()->clear();
103+
sw_tg_buffer()->set_null_terminated();
104+
ERR_print_errors_cb(ssl_error_cb, sw_tg_buffer());
101105
return sw_tg_buffer()->str;
102106
}
103107

0 commit comments

Comments
 (0)