Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/registry/nonLocalRegistry/NonLocalRegistryTest.java
38828 views
1
/*
2
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.net.InetAddress;
25
import java.rmi.AccessException;
26
import java.rmi.registry.LocateRegistry;
27
import java.rmi.registry.Registry;
28
import java.util.Set;
29
import java.util.HashSet;
30
31
/* @test
32
* @bug 8174770
33
* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.
34
* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.
35
* @run main/othervm/manual -Dregistry.host=rmi-registry-host NonLocalRegistryTest
36
*/
37
38
/**
39
* Verify that access checks for Registry.bind(), .rebind(), and .unbind()
40
* are prevented on remote access to the registry.
41
*
42
* This test is a manual test and uses a standard rmiregistry running
43
* on a *different* host.
44
* The test verifies that the access check is performed *before* the object to be
45
* bound or rebound is deserialized.
46
*
47
* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmiregistry}.
48
* It will not show any output.
49
*
50
* On the first host modify the @run command above to replace "rmi-registry-host"
51
* with the hostname or IP address of the different host and run the test with jtreg.
52
*/
53
public class NonLocalRegistryTest {
54
55
public static void main(String[] args) throws Exception {
56
57
String host = System.getProperty("registry.host");
58
if (host == null || host.isEmpty()) {
59
throw new RuntimeException("Specify host with system property: -Dregistry.host=<host>");
60
}
61
62
// Check if running the test on a local system; it only applies to remote
63
String myHostName = InetAddress.getLocalHost().getHostName();
64
Set<InetAddress> myAddrs = new HashSet<>();
65
InetAddress[] myAddrsArr = InetAddress.getAllByName(myHostName);
66
for (InetAddress a : myAddrsArr) {
67
myAddrs.add(a);
68
}
69
Set<InetAddress> hostAddrs = new HashSet<>();
70
InetAddress[] hostAddrsArr = InetAddress.getAllByName(host);
71
for (InetAddress a : hostAddrsArr) {
72
hostAddrs.add(a);
73
}
74
if (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))
75
|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress())) {
76
throw new RuntimeException("Error: property 'registry.host' must not be the local host%n");
77
}
78
79
Registry registry = LocateRegistry.getRegistry(host, Registry.REGISTRY_PORT);
80
81
try {
82
registry.bind("foo", null);
83
throw new RuntimeException("Remote access should not succeed for method: bind");
84
} catch (Exception e) {
85
assertIsAccessException(e);
86
}
87
88
try {
89
registry.rebind("foo", null);
90
throw new RuntimeException("Remote access should not succeed for method: rebind");
91
} catch (Exception e) {
92
assertIsAccessException(e);
93
}
94
95
try {
96
registry.unbind("foo");
97
throw new RuntimeException("Remote access should not succeed for method: unbind");
98
} catch (Exception e) {
99
assertIsAccessException(e);
100
}
101
}
102
103
/**
104
* Check the exception chain for the expected AccessException and message.
105
* @param ex the exception from the remote invocation.
106
*/
107
private static void assertIsAccessException(Throwable ex) {
108
Throwable t = ex;
109
while (!(t instanceof AccessException) && t.getCause() != null) {
110
t = t.getCause();
111
}
112
if (t instanceof AccessException) {
113
String msg = t.getMessage();
114
int asIndex = msg.indexOf("Registry");
115
int rrIndex = msg.indexOf("Registry.Registry"); // Obsolete error text
116
int disallowIndex = msg.indexOf("disallowed");
117
int nonLocalHostIndex = msg.indexOf("non-local host");
118
if (asIndex < 0 ||
119
rrIndex != -1 ||
120
disallowIndex < 0 ||
121
nonLocalHostIndex < 0 ) {
122
throw new RuntimeException("exception message is malformed", t);
123
}
124
System.out.printf("Found expected AccessException: %s%n%n", t);
125
} else {
126
throw new RuntimeException("AccessException did not occur when expected", ex);
127
}
128
}
129
}
130
131