Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/transport/tcp/blockAccept/BlockAcceptTest.java
38867 views
/*1* Copyright (c) 1999, 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 420316725*26* @summary RMI blocks in HttpAwareServerSocket.accept() if you telnet to it27* @author Adrian Colley28*29* @library ../../../../../java/rmi/testlibrary30* @build TestIface TestImpl TestImpl_Stub31* @run main/othervm/policy=security.policy/timeout=60 -Dsun.rmi.server.disableIncomingHttp=false BlockAcceptTest32*/3334/* This test attempts to stymie the RMI accept loop. The accept loop in35* RMI endlessly accepts a connection, spawns a thread for it, and repeats.36* The accept() call can be replaced by a user-supplied library which37* might foolishly block indefinitely in its accept() method, which would38* prevent RMI from accepting other connections on that socket.39*40* Unfortunately, HttpAwareServerSocket (default server socket) is/was such41* a foolish thing. It reads 4 bytes to see if they're "POST" before42* returning. The bug fix is to move the HTTP stuff into the mainloop,43* which has the side effect of enabling it for non-default socketfactories.44*45* This test:46* 1. Creates an object and exports it.47* 2. Connects to the listening RMI port and sends nothing, to hold it up.48* 3. Makes a regular call, using HTTP tunnelling.49* 4. Fails to deadlock, thereby passing the test.50*51* Some runtime dependencies I'm trying to eliminate:52* 1. We don't know the port number until after exporting the object, but53* have to set it in http.proxyPort somehow. Hopefully http.proxyPort54* isn't read too soon or this test will fail with a ConnectException.55*/5657import java.rmi.*;58import java.rmi.server.RMISocketFactory;59import java.io.*;60import java.net.*;6162import sun.rmi.transport.proxy.RMIMasterSocketFactory;63import sun.rmi.transport.proxy.RMIHttpToPortSocketFactory;6465public class BlockAcceptTest66{67public static void main(String[] args)68throws Exception69{70// Make trouble for ourselves71if (System.getSecurityManager() == null)72System.setSecurityManager(new RMISecurityManager());7374// HTTP direct to the server port75System.setProperty("http.proxyHost", "127.0.0.1");7677// Set the socket factory.78System.err.println("(installing HTTP-out socket factory)");79HttpOutFactory fac = new HttpOutFactory();80RMISocketFactory.setSocketFactory(fac);8182// Create remote object83TestImpl impl = new TestImpl();8485// Export and get which port.86System.err.println("(exporting remote object)");87TestIface stub = impl.export();88try {89int port = fac.whichPort();9091// Sanity92if (port == 0)93throw new Error("TEST FAILED: export didn't reserve a port(?)");9495// Set the HTTP port, at last.96System.setProperty("http.proxyPort", port+"");9798// Now, connect to that port99//Thread.sleep(2000);100System.err.println("(connecting to listening port on 127.0.0.1:" +101port + ")");102Socket DoS = new Socket("127.0.0.1", port);103// we hold the connection open until done with the test.104105// The test itself: make a remote call and see if it's blocked or106// if it works107//Thread.sleep(2000);108System.err.println("(making RMI-through-HTTP call)");109System.err.println("(typical test failure deadlocks here)");110String result = stub.testCall("dummy load");111112System.err.println(" => " + result);113if (!("OK".equals(result)))114throw new Error("TEST FAILED: result not OK");115System.err.println("Test passed.");116117// Clean up, including writing a byte to that connection just in118// case an optimizer thought of optimizing it out of existence119try {120DoS.getOutputStream().write(0);121DoS.getOutputStream().close();122} catch (Throwable apathy) {123}124125} finally {126try {127impl.unexport();128} catch (Throwable unmatter) {129}130}131132// Should exit here133}134135private static class HttpOutFactory136extends RMISocketFactory137{138private int servport = 0;139140public Socket createSocket(String h, int p)141throws IOException142{143return ((new RMIHttpToPortSocketFactory()).createSocket(h, p));144}145146/** Create a server socket and remember which port it's on.147* Aborts if createServerSocket(0) is called twice, because then148* it doesn't know whether to remember the first or second port.149*/150public ServerSocket createServerSocket(int p)151throws IOException152{153ServerSocket ss;154ss = (new RMIMasterSocketFactory()).createServerSocket(p);155if (p == 0) {156if (servport != 0) {157System.err.println("TEST FAILED: " +158"Duplicate createServerSocket(0)");159throw new Error("Test aborted (createServerSocket)");160}161servport = ss.getLocalPort();162}163return (ss);164}165166/** Return which port was reserved by createServerSocket(0).167* If the return value was 0, createServerSocket(0) wasn't called.168*/169public int whichPort() {170return (servport);171}172} // end class HttpOutFactory173}174175176