Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/transport/handshakeFailure/HandshakeFailure.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @library ../../testlibrary33* @build TestLibrary34* @run main/othervm HandshakeFailure35*/3637import java.net.ServerSocket;38import java.net.Socket;39import java.rmi.ConnectException;40import java.rmi.ConnectIOException;41import java.rmi.MarshalException;42import java.rmi.registry.LocateRegistry;43import java.rmi.registry.Registry;4445public class HandshakeFailure {4647private static final int PORT = TestLibrary.getUnusedRandomPort();48private static final int TIMEOUT = 10000;4950public static void main(String[] args) throws Exception {5152/*53* Listen on port...54*/55ServerSocket serverSocket = new ServerSocket(PORT);5657/*58* (Attempt RMI call to port in separate thread.)59*/60Registry registry = LocateRegistry.getRegistry(PORT);61Connector connector = new Connector(registry);62Thread t = new Thread(connector);63t.setDaemon(true);64t.start();6566/*67* ...accept one connection from port and send non-JRMP data.68*/69Socket socket = serverSocket.accept();70socket.getOutputStream().write("Wrong way".getBytes());71socket.close();7273/*74* Wait for call attempt to finish, and analyze result.75*/76t.join(TIMEOUT);77synchronized (connector) {78if (connector.success) {79throw new RuntimeException(80"TEST FAILED: remote call succeeded??");81}82if (connector.exception == null) {83throw new RuntimeException(84"TEST FAILED: remote call did not time out");85} else {86System.err.println("remote call failed with exception:");87connector.exception.printStackTrace();88System.err.println();8990if (connector.exception instanceof MarshalException) {91System.err.println(92"TEST FAILED: MarshalException thrown, expecting " +93"java.rmi.ConnectException or ConnectIOException");94} else if (connector.exception instanceof ConnectException ||95connector.exception instanceof ConnectIOException)96{97System.err.println(98"TEST PASSED: java.rmi.ConnectException or " +99"ConnectIOException thrown");100} else {101throw new RuntimeException(102"TEST FAILED: unexpected Exception thrown",103connector.exception);104}105}106}107}108109private static class Connector implements Runnable {110111private final Registry registry;112113boolean success = false;114Exception exception = null;115116Connector(Registry registry) {117this.registry = registry;118}119120public void run() {121try {122registry.lookup("Dale Cooper");123synchronized (this) {124success = true;125}126} catch (Exception e) {127synchronized (this) {128exception = e;129}130}131}132}133}134135136