|
35 | 35 | #include "brpc/channel.h" |
36 | 36 | #include "brpc/socket_map.h" |
37 | 37 | #include "brpc/controller.h" |
| 38 | +#include "brpc/details/ssl_helper.h" |
38 | 39 | #include "echo.pb.h" |
39 | 40 |
|
40 | 41 | namespace brpc { |
@@ -498,78 +499,67 @@ TEST_F(SSLTest, ssl_perf) { |
498 | 499 | close(servfd); |
499 | 500 | } |
500 | 501 |
|
501 | | -struct AbruptCloseArgs { int listenfd; }; |
502 | | - |
503 | | -static void* abrupt_close_server(void* arg) { |
504 | | - AbruptCloseArgs* a = (AbruptCloseArgs*)arg; |
505 | | - int connfd = accept(a->listenfd, NULL, NULL); |
506 | | - if (connfd < 0) return NULL; |
507 | | - SSL_CTX* ctx = brpc::CreateServerSSLContext( |
508 | | - "cert1.crt", "cert1.key", brpc::SSLOptions(), NULL, NULL); |
509 | | - SSL* ssl = brpc::CreateSSLSession(ctx, 0, connfd, true); |
510 | | - if (ssl) { SSL_do_handshake(ssl); SSL_free(ssl); } |
511 | | - close(connfd); |
| 502 | + |
| 503 | +#ifdef TLS1_3_VERSION |
| 504 | + |
| 505 | +void* tls13_do_handshake(void* arg) { |
| 506 | + SSL* ssl = (SSL*)arg; |
| 507 | + EXPECT_EQ(1, SSL_do_handshake(ssl)); |
512 | 508 | return NULL; |
513 | 509 | } |
514 | 510 |
|
515 | | -TEST_F(SSLTest, ssl_unexpected_eof) { |
516 | | - // Verify that Socket::DoRead() returns -1 with errno=ESSL when the |
517 | | - // remote side closes the TCP connection without sending close_notify. |
518 | | - // Without the fix, DoRead() returns 0, causing error_code=0 to |
519 | | - // propagate to Controller::SetFailed() which triggers CHECK(false). |
520 | | - |
521 | | - const int port = 5962; |
522 | | - butil::EndPoint ep(butil::IP_ANY, port); |
| 511 | +TEST_F(SSLTest, tls13_protocol_string) { |
| 512 | + // Same style as ssl_perf: direct SSL handshake, no SocketMap / socket internals. |
| 513 | + const butil::EndPoint ep(butil::IP_ANY, 8613); |
523 | 514 | butil::fd_guard listenfd(butil::tcp_listen(ep)); |
524 | 515 | ASSERT_GT(listenfd, 0); |
| 516 | + int clifd = tcp_connect(ep, NULL); |
| 517 | + ASSERT_GT(clifd, 0); |
| 518 | + int servfd = accept(listenfd, NULL, NULL); |
| 519 | + ASSERT_GT(servfd, 0); |
525 | 520 |
|
526 | | - AbruptCloseArgs server_args = { listenfd }; |
527 | | - pthread_t server_tid; |
528 | | - ASSERT_EQ(0, pthread_create(&server_tid, NULL, abrupt_close_server, |
529 | | - &server_args)); |
530 | | - |
531 | | - brpc::Protocol dummy_protocol = { |
532 | | - brpc::policy::ParseRpcMessage, brpc::SerializeRequestDefault, |
533 | | - brpc::policy::PackRpcRequest, NULL, ProcessResponse, |
534 | | - NULL, NULL, NULL, brpc::CONNECTION_TYPE_ALL, "ssl_ut_eof" |
535 | | - }; |
536 | | - ASSERT_EQ(0, RegisterProtocol((brpc::ProtocolType)31, dummy_protocol)); |
537 | | - |
538 | | - brpc::InputMessageHandler dummy_handler = { |
539 | | - dummy_protocol.parse, dummy_protocol.process_response, |
540 | | - NULL, NULL, dummy_protocol.name |
541 | | - }; |
542 | | - brpc::InputMessenger messenger; |
543 | | - ASSERT_EQ(0, messenger.AddHandler(dummy_handler)); |
544 | | - |
545 | | - brpc::SocketOptions socket_options; |
546 | | - butil::EndPoint server_ep(butil::IP_ANY, port); |
547 | | - socket_options.remote_side = server_ep; |
548 | | - socket_options.connect_on_create = true; |
549 | | - // Do NOT set on_edge_triggered_events — we will call DoRead manually. |
550 | | - socket_options.user = &messenger; |
551 | | - |
552 | | - brpc::ChannelSSLOptions ssl_options; |
553 | | - SSL_CTX* raw_ctx = brpc::CreateClientSSLContext(ssl_options); |
554 | | - ASSERT_NE(nullptr, raw_ctx); |
555 | | - std::shared_ptr<brpc::SocketSSLContext> ssl_ctx = |
556 | | - std::make_shared<brpc::SocketSSLContext>(); |
557 | | - ssl_ctx->raw_ctx = raw_ctx; |
558 | | - socket_options.initial_ssl_ctx = ssl_ctx; |
| 521 | + brpc::ChannelSSLOptions opt; |
| 522 | + opt.protocols = "TLSv1.3"; |
| 523 | + SSL_CTX* cli_ctx = brpc::CreateClientSSLContext(opt); |
| 524 | + ASSERT_NE(nullptr, cli_ctx); |
| 525 | + SSL_CTX* serv_ctx = |
| 526 | + brpc::CreateServerSSLContext("cert1.crt", "cert1.key", |
| 527 | + brpc::SSLOptions(), NULL, NULL); |
| 528 | + ASSERT_NE(nullptr, serv_ctx); |
| 529 | + SSL* cli_ssl = brpc::CreateSSLSession(cli_ctx, 0, clifd, false); |
| 530 | +#if defined(SSL_CTRL_SET_TLSEXT_HOSTNAME) || defined(USE_MESALINK) |
| 531 | + SSL_set_tlsext_host_name(cli_ssl, "localhost"); |
| 532 | +#endif |
| 533 | + SSL* serv_ssl = brpc::CreateSSLSession(serv_ctx, 0, servfd, true); |
| 534 | + ASSERT_NE(nullptr, cli_ssl); |
| 535 | + ASSERT_NE(nullptr, serv_ssl); |
| 536 | + pthread_t cpid; |
| 537 | + pthread_t spid; |
| 538 | + ASSERT_EQ(0, pthread_create(&cpid, NULL, tls13_do_handshake, cli_ssl)); |
| 539 | + ASSERT_EQ(0, pthread_create(&spid, NULL, tls13_do_handshake, serv_ssl)); |
| 540 | + ASSERT_EQ(0, pthread_join(cpid, NULL)); |
| 541 | + ASSERT_EQ(0, pthread_join(spid, NULL)); |
559 | 542 |
|
560 | | - brpc::SocketId socket_id; |
561 | | - ASSERT_EQ(0, brpc::Socket::Create(socket_options, &socket_id)); |
562 | | - brpc::SocketUniquePtr ptr; |
563 | | - ASSERT_EQ(0, brpc::Socket::Address(socket_id, &ptr)); |
| 543 | + const char* version = SSL_get_version(cli_ssl); |
| 544 | + ASSERT_TRUE(version != NULL); |
| 545 | + EXPECT_STREQ("TLSv1.3", version) << "negotiated protocol=" << version; |
564 | 546 |
|
565 | | - // Wait for server to close the connection without close_notify. |
566 | | - pthread_join(server_tid, NULL); |
567 | | - usleep(50000); |
| 547 | + SSL_free(cli_ssl); |
| 548 | + SSL_free(serv_ssl); |
| 549 | + SSL_CTX_free(cli_ctx); |
| 550 | + SSL_CTX_free(serv_ctx); |
| 551 | + close(clifd); |
| 552 | + close(servfd); |
| 553 | +} |
568 | 554 |
|
569 | | - // DoRead should detect the unexpected EOF and return -1 with errno=ESSL. |
570 | | - ssize_t nr = ptr->DoRead(1024); |
571 | | - EXPECT_EQ(-1, nr); |
572 | | - EXPECT_EQ(brpc::ESSL, errno); |
| 555 | +#else // TLS1_3_VERSION |
573 | 556 |
|
574 | | - ptr->SetFailed(); |
| 557 | +TEST_F(SSLTest, tls13_protocol_string) { |
| 558 | + brpc::ChannelSSLOptions opt; |
| 559 | + opt.protocols = "TLSv1.3"; |
| 560 | + SSL_CTX* ctx = brpc::CreateClientSSLContext(opt); |
| 561 | + ASSERT_TRUE(ctx != NULL); |
| 562 | + SSL_CTX_free(ctx); |
575 | 563 | } |
| 564 | + |
| 565 | +#endif // TLS1_3_VERSION |
0 commit comments