Skip to content

Commit 79910e1

Browse files
committed
fix #4 since my logic in InvalidUserTest.java needed fixing to actually run the assert; also added SSLTest.java for additional validation
1 parent b737877 commit 79910e1

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.test;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import javax.net.ssl.SSLContext;
21+
import javax.net.ssl.TrustManager;
22+
import javax.net.ssl.X509TrustManager;
23+
import java.security.KeyManagementException;
24+
import java.security.NoSuchAlgorithmException;
25+
import java.security.cert.X509Certificate;
26+
27+
import org.junit.Test;
28+
29+
import com.marklogic.client.DatabaseClient;
30+
import com.marklogic.client.DatabaseClientFactory;
31+
import com.marklogic.client.DatabaseClientFactory.Authentication;
32+
import com.marklogic.client.DatabaseClientFactory.SSLHostnameVerifier;
33+
import com.marklogic.client.document.TextDocumentManager;
34+
import com.marklogic.client.io.StringHandle;
35+
36+
public class SSLTest {
37+
@Test
38+
public void testSSLAuth() throws NoSuchAlgorithmException, KeyManagementException {
39+
40+
// create a trust manager
41+
// (note: a real application should verify certificates)
42+
TrustManager naiveTrustMgr = new X509TrustManager() {
43+
@Override
44+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
45+
}
46+
@Override
47+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
48+
}
49+
@Override
50+
public X509Certificate[] getAcceptedIssuers() {
51+
return new X509Certificate[0];
52+
}
53+
};
54+
55+
// create an SSL context
56+
SSLContext sslContext = SSLContext.getInstance("SSLv3");
57+
sslContext.init(null, new TrustManager[] { naiveTrustMgr }, null);
58+
59+
// create the client
60+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8012, "MyFooUser", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY);
61+
62+
63+
String expectedException = "FailedRequestException: Local message: write failed: Unauthorized";
64+
String exception = "";
65+
66+
// write doc
67+
try
68+
{
69+
// make use of the client connection
70+
TextDocumentManager docMgr = client.newTextDocumentManager();
71+
String docId = "/example/text.txt";
72+
StringHandle handle = new StringHandle();
73+
handle.set("A simple text document");
74+
docMgr.write(docId, handle);
75+
}
76+
catch (Exception e) {
77+
exception = e.toString();
78+
}
79+
assertEquals(expectedException, exception);
80+
81+
}
82+
}

0 commit comments

Comments
 (0)