Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/LdapDnsProviderTest.java
38855 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
import java.io.File;
27
import java.io.FileOutputStream;
28
import java.io.IOException;
29
import java.security.Permission;
30
import java.util.Hashtable;
31
import java.util.concurrent.Callable;
32
import java.util.concurrent.FutureTask;
33
34
import javax.naming.Context;
35
import javax.naming.InitialContext;
36
import javax.naming.NamingException;
37
import javax.naming.directory.InitialDirContext;
38
import javax.naming.directory.SearchControls;
39
40
/**
41
* @test
42
* @bug 8160768
43
* @summary ctx provider tests for ldap
44
* @compile dnsprovider/TestDnsProvider.java
45
* @run main/othervm LdapDnsProviderTest
46
* @run main/othervm LdapDnsProviderTest nosm
47
* @run main/othervm LdapDnsProviderTest smnodns
48
* @run main/othervm LdapDnsProviderTest smdns
49
* @run main/othervm LdapDnsProviderTest nosmbaddns
50
*/
51
52
class DNSSecurityManager extends SecurityManager {
53
54
55
56
/* run main/othervm LdapDnsProviderTest
57
58
* run main/othervm LdapDnsProviderTest nosm
59
* run main/othervm LdapDnsProviderTest smnodns
60
* run main/othervm LdapDnsProviderTest smdns
61
* run main/othervm LdapDnsProviderTest nosmbaddns
62
*/
63
64
private boolean dnsProvider = false;
65
66
public void setAllowDnsProvider(boolean allow) {
67
dnsProvider = allow;
68
}
69
70
@Override
71
public void checkPermission(Permission p) {
72
if (p.getName().equals("ldapDnsProvider") && !dnsProvider) {
73
throw new SecurityException(p.getName());
74
}
75
}
76
}
77
78
class ProviderTest implements Callable<Boolean> {
79
80
private final String url;
81
private final String expected;
82
private final Hashtable<String, String> env = new Hashtable<>(11);
83
84
public ProviderTest(String url, String expected) {
85
this.url = url;
86
this.expected = expected;
87
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
88
}
89
90
boolean shutItDown(InitialContext ctx) {
91
try {
92
if (ctx != null) ctx.close();
93
return true;
94
} catch (NamingException ex) {
95
return false;
96
}
97
}
98
99
public Boolean call() {
100
boolean passed;
101
InitialContext ctx = null;
102
103
if (url != null) {
104
env.put(Context.PROVIDER_URL, url);
105
}
106
107
try {
108
ctx = new InitialDirContext(env);
109
SearchControls scl = new SearchControls();
110
scl.setSearchScope(SearchControls.SUBTREE_SCOPE);
111
((InitialDirContext)ctx).search(
112
"ou=People,o=Test", "(objectClass=*)", scl);
113
throw new RuntimeException("Search should not complete");
114
} catch (NamingException e) {
115
e.printStackTrace();
116
passed = e.toString().contains(expected);
117
} finally {
118
shutItDown(ctx);
119
}
120
return passed;
121
}
122
}
123
124
public class LdapDnsProviderTest {
125
126
private static final String TEST_CLASSES =
127
System.getProperty("test.classes", ".");
128
129
public static void writeFile(String content, File dstFile)
130
throws IOException
131
{
132
try (FileOutputStream dst = new FileOutputStream(dstFile)) {
133
byte[] buf = content.getBytes();
134
dst.write(buf, 0, buf.length);
135
}
136
}
137
138
public static void installServiceConfigurationFile(String content) {
139
String filename = "com.sun.jndi.ldap.spi.LdapDnsProvider";
140
141
File dstDir = new File(TEST_CLASSES, "META-INF/services");
142
if (!dstDir.exists()) {
143
if (!dstDir.mkdirs()) {
144
throw new RuntimeException(
145
"could not create META-INF/services directory " + dstDir);
146
}
147
}
148
File dstFile = new File(dstDir, filename);
149
150
try {
151
writeFile(content, dstFile);
152
} catch (IOException e) {
153
throw new RuntimeException("could not install " + dstFile, e);
154
}
155
}
156
157
public static void main(String[] args) throws Exception {
158
if (args.length > 0 && args[0].equals("nosm")) {
159
// no security manager, serviceloader
160
installServiceConfigurationFile("dnsprovider.TestDnsProvider");
161
runTest("ldap:///dc=example,dc=com", "yupyupyup:389");
162
} else if (args.length > 0 && args[0].equals("smnodns")) {
163
// security manager & serviceloader
164
installServiceConfigurationFile("dnsprovider.TestDnsProvider");
165
// install security manager
166
System.setSecurityManager(new DNSSecurityManager());
167
runTest("ldap:///dc=example,dc=com", "ServiceConfigurationError");
168
} else if (args.length > 0 && args[0].equals("smdns")) {
169
// security manager & serviceloader
170
DNSSecurityManager sm = new DNSSecurityManager();
171
installServiceConfigurationFile("dnsprovider.TestDnsProvider");
172
// install security manager
173
System.setSecurityManager(sm);
174
sm.setAllowDnsProvider(true);
175
runTest("ldap:///dc=example,dc=com", "yupyupyup:389");
176
} else if (args.length > 0 && args[0].equals("nosmbaddns")) {
177
// no security manager, no serviceloader
178
// DefaultLdapDnsProvider
179
installServiceConfigurationFile("dnsprovider.MissingDnsProvider");
180
// no SecurityManager
181
runTest("ldap:///dc=example,dc=com", "not found");
182
} else {
183
// no security manager, no serviceloader
184
// DefaultLdapDnsProvider
185
System.err.println("TEST_CLASSES:");
186
System.err.println(TEST_CLASSES);
187
File f = new File(
188
TEST_CLASSES, "META-INF/services/com.sun.jndi.ldap.spi.LdapDnsProvider");
189
if (f.exists()) {
190
f.delete();
191
}
192
193
// no SecurityManager
194
runTest("ldap:///dc=example,dc=com", "localhost:389");
195
runTest("ldap://localhost/dc=example,dc=com", "localhost:389");
196
runTest("ldap://localhost:111/dc=example,dc=com", "localhost:111");
197
runTest("ldaps://localhost:111/dc=example,dc=com", "localhost:111");
198
runTest("ldaps://localhost/dc=example,dc=com", "localhost:636");
199
runTest(null, "localhost:389");
200
runTest("", "ConfigurationException");
201
}
202
}
203
204
private static void runTest(String url, String expected) {
205
FutureTask<Boolean> future =
206
new FutureTask<>(
207
new ProviderTest(url, expected));
208
new Thread(future).start();
209
210
System.err.println("Testing: " + url + ", " + expected);
211
while (!future.isDone()) {
212
try {
213
if (!future.get()) {
214
System.err.println("Test failed");
215
throw new RuntimeException(
216
"Test failed, ProviderTest returned false");
217
}
218
} catch (Exception e) {
219
if (!e.toString().contains(expected)) {
220
System.err.println("Test failed");
221
throw new RuntimeException(
222
"Test failed, unexpected result");
223
}
224
}
225
}
226
System.err.println("Test passed");
227
}
228
229
}
230
231
232