Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/BalancedParentheses.java
41153 views
1
/*
2
* Copyright (c) 2009, 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 6449574
27
* @library /test/lib
28
* @summary Invalid ldap filter is accepted and processed
29
*/
30
31
import java.io.*;
32
import javax.naming.*;
33
import javax.naming.directory.*;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.net.SocketAddress;
37
import java.util.Hashtable;
38
39
import java.net.Socket;
40
import java.net.ServerSocket;
41
42
import jdk.test.lib.net.URIBuilder;
43
44
public class BalancedParentheses {
45
// Should we run the client or server in a separate thread?
46
//
47
// Both sides can throw exceptions, but do you have a preference
48
// as to which side should be the main thread.
49
static boolean separateServerThread = true;
50
51
// use any free port by default
52
volatile int serverPort = 0;
53
54
// Is the server ready to serve?
55
volatile static boolean serverReady = false;
56
57
// Define the server side of the test.
58
//
59
// If the server prematurely exits, serverReady will be set to true
60
// to avoid infinite hangs.
61
void doServerSide() throws Exception {
62
// Create unbound server socket
63
ServerSocket serverSock = new ServerSocket();
64
65
// And bind it to the loopback address
66
SocketAddress sockAddr = new InetSocketAddress(
67
InetAddress.getLoopbackAddress(), 0);
68
serverSock.bind(sockAddr);
69
70
// signal client, it's ready to accecpt connection
71
serverPort = serverSock.getLocalPort();
72
serverReady = true;
73
74
// accept a connection
75
Socket socket = serverSock.accept();
76
System.out.println("Server: Connection accepted");
77
78
InputStream is = socket.getInputStream();
79
OutputStream os = socket.getOutputStream();
80
81
// read the bindRequest
82
while (is.read() != -1) {
83
// ignore
84
is.skip(is.available());
85
break;
86
}
87
88
byte[] bindResponse = {0x30, 0x0C, 0x02, 0x01, 0x01, 0x61, 0x07, 0x0A,
89
0x01, 0x00, 0x04, 0x00, 0x04, 0x00};
90
// write bindResponse
91
os.write(bindResponse);
92
os.flush();
93
94
// ignore any more request.
95
while (is.read() != -1) {
96
// ignore
97
is.skip(is.available());
98
}
99
100
is.close();
101
os.close();
102
socket.close();
103
serverSock.close();
104
}
105
106
// Define the client side of the test.
107
//
108
// If the server prematurely exits, serverReady will be set to true
109
// to avoid infinite hangs.
110
void doClientSide() throws Exception {
111
// Wait for server to get started.
112
while (!serverReady) {
113
Thread.sleep(50);
114
}
115
116
// set up the environment for creating the initial context
117
Hashtable<Object, Object> env = new Hashtable<>();
118
env.put(Context.INITIAL_CONTEXT_FACTORY,
119
"com.sun.jndi.ldap.LdapCtxFactory");
120
// Construct the provider URL
121
String providerURL = URIBuilder.newBuilder()
122
.scheme("ldap")
123
.loopback()
124
.port(serverPort)
125
.build().toString();
126
env.put(Context.PROVIDER_URL, providerURL);
127
env.put("com.sun.jndi.ldap.read.timeout", "1000");
128
129
// env.put(Context.SECURITY_AUTHENTICATION, "simple");
130
// env.put(Context.SECURITY_PRINCIPAL,"cn=root");
131
// env.put(Context.SECURITY_CREDENTIALS,"root");
132
133
// create initial context
134
DirContext context = new InitialDirContext(env);
135
136
// searching
137
SearchControls scs = new SearchControls();
138
scs.setSearchScope(SearchControls.SUBTREE_SCOPE);
139
140
try {
141
NamingEnumeration<SearchResult> answer = context.search(
142
"o=sun,c=us", "(&(cn=Bob)))", scs);
143
} catch (InvalidSearchFilterException isfe) {
144
// ignore, it is the expected filter exception.
145
System.out.println("Expected exception: " + isfe.getMessage());
146
} catch (NamingException ne) {
147
// maybe a read timeout exception, as the server does not response.
148
throw new Exception("Expect a InvalidSearchFilterException", ne);
149
}
150
151
try {
152
NamingEnumeration<SearchResult> answer = context.search(
153
"o=sun,c=us", ")(&(cn=Bob)", scs);
154
} catch (InvalidSearchFilterException isfe) {
155
// ignore, it is the expected filter exception.
156
System.out.println("Expected exception: " + isfe.getMessage());
157
} catch (NamingException ne) {
158
// maybe a read timeout exception, as the server does not response.
159
throw new Exception("Expect a InvalidSearchFilterException", ne);
160
}
161
162
try {
163
NamingEnumeration<SearchResult> answer = context.search(
164
"o=sun,c=us", "(&(cn=Bob))", scs);
165
} catch (InvalidSearchFilterException isfe) {
166
// ignore, it is the expected filter exception.
167
throw new Exception("Unexpected ISFE", isfe);
168
} catch (NamingException ne) {
169
// maybe a read timeout exception, as the server does not response.
170
System.out.println("Expected exception: " + ne.getMessage());
171
}
172
173
context.close();
174
}
175
176
/*
177
* ============================================================
178
* The remainder is just support stuff
179
*/
180
181
// client and server thread
182
Thread clientThread = null;
183
Thread serverThread = null;
184
185
// client and server exceptions
186
volatile Exception serverException = null;
187
volatile Exception clientException = null;
188
189
void startServer(boolean newThread) throws Exception {
190
if (newThread) {
191
serverThread = new Thread() {
192
public void run() {
193
try {
194
doServerSide();
195
} catch (Exception e) {
196
/*
197
* Our server thread just died.
198
*
199
* Release the client, if not active already...
200
*/
201
System.err.println("Server died...");
202
System.err.println(e);
203
serverReady = true;
204
serverException = e;
205
}
206
}
207
};
208
serverThread.start();
209
} else {
210
doServerSide();
211
}
212
}
213
214
void startClient(boolean newThread) throws Exception {
215
if (newThread) {
216
clientThread = new Thread() {
217
public void run() {
218
try {
219
doClientSide();
220
} catch (Exception e) {
221
/*
222
* Our client thread just died.
223
*/
224
System.err.println("Client died...");
225
clientException = e;
226
}
227
}
228
};
229
clientThread.start();
230
} else {
231
doClientSide();
232
}
233
}
234
235
// Primary constructor, used to drive remainder of the test.
236
BalancedParentheses() throws Exception {
237
if (separateServerThread) {
238
startServer(true);
239
startClient(false);
240
} else {
241
startClient(true);
242
startServer(false);
243
}
244
245
/*
246
* Wait for other side to close down.
247
*/
248
if (separateServerThread) {
249
serverThread.join();
250
} else {
251
clientThread.join();
252
}
253
254
/*
255
* When we get here, the test is pretty much over.
256
*
257
* If the main thread excepted, that propagates back
258
* immediately. If the other thread threw an exception, we
259
* should report back.
260
*/
261
if (serverException != null) {
262
System.out.print("Server Exception:");
263
throw serverException;
264
}
265
if (clientException != null) {
266
System.out.print("Client Exception:");
267
throw clientException;
268
}
269
}
270
271
public static void main(String[] args) throws Exception {
272
// start the test
273
new BalancedParentheses();
274
}
275
276
}
277
278