26390
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
import java.io.PrintWriter;
|
|
25 |
import java.io.StringWriter;
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.regex.Matcher;
|
|
29 |
import java.util.regex.Pattern;
|
|
30 |
|
|
31 |
/*
|
|
32 |
* @test
|
|
33 |
* @bug 8038414
|
|
34 |
* @summary Constant pool's strings are not escaped properly
|
|
35 |
*/
|
|
36 |
public class T8038414 {
|
|
37 |
private static final String NEW_LINE = System.getProperty("line.separator");
|
|
38 |
private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
|
|
39 |
private static final String GOLDEN_STRING = escapeString(Test.test);
|
|
40 |
|
|
41 |
private static String escapeString(String s) {
|
|
42 |
StringBuilder sb = new StringBuilder();
|
|
43 |
for (int i = 0; i < s.length(); i++) {
|
|
44 |
char c = s.charAt(i);
|
|
45 |
switch (c) {
|
|
46 |
case '\t':
|
|
47 |
sb.append('\\').append('t');
|
|
48 |
break;
|
|
49 |
case '\n':
|
|
50 |
sb.append('\\').append('n');
|
|
51 |
break;
|
|
52 |
case '\r':
|
|
53 |
sb.append('\\').append('r');
|
|
54 |
break;
|
|
55 |
case '\b':
|
|
56 |
sb.append('\\').append('b');
|
|
57 |
break;
|
|
58 |
case '\f':
|
|
59 |
sb.append('\\').append('f');
|
|
60 |
break;
|
|
61 |
case '\"':
|
|
62 |
sb.append('\\').append('\"');
|
|
63 |
break;
|
|
64 |
case '\'':
|
|
65 |
sb.append('\\').append('\'');
|
|
66 |
break;
|
|
67 |
case '\\':
|
|
68 |
sb.append('\\').append('\\');
|
|
69 |
break;
|
|
70 |
default:
|
|
71 |
sb.append(c);
|
|
72 |
}
|
|
73 |
}
|
|
74 |
return sb.toString();
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void main(String... args) {
|
|
78 |
new T8038414().run();
|
|
79 |
}
|
|
80 |
|
|
81 |
public void run() {
|
|
82 |
String output = javap(Test.class.getName());
|
|
83 |
List<String> actualValues = extractEscapedComments(output);
|
|
84 |
for (String a : actualValues) {
|
|
85 |
check(!GOLDEN_STRING.equals(a), String.format("Expected: %s, got: %s", GOLDEN_STRING, a));
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
private List<String> extractConstantPool(String output) {
|
|
90 |
List<String> cp = new ArrayList<>();
|
|
91 |
boolean inCp = false;
|
|
92 |
for (String s : output.split("\n")) {
|
|
93 |
if (s.equals("{")) {
|
|
94 |
break;
|
|
95 |
}
|
|
96 |
if (inCp) {
|
|
97 |
cp.add(s);
|
|
98 |
}
|
|
99 |
if (s.equals("Constant pool:")) {
|
|
100 |
inCp = true;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
return cp;
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Returns a list which contains comments of the string entry in the constant pool
|
|
108 |
* and the appropriate UTF-8 value.
|
|
109 |
*
|
|
110 |
* @return a list
|
|
111 |
*/
|
|
112 |
private List<String> extractEscapedComments(String output) {
|
|
113 |
List<String> result = new ArrayList<>();
|
|
114 |
Pattern stringPattern = Pattern.compile(" +#\\d+ = String +#(\\d+) +// +(.*)");
|
|
115 |
int index = -1;
|
|
116 |
List<String> cp = extractConstantPool(output);
|
|
117 |
for (String c : cp) {
|
|
118 |
Matcher matcher = stringPattern.matcher(c);
|
|
119 |
if (matcher.matches()) {
|
|
120 |
index = Integer.parseInt(matcher.group(1)) - 1;
|
|
121 |
result.add(matcher.group(2));
|
|
122 |
// only one String entry
|
|
123 |
break;
|
|
124 |
}
|
|
125 |
}
|
|
126 |
check(index == -1, "Escaped string is not found in constant pool");
|
|
127 |
result.add(cp.get(index).replaceAll(".* +", "")); // remove #16 = Utf8
|
|
128 |
return result;
|
|
129 |
}
|
|
130 |
|
|
131 |
private String javap(String className) {
|
|
132 |
StringWriter sw = new StringWriter();
|
|
133 |
PrintWriter out = new PrintWriter(sw);
|
|
134 |
int rc = com.sun.tools.javap.Main.run(new String[]{"-v", "-classpath", TEST_CLASSES, className}, out);
|
|
135 |
out.close();
|
|
136 |
String output = sw.toString();
|
|
137 |
System.err.println("class " + className);
|
|
138 |
System.err.println(output);
|
|
139 |
|
|
140 |
check(rc != 0, "javap failed. rc=" + rc);
|
|
141 |
return output.replaceAll(NEW_LINE, "\n");
|
|
142 |
}
|
|
143 |
|
|
144 |
private void check(boolean cond, String msg) {
|
|
145 |
if (cond) {
|
|
146 |
throw new RuntimeException(msg);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
static class Test {
|
|
151 |
static String test = "\\t\t\b\r\n\f\"\'\\";
|
|
152 |
}
|
|
153 |
}
|