author | alanb |
Fri, 24 Mar 2017 16:35:51 +0000 | |
changeset 44364 | 9cc9dc782213 |
parent 44359 | c6761862ca0b |
permissions | -rw-r--r-- |
44359 | 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 |
/** |
|
25 |
* @test |
|
26 |
* @library /lib/testlibrary |
|
27 |
* @build PermitIllegalAccess AttemptAccess jdk.testlibrary.* |
|
28 |
* @run testng PermitIllegalAccess |
|
29 |
* @summary Basic test for java --permit-illegal-access |
|
30 |
*/ |
|
31 |
||
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
32 |
import java.util.ArrayList; |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
33 |
import java.util.Arrays; |
44359 | 34 |
import java.util.List; |
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
35 |
import java.util.stream.Stream; |
44359 | 36 |
|
37 |
import jdk.testlibrary.ProcessTools; |
|
38 |
import jdk.testlibrary.OutputAnalyzer; |
|
39 |
||
40 |
import org.testng.annotations.Test; |
|
41 |
import static org.testng.Assert.*; |
|
42 |
||
43 |
/** |
|
44 |
* Basic test of --permit-illegal-access to ensure that it permits access |
|
45 |
* via core reflection and setAccessible/trySetAccessible. |
|
46 |
*/ |
|
47 |
||
48 |
@Test |
|
49 |
public class PermitIllegalAccess { |
|
50 |
||
51 |
static final String TEST_CLASSES = System.getProperty("test.classes"); |
|
52 |
static final String TEST_MAIN = "AttemptAccess"; |
|
53 |
||
54 |
static final String WARNING = "WARNING"; |
|
55 |
static final String STARTUP_WARNING = |
|
56 |
"WARNING: --permit-illegal-access will be removed in the next major release"; |
|
57 |
static final String ILLEGAL_ACCESS_WARNING = |
|
58 |
"WARNING: Illegal access by " + TEST_MAIN; |
|
59 |
||
60 |
/** |
|
61 |
* Launches AttemptAccess to execute an action, returning the OutputAnalyzer |
|
62 |
* to analyze the output/exitCode. |
|
63 |
*/ |
|
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
64 |
private OutputAnalyzer tryAction(String action, int count, String... args) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
65 |
throws Exception |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
66 |
{ |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
67 |
Stream<String> s1 = Stream.of(args); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
68 |
Stream<String> s2 = Stream.of("-cp", TEST_CLASSES, TEST_MAIN, action, "" + count); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
69 |
String[] opts = Stream.concat(s1, s2).toArray(String[]::new); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
70 |
return ProcessTools.executeTestJava(opts) |
44359 | 71 |
.outputTo(System.out) |
72 |
.errorTo(System.out); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* Launches AttemptAccess with --permit-illegal-access to execute an action, |
|
77 |
* returning the OutputAnalyzer to analyze the output/exitCode. |
|
78 |
*/ |
|
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
79 |
private OutputAnalyzer tryActionPermittingIllegalAccess(String action, int count) |
44359 | 80 |
throws Exception |
81 |
{ |
|
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
82 |
return tryAction(action, count, "--permit-illegal-access"); |
44359 | 83 |
} |
84 |
||
85 |
/** |
|
86 |
* Sanity check to ensure that IllegalAccessException is thrown. |
|
87 |
*/ |
|
88 |
public void testAccessFail() throws Exception { |
|
89 |
int exitValue = tryAction("access", 1) |
|
90 |
.stdoutShouldNotContain(WARNING) |
|
91 |
.stdoutShouldNotContain("IllegalAccessException") |
|
92 |
.stderrShouldNotContain(WARNING) |
|
93 |
.stderrShouldContain("IllegalAccessException") |
|
94 |
.getExitValue(); |
|
95 |
assertTrue(exitValue != 0); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Sanity check to ensure that InaccessibleObjectException is thrown. |
|
100 |
*/ |
|
101 |
public void testSetAccessibleFail() throws Exception { |
|
102 |
int exitValue = tryAction("setAccessible", 1) |
|
103 |
.stdoutShouldNotContain(WARNING) |
|
104 |
.stdoutShouldNotContain("InaccessibleObjectException") |
|
105 |
.stderrShouldNotContain(WARNING) |
|
106 |
.stderrShouldContain("InaccessibleObjectException") |
|
107 |
.getExitValue(); |
|
108 |
assertTrue(exitValue != 0); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Permit illegal access to succeed |
|
113 |
*/ |
|
114 |
public void testAccessPermitted() throws Exception { |
|
115 |
tryActionPermittingIllegalAccess("access", 1) |
|
116 |
.stdoutShouldNotContain(WARNING) |
|
117 |
.stdoutShouldNotContain("IllegalAccessException") |
|
118 |
.stderrShouldContain(STARTUP_WARNING) |
|
119 |
.stderrShouldNotContain("IllegalAccessException") |
|
120 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
121 |
.shouldHaveExitValue(0); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Permit repeated illegal access to succeed |
|
126 |
*/ |
|
127 |
public void testRepeatedAccessPermitted() throws Exception { |
|
128 |
OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("access", 10) |
|
129 |
.stdoutShouldNotContain(WARNING) |
|
130 |
.stdoutShouldNotContain("IllegalAccessException") |
|
131 |
.stderrShouldContain(STARTUP_WARNING) |
|
132 |
.stderrShouldNotContain("IllegalAccessException") |
|
133 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
134 |
.shouldHaveExitValue(0);; |
|
135 |
||
136 |
// should only have one illegal access warning |
|
137 |
assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1); |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Permit setAccessible to succeed |
|
142 |
*/ |
|
143 |
public void testSetAccessiblePermitted() throws Exception { |
|
144 |
tryActionPermittingIllegalAccess("setAccessible", 1) |
|
145 |
.stdoutShouldNotContain(WARNING) |
|
146 |
.stdoutShouldNotContain("InaccessibleObjectException") |
|
147 |
.stderrShouldContain(STARTUP_WARNING) |
|
148 |
.stderrShouldNotContain("InaccessibleObjectException") |
|
149 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
150 |
.shouldHaveExitValue(0); |
|
151 |
} |
|
152 |
||
153 |
/** |
|
154 |
* Permit repeated calls to setAccessible to succeed |
|
155 |
*/ |
|
156 |
public void testRepeatedSetAccessiblePermitted() throws Exception { |
|
157 |
OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("setAccessible", 10) |
|
158 |
.stdoutShouldNotContain(WARNING) |
|
159 |
.stdoutShouldNotContain("InaccessibleObjectException") |
|
160 |
.stderrShouldContain(STARTUP_WARNING) |
|
161 |
.stderrShouldNotContain("InaccessibleObjectException") |
|
162 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
163 |
.shouldHaveExitValue(0); |
|
164 |
||
165 |
// should only have one illegal access warning |
|
166 |
assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1); |
|
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Permit trySetAccessible to succeed |
|
171 |
*/ |
|
172 |
public void testTrySetAccessiblePermitted() throws Exception { |
|
173 |
tryActionPermittingIllegalAccess("trySetAccessible", 1) |
|
174 |
.stdoutShouldNotContain(WARNING) |
|
175 |
.stderrShouldContain(STARTUP_WARNING) |
|
176 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
177 |
.shouldHaveExitValue(0); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Permit repeated calls to trySetAccessible to succeed |
|
182 |
*/ |
|
183 |
public void testRepeatedTrySetAccessiblePermitted() throws Exception { |
|
184 |
OutputAnalyzer outputAnalyzer = tryActionPermittingIllegalAccess("trySetAccessible", 10) |
|
185 |
.stdoutShouldNotContain(WARNING) |
|
186 |
.stdoutShouldNotContain("InaccessibleObjectException") |
|
187 |
.stderrShouldContain(STARTUP_WARNING) |
|
188 |
.stderrShouldNotContain("InaccessibleObjectException") |
|
189 |
.stderrShouldContain(ILLEGAL_ACCESS_WARNING) |
|
190 |
.shouldHaveExitValue(0); |
|
191 |
||
192 |
// should only have one illegal access warning |
|
193 |
assertTrue(containsCount(outputAnalyzer.asLines(), ILLEGAL_ACCESS_WARNING) == 1); |
|
194 |
||
195 |
} |
|
196 |
||
197 |
/** |
|
44364
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
198 |
* Permit access to succeed with --add-exports. No warning should be printed. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
199 |
*/ |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
200 |
public void testAccessWithAddExports() throws Exception { |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
201 |
tryAction("access", 1, "--add-exports", "java.base/sun.security.x509=ALL-UNNAMED") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
202 |
.stdoutShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
203 |
.stdoutShouldNotContain("IllegalAccessException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
204 |
.stderrShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
205 |
.stderrShouldNotContain("IllegalAccessException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
206 |
.shouldHaveExitValue(0); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
207 |
} |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
208 |
|
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
209 |
/** |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
210 |
* Permit access to succeed with --add-exports and --permit-illegal-access. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
211 |
* The only warning emitted should be the startup warning. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
212 |
*/ |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
213 |
public void testAccessWithePermittedAddExports() throws Exception { |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
214 |
tryAction("access", 1, "--permit-illegal-access", |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
215 |
"--add-exports", "java.base/sun.security.x509=ALL-UNNAMED") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
216 |
.stdoutShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
217 |
.stdoutShouldNotContain("IllegalAccessException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
218 |
.stderrShouldContain(STARTUP_WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
219 |
.stderrShouldNotContain("IllegalAccessException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
220 |
.stderrShouldNotContain(ILLEGAL_ACCESS_WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
221 |
.shouldHaveExitValue(0); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
222 |
} |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
223 |
|
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
224 |
/** |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
225 |
* Permit setAccessible to succeed with --add-opens. No warning should be printed. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
226 |
*/ |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
227 |
public void testSetAccessibleWithAddOpens() throws Exception { |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
228 |
tryAction("setAccessible", 1, "--add-opens", "java.base/java.lang=ALL-UNNAMED") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
229 |
.stdoutShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
230 |
.stdoutShouldNotContain("InaccessibleObjectException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
231 |
.stderrShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
232 |
.stderrShouldNotContain("InaccessibleObjectException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
233 |
.shouldHaveExitValue(0); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
234 |
} |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
235 |
|
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
236 |
/** |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
237 |
* Permit setAccessible to succeed with both --add-opens and --permit-illegal-access. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
238 |
* The only warning emitted should be the startup warning. |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
239 |
*/ |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
240 |
public void testSetAccessiblePermittedWithAddOpens() throws Exception { |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
241 |
tryAction("setAccessible", 1, "--permit-illegal-access", |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
242 |
"--add-opens", "java.base/java.lang=ALL-UNNAMED") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
243 |
.stdoutShouldNotContain(WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
244 |
.stdoutShouldNotContain("InaccessibleObjectException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
245 |
.stderrShouldContain(STARTUP_WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
246 |
.stderrShouldNotContain("InaccessibleObjectException") |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
247 |
.stderrShouldNotContain(ILLEGAL_ACCESS_WARNING) |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
248 |
.shouldHaveExitValue(0); |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
249 |
} |
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
250 |
|
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
251 |
|
9cc9dc782213
8177474: Do not emit warnings when illegal access is allowed by --add-exports/--add-opens
alanb
parents:
44359
diff
changeset
|
252 |
/** |
44359 | 253 |
* Returns the number of lines in the given input that contain the |
254 |
* given char sequence. |
|
255 |
*/ |
|
256 |
private int containsCount(List<String> lines, CharSequence cs) { |
|
257 |
int count = 0; |
|
258 |
for (String line : lines) { |
|
259 |
if (line.contains(cs)) count++; |
|
260 |
} |
|
261 |
return count; |
|
262 |
} |
|
263 |
} |