Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/RestrictReceiverTest.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2014, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.j9.jsr292;
23
24
import org.testng.annotations.Test;
25
import org.testng.AssertJUnit;
26
import java.lang.reflect.*;
27
import java.lang.invoke.*;
28
29
import examples.A;
30
31
public class RestrictReceiverTest {
32
33
private String callMethod(Object testInstance, String method) throws Exception {
34
Method m = testInstance.getClass().getMethod(method,(Class<?>[] ) null);
35
return (String) m.invoke(testInstance, (Object[]) null);
36
}
37
/**
38
* This test attempts to perform lookup.unreflect on "protectedMethod" in class A without setting the method
39
* as accessible.
40
*
41
* @param testInstance
42
*/
43
@Test(enabled = false)
44
private void testUnrelfectNoAccess(Object testInstance) {
45
boolean pass = false;
46
String result = null;
47
48
try {
49
result = callMethod(testInstance, "testUnreflectNoAccess");
50
confirmResult(testInstance, result);
51
pass = true;
52
} catch (Exception e) {
53
if (testInstance instanceof B) {
54
pass = true;
55
}
56
}
57
AssertJUnit.assertTrue(testInstance.getClass().toString() + "failed testUnreflectNoAccess test", pass);
58
}
59
60
61
/**
62
* This test attempts to perform lookup.unreflect() on a "protectedMethod" in class A and also sets the method
63
* as accessible.
64
*
65
* The spec states:
66
* "If the method's accessible flag is not set, access checking is performed immediately on behalf of the
67
* lookup class. If m is not public, do not share the resulting handle with untrusted parties."
68
* Based on Oracle behaviour "restrictReceiver()" is also not enforced in this case.
69
*
70
* @param testInstance
71
*/
72
@Test(enabled = false)
73
private void testUnreflect(Object testInstance) {
74
boolean pass = false;
75
String result = null;
76
pass = false;
77
try {
78
result = callMethod(testInstance, "testUnreflect");
79
pass = true;
80
} catch (Exception e) {
81
e.printStackTrace();
82
}
83
AssertJUnit.assertTrue(testInstance.getClass().toString() + " was not able to access un-protected member of class A", pass);
84
confirmResultWithAccess(testInstance, result);
85
}
86
87
/**
88
* This test attempts to perform lookup.findVirtual() on a "protectedMethod" in class A.
89
*
90
* @param testInstance
91
*/
92
@Test(enabled = false)
93
private void testFindVirtual(Object testInstance) {
94
boolean pass = false;
95
String result = null;
96
try {
97
result = callMethod(testInstance, "testFindVirtual");
98
confirmResult(testInstance, result);
99
pass = true;
100
} catch (Exception e) {
101
if (testInstance instanceof B) {
102
pass = true;
103
//class B is not a subclass of A
104
} else {
105
e.printStackTrace();
106
}
107
}
108
AssertJUnit.assertTrue(testInstance.getClass().toString() + "did not properly access member of class A via findVirtual", pass);
109
}
110
111
/**
112
* This test attempts to perform lookup.findSpecial() on a "protectedMethod" in class A. This
113
* test uses the instanceClass as the specialToken
114
*
115
* @param testInstance
116
*/
117
@Test(enabled = false)
118
private void testFindSpecialSameTokenAsCurrentClass(Object testInstance) {
119
boolean pass = false;
120
String result = null;
121
try {
122
result = callMethod(testInstance, "testFindSpecialSameTokenAsClass");
123
confirmResultSpecial(testInstance, result);
124
pass = true;
125
} catch (Exception e) {
126
if (testInstance instanceof B) {
127
pass = true;
128
//class B is not a subclass of A
129
} else {
130
e.printStackTrace();
131
}
132
}
133
AssertJUnit.assertTrue(testInstance.getClass().toString() + "did not properly access member of class A via findSpecial", pass);
134
}
135
136
/**
137
* This test attempts to perform lookup.findSpecial() on a "protectedMethod" in class A. This
138
* test use class A as the specialToken
139
*
140
* @param testInstance
141
*/
142
@Test(enabled = false)
143
private void testFindSpecial(Object testInstance) {
144
boolean pass = false;
145
String result = null;
146
try {
147
result = callMethod(testInstance, "testFindSpecial");
148
result = callMethod(testInstance, "testFindSpecial");//twice so we can get the cached value
149
confirmResultSpecial(testInstance, result);
150
} catch (Exception e) {
151
pass = true;// none of the classes have private access for invokespecial in class apackage.A
152
}
153
AssertJUnit.assertTrue(testInstance.getClass().toString() + "did not properly access member of class A via findSpecial", pass);
154
}
155
156
/**
157
* This test attempts to perform lookup.bind() on a "protectedMethod" in class A.
158
*
159
* @param testInstance
160
*/
161
@Test(enabled = false)
162
private void testBind(Object testInstance) {
163
boolean pass = false;
164
String result = null;
165
try {
166
result = callMethod(testInstance, "testBind");
167
confirmResultBind(testInstance, result);
168
pass = true;
169
} catch (Exception e) {
170
if (testInstance instanceof B) {
171
pass = true;//B has no access
172
} else {
173
e.printStackTrace();
174
}
175
}
176
AssertJUnit.assertTrue(testInstance.getClass().toString() + "did not properly access member of class A via bind", pass);
177
}
178
179
/**
180
* Runs all the test methods
181
*
182
* @param testInstance
183
*/
184
185
@Test(enabled = false)
186
private void testClass(Object testInstance) {
187
testUnrelfectNoAccess(testInstance);
188
testUnreflect(testInstance);
189
testFindVirtual(testInstance);
190
testFindSpecialSameTokenAsCurrentClass(testInstance);
191
testFindSpecial(testInstance);
192
testBind(testInstance);
193
}
194
195
private void confirmResult(Object testClass, String result) {
196
if (testClass instanceof B) {
197
AssertJUnit.assertTrue("class B has no access", false);
198
} else if (testClass instanceof D) {
199
AssertJUnit.assertEquals("class D return the wrong methodType", "(Lcom/ibm/j9/jsr292/C;)V", result);
200
} else if (testClass instanceof C) {
201
AssertJUnit.assertEquals("class C return the wrong methodType", "(Lcom/ibm/j9/jsr292/C;)V", result);
202
}
203
}
204
205
private void confirmResultSpecial(Object testClass, String result) {
206
if (testClass instanceof B) {
207
AssertJUnit.assertTrue("class B has no findSpecial access", false);
208
} else if (testClass instanceof D) {
209
AssertJUnit.assertEquals("class D return the wrong methodType", "(Lcom/ibm/j9/jsr292/D;)V", result);
210
} else if (testClass instanceof C) {
211
AssertJUnit.assertEquals("class C return the wrong methodType", "(Lcom/ibm/j9/jsr292/C;)V", result);
212
}
213
}
214
215
private void confirmResultBind(Object testClass, String result) {
216
if (testClass instanceof B) {
217
AssertJUnit.assertTrue("class B has no Bind access", false);
218
} else if (testClass instanceof D) {
219
AssertJUnit.assertEquals("class D return the wrong methodType", "()V", result);
220
} else if (testClass instanceof C) {
221
AssertJUnit.assertEquals("class C return the wrong methodType", "()V", result);
222
}
223
}
224
225
private void confirmResultWithAccess(Object testClass, String result) {
226
AssertJUnit.assertEquals(testClass.getClass().toString() + " returned the wrong methodType", "(Lexamples/A;)V", result);
227
}
228
229
/**
230
* Main test case, runs test methods on class B, C, and D
231
*
232
*/
233
@Test(groups = { "level.extended" })
234
public void testRestrictReceiver() {
235
testClass(new B());
236
testClass(new C());
237
testClass(new D());
238
}
239
}
240
241
class B {
242
public String testUnreflectNoAccess() throws Exception {
243
Method m = A.getMethod("protectedMethod");
244
MethodHandle mh = MethodHandles.lookup().unreflect(m);
245
return mh.type().toMethodDescriptorString();
246
}
247
248
public String testUnreflect() throws Exception {
249
Method m = A.getMethod("protectedMethod");
250
m.setAccessible(true);
251
MethodHandle mh = MethodHandles.lookup().unreflect(m);
252
return mh.type().toMethodDescriptorString();
253
}
254
255
public String testFindVirtual() throws Exception {
256
MethodHandle mh = MethodHandles.lookup().findVirtual(A.class, "protectedMethod", MethodType.methodType(void.class));
257
return mh.type().toMethodDescriptorString();
258
}
259
260
public String testFindSpecialSameTokenAsClass() throws Exception {
261
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), B.class);
262
return mh.type().toMethodDescriptorString();
263
}
264
265
public String testFindSpecial() throws Exception {
266
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), A.class);
267
return mh.type().toMethodDescriptorString();
268
}
269
270
public String testBind() throws Exception {
271
MethodHandle mh = MethodHandles.lookup().bind(new B(), "protectedMethod", MethodType.methodType(void.class));
272
return mh.type().toMethodDescriptorString();
273
}
274
}
275
276
class C extends A {
277
public String testUnreflectNoAccess() throws Exception {
278
Method m = A.getMethod("protectedMethod");
279
MethodHandle mh = MethodHandles.lookup().unreflect(m);
280
return mh.type().toMethodDescriptorString();
281
}
282
283
public String testUnreflect() throws Exception {
284
Method m = A.getMethod("protectedMethod");
285
m.setAccessible(true);
286
MethodHandle mh = MethodHandles.lookup().unreflect(m);
287
return mh.type().toMethodDescriptorString();
288
}
289
290
public String testFindVirtual() throws Exception {
291
MethodHandle mh = MethodHandles.lookup().findVirtual(A.class, "protectedMethod", MethodType.methodType(void.class));
292
return mh.type().toMethodDescriptorString();
293
}
294
295
public String testFindSpecialSameTokenAsClass() throws Exception {
296
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), C.class);
297
return mh.type().toMethodDescriptorString();
298
}
299
300
public String testFindSpecial() throws Exception {
301
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), A.class);
302
return mh.type().toMethodDescriptorString();
303
}
304
305
public String testBind() throws Exception {
306
MethodHandle mh = MethodHandles.lookup().bind(new C(), "protectedMethod", MethodType.methodType(void.class));
307
return mh.type().toMethodDescriptorString();
308
}
309
}
310
311
class D extends C {
312
@Override
313
public String testFindSpecialSameTokenAsClass() throws Exception {
314
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), D.class);
315
return mh.type().toMethodDescriptorString();
316
}
317
318
@Override
319
public String testFindSpecial() throws Exception {
320
MethodHandle mh = MethodHandles.lookup().findSpecial(A.class, "protectedMethod", MethodType.methodType(void.class), A.class);
321
return mh.type().toMethodDescriptorString();
322
}
323
324
@Override
325
public String testBind() throws Exception {
326
MethodHandle mh = MethodHandles.lookup().bind(new D(), "protectedMethod", MethodType.methodType(void.class));
327
return mh.type().toMethodDescriptorString();
328
}
329
}
330
331