0N/A/*
2362N/A * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4203167
0N/A *
0N/A * @summary RMI blocks in HttpAwareServerSocket.accept() if you telnet to it
0N/A * @author Adrian Colley
0N/A *
5551N/A * @library ../../../../../java/rmi/testlibrary
5551N/A * @build TestIface TestImpl TestImpl_Stub
0N/A * @run main/othervm/policy=security.policy/timeout=60 BlockAcceptTest
0N/A */
0N/A
0N/A/* This test attempts to stymie the RMI accept loop. The accept loop in
0N/A * RMI endlessly accepts a connection, spawns a thread for it, and repeats.
0N/A * The accept() call can be replaced by a user-supplied library which
0N/A * might foolishly block indefinitely in its accept() method, which would
0N/A * prevent RMI from accepting other connections on that socket.
0N/A *
0N/A * Unfortunately, HttpAwareServerSocket (default server socket) is/was such
0N/A * a foolish thing. It reads 4 bytes to see if they're "POST" before
0N/A * returning. The bug fix is to move the HTTP stuff into the mainloop,
0N/A * which has the side effect of enabling it for non-default socketfactories.
0N/A *
0N/A * This test:
0N/A * 1. Creates an object and exports it.
0N/A * 2. Connects to the listening RMI port and sends nothing, to hold it up.
0N/A * 3. Makes a regular call, using HTTP tunnelling.
0N/A * 4. Fails to deadlock, thereby passing the test.
0N/A *
0N/A * Some runtime dependencies I'm trying to eliminate:
0N/A * 1. We don't know the port number until after exporting the object, but
0N/A * have to set it in http.proxyPort somehow. Hopefully http.proxyPort
0N/A * isn't read too soon or this test will fail with a ConnectException.
0N/A */
0N/A
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.server.RMISocketFactory;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/Aimport sun.rmi.transport.proxy.RMIMasterSocketFactory;
0N/Aimport sun.rmi.transport.proxy.RMIHttpToPortSocketFactory;
0N/A
0N/Apublic class BlockAcceptTest
0N/A{
0N/A public static void main(String[] args)
0N/A throws Exception
0N/A {
0N/A // Make trouble for ourselves
0N/A if (System.getSecurityManager() == null)
0N/A System.setSecurityManager(new RMISecurityManager());
0N/A
0N/A // HTTP direct to the server port
0N/A System.setProperty("http.proxyHost", "127.0.0.1");
0N/A
0N/A // Set the socket factory.
0N/A System.err.println("(installing HTTP-out socket factory)");
0N/A HttpOutFactory fac = new HttpOutFactory();
0N/A RMISocketFactory.setSocketFactory(fac);
0N/A
0N/A // Create remote object
0N/A TestImpl impl = new TestImpl();
0N/A
0N/A // Export and get which port.
0N/A System.err.println("(exporting remote object)");
0N/A TestIface stub = impl.export();
0N/A try {
0N/A int port = fac.whichPort();
0N/A
0N/A // Sanity
0N/A if (port == 0)
0N/A throw new Error("TEST FAILED: export didn't reserve a port(?)");
0N/A
0N/A // Set the HTTP port, at last.
0N/A System.setProperty("http.proxyPort", port+"");
0N/A
0N/A // Now, connect to that port
0N/A //Thread.sleep(2000);
0N/A System.err.println("(connecting to listening port on 127.0.0.1:" +
0N/A port + ")");
0N/A Socket DoS = new Socket("127.0.0.1", port);
0N/A // we hold the connection open until done with the test.
0N/A
0N/A // The test itself: make a remote call and see if it's blocked or
0N/A // if it works
0N/A //Thread.sleep(2000);
0N/A System.err.println("(making RMI-through-HTTP call)");
0N/A System.err.println("(typical test failure deadlocks here)");
0N/A String result = stub.testCall("dummy load");
0N/A
0N/A System.err.println(" => " + result);
0N/A if (!("OK".equals(result)))
0N/A throw new Error("TEST FAILED: result not OK");
0N/A System.err.println("Test passed.");
0N/A
0N/A // Clean up, including writing a byte to that connection just in
0N/A // case an optimizer thought of optimizing it out of existence
0N/A try {
0N/A DoS.getOutputStream().write(0);
0N/A DoS.getOutputStream().close();
0N/A } catch (Throwable apathy) {
0N/A }
0N/A
0N/A } finally {
0N/A try {
0N/A impl.unexport();
0N/A } catch (Throwable unmatter) {
0N/A }
0N/A }
0N/A
0N/A // Should exit here
0N/A }
0N/A
0N/A private static class HttpOutFactory
0N/A extends RMISocketFactory
0N/A {
0N/A private int servport = 0;
0N/A
0N/A public Socket createSocket(String h, int p)
0N/A throws IOException
0N/A {
0N/A return ((new RMIHttpToPortSocketFactory()).createSocket(h, p));
0N/A }
0N/A
0N/A /** Create a server socket and remember which port it's on.
0N/A * Aborts if createServerSocket(0) is called twice, because then
0N/A * it doesn't know whether to remember the first or second port.
0N/A */
0N/A public ServerSocket createServerSocket(int p)
0N/A throws IOException
0N/A {
0N/A ServerSocket ss;
0N/A ss = (new RMIMasterSocketFactory()).createServerSocket(p);
0N/A if (p == 0) {
0N/A if (servport != 0) {
0N/A System.err.println("TEST FAILED: " +
0N/A "Duplicate createServerSocket(0)");
0N/A throw new Error("Test aborted (createServerSocket)");
0N/A }
0N/A servport = ss.getLocalPort();
0N/A }
0N/A return (ss);
0N/A }
0N/A
0N/A /** Return which port was reserved by createServerSocket(0).
0N/A * If the return value was 0, createServerSocket(0) wasn't called.
0N/A */
0N/A public int whichPort() {
0N/A return (servport);
0N/A }
0N/A } // end class HttpOutFactory
0N/A}