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