/*
* Copyright ( c ) 2018 , 2020 , Oracle and / or its affiliates . All rights reserved .
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
*
* This code is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License version 2 only , as
* published by the Free Software Foundation .
*
* This code is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* version 2 for more details ( a copy is included in the LICENSE file that
* accompanied this code ) .
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 USA .
*
* Please contact Oracle , 500 Oracle Parkway , Redwood Shores , CA 94065 USA
* or visit www . oracle . com if you need additional information or have any
* questions .
*/
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
import jdk.test.lib.net.URIBuilder;
/*
* @ test
* @ bug 8205330
* @ summary Test that If a connection has already been established and then
* the LDAP directory server sends an ( unsolicited )
* " Notice of Disconnection " , make sure client handle it correctly ,
* no NPE been thrown .
* @ library lib / / test / lib
* @ run main / othervm DisconnectNPETest
*/
public class DisconnectNPETest {
// Normally the NPE bug should be hit less than 100 times run, but just in
// case, we set repeat count to 1000 here.
private static final int REPEAT_COUNT = 1000 ;
// "Notice of Disconnection" message
private static final byte [] DISCONNECT_MSG = { 0 x30, 0 x4C, 0 x02, 0 x01,
0 x00, 0 x78, 0 x47, 0 x0A, 0 x01, 0 x34, 0 x04, 0 x00, 0 x04, 0 x28,
0 x55, 0 x4E, 0 x41, 0 x56, 0 x41, 0 x49, 0 x4C, 0 x41, 0 x42, 0 x4C,
0 x45, 0 x3A, 0 x20, 0 x54, 0 x68, 0 x65, 0 x20, 0 x73, 0 x65, 0 x72,
0 x76, 0 x65, 0 x72, 0 x20, 0 x77, 0 x69, 0 x6C, 0 x6C, 0 x20, 0 x64,
0 x69, 0 x73, 0 x63, 0 x6F, 0 x6E, 0 x6E, 0 x65, 0 x63, 0 x74, 0 x21,
(byte ) 0 x8A, 0 x16, 0 x31, 0 x2E, 0 x33, 0 x2E, 0 x36, 0 x2E, 0 x31,
0 x2E, 0 x34, 0 x2E, 0 x31, 0 x2E, 0 x31, 0 x34, 0 x36, 0 x36, 0 x2E,
0 x32, 0 x30, 0 x30, 0 x33, 0 x36 };
private static final byte [] BIND_RESPONSE = { 0 x30, 0 x0C, 0 x02, 0 x01,
0 x01, 0 x61, 0 x07, 0 x0A, 0 x01, 0 x00, 0 x04, 0 x00, 0 x04, 0 x00 };
public static void main(String[] args) throws IOException {
new DisconnectNPETest().run();
}
private ServerSocket serverSocket;
private Hashtable<Object, Object> env;
private void initRes() throws IOException {
serverSocket = new ServerSocket(0 , 0 , InetAddress.getLoopbackAddress());
}
private void initTest() {
env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory" );
env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()
.scheme("ldap" )
.loopback()
.port(serverSocket.getLocalPort())
.buildUnchecked().toString());
env.put(Context.SECURITY_AUTHENTICATION, "simple" );
env.put(Context.SECURITY_PRINCIPAL,
"cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US" );
env.put(Context.SECURITY_CREDENTIALS, "secret123" );
}
private void run() throws IOException {
initRes();
initTest();
int count = 0 ;
try (var ignore = new BaseLdapServer(serverSocket) {
@Override
protected void handleRequest(Socket socket, LdapMessage request,
OutputStream out) throws IOException {
if (request.getOperation()
== LdapMessage.Operation.BIND_REQUEST) {
out.write(BIND_RESPONSE);
out.write(DISCONNECT_MSG);
}
}
}.start()) {
while (count < REPEAT_COUNT) {
count++;
InitialDirContext context = null ;
try {
context = new InitialDirContext(env);
} catch (NamingException ne) {
System.out.println("(" + count + "/" + REPEAT_COUNT
+ ") It's ok to get NamingException: " + ne);
// for debug
ne.printStackTrace(System.out);
} finally {
cleanupContext(context);
}
}
} finally {
System.out.println("Test count: " + count + "/" + REPEAT_COUNT);
}
}
private void cleanupContext(DirContext context) {
if (context != null ) {
try {
context.close();
} catch (NamingException e) {
// ignore
}
}
}
}
Messung V0.5 in Prozent C=96 H=92 G=93
¤ Dauer der Verarbeitung: 0.3 Sekunden
¤
*© Formatika GbR, Deutschland