Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/NamingExceptionMessageTest.java
41153 views
1
/*
2
* Copyright (c) 2020, 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.
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 8062947
27
* @summary Test that NamingException message text matches the failure reason
28
* @library /test/lib lib
29
* @run testng NamingExceptionMessageTest
30
*/
31
32
import javax.naming.Context;
33
import javax.naming.NamingException;
34
import javax.naming.ServiceUnavailableException;
35
import javax.naming.directory.InitialDirContext;
36
import java.io.IOException;
37
import java.io.OutputStream;
38
import java.net.InetAddress;
39
import java.net.InetSocketAddress;
40
import java.net.ServerSocket;
41
import java.net.Socket;
42
import java.util.Hashtable;
43
import java.util.concurrent.CountDownLatch;
44
import java.util.concurrent.TimeUnit;
45
46
import org.testng.annotations.Test;
47
import org.testng.Assert;
48
import jdk.test.lib.net.URIBuilder;
49
50
public class NamingExceptionMessageTest {
51
52
@Test
53
public void timeoutMessageTest() throws Exception {
54
try (var ldapServer = TestLdapServer.newInstance(false)) {
55
ldapServer.start();
56
ldapServer.awaitStartup();
57
var env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE);
58
var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
59
System.out.println("Got naming exception:" + namingException);
60
Assert.assertEquals(namingException.getMessage(), EXPECTED_TIMEOUT_MESSAGE);
61
}
62
}
63
64
@Test
65
public void connectionClosureMessageTest() throws Exception {
66
try (var ldapServer = TestLdapServer.newInstance(true)) {
67
ldapServer.start();
68
ldapServer.awaitStartup();
69
var env = ldapServer.getInitialLdapCtxEnvironment(0);
70
var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
71
if (namingException instanceof ServiceUnavailableException) {
72
// If naming exception is ServiceUnavailableException it could mean
73
// that the connection was closed on test server-side before LDAP client starts
74
// read-out of the reply message. For such cases test run is considered as successful.
75
System.out.println("Got ServiceUnavailableException: Test PASSED");
76
} else {
77
// If exception is not ServiceUnavailableException - check the exception message
78
System.out.println("Got NamingException:" + namingException);
79
Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE);
80
}
81
}
82
}
83
84
// Test LDAP server
85
private static class TestLdapServer extends BaseLdapServer {
86
87
private final boolean closeConnections;
88
private final CountDownLatch startupLatch = new CountDownLatch(1);
89
90
public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) {
91
// Create environment for initial LDAP context
92
Hashtable<Object, Object> env = new Hashtable<>();
93
94
// Activate LDAPv3
95
env.put("java.naming.ldap.version", "3");
96
97
// De-activate the ManageDsaIT control
98
env.put(Context.REFERRAL, "follow");
99
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
100
env.put(Context.PROVIDER_URL, getUrlString());
101
env.put(Context.SECURITY_AUTHENTICATION, "simple");
102
env.put(Context.SECURITY_PRINCIPAL, "name");
103
env.put(Context.SECURITY_CREDENTIALS, "pwd");
104
105
if (readTimeoutValue > 0) {
106
env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue));
107
env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue));
108
}
109
110
return env;
111
}
112
113
private String getUrlString() {
114
String url = URIBuilder.newBuilder()
115
.scheme("ldap")
116
.loopback()
117
.port(getPort())
118
.buildUnchecked()
119
.toString();
120
return url;
121
}
122
123
public static TestLdapServer newInstance(boolean closeConnections) throws IOException {
124
ServerSocket srvSock = new ServerSocket();
125
srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
126
return new TestLdapServer(srvSock, closeConnections);
127
}
128
129
void awaitStartup() throws InterruptedException {
130
startupLatch.await();
131
}
132
133
private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) {
134
super(serverSocket);
135
this.closeConnections = closeConnections;
136
137
}
138
139
@Override
140
protected void beforeAcceptingConnections() {
141
startupLatch.countDown();
142
}
143
144
@Override
145
protected void handleRequest(Socket socket,
146
LdapMessage msg,
147
OutputStream out)
148
throws IOException {
149
switch (msg.getOperation()) {
150
case BIND_REQUEST:
151
if (closeConnections) {
152
// Give some time for LDAP client to start-up
153
try {
154
TimeUnit.MILLISECONDS.sleep(100);
155
} catch (InterruptedException e) {
156
}
157
// Close the socket
158
closeSilently(socket);
159
} else {
160
try {
161
TimeUnit.DAYS.sleep(Integer.MAX_VALUE);
162
} catch (InterruptedException e) {
163
Thread.currentThread().interrupt();
164
}
165
}
166
default:
167
break;
168
}
169
}
170
}
171
172
// Expected message for case when connection is closed on server side
173
private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed";
174
// read and connect timeouts value
175
private static final int TIMEOUT_VALUE = 129;
176
// Expected message text when connection is timed-out
177
private static final String EXPECTED_TIMEOUT_MESSAGE = String.format(
178
"LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE);
179
}
180
181