Path: blob/master/test/jdk/java/rmi/registry/nonLocalRegistry/NonLocalSkeletonTest.java
41154 views
/*1* Copyright (c) 2017, 2019, 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*/2223import sun.rmi.server.UnicastRef;24import sun.rmi.transport.LiveRef;25import sun.rmi.transport.tcp.TCPEndpoint;2627import java.lang.reflect.InvocationHandler;2829import java.lang.reflect.Proxy;30import java.net.InetAddress;31import java.rmi.AccessException;32import java.rmi.RemoteException;33import java.rmi.registry.LocateRegistry;34import java.rmi.registry.Registry;35import java.rmi.server.ObjID;36import java.rmi.server.RemoteObjectInvocationHandler;37import java.security.AccessController;38import java.security.PrivilegedAction;39import java.util.Arrays;40import java.util.Set;414243/* @test44* @bug 821845345* @library ../../testlibrary46* @modules java.rmi/sun.rmi.registry:+open java.rmi/sun.rmi.server:+open47* java.rmi/sun.rmi.transport:+open java.rmi/sun.rmi.transport.tcp:+open48* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.49* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.50* @run main/othervm -Dregistry.host=localhost NonLocalSkeletonTest51*/5253/*54* @test55* @library ../../testlibrary56* @modules java.rmi/sun.rmi.registry:+open java.rmi/sun.rmi.server:+open57* java.rmi/sun.rmi.transport:+open java.rmi/sun.rmi.transport.tcp:+open58* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.59* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.60* @run main/othervm/manual -Dregistry.host=rmi-registry-host NonLocalSkeletonTest61*/6263/**64* Verify that access checks for Registry.bind(), .rebind(), and .unbind()65* are prevented on remote access to the registry.66*67* This test is a manual test and uses a standard rmiregistry running68* on a *different* host.69* The test verifies that the access check is performed *before* the object to be70* bound or rebound is deserialized.71*72* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmiregistry}.73* It will not show any output.74*75* On the first host modify the @run command above to replace "rmi-registry-host"76* with the hostname or IP address of the different host and run the test with jtreg.77*/78public class NonLocalSkeletonTest {7980public static void main(String[] args) throws Exception {81String host = System.getProperty("registry.host");82if (host == null || host.isEmpty()) {83throw new RuntimeException("supply a remote host with -Dregistry.host=hostname");84}8586// Check if running the test on a local system; it only applies to remote87String myHostName = InetAddress.getLocalHost().getHostName();88Set<InetAddress> myAddrs = Set.copyOf(Arrays.asList(InetAddress.getAllByName(myHostName)));89Set<InetAddress> hostAddrs = Set.copyOf(Arrays.asList(InetAddress.getAllByName(host)));90boolean isLocal = (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))91|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress()));9293int port;94if (isLocal) {95// Create a local Registry to use for the test96port = TestLibrary.getUnusedRandomPort();97Registry registry = LocateRegistry.createRegistry(port);98System.out.printf("local registry port: %s%n", registry);99} else {100// Use regular rmi registry for non-local test101port = Registry.REGISTRY_PORT;102}103104try {105106Registry r = nonStaticRegistryProxy(host, port);107108System.out.printf("RegistryRef: %s%n", r);109110r.rebind("anyRef", r);111if (!isLocal) {112throw new RuntimeException("non-local bind should have failed to host: " + host);113} else {114System.out.printf("local rebind succeeded%n");115}116} catch (RemoteException rex) {117if (!isLocal) {118assertIsAccessException(rex);119} else {120throw rex;121}122}123}124125/* Returns a non-static proxy for the registry.126* Follows the form of sun.rmi.server.Util.createProxy.127* @param implClass the RegistryImpl128* @param clientRef the registry reference129**/130static Registry nonStaticRegistryProxy(String host, int port) {131final ClassLoader loader = Registry.class.getClassLoader();132final Class<?>[] interfaces = new Class<?>[]{Registry.class};133134LiveRef liveRef = new LiveRef(new ObjID(ObjID.REGISTRY_ID),135new TCPEndpoint(host, port, null, null),136false);137138final InvocationHandler handler = new RemoteObjectInvocationHandler(new UnicastRef(liveRef));139140PrivilegedAction<Registry> action = () -> (Registry) Proxy.newProxyInstance(loader,141interfaces, handler);142return AccessController.doPrivileged(action);143}144145/**146* Check the exception chain for the expected AccessException and message.147* @param ex the exception from the remote invocation.148*/149private static void assertIsAccessException(Throwable ex) {150Throwable t = ex;151while (!(t instanceof AccessException) && t.getCause() != null) {152t = t.getCause();153}154if (t instanceof AccessException) {155String msg = t.getMessage();156int asIndex = msg.indexOf("Registry");157int rrIndex = msg.indexOf("Registry.Registry"); // Obsolete error text158int disallowIndex = msg.indexOf("disallowed");159int nonLocalHostIndex = msg.indexOf("non-local host");160if (asIndex < 0 ||161rrIndex != -1 ||162disallowIndex < 0 ||163nonLocalHostIndex < 0 ) {164throw new RuntimeException("exception message is malformed", t);165}166System.out.printf("Found expected AccessException: %s%n%n", t);167} else {168throw new RuntimeException("AccessException did not occur when expected", ex);169}170}171}172173174