Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/activation/nonLocalActivation/NonLocalActivationTest.java
38821 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.activation.ActivationSystem;26import java.rmi.registry.LocateRegistry;27import java.rmi.registry.Registry;28import java.util.Set;29import java.util.HashSet;3031/*32* @test33* @bug 817477034* @summary Verify that ActivationSystem rejects non-local access.35* The test is manual because the (non-local) host running rmid must be supplied as a property.36* @run main/manual/othervm -Dactivation.host=rmid-host NonLocalActivationTest37*/3839/**40* Lookup the ActivationSystem on a different host and invoke its remote interface methods.41* They should all throw an exception, non-local access is prohibited.42*43* This test is a manual test and uses rmid running on a *different* host.44* The default port (1098) for the Activation System is ok and expected.45* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmid}.46* It will not show any output.47*48* On the first host modify the @run command above to replace "rmid-host"49* with the hostname or IP address of the different host and run the test with jtreg.50*/51public class NonLocalActivationTest52{53public static void main(String[] args) throws Exception {5455String host = System.getProperty("activation.host");56if (host == null || host.isEmpty()) {57throw new RuntimeException("Specify host with system property: -Dactivation.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 = new HashSet<>();63InetAddress[] myAddrsArr = InetAddress.getAllByName(myHostName);64for (InetAddress a : myAddrsArr) {65myAddrs.add(a);66}67Set<InetAddress> hostAddrs = new HashSet<>();68InetAddress[] hostAddrsArr = InetAddress.getAllByName(host);69for (InetAddress a : hostAddrsArr) {70hostAddrs.add(a);71}72if (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))73|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress())) {74throw new RuntimeException("Error: property 'activation.host' must not be the local host%n");75}7677// Locate the registry operated by the ActivationSystem78// Test SystemRegistryImpl79Registry registry = LocateRegistry.getRegistry(host, ActivationSystem.SYSTEM_PORT);80try {81// Verify it is an ActivationSystem registry82registry.lookup("java.rmi.activation.ActivationSystem");83} catch (Exception nf) {84throw new RuntimeException("Not a ActivationSystem registry, does not contain java.rmi.activation.ActivationSystem", nf);85}8687try {88registry.bind("foo", null);89throw new RuntimeException("Remote access should not succeed for method: bind");90} catch (Exception e) {91assertIsAccessException(e, "Registry.bind");92}9394try {95registry.rebind("foo", null);96throw new RuntimeException("Remote access should not succeed for method: rebind");97} catch (Exception e) {98assertIsAccessException(e, "Registry.rebind");99}100101try {102registry.unbind("foo");103throw new RuntimeException("Remote access should not succeed for method: unbind");104} catch (Exception e) {105assertIsAccessException(e, "Registry.unbind");106}107108109// Locate the ActivationSystem on the specified host and default port.110// Test each of the ActivationSystem methods111ActivationSystem as = (ActivationSystem) registry.lookup("java.rmi.activation.ActivationSystem");112113// Argument is not material, access check is before arg processing114115try {116as.registerGroup(null);117} catch (Exception aex) {118assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");119}120121try {122as.getActivationDesc(null);123} catch (Exception aex) {124assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");125}126127try {128as.getActivationGroupDesc(null);129} catch (Exception aex) {130assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");131}132133try {134as.registerObject(null);135} catch (Exception aex) {136assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");137}138139try {140as.unregisterGroup(null);141} catch (Exception aex) {142assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");143}144145try {146as.unregisterObject(null);147} catch (Exception aex) {148assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");149}150151try {152as.setActivationDesc(null, null);153} catch (Exception aex) {154assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");155}156157try {158as.setActivationGroupDesc(null, null);159} catch (Exception aex) {160assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");161}162}163164/**165* Check the exception chain for the expected AccessException and message.166* @param ex the exception from the remote invocation.167*/168private static void assertIsAccessException(Exception ex, String msg1) {169Throwable t = ex;170System.out.println();171while (!(t instanceof AccessException) && t.getCause() != null) {172t = t.getCause();173}174if (t instanceof AccessException) {175String msg = t.getMessage();176int asIndex = msg.indexOf(msg1);177int disallowIndex = msg.indexOf("disallowed");178int nonLocalHostIndex = msg.indexOf("non-local host");179if (asIndex < 0 ||180disallowIndex < 0 ||181nonLocalHostIndex < 0 ) {182throw new RuntimeException("exception message is malformed", t);183}184System.out.printf("Found expected AccessException: %s%n", t);185} else {186throw new RuntimeException("AccessException did not occur", ex);187}188}189}190191192