test/tools/javadoc/TestScriptInComment.java

Thu, 27 Aug 2020 21:00:00 -0300

author
mbalao
date
Thu, 27 Aug 2020 21:00:00 -0300
changeset 3926
571945b07fd4
parent 3845
735048c9f2d6
permissions
-rw-r--r--

8080462: Update SunPKCS11 provider with PKCS11 v2.40 support
Summary: Added support for GCM, PSS, and other mechanisms
Reviewed-by: andrew

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

mercurial