|
3 | 3 | import com.marklogic.client.DatabaseClient;
|
4 | 4 | import com.marklogic.client.DatabaseClientBuilder;
|
5 | 5 | import com.marklogic.client.DatabaseClientFactory;
|
| 6 | +import com.marklogic.client.ext.modulesloader.ssl.SimpleX509TrustManager; |
6 | 7 | import org.junit.jupiter.api.Test;
|
7 | 8 |
|
8 | 9 | import javax.net.ssl.SSLContext;
|
| 10 | +import javax.net.ssl.X509TrustManager; |
| 11 | + |
| 12 | +import java.security.NoSuchAlgorithmException; |
9 | 13 |
|
10 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
11 | 15 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
12 | 16 | import static org.junit.jupiter.api.Assertions.assertNull;
|
| 17 | +import static org.junit.jupiter.api.Assertions.assertSame; |
13 | 18 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
14 | 19 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
15 | 20 |
|
@@ -138,14 +143,45 @@ void kerberos() {
|
138 | 143 | }
|
139 | 144 |
|
140 | 145 | @Test
|
141 |
| - void certificate() { |
| 146 | + void certificateValidFile() { |
| 147 | + DatabaseClient client = Common.newClientBuilder() |
| 148 | + .withCertificateAuth("src/test/resources/test_certificate.p12", "abc") |
| 149 | + .build(); |
| 150 | + |
| 151 | + assertNotNull(client); |
| 152 | + assertNotNull(client.getSecurityContext().getSSLContext(), "An SSLContext should have been created based " + |
| 153 | + "on the test keystore."); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void certificateInvalidFile() { |
142 | 158 | DatabaseClientBuilder builder = Common.newClientBuilder()
|
143 | 159 | .withCertificateAuth("not.found", "passwd");
|
144 | 160 |
|
145 | 161 | Exception ex = assertThrows(Exception.class, () -> builder.buildBean());
|
146 |
| - assertTrue(ex.getMessage().contains("Unable to create CertificateAuthContext"), |
147 |
| - "We don't yet have a real test for certificate authentication, so there's not yet a certificate store " + |
148 |
| - "to test against; just making sure that an attempt is made to create a CertificateAuthContext"); |
| 162 | + assertEquals("Unable to create CertificateAuthContext; cause not.found (No such file or directory)", |
| 163 | + ex.getMessage(), "Should fail because the certificate file path is not valid, and thus a keystore " + |
| 164 | + "cannot be created from it."); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void certificateWithNoFile() throws NoSuchAlgorithmException { |
| 169 | + SSLContext defaultContext = SSLContext.getDefault(); |
| 170 | + X509TrustManager trustManager = new SimpleX509TrustManager(); |
| 171 | + DatabaseClientBuilder builder = Common.newClientBuilder() |
| 172 | + .withCertificateAuth(defaultContext, trustManager) |
| 173 | + .withSSLHostnameVerifier(DatabaseClientFactory.SSLHostnameVerifier.STRICT); |
| 174 | + |
| 175 | + // Verify the SSL-related objects are the same ones passed in above. |
| 176 | + DatabaseClientFactory.Bean bean = builder.buildBean(); |
| 177 | + assertSame(defaultContext, bean.getSecurityContext().getSSLContext()); |
| 178 | + assertSame(trustManager, bean.getSecurityContext().getTrustManager()); |
| 179 | + assertSame(DatabaseClientFactory.SSLHostnameVerifier.STRICT, bean.getSecurityContext().getSSLHostnameVerifier()); |
| 180 | + |
| 181 | + DatabaseClient client = bean.newClient(); |
| 182 | + assertNotNull(client, "The client can be instantiated because a certificate file and password aren't " + |
| 183 | + "required. In this scenario, it's expected that a user will provide their own SSLContext to use for " + |
| 184 | + "certificate authentication."); |
149 | 185 | }
|
150 | 186 |
|
151 | 187 | @Test
|
|
0 commit comments