|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +import javax.naming.Context; |
| 25 | +import javax.naming.directory.DirContext; |
| 26 | +import javax.naming.directory.InitialDirContext; |
| 27 | +import javax.net.SocketFactory; |
| 28 | +import java.net.InetAddress; |
| 29 | +import java.net.Socket; |
| 30 | +import java.net.SocketAddress; |
| 31 | +import java.io.ByteArrayInputStream; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.io.OutputStream; |
| 35 | +import java.util.Hashtable; |
| 36 | + |
| 37 | +import jdk.test.lib.process.OutputAnalyzer; |
| 38 | +import jdk.test.lib.process.ProcessTools; |
| 39 | + |
| 40 | +/* |
| 41 | + * @test |
| 42 | + * @bug 8313657 |
| 43 | + * @summary make sure socket is closed when the error happens for OutputStream flushing |
| 44 | + * The value of provider url can be random, not necessary to be the one in the code |
| 45 | + * @library /test/lib |
| 46 | + * @run main/othervm SocketCloseTest |
| 47 | + */ |
| 48 | + |
| 49 | +public class SocketCloseTest { |
| 50 | + public static String SOCKET_CLOSED_MSG = "The socket has been closed."; |
| 51 | + public static String SOCKET_NOT_CLOSED_MSG = "The socket was not closed."; |
| 52 | + public static String BAD_FLUSH = "Bad flush!"; |
| 53 | + private static final byte[] BIND_RESPONSE = new byte[]{ |
| 54 | + 48, 12, 2, 1, 1, 97, 7, 10, 1, 0, 4, 0, 4, 0 |
| 55 | + }; |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + SocketCloseTest scTest = new SocketCloseTest(); |
| 59 | + scTest.runCloseSocketScenario(); |
| 60 | + } |
| 61 | + |
| 62 | + public void runCloseSocketScenario() throws Exception { |
| 63 | + Hashtable<String, Object> props = new Hashtable<>(); |
| 64 | + |
| 65 | + props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); |
| 66 | + props.put(Context.PROVIDER_URL, "ldap://localhost:1389/o=example"); |
| 67 | + props.put("java.naming.ldap.factory.socket", CustomSocketFactory.class.getName()); |
| 68 | + try { |
| 69 | + final DirContext ctx = new InitialDirContext(props); |
| 70 | + } catch (Exception e) { |
| 71 | + if (CustomSocketFactory.customSocket.closeMethodCalledCount() > 0) { |
| 72 | + System.out.println(SOCKET_CLOSED_MSG); |
| 73 | + } else { |
| 74 | + System.out.println(SOCKET_NOT_CLOSED_MSG); |
| 75 | + throw e; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static class CustomSocketFactory extends SocketFactory { |
| 81 | + public static CustomSocket customSocket = new CustomSocket(); |
| 82 | + |
| 83 | + public static CustomSocketFactory getDefault() { |
| 84 | + return new CustomSocketFactory(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Socket createSocket() { |
| 89 | + return customSocket; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Socket createSocket(String s, int timeout) { |
| 94 | + return customSocket; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Socket createSocket(String host, int port, InetAddress localHost, |
| 99 | + int localPort) { |
| 100 | + return customSocket; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Socket createSocket(InetAddress host, int port) { |
| 105 | + return customSocket; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Socket createSocket(InetAddress address, int port, |
| 110 | + InetAddress localAddress, int localPort) { |
| 111 | + return customSocket; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static class LdapInputStream extends InputStream { |
| 116 | + private ByteArrayInputStream bos; |
| 117 | + |
| 118 | + public LdapInputStream() { |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int read() throws IOException { |
| 123 | + bos = new ByteArrayInputStream(BIND_RESPONSE); |
| 124 | + return bos.read(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static class LdapOutputStream extends OutputStream { |
| 129 | + |
| 130 | + @Override |
| 131 | + public void write(int b) throws IOException { |
| 132 | + System.out.println("output stream writing"); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void flush() throws IOException { |
| 137 | + System.out.println(BAD_FLUSH); |
| 138 | + throw new IOException(BAD_FLUSH); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static class CustomSocket extends Socket { |
| 143 | + private int closeMethodCalled = 0; |
| 144 | + private LdapOutputStream output = new LdapOutputStream(); |
| 145 | + private LdapInputStream input = new LdapInputStream(); |
| 146 | + |
| 147 | + public void connect(SocketAddress address, int timeout) { |
| 148 | + } |
| 149 | + |
| 150 | + public InputStream getInputStream() { |
| 151 | + return input; |
| 152 | + } |
| 153 | + |
| 154 | + public OutputStream getOutputStream() { |
| 155 | + return output; |
| 156 | + } |
| 157 | + |
| 158 | + public int closeMethodCalledCount() { |
| 159 | + return closeMethodCalled; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void close() throws IOException { |
| 164 | + closeMethodCalled++; |
| 165 | + super.close(); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments