Skip to content

Commit 4966f45

Browse files
committed
a negative test to validate that an invalid user has been identified as unauthorized
1 parent 0a852ca commit 4966f45

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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(
61+
"localhost", 8012, "MyFooUser", "x", Authentication.DIGEST, sslContext, SSLHostnameVerifier.ANY);
62+
63+
64+
String expectedException = "com.marklogic.client.FailedRequestException: " +
65+
"Local message: write failed: Unauthorized. Server Message: Unauthorized";
66+
String exception = "";
67+
68+
// write doc
69+
try
70+
{
71+
// make use of the client connection
72+
TextDocumentManager docMgr = client.newTextDocumentManager();
73+
String docId = "/example/text.txt";
74+
StringHandle handle = new StringHandle();
75+
handle.set("A simple text document");
76+
docMgr.write(docId, handle);
77+
}
78+
catch (Exception e) {
79+
exception = e.toString();
80+
}
81+
assertEquals(expectedException, exception);
82+
83+
}
84+
}

0 commit comments

Comments
 (0)