diff --git a/test/brpc_server_unittest.cpp b/test/brpc_server_unittest.cpp index 58f47b68ea..733a5892b8 100644 --- a/test/brpc_server_unittest.cpp +++ b/test/brpc_server_unittest.cpp @@ -1277,11 +1277,10 @@ TEST_F(ServerTest, too_big_message) { server.Join(); } -void CheckCert(const char* fname, const char* cert) { - butil::ScopedFILE fp(fname, "r"); - char buf[1024]; - fgets(buf, sizeof(buf), fp); - ASSERT_EQ(0, strncmp(cert, buf + 1, strlen(cert))) << cert; // Skip the first blank +void CheckCert(const char* address, const char* cert) { + std::string cmd = butil::string_printf( + "/usr/bin/curl -Ikv https://%s 2>&1 | grep %s", address, cert); + ASSERT_EQ(0, system(cmd.c_str())); } std::string GetRawPemString(const char* fname) { @@ -1311,20 +1310,12 @@ TEST_F(ServerTest, ssl_sni) { options.ssl_options.certs.push_back(cert); } ASSERT_EQ(0, server.Start(8613, &options)); - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost:8613 2>&1 " - "| grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert1"); - } + CheckCert("localhost:8613", "cert1"); + #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost.localdomain:8613 " - "2>&1 | grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert2"); - } + CheckCert("localhost.localdomain:8613", "cert2"); #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME + server.Stop(0); server.Join(); } @@ -1340,12 +1331,8 @@ TEST_F(ServerTest, ssl_reload) { options.ssl_options.default_cert = cert; } ASSERT_EQ(0, server.Start(8613, &options)); - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost:8613 2>&1 " - "| grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert1"); - } + CheckCert("localhost:8613", "cert1"); + { brpc::CertInfo cert; cert.certificate = GetRawPemString("cert2.crt"); @@ -1354,12 +1341,7 @@ TEST_F(ServerTest, ssl_reload) { ASSERT_EQ(0, server.AddCertificate(cert)); } #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost.localdomain:8613 " - "2>&1 | grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert2"); - } + CheckCert("localhost.localdomain:8613", "cert2"); #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME { @@ -1369,12 +1351,7 @@ TEST_F(ServerTest, ssl_reload) { ASSERT_EQ(0, server.RemoveCertificate(cert)); } #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost.localdomain:8613 " - "2>&1 | grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert1"); - } + CheckCert("localhost.localdomain:8613", "cert1"); #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME { @@ -1387,12 +1364,7 @@ TEST_F(ServerTest, ssl_reload) { ASSERT_EQ(0, server.ResetCertificates(certs)); } #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME - { - std::string cmd = "/usr/bin/curl -Ikv https://localhost.localdomain:8613 " - "2>&1 | grep common | cut -d':' -f2 > cname.out"; - ASSERT_EQ(0, system(cmd.c_str())); - CheckCert("cname.out", "cert2"); - } + CheckCert("localhost.localdomain:8613", "cert2"); #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME server.Stop(0);