Path: blob/master/test/jdk/java/rmi/registry/nonLocalRegistry/NonLocalRegistryTest.java
41154 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;2829/* @test30* @bug 817477031* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.32* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.33* @run main/othervm/manual -Dregistry.host=rmi-registry-host NonLocalRegistryTest34*/3536/**37* Verify that access checks for Registry.bind(), .rebind(), and .unbind()38* are prevented on remote access to the registry.39*40* This test is a manual test and uses a standard rmiregistry running41* on a *different* host.42* The test verifies that the access check is performed *before* the object to be43* bound or rebound is deserialized.44*45* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmiregistry}.46* It will not show any output.47*48* On the first host modify the @run command above to replace "rmi-registry-host"49* with the hostname or IP address of the different host and run the test with jtreg.50*/51public class NonLocalRegistryTest {5253public static void main(String[] args) throws Exception {5455String host = System.getProperty("registry.host");56if (host == null || host.isEmpty()) {57throw new RuntimeException("Specify host with system property: -Dregistry.host=<host>");58}5960// Check if running the test on a local system; it only applies to remote61String myHostName = InetAddress.getLocalHost().getHostName();62Set<InetAddress> myAddrs = Set.of(InetAddress.getAllByName(myHostName));63Set<InetAddress> hostAddrs = Set.of(InetAddress.getAllByName(host));64if (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))65|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress())) {66throw new RuntimeException("Error: property 'registry.host' must not be the local host%n");67}6869Registry registry = LocateRegistry.getRegistry(host, Registry.REGISTRY_PORT);7071try {72registry.bind("foo", null);73throw new RuntimeException("Remote access should not succeed for method: bind");74} catch (Exception e) {75assertIsAccessException(e);76}7778try {79registry.rebind("foo", null);80throw new RuntimeException("Remote access should not succeed for method: rebind");81} catch (Exception e) {82assertIsAccessException(e);83}8485try {86registry.unbind("foo");87throw new RuntimeException("Remote access should not succeed for method: unbind");88} catch (Exception e) {89assertIsAccessException(e);90}91}9293/**94* Check the exception chain for the expected AccessException and message.95* @param ex the exception from the remote invocation.96*/97private static void assertIsAccessException(Throwable ex) {98Throwable t = ex;99while (!(t instanceof AccessException) && t.getCause() != null) {100t = t.getCause();101}102if (t instanceof AccessException) {103String msg = t.getMessage();104int asIndex = msg.indexOf("Registry");105int rrIndex = msg.indexOf("Registry.Registry"); // Obsolete error text106int disallowIndex = msg.indexOf("disallowed");107int nonLocalHostIndex = msg.indexOf("non-local host");108if (asIndex < 0 ||109rrIndex != -1 ||110disallowIndex < 0 ||111nonLocalHostIndex < 0 ) {112throw new RuntimeException("exception message is malformed", t);113}114System.out.printf("Found expected AccessException: %s%n%n", t);115} else {116throw new RuntimeException("AccessException did not occur when expected", ex);117}118}119}120121122