|
29 | 29 | import java.io.IOException; |
30 | 30 | import java.io.InputStream; |
31 | 31 | import java.security.KeyManagementException; |
| 32 | +import java.security.KeyStoreException; |
32 | 33 | import java.security.NoSuchAlgorithmException; |
| 34 | +import java.security.UnrecoverableKeyException; |
| 35 | +import java.security.cert.CertificateException; |
| 36 | +import java.security.cert.X509Certificate; |
33 | 37 | import java.text.DecimalFormat; |
34 | 38 | import java.util.Iterator; |
35 | 39 | import java.util.Map; |
36 | 40 | import java.util.Scanner; |
37 | 41 | import java.util.TreeMap; |
38 | 42 |
|
| 43 | +import javax.net.ssl.SSLContext; |
| 44 | +import javax.net.ssl.X509TrustManager; |
39 | 45 | import javax.xml.bind.JAXBException; |
40 | 46 | import javax.xml.parsers.ParserConfigurationException; |
41 | 47 | import javax.xml.transform.TransformerException; |
|
49 | 55 |
|
50 | 56 | import com.marklogic.client.DatabaseClient; |
51 | 57 | import com.marklogic.client.DatabaseClientFactory; |
| 58 | +import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier; |
52 | 59 | import com.marklogic.client.DatabaseClientFactory.SecurityContext; |
53 | 60 | import com.marklogic.client.FailedRequestException; |
54 | 61 | import com.marklogic.client.ForbiddenUserException; |
@@ -158,6 +165,52 @@ public void testDatabaseClientConnectionExist() throws KeyManagementException, N |
158 | 165 | // release client |
159 | 166 | client.release(); |
160 | 167 | } |
| 168 | + |
| 169 | + // To test getters of SecurityContext |
| 170 | + @Test |
| 171 | + public void testDatabaseClientGetters() throws KeyManagementException, NoSuchAlgorithmException, IOException |
| 172 | + { |
| 173 | + System.out.println("Running testDatabaseClientGetters"); |
| 174 | + |
| 175 | + DatabaseClient client = null; |
| 176 | + SSLContext sslcontext = null; |
| 177 | + SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("rest-reader", "x"); |
| 178 | + |
| 179 | + try { |
| 180 | + sslcontext = getSslContext(); |
| 181 | + } catch (UnrecoverableKeyException | KeyStoreException | CertificateException e) { |
| 182 | + e.printStackTrace(); |
| 183 | + } |
| 184 | + |
| 185 | + secContext.withSSLContext(sslcontext, new X509TrustManager() { |
| 186 | + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
| 187 | + // nothing to do |
| 188 | + } |
| 189 | + |
| 190 | + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
| 191 | + // nothing to do |
| 192 | + } |
| 193 | + |
| 194 | + public X509Certificate[] getAcceptedIssuers() { |
| 195 | + return new X509Certificate[0]; |
| 196 | + } |
| 197 | + }) |
| 198 | + .withSSLHostnameVerifier(SSLHostnameVerifier.ANY); |
| 199 | + |
| 200 | + client = DatabaseClientFactory.newClient(getRestServerHostName(), getRestServerPort(), |
| 201 | + secContext, getConnType()); |
| 202 | + SecurityContext readSecContext = client.getSecurityContext(); |
| 203 | + String verifier = readSecContext.getSSLHostnameVerifier().toString(); |
| 204 | + String protocol = readSecContext.getSSLContext().getProtocol(); |
| 205 | + boolean needClient = readSecContext.getSSLContext().getSupportedSSLParameters().getNeedClientAuth(); |
| 206 | + |
| 207 | + assertTrue("Verifier not Builtin", verifier.contains("Builtin")); |
| 208 | + assertTrue("Protocol incorrect", protocol.contains("TLSv1.2")); |
| 209 | + assertTrue("NeedClientAuth incorrect", needClient == false); |
| 210 | + // release client |
| 211 | + client.release(); |
| 212 | + } |
| 213 | + |
161 | 214 |
|
162 | 215 | @Test |
163 | 216 | public void testDatabaseClientConnectionInvalidPort() throws IOException |
|
0 commit comments