Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapCBPropertiesTest.java
41153 views
1
/*
2
* Copyright (c) 2020, Azul Systems, Inc. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8245527
27
* @library lib/ /test/lib
28
* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point
29
* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point
30
* @run main/othervm LdapCBPropertiesTest true true com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 2000
31
* @run main/othervm LdapCBPropertiesTest false false com.sun.jndi.ldap.tls.cbtype tls-server-end-point com.sun.jndi.ldap.connect.timeout 2000
32
* @run main/othervm LdapCBPropertiesTest false true com.sun.jndi.ldap.tls.cbtype tls-unknown
33
* @run main/othervm LdapCBPropertiesTest false true jdk.internal.sasl.tlschannelbinding value
34
* @summary test new JNDI property to control the Channel Binding data
35
*/
36
37
import javax.naming.AuthenticationException;
38
import javax.naming.CommunicationException;
39
import javax.naming.Context;
40
import javax.naming.NamingException;
41
import javax.naming.directory.DirContext;
42
import javax.naming.directory.InitialDirContext;
43
import java.net.InetAddress;
44
import java.net.URI;
45
import java.util.Hashtable;
46
47
import org.ietf.jgss.GSSException;
48
49
import javax.net.ssl.SSLException;
50
import javax.net.ssl.SSLServerSocket;
51
import javax.net.ssl.SSLServerSocketFactory;
52
import javax.security.sasl.SaslException;
53
54
import jdk.test.lib.net.URIBuilder;
55
56
public class LdapCBPropertiesTest {
57
/*
58
* Where do we find the keystores?
59
*/
60
static String pathToStores = "../../../../javax/net/ssl/etc";
61
static String keyStoreFile = "keystore";
62
static String trustStoreFile = "truststore";
63
static String passwd = "passphrase";
64
65
static boolean debug = false;
66
67
public static void main(String[] args) throws Exception {
68
String keyFilename =
69
System.getProperty("test.src", "./") + "/" + pathToStores +
70
"/" + keyStoreFile;
71
String trustFilename =
72
System.getProperty("test.src", "./") + "/" + pathToStores +
73
"/" + trustStoreFile;
74
75
System.setProperty("javax.net.ssl.keyStore", keyFilename);
76
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
77
System.setProperty("javax.net.ssl.trustStore", trustFilename);
78
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
79
80
if (debug)
81
System.setProperty("javax.net.debug", "all");
82
83
/*
84
* Start the tests.
85
*/
86
new LdapCBPropertiesTest(args);
87
}
88
89
/*
90
* Primary constructor, used to drive remainder of the test.
91
*/
92
LdapCBPropertiesTest(String[] args) throws Exception {
93
InetAddress loopback = InetAddress.getLoopbackAddress();
94
SSLServerSocketFactory sslssf =
95
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
96
SSLServerSocket sslServerSocket =
97
(SSLServerSocket) sslssf.createServerSocket(0, 0, loopback);
98
int serverPort = sslServerSocket.getLocalPort();
99
100
try (var ignore = new BaseLdapServer(sslServerSocket).start()) {
101
doClientSide(serverPort, args);
102
}
103
}
104
105
/*
106
* Define the client side of the test.
107
*
108
* The server should start at this time already
109
*/
110
void doClientSide(int serverPort, String[] args) throws Exception {
111
boolean passed = false;
112
boolean shouldPass = Boolean.parseBoolean(args[0]);
113
boolean shouldConnect = Boolean.parseBoolean(args[1]);
114
// set disableEndpointIdentification to disable hostname verification
115
if (shouldConnect) {
116
System.setProperty(
117
"com.sun.jndi.ldap.object.disableEndpointIdentification", "true");
118
}
119
120
// Set up the environment for creating the initial context
121
Hashtable env = new Hashtable();
122
URI uri = URIBuilder.newBuilder()
123
.scheme("ldaps")
124
.loopback()
125
.port(serverPort)
126
.build();
127
env.put(Context.PROVIDER_URL, uri.toString());
128
env.put(Context.INITIAL_CONTEXT_FACTORY,
129
"com.sun.jndi.ldap.LdapCtxFactory");
130
env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
131
132
// read properties
133
for (int i = 2; i < args.length; i += 2) {
134
env.put(args[i], args[i + 1]);
135
if (debug)
136
System.out.println("Env=" + args[i] + "=" + args[i + 1]);
137
}
138
139
try {
140
DirContext ctx = new InitialDirContext(env);
141
passed = shouldPass;
142
ctx.close();
143
} catch (NamingException ne) {
144
// only NamingException is allowed
145
if (debug)
146
System.out.println("Exception=" + ne + " cause=" + ne.getRootCause());
147
passed = handleNamingException(ne, shouldPass, shouldConnect);
148
} catch(Exception e) {
149
System.err.println("Failed: caught an unexpected Exception - " + e);
150
throw e;
151
} finally {
152
// test if internal property accessible to application
153
if(shouldPass &&
154
env.get("jdk.internal.sasl.tlschannelbinding") != null) {
155
throw new Exception(
156
"Test FAILED: jdk.internal.sasl.tlschannelbinding should not be accessible");
157
}
158
}
159
if (!passed) {
160
throw new Exception(
161
"Test FAILED: NamingException exception should be thrown");
162
}
163
System.out.println("Test PASSED");
164
}
165
166
private static boolean handleNamingException(NamingException ne, boolean shouldPass, boolean shouldConnect)
167
throws NamingException {
168
if (ne instanceof AuthenticationException &&
169
ne.getRootCause() instanceof SaslException) {
170
SaslException saslEx = (SaslException) ne.getRootCause();
171
if (shouldConnect && saslEx.getCause() instanceof GSSException) {
172
// SSL connection successful, expected exception from SaslClient
173
if (shouldPass)
174
return true;
175
}
176
}
177
if (!shouldConnect) {
178
// SSL handshake fails
179
Exception ex = ne;
180
while(ex != null && !(ex instanceof CommunicationException)) {
181
ex = (Exception)ex.getCause();
182
}
183
if (ex != null) {
184
if (ex.getCause() instanceof SSLException) {
185
if (!shouldPass)
186
return true;
187
}
188
}
189
}
190
if (!shouldPass && ne.getRootCause() == null) {
191
// Expected exception caused by Channel Binding parameter inconsistency
192
return true;
193
}
194
throw ne;
195
}
196
}
197
198