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/DisconnectNPETest.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.
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
import javax.naming.Context;
25
import javax.naming.NamingException;
26
import javax.naming.directory.DirContext;
27
import javax.naming.directory.InitialDirContext;
28
import java.io.Closeable;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
import java.net.InetAddress;
33
import java.net.ServerSocket;
34
import java.net.Socket;
35
import java.util.Hashtable;
36
37
/*
38
* @test
39
* @bug 8205330
40
* @summary Test that If a connection has already been established and then
41
* the LDAP directory server sends an (unsolicited)
42
* "Notice of Disconnection", make sure client handle it correctly,
43
* no NPE been thrown.
44
* @run main/othervm DisconnectNPETest
45
*/
46
47
public class DisconnectNPETest {
48
// Normally the NPE bug should be hit less than 100 times run, but just in
49
// case, we set repeat count to 1000 here.
50
private static final int REPEAT_COUNT = 1000;
51
52
public static void main(String[] args) throws IOException {
53
new DisconnectNPETest().run();
54
}
55
56
private ServerSocket serverSocket;
57
private Hashtable<Object, Object> env;
58
private TestLDAPServer server;
59
60
private void initRes() throws IOException {
61
serverSocket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
62
server = new TestLDAPServer();
63
server.start();
64
}
65
66
private void initTest() {
67
env = new Hashtable<>();
68
env.put(Context.INITIAL_CONTEXT_FACTORY,
69
"com.sun.jndi.ldap.LdapCtxFactory");
70
env.put(Context.PROVIDER_URL, String.format("ldap://%s:%d/",
71
InetAddress.getLoopbackAddress().getHostName(),
72
serverSocket.getLocalPort()));
73
env.put(Context.SECURITY_AUTHENTICATION, "simple");
74
env.put(Context.SECURITY_PRINCIPAL,
75
"cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US");
76
env.put(Context.SECURITY_CREDENTIALS, "secret123");
77
}
78
79
private void run() throws IOException {
80
initRes();
81
initTest();
82
int count = 0;
83
try {
84
while (count < REPEAT_COUNT) {
85
count++;
86
InitialDirContext context = null;
87
try {
88
context = new InitialDirContext(env);
89
} catch (NamingException ne) {
90
System.out.println("(" + count + "/" + REPEAT_COUNT
91
+ ") It's ok to get NamingException: " + ne);
92
// for debug
93
ne.printStackTrace(System.out);
94
} finally {
95
cleanupContext(context);
96
}
97
}
98
} finally {
99
System.out.println("Test count: " + count + "/" + REPEAT_COUNT);
100
cleanupTest();
101
}
102
}
103
104
private void cleanupTest() {
105
if (server != null) {
106
server.stopServer();
107
}
108
cleanupClosableRes(serverSocket);
109
}
110
111
private void cleanupContext(DirContext context) {
112
if (context != null) {
113
try {
114
context.close();
115
} catch (NamingException e) {
116
// ignore
117
}
118
}
119
}
120
121
private static void cleanupClosableRes(Closeable res) {
122
if (res != null) {
123
try {
124
res.close();
125
} catch (Exception e) {
126
// ignore
127
}
128
}
129
}
130
131
class TestLDAPServer extends Thread {
132
private volatile boolean isRunning;
133
134
TestLDAPServer() {
135
isRunning = true;
136
}
137
138
private void stopServer() {
139
isRunning = false;
140
}
141
142
@Override
143
public void run() {
144
try {
145
while (isRunning) {
146
Socket clientSocket = serverSocket.accept();
147
Thread handler = new Thread(
148
new LDAPServerHandler(clientSocket));
149
handler.start();
150
}
151
} catch (IOException e) {
152
if (isRunning) {
153
throw new RuntimeException(e);
154
}
155
}
156
}
157
}
158
159
static class LDAPServerHandler implements Runnable {
160
// "Notice of Disconnection" message
161
private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,
162
0x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,
163
0x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,
164
0x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,
165
0x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,
166
0x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,
167
(byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,
168
0x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,
169
0x32, 0x30, 0x30, 0x33, 0x36 };
170
private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,
171
0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
172
private final Socket clientSocket;
173
174
private LDAPServerHandler(final Socket clientSocket) {
175
this.clientSocket = clientSocket;
176
}
177
178
@Override
179
public void run() {
180
try (OutputStream out = clientSocket.getOutputStream();
181
InputStream in = clientSocket.getInputStream()) {
182
if (in.read() > 0) {
183
in.skip(in.available());
184
out.write(BIND_RESPONSE);
185
out.write(DISCONNECT_MSG);
186
}
187
} catch (IOException e) {
188
e.printStackTrace();
189
} finally {
190
try {
191
clientSocket.close();
192
} catch (IOException e) {
193
e.printStackTrace();
194
}
195
}
196
}
197
}
198
}
199
200