Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/javadoc/tool/TestScriptInComment.java
40957 views
1
/*
2
* Copyright (c) 2016, 2020, 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
* @bug 8138725 8226765
27
* @summary test --allow-script-in-comments
28
* @modules jdk.javadoc/jdk.javadoc.internal.tool
29
*/
30
31
import java.io.File;
32
import java.io.FileWriter;
33
import java.io.IOException;
34
import java.io.PrintStream;
35
import java.io.PrintWriter;
36
import java.io.StringWriter;
37
import java.util.ArrayList;
38
import java.util.Arrays;
39
import java.util.Collections;
40
import java.util.List;
41
import java.util.regex.Matcher;
42
import java.util.regex.Pattern;
43
44
/**
45
* Combo-style test, exercising combinations of different HTML fragments that may contain
46
* JavaScript, different places to place those fragments, and whether or not to allow the use
47
* of JavaScript.
48
*/
49
public class TestScriptInComment {
50
public static void main(String... args) throws Exception {
51
new TestScriptInComment().run();
52
}
53
54
/**
55
* Representative samples of different fragments of HTML that may contain JavaScript.
56
* To facilitate checking the output manually in a browser, the text "#ALERT" will be
57
* replaced by a JavaScript call of "alert(msg)", using a message string that is specific
58
* to the test case.
59
*/
60
enum Comment {
61
LC("<script>#ALERT</script>", true), // script tag in Lower Case
62
UC("<SCRIPT>#ALERT</script>", true), // script tag in Upper Case
63
WS("< script >#ALERT</script>", false, "-Xdoclint:none"), // script tag with invalid white space
64
SP("""
65
<script src="file"> #ALERT </script>""", true), // script tag with an attribute
66
ON("<a onclick='#ALERT'>x</a>", true), // event handler attribute
67
OME("<img alt='1' onmouseenter='#ALERT'>", true), // onmouseenter event handler attribute
68
OML("<img alt='1' onmouseleave='#ALERT'>", true), // onmouseleave event handler attribute
69
OFI("<a href='#' onfocusin='#ALERT'>x</a>", true), // onfocusin event handler attribute
70
OBE("<a onbogusevent='#ALERT'>x</a>", true), // bogus/future event handler attribute
71
URI("<a href='javascript:#ALERT'>x</a>", true); // javascript URI
72
73
/**
74
* Creates an HTML fragment to be injected into a template.
75
* @param text the HTML fragment to put into a doc comment or option.
76
* @param hasScript whether or not this fragment does contain legal JavaScript
77
* @param opts any additional options to be specified when javadoc is run
78
*/
79
Comment(String text, boolean hasScript, String... opts) {
80
this.text = text;
81
this.hasScript = hasScript;
82
this.opts = Arrays.asList(opts);
83
}
84
85
final String text;
86
final boolean hasScript;
87
final List<String> opts;
88
};
89
90
/**
91
* Representative samples of positions in which javadoc may find JavaScript.
92
* Each template contains a series of strings, which are written to files or inferred as options.
93
* The first source file implies a corresponding output file which should not be written
94
* if the comment contains JavaScript and JavaScript is not allowed.
95
*/
96
enum Template {
97
OVR("<html><body> overview #COMMENT </body></html>", "package p; public class C { }"),
98
PKGINFO("#COMMENT package p;", "package p; public class C { }"),
99
PKGHTML("<html><body>#COMMENT package p;</body></html>", "package p; public class C { }"),
100
CLS("package p; #COMMENT public class C { }"),
101
CON("package p; public class C { #COMMENT public C() { } }"),
102
FLD("package p; public class C { #COMMENT public int f; }"),
103
MTH("package p; public class C { #COMMENT public void m() { } }"),
104
TOP("-top", "lorem #COMMENT ipsum", "package p; public class C { }"),
105
HDR("-header", "lorem #COMMENT ipsum", "package p; public class C { }"),
106
BTM("-bottom", "lorem #COMMENT ipsum", "package p; public class C { }"),
107
DTTL("-doctitle", "lorem #COMMENT ipsum", "package p; public class C { }"),
108
PHDR("-packagesheader", "lorem #COMMENT ipsum", "package p; public class C { }");
109
110
Template(String... args) {
111
opts = new ArrayList<String>();
112
sources = new ArrayList<String>();
113
int i = 0;
114
while (args[i].startsWith("-")) {
115
// all options being tested have a single argument that follow the option
116
opts.add(args[i++]);
117
opts.add(args[i++]);
118
}
119
while(i < args.length) {
120
sources.add(args[i++]);
121
}
122
}
123
124
// groups: 1 <html> or not; 2: package name; 3: class name
125
private final Pattern pat =
126
Pattern.compile("(?i)(<html>)?.*?(?:package ([a-z]+);.*?(?:class ([a-z]+).*)?)?");
127
128
/**
129
* Infer the file in which to write the given source.
130
* @param dir the base source directory
131
* @param src the source text
132
* @return the file in which the source should be written
133
*/
134
File getSrcFile(File srcDir, String src) {
135
String f;
136
Matcher m = pat.matcher(src);
137
if (!m.matches())
138
throw new Error("match failed");
139
if (m.group(3) != null) {
140
f = m.group(2) + "/" + m.group(3) + ".java";
141
} else if (m.group(2) != null) {
142
f = m.group(2) + "/" + (m.group(1) == null ? "package-info.java" : "package.html");
143
} else {
144
f = "overview.html";
145
}
146
return new File(srcDir, f);
147
}
148
149
/**
150
* Get the options to give to javadoc.
151
* @param srcDir the srcDir to use -overview is needed
152
* @return
153
*/
154
List<String> getOpts(File srcDir) {
155
if (!opts.isEmpty()) {
156
return opts;
157
} else if (sources.get(0).contains("overview")) {
158
return Arrays.asList("-overview", getSrcFile(srcDir, sources.get(0)).getPath());
159
} else {
160
return Collections.emptyList();
161
}
162
}
163
164
/**
165
* Gets the output file corresponding to the first source file.
166
* This file should not be written if the comment contains JavaScript and JavaScripot is
167
* not allowed.
168
* @param dir the base output directory
169
* @return the output file
170
*/
171
File getOutFile(File outDir) {
172
String f;
173
Matcher m = pat.matcher(sources.get(0));
174
if (!m.matches())
175
throw new Error("match failed");
176
if (m.group(3) != null) {
177
f = m.group(2) + "/" + m.group(3) + ".html";
178
} else if (m.group(2) != null) {
179
f = m.group(2) + "/package-summary.html";
180
} else {
181
f = "overview-summary.html";
182
}
183
return new File(outDir, f);
184
}
185
186
final List<String> opts;
187
final List<String> sources;
188
};
189
190
enum Option {
191
OFF(null),
192
ON("--allow-script-in-comments");
193
194
Option(String text) {
195
this.text = text;
196
}
197
198
final String text;
199
};
200
201
private PrintStream out = System.err;
202
203
public void run() throws Exception {
204
int count = 0;
205
for (Template template: Template.values()) {
206
for (Comment comment: Comment.values()) {
207
for (Option option: Option.values()) {
208
if (test(template, comment, option)) {
209
count++;
210
}
211
}
212
}
213
}
214
215
out.println(count + " test cases run");
216
if (errors > 0) {
217
throw new Exception(errors + " errors occurred");
218
}
219
}
220
221
boolean test(Template template, Comment comment, Option option) throws IOException {
222
if (option == Option.ON && !comment.hasScript) {
223
// skip --allowScriptInComments if comment does not contain JavaScript
224
return false;
225
}
226
227
String test = template + "-" + comment + "-" + option;
228
out.println("Test: " + test);
229
230
File dir = new File(test);
231
dir.mkdirs();
232
File srcDir = new File(dir, "src");
233
File outDir = new File(dir, "out");
234
235
String alert = "alert(\"" + test + "\");";
236
for (String src: template.sources) {
237
writeFile(template.getSrcFile(srcDir, src),
238
src.replace("#COMMENT",
239
"/** " + comment.text.replace("#ALERT", alert) + " **/"));
240
}
241
242
List<String> opts = new ArrayList<String>();
243
opts.add("-sourcepath");
244
opts.add(srcDir.getPath());
245
opts.add("-d");
246
opts.add(outDir.getPath());
247
if (option.text != null)
248
opts.add(option.text);
249
for (String opt: template.getOpts(srcDir)) {
250
opts.add(opt.replace("#COMMENT", comment.text.replace("#ALERT", alert)));
251
}
252
opts.addAll(comment.opts);
253
opts.add("-noindex"); // index not required; save time/space writing files
254
opts.add("p");
255
256
StringWriter sw = new StringWriter();
257
PrintWriter pw = new PrintWriter(sw);
258
int rc = javadoc(opts, pw);
259
pw.close();
260
String log = sw.toString();
261
writeFile(new File(dir, "log.txt"), log);
262
263
out.println("opts: " + opts);
264
out.println(" rc: " + rc);
265
out.println(" log:");
266
out.println(log);
267
268
String ERROR = "Use --allow-script-in-comment";
269
File outFile = template.getOutFile(outDir);
270
271
boolean expectErrors = comment.hasScript && (option == Option.OFF);
272
273
if (expectErrors) {
274
check(rc != 0, "unexpected exit code: " + rc);
275
check(log.contains(ERROR), "expected error message not found");
276
check(!outFile.exists(), "output file found unexpectedly");
277
} else {
278
check(rc == 0, "unexpected exit code: " + rc);
279
check(!log.contains(ERROR), "error message found");
280
check(outFile.exists(), "output file not found");
281
}
282
283
out.println();
284
return true;
285
}
286
287
int javadoc(List<String> opts, PrintWriter pw) {
288
return jdk.javadoc.internal.tool.Main.execute(opts.toArray(new String[opts.size()]), pw);
289
}
290
291
File writeFile(File f, String text) throws IOException {
292
f.getParentFile().mkdirs();
293
FileWriter fw = new FileWriter(f);
294
try {
295
fw.write(text);
296
} finally {
297
fw.close();
298
}
299
return f;
300
}
301
302
void check(boolean cond, String errMessage) {
303
if (!cond) {
304
error(errMessage);
305
}
306
}
307
308
void error(String message) {
309
out.println("Error: " + message);
310
errors++;
311
}
312
313
int errors = 0;
314
}
315
316
317