1515 */
1616package com .marklogic .client .test ;
1717
18- import static org .junit .Assert .assertArrayEquals ;
19- import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .*;
2019import static org .mockito .Mockito .mock ;
2120import static org .mockito .Mockito .when ;
2221
23- import javax .net .ssl .SSLContext ;
24- import javax .net .ssl .SSLException ;
25- import javax .net .ssl .TrustManager ;
26- import javax .net .ssl .X509TrustManager ;
22+ import javax .net .ssl .*;
2723import javax .security .auth .x500 .X500Principal ;
2824import java .security .KeyManagementException ;
25+ import java .security .KeyStore ;
26+ import java .security .KeyStoreException ;
2927import java .security .NoSuchAlgorithmException ;
3028import java .security .cert .CertificateParsingException ;
3129import java .security .cert .X509Certificate ;
3432import java .util .Collection ;
3533import java .util .List ;
3634import java .util .concurrent .atomic .AtomicReference ;
35+
36+ import com .marklogic .client .MarkLogicIOException ;
3737import org .junit .Test ;
3838
3939import com .marklogic .client .DatabaseClient ;
4242import com .marklogic .client .DatabaseClientFactory .SSLHostnameVerifier ;
4343import com .marklogic .client .document .TextDocumentManager ;
4444import com .marklogic .client .io .StringHandle ;
45- import com .marklogic .client .impl .OkHttpServices ;
4645
4746public class SSLTest {
4847 @ Test
49- public void testSSLAuth () throws NoSuchAlgorithmException , KeyManagementException {
48+ public void testSSLAuth () throws NoSuchAlgorithmException , KeyManagementException , KeyStoreException {
49+ TrustManagerFactory trustMgrFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
50+ trustMgrFactory .init (KeyStore .getInstance (KeyStore .getDefaultType ()));
51+
52+ TrustManager [] trustMgrs = trustMgrFactory .getTrustManagers ();
53+ assertNotNull (trustMgrs );
54+ assertTrue (trustMgrs .length > 0 );
55+
56+ X509TrustManager x509trustMgr = null ;
57+ for (TrustManager trustMgr : trustMgrs ) {
58+ if (trustMgr instanceof X509TrustManager ) {
59+ x509trustMgr = (X509TrustManager ) trustMgr ;
60+ break ;
61+ }
62+ }
63+ assertNotNull (x509trustMgr );
5064
5165 // create an SSL context
5266 SSLContext sslContext = SSLContext .getInstance ("SSLv3" );
53- sslContext .init (null , null , null );
67+ sslContext .init (null , trustMgrs , null );
5468
5569 // create the client
5670 DatabaseClient client = DatabaseClientFactory .newClient (Common .HOST , Common .PORT , new DigestAuthContext ("rest-writer" , "x" )
57- .withSSLContext (sslContext )
71+ .withSSLContext (sslContext , x509trustMgr )
5872 .withSSLHostnameVerifier (SSLHostnameVerifier .ANY ));
5973
60-
6174 String expectedException = "com.marklogic.client.MarkLogicIOException: " +
62- "javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?" ;
63- String exception = "" ;
75+ "javax.net.ssl.SSLException: " ;
6476
6577 try {
6678 // make use of the client connection so we get an auth exception if it
@@ -75,10 +87,11 @@ public void testSSLAuth() throws NoSuchAlgorithmException, KeyManagementExceptio
7587 docMgr .read (docId , handle );
7688 assertEquals (handle .get (), "A simple text document by SSL connection" );
7789 docMgr .delete (docId );
78- }
79- catch (Exception e ) {
80- exception = e .toString ();
81- assertEquals (expectedException , exception );
90+ } catch (MarkLogicIOException e ) {
91+ String exception = e .toString ();
92+ System .out .println (exception );
93+ assertTrue (exception .startsWith (expectedException ));
94+ assertTrue (exception .toLowerCase ().contains ("unrecognized ssl message" ));
8295 }
8396 }
8497
0 commit comments