Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/nonLocalRegistry/NonLocalRegistryTest.java
38828 views
/*1* Copyright (c) 2017, 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 java.net.InetAddress;24import java.rmi.AccessException;25import java.rmi.registry.LocateRegistry;26import java.rmi.registry.Registry;27import java.util.Set;28import java.util.HashSet;2930/* @test31* @bug 817477032* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.33* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.34* @run main/othervm/manual -Dregistry.host=rmi-registry-host NonLocalRegistryTest35*/3637/**38* Verify that access checks for Registry.bind(), .rebind(), and .unbind()39* are prevented on remote access to the registry.40*41* This test is a manual test and uses a standard rmiregistry running42* on a *different* host.43* The test verifies that the access check is performed *before* the object to be44* bound or rebound is deserialized.45*46* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmiregistry}.47* It will not show any output.48*49* On the first host modify the @run command above to replace "rmi-registry-host"50* with the hostname or IP address of the different host and run the test with jtreg.51*/52public class NonLocalRegistryTest {5354public static void main(String[] args) throws Exception {5556String host = System.getProperty("registry.host");57if (host == null || host.isEmpty()) {58throw new RuntimeException("Specify host with system property: -Dregistry.host=<host>");59}6061// Check if running the test on a local system; it only applies to remote62String myHostName = InetAddress.getLocalHost().getHostName();63Set<InetAddress> myAddrs = new HashSet<>();64InetAddress[] myAddrsArr = InetAddress.getAllByName(myHostName);65for (InetAddress a : myAddrsArr) {66myAddrs.add(a);67}68Set<InetAddress> hostAddrs = new HashSet<>();69InetAddress[] hostAddrsArr = InetAddress.getAllByName(host);70for (InetAddress a : hostAddrsArr) {71hostAddrs.add(a);72}73if (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))74|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress())) {75throw new RuntimeException("Error: property 'registry.host' must not be the local host%n");76}7778Registry registry = LocateRegistry.getRegistry(host, Registry.REGISTRY_PORT);7980try {81registry.bind("foo", null);82throw new RuntimeException("Remote access should not succeed for method: bind");83} catch (Exception e) {84assertIsAccessException(e);85}8687try {88registry.rebind("foo", null);89throw new RuntimeException("Remote access should not succeed for method: rebind");90} catch (Exception e) {91assertIsAccessException(e);92}9394try {95registry.unbind("foo");96throw new RuntimeException("Remote access should not succeed for method: unbind");97} catch (Exception e) {98assertIsAccessException(e);99}100}101102/**103* Check the exception chain for the expected AccessException and message.104* @param ex the exception from the remote invocation.105*/106private static void assertIsAccessException(Throwable ex) {107Throwable t = ex;108while (!(t instanceof AccessException) && t.getCause() != null) {109t = t.getCause();110}111if (t instanceof AccessException) {112String msg = t.getMessage();113int asIndex = msg.indexOf("Registry");114int rrIndex = msg.indexOf("Registry.Registry"); // Obsolete error text115int disallowIndex = msg.indexOf("disallowed");116int nonLocalHostIndex = msg.indexOf("non-local host");117if (asIndex < 0 ||118rrIndex != -1 ||119disallowIndex < 0 ||120nonLocalHostIndex < 0 ) {121throw new RuntimeException("exception message is malformed", t);122}123System.out.printf("Found expected AccessException: %s%n%n", t);124} else {125throw new RuntimeException("AccessException did not occur when expected", ex);126}127}128}129130131