Path: blob/master/test/jdk/com/sun/jndi/ldap/DisconnectNPETest.java
41153 views
/*1* Copyright (c) 2018, 2020, 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.IOException;28import java.io.OutputStream;29import java.net.InetAddress;30import java.net.ServerSocket;31import java.net.Socket;32import java.util.Hashtable;33import jdk.test.lib.net.URIBuilder;3435/*36* @test37* @bug 820533038* @summary Test that If a connection has already been established and then39* the LDAP directory server sends an (unsolicited)40* "Notice of Disconnection", make sure client handle it correctly,41* no NPE been thrown.42* @library lib/ /test/lib43* @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;5051// "Notice of Disconnection" message52private static final byte[] DISCONNECT_MSG = { 0x30, 0x4C, 0x02, 0x01,530x00, 0x78, 0x47, 0x0A, 0x01, 0x34, 0x04, 0x00, 0x04, 0x28,540x55, 0x4E, 0x41, 0x56, 0x41, 0x49, 0x4C, 0x41, 0x42, 0x4C,550x45, 0x3A, 0x20, 0x54, 0x68, 0x65, 0x20, 0x73, 0x65, 0x72,560x76, 0x65, 0x72, 0x20, 0x77, 0x69, 0x6C, 0x6C, 0x20, 0x64,570x69, 0x73, 0x63, 0x6F, 0x6E, 0x6E, 0x65, 0x63, 0x74, 0x21,58(byte) 0x8A, 0x16, 0x31, 0x2E, 0x33, 0x2E, 0x36, 0x2E, 0x31,590x2E, 0x34, 0x2E, 0x31, 0x2E, 0x31, 0x34, 0x36, 0x36, 0x2E,600x32, 0x30, 0x30, 0x33, 0x36 };61private static final byte[] BIND_RESPONSE = { 0x30, 0x0C, 0x02, 0x01,620x01, 0x61, 0x07, 0x0A, 0x01, 0x00, 0x04, 0x00, 0x04, 0x00 };6364public static void main(String[] args) throws IOException {65new DisconnectNPETest().run();66}6768private ServerSocket serverSocket;69private Hashtable<Object, Object> env;7071private void initRes() throws IOException {72serverSocket = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());73}7475private void initTest() {76env = new Hashtable<>();77env.put(Context.INITIAL_CONTEXT_FACTORY,78"com.sun.jndi.ldap.LdapCtxFactory");79env.put(Context.PROVIDER_URL, URIBuilder.newBuilder()80.scheme("ldap")81.loopback()82.port(serverSocket.getLocalPort())83.buildUnchecked().toString());84env.put(Context.SECURITY_AUTHENTICATION, "simple");85env.put(Context.SECURITY_PRINCIPAL,86"cn=8205330,ou=Client6,ou=Vendor1,o=IMC,c=US");87env.put(Context.SECURITY_CREDENTIALS, "secret123");88}8990private void run() throws IOException {91initRes();92initTest();93int count = 0;94try (var ignore = new BaseLdapServer(serverSocket) {95@Override96protected void handleRequest(Socket socket, LdapMessage request,97OutputStream out) throws IOException {98if (request.getOperation()99== LdapMessage.Operation.BIND_REQUEST) {100out.write(BIND_RESPONSE);101out.write(DISCONNECT_MSG);102}103}104}.start()) {105while (count < REPEAT_COUNT) {106count++;107InitialDirContext context = null;108try {109context = new InitialDirContext(env);110} catch (NamingException ne) {111System.out.println("(" + count + "/" + REPEAT_COUNT112+ ") It's ok to get NamingException: " + ne);113// for debug114ne.printStackTrace(System.out);115} finally {116cleanupContext(context);117}118}119} finally {120System.out.println("Test count: " + count + "/" + REPEAT_COUNT);121}122}123124private void cleanupContext(DirContext context) {125if (context != null) {126try {127context.close();128} catch (NamingException e) {129// ignore130}131}132}133}134135136