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/activation/nonLocalActivation/NonLocalActivationTest.java
38821 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.activation.ActivationSystem;
27
import java.rmi.registry.LocateRegistry;
28
import java.rmi.registry.Registry;
29
import java.util.Set;
30
import java.util.HashSet;
31
32
/*
33
* @test
34
* @bug 8174770
35
* @summary Verify that ActivationSystem rejects non-local access.
36
* The test is manual because the (non-local) host running rmid must be supplied as a property.
37
* @run main/manual/othervm -Dactivation.host=rmid-host NonLocalActivationTest
38
*/
39
40
/**
41
* Lookup the ActivationSystem on a different host and invoke its remote interface methods.
42
* They should all throw an exception, non-local access is prohibited.
43
*
44
* This test is a manual test and uses rmid running on a *different* host.
45
* The default port (1098) for the Activation System is ok and expected.
46
* Login or ssh to the different host and invoke {@code $JDK_HOME/bin/rmid}.
47
* It will not show any output.
48
*
49
* On the first host modify the @run command above to replace "rmid-host"
50
* with the hostname or IP address of the different host and run the test with jtreg.
51
*/
52
public class NonLocalActivationTest
53
{
54
public static void main(String[] args) throws Exception {
55
56
String host = System.getProperty("activation.host");
57
if (host == null || host.isEmpty()) {
58
throw new RuntimeException("Specify host with system property: -Dactivation.host=<host>");
59
}
60
61
// Check if running the test on a local system; it only applies to remote
62
String myHostName = InetAddress.getLocalHost().getHostName();
63
Set<InetAddress> myAddrs = new HashSet<>();
64
InetAddress[] myAddrsArr = InetAddress.getAllByName(myHostName);
65
for (InetAddress a : myAddrsArr) {
66
myAddrs.add(a);
67
}
68
Set<InetAddress> hostAddrs = new HashSet<>();
69
InetAddress[] hostAddrsArr = InetAddress.getAllByName(host);
70
for (InetAddress a : hostAddrsArr) {
71
hostAddrs.add(a);
72
}
73
if (hostAddrs.stream().anyMatch(i -> myAddrs.contains(i))
74
|| hostAddrs.stream().anyMatch(h -> h.isLoopbackAddress())) {
75
throw new RuntimeException("Error: property 'activation.host' must not be the local host%n");
76
}
77
78
// Locate the registry operated by the ActivationSystem
79
// Test SystemRegistryImpl
80
Registry registry = LocateRegistry.getRegistry(host, ActivationSystem.SYSTEM_PORT);
81
try {
82
// Verify it is an ActivationSystem registry
83
registry.lookup("java.rmi.activation.ActivationSystem");
84
} catch (Exception nf) {
85
throw new RuntimeException("Not a ActivationSystem registry, does not contain java.rmi.activation.ActivationSystem", nf);
86
}
87
88
try {
89
registry.bind("foo", null);
90
throw new RuntimeException("Remote access should not succeed for method: bind");
91
} catch (Exception e) {
92
assertIsAccessException(e, "Registry.bind");
93
}
94
95
try {
96
registry.rebind("foo", null);
97
throw new RuntimeException("Remote access should not succeed for method: rebind");
98
} catch (Exception e) {
99
assertIsAccessException(e, "Registry.rebind");
100
}
101
102
try {
103
registry.unbind("foo");
104
throw new RuntimeException("Remote access should not succeed for method: unbind");
105
} catch (Exception e) {
106
assertIsAccessException(e, "Registry.unbind");
107
}
108
109
110
// Locate the ActivationSystem on the specified host and default port.
111
// Test each of the ActivationSystem methods
112
ActivationSystem as = (ActivationSystem) registry.lookup("java.rmi.activation.ActivationSystem");
113
114
// Argument is not material, access check is before arg processing
115
116
try {
117
as.registerGroup(null);
118
} catch (Exception aex) {
119
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
120
}
121
122
try {
123
as.getActivationDesc(null);
124
} catch (Exception aex) {
125
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
126
}
127
128
try {
129
as.getActivationGroupDesc(null);
130
} catch (Exception aex) {
131
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
132
}
133
134
try {
135
as.registerObject(null);
136
} catch (Exception aex) {
137
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
138
}
139
140
try {
141
as.unregisterGroup(null);
142
} catch (Exception aex) {
143
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
144
}
145
146
try {
147
as.unregisterObject(null);
148
} catch (Exception aex) {
149
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
150
}
151
152
try {
153
as.setActivationDesc(null, null);
154
} catch (Exception aex) {
155
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
156
}
157
158
try {
159
as.setActivationGroupDesc(null, null);
160
} catch (Exception aex) {
161
assertIsAccessException(aex, "ActivationSystem.nonLocalAccess");
162
}
163
}
164
165
/**
166
* Check the exception chain for the expected AccessException and message.
167
* @param ex the exception from the remote invocation.
168
*/
169
private static void assertIsAccessException(Exception ex, String msg1) {
170
Throwable t = ex;
171
System.out.println();
172
while (!(t instanceof AccessException) && t.getCause() != null) {
173
t = t.getCause();
174
}
175
if (t instanceof AccessException) {
176
String msg = t.getMessage();
177
int asIndex = msg.indexOf(msg1);
178
int disallowIndex = msg.indexOf("disallowed");
179
int nonLocalHostIndex = msg.indexOf("non-local host");
180
if (asIndex < 0 ||
181
disallowIndex < 0 ||
182
nonLocalHostIndex < 0 ) {
183
throw new RuntimeException("exception message is malformed", t);
184
}
185
System.out.printf("Found expected AccessException: %s%n", t);
186
} else {
187
throw new RuntimeException("AccessException did not occur", ex);
188
}
189
}
190
}
191
192