Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/rmi/registry/nonLocalRegistry/NonLocalSkeletonTest.java
41154 views
1
/*
2
* Copyright (c) 2017, 2019, 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 sun.rmi.server.UnicastRef;
25
import sun.rmi.transport.LiveRef;
26
import sun.rmi.transport.tcp.TCPEndpoint;
27
28
import java.lang.reflect.InvocationHandler;
29
30
import java.lang.reflect.Proxy;
31
import java.net.InetAddress;
32
import java.rmi.AccessException;
33
import java.rmi.RemoteException;
34
import java.rmi.registry.LocateRegistry;
35
import java.rmi.registry.Registry;
36
import java.rmi.server.ObjID;
37
import java.rmi.server.RemoteObjectInvocationHandler;
38
import java.security.AccessController;
39
import java.security.PrivilegedAction;
40
import java.util.Arrays;
41
import java.util.Set;
42
43
44
/* @test
45
* @bug 8218453
46
* @library ../../testlibrary
47
* @modules java.rmi/sun.rmi.registry:+open java.rmi/sun.rmi.server:+open
48
* java.rmi/sun.rmi.transport:+open java.rmi/sun.rmi.transport.tcp:+open
49
* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.
50
* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.
51
* @run main/othervm -Dregistry.host=localhost NonLocalSkeletonTest
52
*/
53
54
/*
55
* @test
56
* @library ../../testlibrary
57
* @modules java.rmi/sun.rmi.registry:+open java.rmi/sun.rmi.server:+open
58
* java.rmi/sun.rmi.transport:+open java.rmi/sun.rmi.transport.tcp:+open
59
* @summary Verify that Registry rejects non-local access for bind, unbind, rebind.
60
* The test is manual because the (non-local) host running rmiregistry must be supplied as a property.
61
* @run main/othervm/manual -Dregistry.host=rmi-registry-host NonLocalSkeletonTest
62
*/
63
64
/**
65
* Verify that access checks for Registry.bind(), .rebind(), and .unbind()
66
* are prevented on remote access to the registry.
67
*
68
* This test is a manual test and uses a standard rmiregistry running
69
* on a *different* host.
70
* The test verifies that the access check is performed *before* the object to be
71
* bound or rebound is deserialized.
72
*
73
* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmiregistry}.
74
* It will not show any output.
75
*
76
* On the first host modify the @run command above to replace "rmi-registry-host"
77
* with the hostname or IP address of the different host and run the test with jtreg.
78
*/
79
public class NonLocalSkeletonTest {
80
81
public static void main(String[] args) throws Exception {
82
String host = System.getProperty("registry.host");
83
if (host == null || host.isEmpty()) {
84
throw new RuntimeException("supply a remote host with -Dregistry.host=hostname");
85
}
86
87
// Check if running the test on a local system; it only applies to remote
88
String myHostName = InetAddress.getLocalHost().getHostName();
89
Set<InetAddress> myAddrs = Set.copyOf(Arrays.asList(InetAddress.getAllByName(myHostName)));
90
Set<InetAddress> hostAddrs = Set.copyOf(Arrays.asList(InetAddress.getAllByName(host)));
91
boolean isLocal = (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))
92
|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress()));
93
94
int port;
95
if (isLocal) {
96
// Create a local Registry to use for the test
97
port = TestLibrary.getUnusedRandomPort();
98
Registry registry = LocateRegistry.createRegistry(port);
99
System.out.printf("local registry port: %s%n", registry);
100
} else {
101
// Use regular rmi registry for non-local test
102
port = Registry.REGISTRY_PORT;
103
}
104
105
try {
106
107
Registry r = nonStaticRegistryProxy(host, port);
108
109
System.out.printf("RegistryRef: %s%n", r);
110
111
r.rebind("anyRef", r);
112
if (!isLocal) {
113
throw new RuntimeException("non-local bind should have failed to host: " + host);
114
} else {
115
System.out.printf("local rebind succeeded%n");
116
}
117
} catch (RemoteException rex) {
118
if (!isLocal) {
119
assertIsAccessException(rex);
120
} else {
121
throw rex;
122
}
123
}
124
}
125
126
/* Returns a non-static proxy for the registry.
127
* Follows the form of sun.rmi.server.Util.createProxy.
128
* @param implClass the RegistryImpl
129
* @param clientRef the registry reference
130
**/
131
static Registry nonStaticRegistryProxy(String host, int port) {
132
final ClassLoader loader = Registry.class.getClassLoader();
133
final Class<?>[] interfaces = new Class<?>[]{Registry.class};
134
135
LiveRef liveRef = new LiveRef(new ObjID(ObjID.REGISTRY_ID),
136
new TCPEndpoint(host, port, null, null),
137
false);
138
139
final InvocationHandler handler = new RemoteObjectInvocationHandler(new UnicastRef(liveRef));
140
141
PrivilegedAction<Registry> action = () -> (Registry) Proxy.newProxyInstance(loader,
142
interfaces, handler);
143
return AccessController.doPrivileged(action);
144
}
145
146
/**
147
* Check the exception chain for the expected AccessException and message.
148
* @param ex the exception from the remote invocation.
149
*/
150
private static void assertIsAccessException(Throwable ex) {
151
Throwable t = ex;
152
while (!(t instanceof AccessException) && t.getCause() != null) {
153
t = t.getCause();
154
}
155
if (t instanceof AccessException) {
156
String msg = t.getMessage();
157
int asIndex = msg.indexOf("Registry");
158
int rrIndex = msg.indexOf("Registry.Registry"); // Obsolete error text
159
int disallowIndex = msg.indexOf("disallowed");
160
int nonLocalHostIndex = msg.indexOf("non-local host");
161
if (asIndex < 0 ||
162
rrIndex != -1 ||
163
disallowIndex < 0 ||
164
nonLocalHostIndex < 0 ) {
165
throw new RuntimeException("exception message is malformed", t);
166
}
167
System.out.printf("Found expected AccessException: %s%n%n", t);
168
} else {
169
throw new RuntimeException("AccessException did not occur when expected", ex);
170
}
171
}
172
}
173
174