1/* 2 * Copyright (c) 2001, 2016, 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/* @test 25 * @bug 4485966 26 * @summary Whan an RMI (JRMP) connection is made to a TCP address that is 27 * listening, so the connection is accepted, but the server responds with 28 * invalid JRMP protocol (such as because a non-JRMP server is currently 29 * listening at that address), the client application should receive a 30 * java.rmi.ConnectException or ConnectIOException, not a MarshalException. 31 * @author Peter Jones 32 * 33 * @modules java.rmi/sun.rmi.registry 34 * java.rmi/sun.rmi.server 35 * java.rmi/sun.rmi.transport 36 * java.rmi/sun.rmi.transport.tcp 37 * @run main/othervm HandshakeFailure 38 */ 39 40import java.net.ServerSocket; 41import java.net.Socket; 42import java.rmi.ConnectException; 43import java.rmi.ConnectIOException; 44import java.rmi.MarshalException; 45import java.rmi.registry.LocateRegistry; 46import java.rmi.registry.Registry; 47 48public class HandshakeFailure { 49 50 private static final int TIMEOUT = 10000; 51 52 public static void main(String[] args) throws Exception { 53 54 /* 55 * Listen on port... 56 */ 57 ServerSocket serverSocket = new ServerSocket(0); 58 int port = serverSocket.getLocalPort(); 59 60 /* 61 * (Attempt RMI call to port in separate thread.) 62 */ 63 Registry registry = LocateRegistry.getRegistry(port); 64 Connector connector = new Connector(registry); 65 Thread t = new Thread(connector); 66 t.setDaemon(true); 67 t.start(); 68 69 /* 70 * ...accept one connection from port and send non-JRMP data. 71 */ 72 Socket socket = serverSocket.accept(); 73 socket.getOutputStream().write("Wrong way".getBytes()); 74 socket.close(); 75 76 /* 77 * Wait for call attempt to finish, and analyze result. 78 */ 79 t.join(TIMEOUT); 80 synchronized (connector) { 81 if (connector.success) { 82 throw new RuntimeException( 83 "TEST FAILED: remote call succeeded??"); 84 } 85 if (connector.exception == null) { 86 throw new RuntimeException( 87 "TEST FAILED: remote call did not time out"); 88 } else { 89 System.err.println("remote call failed with exception:"); 90 connector.exception.printStackTrace(); 91 System.err.println(); 92 93 if (connector.exception instanceof MarshalException) { 94 throw new RuntimeException( 95 "TEST FAILED: MarshalException thrown, expecting " + 96 "java.rmi.ConnectException or ConnectIOException"); 97 } else if (connector.exception instanceof ConnectException || 98 connector.exception instanceof ConnectIOException) 99 { 100 System.err.println( 101 "TEST PASSED: java.rmi.ConnectException or " + 102 "ConnectIOException thrown"); 103 } else { 104 throw new RuntimeException( 105 "TEST FAILED: unexpected Exception thrown", 106 connector.exception); 107 } 108 } 109 } 110 } 111 112 private static class Connector implements Runnable { 113 114 private final Registry registry; 115 116 boolean success = false; 117 Exception exception = null; 118 119 Connector(Registry registry) { 120 this.registry = registry; 121 } 122 123 public void run() { 124 try { 125 registry.lookup("Dale Cooper"); 126 synchronized (this) { 127 success = true; 128 } 129 } catch (Exception e) { 130 synchronized (this) { 131 exception = e; 132 } 133 } 134 } 135 } 136} 137