Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/DisconnectNPETest.java
41153 views
1
/*
2
* Copyright (c) 2018, 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
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.IOException;
29
import java.io.OutputStream;
30
import java.net.InetAddress;
31
import java.net.ServerSocket;
32
import java.net.Socket;
33
import java.util.Hashtable;
34
import jdk.test.lib.net.URIBuilder;
35
36
/*
37
* @test
38
* @bug 8205330
39
* @summary Test that If a connection has already been established and then
40
* the LDAP directory server sends an (unsolicited)
41
* "Notice of Disconnection", make sure client handle it correctly,
42
* no NPE been thrown.
43
* @library lib/ /test/lib
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
// "Notice of Disconnection" message
53
private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,
54
0x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,
55
0x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,
56
0x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,
57
0x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,
58
0x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,
59
(byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,
60
0x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,
61
0x32, 0x30, 0x30, 0x33, 0x36 };
62
private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,
63
0x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };
64
65
public static void main(String[] args) throws IOException {
66
new DisconnectNPETest().run();
67
}
68
69
private ServerSocket serverSocket;
70
private Hashtable<Object, Object> env;
71
72
private void initRes() throws IOException {
73
serverSocket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
74
}
75
76
private void initTest() {
77
env = new Hashtable<>();
78
env.put(Context.INITIAL_CONTEXT_FACTORY,
79
"com.sun.jndi.ldap.LdapCtxFactory");
80
env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()
81
.scheme("ldap")
82
.loopback()
83
.port(serverSocket.getLocalPort())
84
.buildUnchecked().toString());
85
env.put(Context.SECURITY_AUTHENTICATION, "simple");
86
env.put(Context.SECURITY_PRINCIPAL,
87
"cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US");
88
env.put(Context.SECURITY_CREDENTIALS, "secret123");
89
}
90
91
private void run() throws IOException {
92
initRes();
93
initTest();
94
int count = 0;
95
try (var ignore = new BaseLdapServer(serverSocket) {
96
@Override
97
protected void handleRequest(Socket socket, LdapMessage request,
98
OutputStream out) throws IOException {
99
if (request.getOperation()
100
== LdapMessage.Operation.BIND_REQUEST) {
101
out.write(BIND_RESPONSE);
102
out.write(DISCONNECT_MSG);
103
}
104
}
105
}.start()) {
106
while (count < REPEAT_COUNT) {
107
count++;
108
InitialDirContext context = null;
109
try {
110
context = new InitialDirContext(env);
111
} catch (NamingException ne) {
112
System.out.println("(" + count + "/" + REPEAT_COUNT
113
+ ") It's ok to get NamingException: " + ne);
114
// for debug
115
ne.printStackTrace(System.out);
116
} finally {
117
cleanupContext(context);
118
}
119
}
120
} finally {
121
System.out.println("Test count: " + count + "/" + REPEAT_COUNT);
122
}
123
}
124
125
private void cleanupContext(DirContext context) {
126
if (context != null) {
127
try {
128
context.close();
129
} catch (NamingException e) {
130
// ignore
131
}
132
}
133
}
134
}
135
136