Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/LdapCtx/Reconnect.java
41159 views
1
/*
2
* Copyright (c) 2019, 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.ldap.InitialLdapContext;
26
import java.io.IOException;
27
import java.io.OutputStream;
28
import java.net.InetAddress;
29
import java.net.Socket;
30
import java.util.Hashtable;
31
import java.util.concurrent.Semaphore;
32
import java.util.concurrent.TimeUnit;
33
34
/*
35
* @test
36
* @bug 8217606
37
* @summary The LdapContext.reconnect method allows LDAP clients to initiate an
38
* LDAP bind operation on the existing connection. Invoking this method
39
* should not open a new connection under those circumstances.
40
*
41
* @library ../lib/
42
* @run main Reconnect
43
*/
44
public class Reconnect {
45
46
private static final byte[] BIND_RESPONSE = {
47
0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
48
0x01, 0x00, 0x04, 0x00, 0x04, 0x00
49
};
50
51
/*
52
* This test checks that there's only one connection from the client to
53
* the server.
54
*
55
* The mechanics is as follows. The first connection is awaited for some
56
* generous timeout to factor in a possibility of running on a slow system.
57
* Once the connection has been made, the second timeout begins. This
58
* second timeout is smaller. The test then verifies that no further
59
* connections have been made for that amount of time.
60
*/
61
public static void main(String[] args) throws Exception {
62
63
final Semaphore s = new Semaphore(0);
64
65
BaseLdapServer server = new BaseLdapServer() {
66
67
@Override
68
protected void beforeConnectionHandled(Socket socket) {
69
// Increment the number of connections from LDAP client
70
s.release(1);
71
}
72
73
@Override
74
protected void handleRequest(Socket socket,
75
LdapMessage msg,
76
OutputStream out)
77
throws IOException
78
{
79
switch (msg.getOperation()) {
80
case BIND_REQUEST:
81
out.write(BIND_RESPONSE);
82
default:
83
break;
84
}
85
}
86
};
87
88
try (var s1 = server.start()) {
89
Hashtable<String, Object> env = new Hashtable<>();
90
env.put(Context.INITIAL_CONTEXT_FACTORY,
91
"com.sun.jndi.ldap.LdapCtxFactory");
92
env.put(Context.PROVIDER_URL,
93
"ldap://" + InetAddress.getLoopbackAddress().getHostName()
94
+ ":" + server.getPort());
95
env.put("java.naming.ldap.version", "3");
96
97
// open connection
98
InitialLdapContext context = new InitialLdapContext(env, null);
99
100
// send bind request
101
context.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
102
context.addToEnvironment(Context.SECURITY_PRINCIPAL, "test");
103
context.addToEnvironment(Context.SECURITY_CREDENTIALS, "secret");
104
105
context.reconnect(null);
106
}
107
108
if (!s.tryAcquire(60L, TimeUnit.SECONDS)) {
109
throw new RuntimeException("No connection has been made");
110
}
111
112
if (s.tryAcquire(5L, TimeUnit.SECONDS)) {
113
throw new RuntimeException("Expected 1 connection, but found: "
114
+ (s.availablePermits() + 2));
115
}
116
}
117
}
118
119