### Question:
ZKTrustManager extends X509ExtendedTrustManager { @Override public void checkClientTrusted( X509Certificate[] chain, String authType, Socket socket) throws CertificateException { x509ExtendedTrustManager.checkClientTrusted(chain, authType, socket); if (clientHostnameVerificationEnabled) { performHostVerification(socket.getInetAddress(), chain[0]); } } ZKTrustManager(
        X509ExtendedTrustManager x509ExtendedTrustManager,
        boolean serverHostnameVerificationEnabled,
        boolean clientHostnameVerificationEnabled); @Override X509Certificate[] getAcceptedIssuers(); @Override void checkClientTrusted(
        X509Certificate[] chain,
        String authType,
        Socket socket); @Override void checkServerTrusted(
        X509Certificate[] chain,
        String authType,
        Socket socket); @Override void checkClientTrusted(
        X509Certificate[] chain,
        String authType,
        SSLEngine engine); @Override void checkServerTrusted(
        X509Certificate[] chain,
        String authType,
        SSLEngine engine
                                  ); @Override void checkClientTrusted(X509Certificate[] chain, String authType); @Override void checkServerTrusted(X509Certificate[] chain, String authType);  }### Answer:
@Test public void testClientHostnameVerificationWithHostnameVerificationDisabled() throws Exception { ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, false, true); X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); verify(mockInetAddress, times(1)).getHostAddress(); verify(mockInetAddress, times(1)).getHostName(); verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); }
@Test public void testClientHostnameVerificationWithClientHostnameVerificationDisabled() throws Exception { ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, false); X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); verify(mockInetAddress, times(0)).getHostAddress(); verify(mockInetAddress, times(0)).getHostName(); verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); }
@Test public void testClientHostnameVerificationWithIPAddress() throws Exception { ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, true); X509Certificate[] certificateChain = createSelfSignedCertifcateChain(IP_ADDRESS, null); zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); verify(mockInetAddress, times(1)).getHostAddress(); verify(mockInetAddress, times(0)).getHostName(); verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); }
@Test public void testClientHostnameVerificationWithHostname() throws Exception { ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, true); X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); verify(mockInetAddress, times(1)).getHostAddress(); verify(mockInetAddress, times(1)).getHostName(); verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); }