Path: blob/master/test/jdk/java/rmi/transport/handshakeFailure/HandshakeFailure.java
41155 views
/*1* Copyright (c) 2001, 2016, 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*/2223/* @test24* @bug 448596625* @summary Whan an RMI (JRMP) connection is made to a TCP address that is26* listening, so the connection is accepted, but the server responds with27* invalid JRMP protocol (such as because a non-JRMP server is currently28* listening at that address), the client application should receive a29* java.rmi.ConnectException or ConnectIOException, not a MarshalException.30* @author Peter Jones31*32* @modules java.rmi/sun.rmi.registry33* java.rmi/sun.rmi.server34* java.rmi/sun.rmi.transport35* java.rmi/sun.rmi.transport.tcp36* @run main/othervm HandshakeFailure37*/3839import java.net.ServerSocket;40import java.net.Socket;41import java.rmi.ConnectException;42import java.rmi.ConnectIOException;43import java.rmi.MarshalException;44import java.rmi.registry.LocateRegistry;45import java.rmi.registry.Registry;4647public class HandshakeFailure {4849private static final int TIMEOUT = 10000;5051public static void main(String[] args) throws Exception {5253/*54* Listen on port...55*/56ServerSocket serverSocket = new ServerSocket(0);57int port = serverSocket.getLocalPort();5859/*60* (Attempt RMI call to port in separate thread.)61*/62Registry registry = LocateRegistry.getRegistry(port);63Connector connector = new Connector(registry);64Thread t = new Thread(connector);65t.setDaemon(true);66t.start();6768/*69* ...accept one connection from port and send non-JRMP data.70*/71Socket socket = serverSocket.accept();72socket.getOutputStream().write("Wrong way".getBytes());73socket.close();7475/*76* Wait for call attempt to finish, and analyze result.77*/78t.join(TIMEOUT);79synchronized (connector) {80if (connector.success) {81throw new RuntimeException(82"TEST FAILED: remote call succeeded??");83}84if (connector.exception == null) {85throw new RuntimeException(86"TEST FAILED: remote call did not time out");87} else {88System.err.println("remote call failed with exception:");89connector.exception.printStackTrace();90System.err.println();9192if (connector.exception instanceof MarshalException) {93throw new RuntimeException(94"TEST FAILED: MarshalException thrown, expecting " +95"java.rmi.ConnectException or ConnectIOException");96} else if (connector.exception instanceof ConnectException ||97connector.exception instanceof ConnectIOException)98{99System.err.println(100"TEST PASSED: java.rmi.ConnectException or " +101"ConnectIOException thrown");102} else {103throw new RuntimeException(104"TEST FAILED: unexpected Exception thrown",105connector.exception);106}107}108}109}110111private static class Connector implements Runnable {112113private final Registry registry;114115boolean success = false;116Exception exception = null;117118Connector(Registry registry) {119this.registry = registry;120}121122public void run() {123try {124registry.lookup("Dale Cooper");125synchronized (this) {126success = true;127}128} catch (Exception e) {129synchronized (this) {130exception = e;131}132}133}134}135}136137138