Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/transport/tcp/DisableRMIOverHttp/DisableRMIOverHTTPTest.java
38867 views
/*1* Copyright (c) 2018, 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 815896325*26* @summary Disable RMI over HTTP by default27*28* @library ../../../../../java/rmi/testlibrary29* @build TestIface TestImpl30* @run main/othervm/timeout=60 DisableRMIOverHTTPTest31* @run main/othervm/timeout=60 -Dsun.rmi.server.disableIncomingHttp=false DisableRMIOverHTTPTest32*/3334/*35* This test is an adaptation of ../blockAccept/BlockAcceptTest.java36*37* This test:38* 1. Creates an object and exports it.39* 2. Makes a regular call, using HTTP tunnelling.40* 3. Either throws an exception if RMI over HTTP is disabled or completes41* execution if not.42*/4344import java.rmi.*;45import java.rmi.server.RMISocketFactory;46import java.io.*;47import java.net.*;4849import sun.rmi.transport.proxy.RMIMasterSocketFactory;50import sun.rmi.transport.proxy.RMIHttpToPortSocketFactory;5152public class DisableRMIOverHTTPTest53{54public static void main(String[] args)55throws Exception56{57// HTTP direct to the server port58System.setProperty("http.proxyHost", "127.0.0.1");59boolean incomingHttpDisabled =60Boolean.valueOf(61System.getProperty(62"sun.rmi.server.disableIncomingHttp", "true")63.equalsIgnoreCase("true"));6465// Set the socket factory.66System.err.println("(installing HTTP-out socket factory)");67HttpOutFactory fac = new HttpOutFactory();68RMISocketFactory.setSocketFactory(fac);6970// Create remote object71TestImpl impl = new TestImpl();7273// Export and get which port.74System.err.println("(exporting remote object)");75TestIface stub = impl.export();76try {77int port = fac.whichPort();7879// Sanity80if (port == 0)81throw new Error("TEST FAILED: export didn't reserve a port(?)");8283// The test itself: make a remote call and see if it's blocked or84// if it works85//Thread.sleep(2000);86System.err.println("(making RMI-through-HTTP call)");87String result = stub.testCall("dummy load");88System.err.println(" => " + result);8990if ("OK".equals(result)) {91if (incomingHttpDisabled) {92throw new Error(93"TEST FAILED: should not receive result if incoming http is disabled");94}95} else {96if (!incomingHttpDisabled) {97throw new Error("TEST FAILED: result not OK");98}99}100System.err.println("Test passed.");101} catch (UnmarshalException e) {102if (!incomingHttpDisabled) {103throw e;104} else {105System.err.println("Test passed.");106}107} finally {108try {109impl.unexport();110} catch (Throwable unmatter) {111}112}113114// Should exit here115}116117private static class HttpOutFactory118extends RMISocketFactory119{120private int servport = 0;121122public Socket createSocket(String h, int p)123throws IOException124{125return ((new RMIHttpToPortSocketFactory()).createSocket(h, p));126}127128/** Create a server socket and remember which port it's on.129* Aborts if createServerSocket(0) is called twice, because then130* it doesn't know whether to remember the first or second port.131*/132public ServerSocket createServerSocket(int p)133throws IOException134{135ServerSocket ss;136ss = (new RMIMasterSocketFactory()).createServerSocket(p);137if (p == 0) {138if (servport != 0) {139System.err.println("TEST FAILED: " +140"Duplicate createServerSocket(0)");141throw new Error("Test aborted (createServerSocket)");142}143servport = ss.getLocalPort();144}145return (ss);146}147148/** Return which port was reserved by createServerSocket(0).149* If the return value was 0, createServerSocket(0) wasn't called.150*/151public int whichPort() {152return (servport);153}154} // end class HttpOutFactory155}156157158