Path: blob/master/test/jdk/com/sun/jndi/ldap/DeadSSLSocketFactory.java
41153 views
/*1* Copyright (c) 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.net.SocketFactory;24import javax.net.ssl.SSLSocket;25import javax.net.ssl.SSLSocketFactory;26import java.io.IOException;27import java.net.InetAddress;28import java.net.Socket;29import java.util.concurrent.atomic.AtomicBoolean;30import java.util.concurrent.atomic.AtomicReference;3132/*33* A custom socket factory used to override the default socket factory and track the LDAP client connection.34* Factory can create only one SSLSocket. See the DeadServerTimeoutSSLTest test.35*/36public class DeadSSLSocketFactory extends SocketFactory {37// Client socket that is used by LDAP connection38public static AtomicReference<SSLSocket> firstCreatedSocket = new AtomicReference<>();3940// Boolean to track if connection socket has been opened41public static AtomicBoolean isConnectionOpened = new AtomicBoolean(false);4243// Default SSLSocketFactory that will be used for SSL socket creation44final SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();4546// Create unconnected socket47public Socket createSocket() throws IOException {48if (!isConnectionOpened.getAndSet(true)) {49System.err.println("DeadSSLSocketFactory: Creating unconnected socket");50firstCreatedSocket.set((SSLSocket) factory.createSocket());51return firstCreatedSocket.get();52} else {53throw new RuntimeException("DeadSSLSocketFactory only allows creation of one SSL socket");54}55}5657public DeadSSLSocketFactory() {58System.err.println("DeadSSLSocketFactory: Constructor call");59}6061public static SocketFactory getDefault() {62System.err.println("DeadSSLSocketFactory: acquiring DeadSSLSocketFactory as default socket factory");63return new DeadSSLSocketFactory();64}6566@Override67public Socket createSocket(String host, int port) throws IOException {68// Not used by DeadSSLLdapTimeoutTest69return factory.createSocket(host, port);70}7172@Override73public Socket createSocket(String host, int port, InetAddress localHost,74int localPort) throws IOException {75// Not used by DeadSSLLdapTimeoutTest76return factory.createSocket(host, port, localHost, localPort);77}7879@Override80public Socket createSocket(InetAddress host, int port) throws IOException {81// Not used by DeadSSLLdapTimeoutTest82return factory.createSocket(host, port);83}8485@Override86public Socket createSocket(InetAddress address, int port,87InetAddress localAddress, int localPort) throws IOException {88// Not used by DeadSSLLdapTimeoutTest89return factory.createSocket(address, port, localAddress, localPort);90}91}92939495