Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/ldap/DisconnectNPETest.java
38855 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.naming.Context;24import javax.naming.NamingException;25import javax.naming.directory.DirContext;26import javax.naming.directory.InitialDirContext;27import java.io.Closeable;28import java.io.IOException;29import java.io.InputStream;30import java.io.OutputStream;31import java.net.InetAddress;32import java.net.ServerSocket;33import java.net.Socket;34import java.util.Hashtable;3536/*37* @test38* @bug 820533039* @summary Test that If a connection has already been established and then40* the LDAP directory server sends an (unsolicited)41* "Notice of Disconnection", make sure client handle it correctly,42* no NPE been thrown.43* @run main/othervm DisconnectNPETest44*/4546public class DisconnectNPETest {47// Normally the NPE bug should be hit less than 100 times run, but just in48// case, we set repeat count to 1000 here.49private static final int REPEAT_COUNT = 1000;5051public static void main(String[] args) throws IOException {52new DisconnectNPETest().run();53}5455private ServerSocket serverSocket;56private Hashtable<Object, Object> env;57private TestLDAPServer server;5859private void initRes() throws IOException {60serverSocket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());61server = new TestLDAPServer();62server.start();63}6465private void initTest() {66env = new Hashtable<>();67env.put(Context.INITIAL_CONTEXT_FACTORY,68"com.sun.jndi.ldap.LdapCtxFactory");69env.put(Context.PROVIDER_URL, String.format("ldap://%s:%d/",70InetAddress.getLoopbackAddress().getHostName(),71serverSocket.getLocalPort()));72env.put(Context.SECURITY_AUTHENTICATION, "simple");73env.put(Context.SECURITY_PRINCIPAL,74"cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US");75env.put(Context.SECURITY_CREDENTIALS, "secret123");76}7778private void run() throws IOException {79initRes();80initTest();81int count = 0;82try {83while (count < REPEAT_COUNT) {84count++;85InitialDirContext context = null;86try {87context = new InitialDirContext(env);88} catch (NamingException ne) {89System.out.println("(" + count + "/" + REPEAT_COUNT90+ ") It's ok to get NamingException: " + ne);91// for debug92ne.printStackTrace(System.out);93} finally {94cleanupContext(context);95}96}97} finally {98System.out.println("Test count: " + count + "/" + REPEAT_COUNT);99cleanupTest();100}101}102103private void cleanupTest() {104if (server != null) {105server.stopServer();106}107cleanupClosableRes(serverSocket);108}109110private void cleanupContext(DirContext context) {111if (context != null) {112try {113context.close();114} catch (NamingException e) {115// ignore116}117}118}119120private static void cleanupClosableRes(Closeable res) {121if (res != null) {122try {123res.close();124} catch (Exception e) {125// ignore126}127}128}129130class TestLDAPServer extends Thread {131private volatile boolean isRunning;132133TestLDAPServer() {134isRunning = true;135}136137private void stopServer() {138isRunning = false;139}140141@Override142public void run() {143try {144while (isRunning) {145Socket clientSocket = serverSocket.accept();146Thread handler = new Thread(147new LDAPServerHandler(clientSocket));148handler.start();149}150} catch (IOException e) {151if (isRunning) {152throw new RuntimeException(e);153}154}155}156}157158static class LDAPServerHandler implements Runnable {159// "Notice of Disconnection" message160private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,1610x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,1620x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,1630x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,1640x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,1650x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,166(byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,1670x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,1680x32, 0x30, 0x30, 0x33, 0x36 };169private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,1700x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };171private final Socket clientSocket;172173private LDAPServerHandler(final Socket clientSocket) {174this.clientSocket = clientSocket;175}176177@Override178public void run() {179try (OutputStream out = clientSocket.getOutputStream();180InputStream in = clientSocket.getInputStream()) {181if (in.read() > 0) {182in.skip(in.available());183out.write(BIND_RESPONSE);184out.write(DISCONNECT_MSG);185}186} catch (IOException e) {187e.printStackTrace();188} finally {189try {190clientSocket.close();191} catch (IOException e) {192e.printStackTrace();193}194}195}196}197}198199200