@@ -229,6 +229,79 @@ TEST_F(SSLTest, force_ssl) {
229229 ASSERT_EQ (0 , server.Join ());
230230}
231231
232+ void CallServerWithExpectedPeerName (int port, const char * server_address,
233+ const char * expected_peer_name,
234+ bool expect_success) {
235+ brpc::Channel channel;
236+ brpc::ChannelOptions options;
237+ options.protocol = brpc::PROTOCOL_HTTP ;
238+ options.mutable_ssl_options ()->verify .verify_mode =
239+ brpc::VerifyMode::VERIFY_PEER ;
240+ options.mutable_ssl_options ()->verify .verify_depth = 1 ;
241+ options.mutable_ssl_options ()->verify .ca_file_path = " cert1.crt" ;
242+ if (expected_peer_name != NULL ) {
243+ options.mutable_ssl_options ()->verify .expected_peer_name = expected_peer_name;
244+ }
245+ std::string url = server_address;
246+ url.append (" :" ).append (std::to_string (port));
247+ ASSERT_EQ (0 , channel.Init (url.c_str (), &options));
248+
249+ test::EchoRequest req;
250+ test::EchoResponse res;
251+ req.set_message (EXP_REQUEST );
252+ brpc::Controller cntl;
253+ test::EchoService_Stub stub (&channel);
254+ stub.Echo (&cntl, &req, &res, NULL );
255+ if (expect_success) {
256+ EXPECT_FALSE (cntl.Failed ()) << cntl.ErrorText ();
257+ EXPECT_EQ (EXP_RESPONSE , res.message ());
258+ } else {
259+ EXPECT_TRUE (cntl.Failed ());
260+ }
261+ }
262+
263+ TEST_F (SSLTest, verify_peer_name) {
264+ const int port = 8613 ;
265+ brpc::Server server;
266+ brpc::ServerOptions server_options;
267+ brpc::CertInfo cert;
268+ cert.certificate = " cert1.crt" ;
269+ cert.private_key = " cert1.key" ;
270+ server_options.mutable_ssl_options ()->default_cert = cert;
271+
272+ EchoServiceImpl echo_svc;
273+ ASSERT_EQ (0 , server.AddService (
274+ &echo_svc, brpc::SERVER_DOESNT_OWN_SERVICE ));
275+ ASSERT_EQ (0 , server.Start (port, &server_options));
276+
277+ CallServerWithExpectedPeerName (port, " https://localhost" , NULL , true );
278+ CallServerWithExpectedPeerName (port, " https://127.0.0.1" , NULL , false );
279+ CallServerWithExpectedPeerName (
280+ port, " https://localhost" , " wrong.local" , false );
281+
282+ ASSERT_EQ (0 , server.Stop (0 ));
283+ ASSERT_EQ (0 , server.Join ());
284+ }
285+
286+ TEST_F (SSLTest, expected_peer_name_requires_peer_verification) {
287+ brpc::ChannelSSLOptions options;
288+ options.verify .expected_peer_name = " localhost" ;
289+ EXPECT_EQ (NULL , brpc::CreateClientSSLContext (options));
290+
291+ options.verify .verify_depth = 1 ;
292+ options.verify .verify_mode = brpc::VerifyMode::VERIFY_NONE ;
293+ EXPECT_EQ (NULL , brpc::CreateClientSSLContext (options));
294+ }
295+
296+ TEST_F (SSLTest, peer_name_verification_capability) {
297+ #if defined(USE_MESALINK) || \
298+ (!defined (OPENSSL_IS_BORINGSSL ) && OPENSSL_VERSION_NUMBER < 0x10002000L )
299+ EXPECT_FALSE (brpc::SupportsPeerNameVerification ());
300+ #else
301+ EXPECT_TRUE (brpc::SupportsPeerNameVerification ());
302+ #endif
303+ }
304+
232305void ProcessResponse (brpc::InputMessageBase* msg_base) {
233306 brpc::DestroyingPtr<brpc::policy::MostCommonMessage> msg (
234307 static_cast <brpc::policy::MostCommonMessage*>(msg_base));
0 commit comments