1 /* 2 * Copyright (c) 2013, 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 /* 25 * @test 26 * @bug 8024952 27 * @summary ClassCastException in PlainSocketImpl.accept() when using custom socketImpl 28 * @run main/othervm CustomSocketImplFactory 29 */ 30 31 import java.net.*; 32 import java.io.*; 33 34 public class CustomSocketImplFactory implements SocketImplFactory { 35 36 @Override createSocketImpl()37 public SocketImpl createSocketImpl() { 38 try { 39 SocketImpl s = new CustomSocketImpl(); 40 System.out.println("Created " + s); 41 return s; 42 } catch (Exception e) { 43 throw new RuntimeException(e); 44 } 45 } 46 main(String[] args)47 public static void main(String[] args) throws Exception { 48 49 Socket.setSocketImplFactory(new CustomSocketImplFactory()); 50 try (ServerSocket ss = new ServerSocket(0)) { 51 ss.setSoTimeout(1); 52 ss.accept(); 53 System.out.println("PASS"); 54 } catch (SocketTimeoutException | NullPointerException e) { 55 // Not a real socket impl 56 } 57 } 58 59 class CustomSocketImpl extends SocketImpl { 60 create(boolean stream)61 public void create(boolean stream) throws IOException { 62 } 63 connect(String host, int port)64 public void connect(String host, int port) throws IOException { 65 } 66 connect(InetAddress addr, int port)67 public void connect(InetAddress addr, int port) throws IOException { 68 } 69 connect(SocketAddress addr, int timeout)70 public void connect(SocketAddress addr, int timeout) throws IOException { 71 } 72 bind(InetAddress host, int port)73 public void bind(InetAddress host, int port) throws IOException { 74 } 75 listen(int backlog)76 public void listen(int backlog) throws IOException { 77 } 78 accept(SocketImpl s)79 public void accept(SocketImpl s) throws IOException { 80 } 81 getInputStream()82 public InputStream getInputStream() throws IOException { 83 return null; 84 } 85 getOutputStream()86 public OutputStream getOutputStream() throws IOException { 87 return null; 88 } 89 available()90 public int available() throws IOException { 91 return 0; 92 } 93 close()94 public void close() throws IOException { 95 } 96 sendUrgentData(int data)97 public void sendUrgentData(int data) throws IOException { 98 } 99 getOption(int i)100 public Object getOption(int i) throws SocketException { 101 return null; 102 } 103 setOption(int i, Object o)104 public void setOption(int i, Object o) throws SocketException { 105 } 106 } 107 } 108