Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe
1 /*
2 * Copyright (c) 2016, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 /**
27 * @test
28 * @bug 8138725 8226765
29 * @summary test --allow-script-in-comments
30 * @run main TestScriptInComment
31 */
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.PrintStream;
37 import java.io.PrintWriter;
38 import java.io.StringWriter;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
46 /**
47 * Combo-style test, exercising combinations of different HTML fragments that may contain
48 * JavaScript, different places to place those fragments, and whether or not to allow the use
49 * of JavaScript.
50 */
51 public class TestScriptInComment {
52 public static void main(String... args) throws Exception {
53 new TestScriptInComment().run();
54 }
56 /**
57 * Representative samples of different fragments of HTML that may contain JavaScript.
58 * To facilitate checking the output manually in a browser, the text "#ALERT" will be
59 * replaced by a JavaScript call of "alert(msg)", using a message string that is specific
60 * to the test case.
61 */
62 enum Comment {
63 LC("<script>#ALERT</script>", true), // script tag in Lower Case
64 UC("<SCRIPT>#ALERT</script>", true), // script tag in Upper Case
65 WS("< script >#ALERT</script>", false, "-Xdoclint:none"), // script tag with invalid white space
66 SA("<script src=\"file\"> #ALERT </script>", true), // script tag with an attribute
67 ON("<a onclick='#ALERT'>x</a>", true), // event handler attribute
68 OME("<img alt='1' onmouseenter='#ALERT'>", true), // onmouseenter event handler attribute
69 OML("<img alt='1' onmouseleave='#ALERT'>", true), // onmouseleave event handler attribute
70 OFI("<a href='#' onfocusin='#ALERT'>x</a>", true), // onfocusin event handler attribute
71 OBE("<a onbogusevent='#ALERT'>x</a>", true), // bogus/future event handler attribute
72 URI("<a href='javascript:#ALERT'>x</a>", true); // javadcript URI
74 /**
75 * Creates an HTML fragment to be injected into a template.
76 * @param text the HTML fragment to put into a doc comment or option.
77 * @param hasScript whether or not this fragment does contain legal JavaScript
78 * @param opts any additional options to be specified when javadoc is run
79 */
80 Comment(String text, boolean hasScript, String... opts) {
81 this.text = text;
82 this.hasScript = hasScript;
83 this.opts = Arrays.asList(opts);
84 }
86 final String text;
87 final boolean hasScript;
88 final List<String> opts;
89 };
91 /**
92 * Representative samples of positions in which javadoc may find JavaScript.
93 * Each template contains a series of strings, which are written to files or inferred as options.
94 * The first source file implies a corresponding output file which should not be written
95 * if the comment contains JavaScript and JavaScript is not allowed.
96 */
97 enum Template {
98 OVR("<html><body> overview #COMMENT </body></html>", "package p; public class C { }"),
99 PKGINFO("#COMMENT package p;", "package p; public class C { }"),
100 PKGHTML("<html><body>#COMMENT package p;</body></html>", "package p; public class C { }"),
101 CLS("package p; #COMMENT public class C { }"),
102 CON("package p; public class C { #COMMENT public C() { } }"),
103 FLD("package p; public class C { #COMMENT public int f; }"),
104 MTH("package p; public class C { #COMMENT public void m() { } }"),
105 TOP("-top", "lorem #COMMENT ipsum", "package p; public class C { }"),
106 HDR("-header", "lorem #COMMENT ipsum", "package p; public class C { }"),
107 FTR("-footer", "lorem #COMMENT ipsum", "package p; public class C { }"),
108 BTM("-bottom", "lorem #COMMENT ipsum", "package p; public class C { }"),
109 DTTL("-doctitle", "lorem #COMMENT ipsum", "package p; public class C { }"),
110 PHDR("-packagesheader", "lorem #COMMENT ipsum", "package p; public class C { }");
112 Template(String... args) {
113 opts = new ArrayList<String>();
114 sources = new ArrayList<String>();
115 int i = 0;
116 while (args[i].startsWith("-")) {
117 // all options being tested have a single argument that follow the option
118 opts.add(args[i++]);
119 opts.add(args[i++]);
120 }
121 while(i < args.length) {
122 sources.add(args[i++]);
123 }
124 }
126 // groups: 1 <html> or not; 2: package name; 3: class name
127 private final Pattern pat =
128 Pattern.compile("(?i)(<html>)?.*?(?:package ([a-z]+);.*?(?:class ([a-z]+).*)?)?");
130 /**
131 * Infer the file in which to write the given source.
132 * @param dir the base source directory
133 * @param src the source text
134 * @return the file in which the source should be written
135 */
136 File getSrcFile(File srcDir, String src) {
137 String f;
138 Matcher m = pat.matcher(src);
139 if (!m.matches())
140 throw new Error("match failed");
141 if (m.group(3) != null) {
142 f = m.group(2) + "/" + m.group(3) + ".java";
143 } else if (m.group(2) != null) {
144 f = m.group(2) + "/" + (m.group(1) == null ? "package-info.java" : "package.html");
145 } else {
146 f = "overview.html";
147 }
148 return new File(srcDir, f);
149 }
151 /**
152 * Get the options to give to javadoc.
153 * @param srcDir the srcDir to use -overview is needed
154 * @return
155 */
156 List<String> getOpts(File srcDir) {
157 if (!opts.isEmpty()) {
158 return opts;
159 } else if (sources.get(0).contains("overview")) {
160 return Arrays.asList("-overview", getSrcFile(srcDir, sources.get(0)).getPath());
161 } else {
162 return Collections.emptyList();
163 }
164 }
166 /**
167 * Gets the output file corresponding to the first source file.
168 * This file should not be written if the comment contains JavaScript and JavaScripot is
169 * not allowed.
170 * @param dir the base output directory
171 * @return the output file
172 */
173 File getOutFile(File outDir) {
174 String f;
175 Matcher m = pat.matcher(sources.get(0));
176 if (!m.matches())
177 throw new Error("match failed");
178 if (m.group(3) != null) {
179 f = m.group(2) + "/" + m.group(3) + ".html";
180 } else if (m.group(2) != null) {
181 f = m.group(2) + "/package-summary.html";
182 } else {
183 f = "overview-summary.html";
184 }
185 return new File(outDir, f);
186 }
188 final List<String> opts;
189 final List<String> sources;
190 };
192 enum Option {
193 OFF(null),
194 ON("--allow-script-in-comments");
196 Option(String text) {
197 this.text = text;
198 }
200 final String text;
201 };
203 private PrintStream out = System.err;
205 public void run() throws Exception {
206 int count = 0;
207 for (Template template: Template.values()) {
208 for (Comment comment: Comment.values()) {
209 for (Option option: Option.values()) {
210 if (test(template, comment, option)) {
211 count++;
212 }
213 }
214 }
215 }
217 out.println(count + " test cases run");
218 if (errors > 0) {
219 throw new Exception(errors + " errors occurred");
220 }
221 }
223 boolean test(Template template, Comment comment, Option option) throws IOException {
224 if (option == Option.ON && !comment.hasScript) {
225 // skip --allowScriptInComments if comment does not contain JavaScript
226 return false;
227 }
229 String test = template + "-" + comment + "-" + option;
230 out.println("Test: " + test);
232 File dir = new File(test);
233 dir.mkdirs();
234 File srcDir = new File(dir, "src");
235 File outDir = new File(dir, "out");
237 String alert = "alert(\"" + test + "\");";
238 for (String src: template.sources) {
239 writeFile(template.getSrcFile(srcDir, src),
240 src.replace("#COMMENT",
241 "/** " + comment.text.replace("#ALERT", alert) + " **/"));
242 }
244 List<String> opts = new ArrayList<String>();
245 opts.add("-sourcepath");
246 opts.add(srcDir.getPath());
247 opts.add("-d");
248 opts.add(outDir.getPath());
249 if (option.text != null)
250 opts.add(option.text);
251 for (String opt: template.getOpts(srcDir)) {
252 opts.add(opt.replace("#COMMENT", comment.text.replace("#ALERT", alert)));
253 }
254 opts.addAll(comment.opts);
255 opts.add("-noindex"); // index not required; save time/space writing files
256 opts.add("p");
258 StringWriter sw = new StringWriter();
259 PrintWriter pw = new PrintWriter(sw);
260 int rc = javadoc(opts, pw);
261 pw.close();
262 String log = sw.toString();
263 writeFile(new File(dir, "log.txt"), log);
265 out.println("opts: " + opts);
266 out.println(" rc: " + rc);
267 out.println(" log:");
268 out.println(log);
270 String ERROR = "Use --allow-script-in-comment";
271 File outFile = template.getOutFile(outDir);
273 boolean expectErrors = comment.hasScript && (option == Option.OFF);
275 if (expectErrors) {
276 check(rc != 0, "unexpected exit code: " + rc);
277 check(log.contains(ERROR), "expected error message not found");
278 check(!outFile.exists(), "output file found unexpectedly");
279 } else {
280 check(rc == 0, "unexpected exit code: " + rc);
281 check(!log.contains(ERROR), "error message found");
282 check(outFile.exists(), "output file not found");
283 }
285 out.println();
286 return true;
287 }
289 int javadoc(List<String> opts, PrintWriter pw) {
290 return com.sun.tools.javadoc.Main.execute("javadoc", pw, pw, pw,
291 "com.sun.tools.doclets.standard.Standard", opts.toArray(new String[opts.size()]));
292 }
294 File writeFile(File f, String text) throws IOException {
295 f.getParentFile().mkdirs();
296 FileWriter fw = new FileWriter(f);
297 try {
298 fw.write(text);
299 } finally {
300 fw.close();
301 }
302 return f;
303 }
305 void check(boolean cond, String errMessage) {
306 if (!cond) {
307 error(errMessage);
308 }
309 }
311 void error(String message) {
312 out.println("Error: " + message);
313 errors++;
314 }
316 int errors = 0;
317 }