Skip to content

Commit 79617a9

Browse files
committed
Fix tests [2] --filter=[core][unit] --verbose
1 parent 30c83a6 commit 79617a9

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

core-tests/src/server/server.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,9 +2270,7 @@ static void test_clean_worker(Server::Mode mode) {
22702270
ac.on_connect([&](AsyncClient *ac) { ac->send(SW_STRL(TEST_STR)); });
22712271

22722272
ac.on_close([_serv](AsyncClient *ac) {
2273-
swoole_timer_after(100, [_serv, ac](TIMER_PARAMS) {
2274-
_serv->shutdown();
2275-
});
2273+
swoole_timer_after(100, [_serv, ac](TIMER_PARAMS) { _serv->shutdown(); });
22762274
});
22772275

22782276
ac.on_error([](AsyncClient *ac) {});

src/protocol/ssl.cc

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ namespace swoole {
147147
#define HTTP2_H2_14_ALPN "\x05h2-14"
148148
#define HTTP1_NPN "\x08http/1.1"
149149

150+
#define ssl_error(str, ...) \
151+
long _ssl_error = ERR_get_error(); \
152+
swoole_warning(str ", Error: %s[%ld]", ##__VA_ARGS__, ERR_reason_error_string(_ssl_error), _ssl_error);
153+
150154
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
151155
static int ssl_alpn_advertised(SSL *ssl, const uchar **out, uchar *outlen, const uchar *in, uint32_t inlen, void *arg) {
152156
unsigned int protos_len;
@@ -201,8 +205,7 @@ bool SSLContext::create() {
201205
}
202206
context = SSL_CTX_new(method);
203207
if (context == nullptr) {
204-
int error = ERR_get_error();
205-
swoole_warning("SSL_CTX_new() failed, Error: %s[%d]", ERR_reason_error_string(error), error);
208+
ssl_error("SSL_CTX_new() failed");
206209
return false;
207210
}
208211

@@ -310,23 +313,15 @@ bool SSLContext::create() {
310313
* set the local certificate from CertFile
311314
*/
312315
if (SSL_CTX_use_certificate_file(context, cert_file.c_str(), SSL_FILETYPE_PEM) <= 0) {
313-
int error = ERR_get_error();
314-
swoole_warning("SSL_CTX_use_certificate_file(%s) failed, Error: %s[%d]",
315-
cert_file.c_str(),
316-
ERR_reason_error_string(error),
317-
error);
316+
ssl_error("SSL_CTX_use_certificate_file(%s) failed", cert_file.c_str());
318317
return true;
319318
}
320319
/*
321320
* if the crt file have many certificate entry ,means certificate chain
322321
* we need call this function
323322
*/
324323
if (SSL_CTX_use_certificate_chain_file(context, cert_file.c_str()) <= 0) {
325-
int error = ERR_get_error();
326-
swoole_warning("SSL_CTX_use_certificate_chain_file(%s) failed, Error: %s[%d]",
327-
cert_file.c_str(),
328-
ERR_reason_error_string(error),
329-
error);
324+
ssl_error("SSL_CTX_use_certificate_chain_file(%s) failed", cert_file.c_str());
330325
return false;
331326
}
332327
}
@@ -335,18 +330,14 @@ bool SSLContext::create() {
335330
* set the private key from KeyFile (may be the same as CertFile)
336331
*/
337332
if (SSL_CTX_use_PrivateKey_file(context, key_file.c_str(), SSL_FILETYPE_PEM) <= 0) {
338-
int error = ERR_get_error();
339-
swoole_warning("SSL_CTX_use_PrivateKey_file(%s) failed, Error: %s[%d]",
340-
key_file.c_str(),
341-
ERR_reason_error_string(error),
342-
error);
333+
ssl_error("SSL_CTX_use_PrivateKey_file(%s) failed", key_file.c_str());
343334
return false;
344335
}
345336
/*
346337
* verify private key
347338
*/
348339
if (!SSL_CTX_check_private_key(context)) {
349-
swoole_warning("Private key does not match the public certificate");
340+
ssl_error("SSL_CTX_check_private_key() failed");
350341
return false;
351342
}
352343
}
@@ -370,6 +361,19 @@ bool SSLContext::create() {
370361
}
371362

372363
if (http || http_v2) {
364+
unsigned int protos_len;
365+
const char *protos;
366+
if (http_v2) {
367+
protos = HTTP2_H2_ALPN HTTP1_NPN;
368+
protos_len = sizeof(HTTP2_H2_ALPN HTTP1_NPN) - 1;
369+
} else {
370+
protos = HTTP1_NPN;
371+
protos_len = sizeof(HTTP1_NPN) - 1;
372+
}
373+
if (SSL_CTX_set_alpn_protos(context, (const uchar *) protos, protos_len) < 0) {
374+
ssl_error("SSL_CTX_set_alpn_protos(%s) failed", protos);
375+
return false;
376+
}
373377
#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
374378
SSL_CTX_set_alpn_select_cb(context, ssl_alpn_advertised, (void *) this);
375379
#endif
@@ -382,7 +386,6 @@ bool SSLContext::create() {
382386
#endif
383387

384388
if (!client_cert_file.empty() && !set_client_certificate()) {
385-
swoole_warning("set_client_certificate() error");
386389
return false;
387390
}
388391

@@ -403,7 +406,7 @@ bool SSLContext::set_capath() {
403406
}
404407
} else {
405408
if (!SSL_CTX_set_default_verify_paths(context)) {
406-
swoole_warning("Unable to set default verify locations and no CA settings specified");
409+
ssl_error("SSL_CTX_set_default_verify_paths() failed");
407410
return false;
408411
}
409412
}
@@ -422,11 +425,12 @@ bool SSLContext::set_ciphers() {
422425

423426
if (!ciphers.empty()) {
424427
if (SSL_CTX_set_cipher_list(context, ciphers.c_str()) == 0) {
425-
swoole_warning("SSL_CTX_set_cipher_list(\"%s\") failed", ciphers.c_str());
428+
ssl_error("SSL_CTX_set_cipher_list(\"%s\") failed", ciphers.c_str());
426429
return false;
427430
}
428-
if (prefer_server_ciphers) {
429-
SSL_CTX_set_options(context, SSL_OP_CIPHER_SERVER_PREFERENCE);
431+
if (prefer_server_ciphers && !SSL_CTX_set_options(context, SSL_OP_CIPHER_SERVER_PREFERENCE)) {
432+
ssl_error("SSL_CTX_set_options(SSL_OP_CIPHER_SERVER_PREFERENCE) failed");
433+
return false;
430434
}
431435
}
432436

@@ -454,14 +458,14 @@ bool SSLContext::set_client_certificate() {
454458
SSL_CTX_set_verify_depth(context, depth);
455459

456460
if (SSL_CTX_load_verify_locations(context, cert_file, nullptr) == 0) {
457-
swoole_warning("SSL_CTX_load_verify_locations(\"%s\") failed", cert_file);
461+
ssl_error("SSL_CTX_load_verify_locations(\"%s\") failed", cert_file);
458462
return false;
459463
}
460464

461465
ERR_clear_error();
462466
list = SSL_load_client_CA_file(cert_file);
463467
if (list == nullptr) {
464-
swoole_warning("SSL_load_client_CA_file(\"%s\") failed", cert_file);
468+
ssl_error("SSL_load_client_CA_file(\"%s\") failed", cert_file);
465469
return false;
466470
}
467471

@@ -540,28 +544,28 @@ bool SSLContext::set_dhparam() {
540544

541545
bio = BIO_new_file(file, "r");
542546
if (bio == nullptr) {
543-
swoole_warning("BIO_new_file(%s) failed", file);
547+
ssl_error("BIO_new_file(%s) failed", file);
544548
return false;
545549
}
546550

547551
#if OPENSSL_VERSION_MAJOR >= 3
548552
EVP_PKEY *pkey = PEM_read_bio_Parameters(bio, nullptr);
549553
if (pkey == nullptr) {
550-
swoole_warning("PEM_read_bio_Parameters('%s') failed", file);
554+
ssl_error("PEM_read_bio_Parameters('%s') failed", file);
551555
BIO_free(bio);
552556
return false;
553557
}
554558

555559
if (SSL_CTX_set0_tmp_dh_pkey(context, pkey) != 1) {
556-
swoole_warning("SSL_CTX_set0_tmp_dh_pkey('%s') failed", file);
560+
ssl_error("SSL_CTX_set0_tmp_dh_pkey('%s') failed", file);
557561
EVP_PKEY_free(pkey);
558562
BIO_free(bio);
559563
return false;
560564
}
561565
#else
562566
DH *dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr);
563567
if (dh == nullptr) {
564-
swoole_warning("PEM_read_bio_DHparams(%s) failed", file);
568+
ssl_error("PEM_read_bio_DHparams(%s) failed", file);
565569
BIO_free(bio);
566570
return false;
567571
}

0 commit comments

Comments
 (0)