author | lancea |
Thu, 14 Nov 2019 10:54:32 -0500 | |
changeset 59083 | 3e4d8b5856f3 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
25149 | 1 |
/* |
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
25149 | 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 |
import java.io.File; |
|
24 |
import java.io.FileOutputStream; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.RandomAccessFile; |
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.List; |
|
29 |
import java.util.jar.JarEntry; |
|
30 |
import java.util.jar.JarOutputStream; |
|
31 |
||
32 |
/* |
|
33 |
* @test |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
34 |
* @bug 8000650 8150469 |
25149 | 35 |
* @summary unpack200.exe should check gzip crc |
36 |
* @compile -XDignore.symbol.file Utils.java PackChecksum.java |
|
37 |
* @run main PackChecksum |
|
38 |
* @author kizune |
|
39 |
*/ |
|
40 |
public class PackChecksum { |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
41 |
final int TRAILER_LEN = 8; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
42 |
final List<String> cmdsList = new ArrayList<>(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
43 |
static enum Case { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
44 |
CRC32, |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
45 |
ISIZE, |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
46 |
BOTH; |
25149 | 47 |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
48 |
}; |
25149 | 49 |
public static void main(String... args) throws Exception { |
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
50 |
new PackChecksum().run(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
51 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
52 |
void run() throws Exception { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
53 |
testBrokenTrailer(Case.CRC32); // negative |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
54 |
testBrokenTrailer(Case.ISIZE); // negative |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
55 |
testBrokenTrailer(Case.BOTH); // negative |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
56 |
testMultipleSegments(); |
25149 | 57 |
} |
58 |
||
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
59 |
void testMultipleSegments() throws Exception { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
60 |
File inputJar = new File("input.jar"); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
61 |
Utils.copyFile(Utils.getGoldenJar(), inputJar); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
62 |
cmdsList.clear(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
63 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
64 |
File testPack = new File("out.jar.pack.gz"); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
65 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
66 |
cmdsList.clear(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
67 |
cmdsList.add(Utils.getPack200Cmd()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
68 |
// force multiple segments |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
69 |
cmdsList.add("--segment-limit=100"); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
70 |
cmdsList.add(testPack.getName()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
71 |
cmdsList.add(inputJar.getName()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
72 |
Utils.runExec(cmdsList); |
25149 | 73 |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
74 |
File destFile = new File("dst.jar"); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
75 |
cmdsList.clear(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
76 |
cmdsList.add(Utils.getUnpack200Cmd()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
77 |
cmdsList.add(testPack.getName()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
78 |
cmdsList.add(destFile.getName()); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
79 |
try { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
80 |
Utils.runExec(cmdsList); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
81 |
if (!destFile.exists()) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
82 |
throw new Exception("file not created: " + destFile); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
83 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
84 |
} finally { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
85 |
if (inputJar.exists()) |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
86 |
inputJar.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
87 |
if (testPack.exists()) |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
88 |
testPack.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
89 |
if (destFile.exists()) |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
90 |
destFile.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
91 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
92 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
93 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
94 |
void testBrokenTrailer(Case type) throws Exception { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
95 |
System.out.println("Testing: case " + type); |
25149 | 96 |
// Create a fresh .jar file |
97 |
File testFile = new File("src_tools.jar"); |
|
98 |
File testPack = new File("src_tools.pack.gz"); |
|
99 |
generateJar(testFile); |
|
100 |
||
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
101 |
cmdsList.clear(); |
25149 | 102 |
// Create .pack file |
103 |
cmdsList.add(Utils.getPack200Cmd()); |
|
104 |
cmdsList.add(testPack.getName()); |
|
105 |
cmdsList.add(testFile.getName()); |
|
106 |
Utils.runExec(cmdsList); |
|
107 |
||
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
108 |
// mutate the checksum of the packed file |
25149 | 109 |
RandomAccessFile raf = new RandomAccessFile(testPack, "rw"); |
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
110 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
111 |
switch (type) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
112 |
case CRC32: |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
113 |
raf.seek(raf.length() - TRAILER_LEN); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
114 |
raf.writeInt(0x0dea0a0d); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
115 |
break; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
116 |
case ISIZE: |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
117 |
raf.seek(raf.length() - (TRAILER_LEN/2)); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
118 |
raf.writeInt(0x0b0e0e0f); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
119 |
break; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
120 |
default: |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
121 |
raf.seek(raf.length() - (TRAILER_LEN)); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
122 |
raf.writeLong(0x0dea0a0d0b0e0e0fL); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
123 |
break; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
124 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
125 |
|
25149 | 126 |
raf.close(); |
127 |
||
128 |
File dstFile = new File("dst_tools.jar"); |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
129 |
if (dstFile.exists()) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
130 |
dstFile.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
131 |
} |
25149 | 132 |
cmdsList.clear(); |
133 |
cmdsList.add(Utils.getUnpack200Cmd()); |
|
134 |
cmdsList.add(testPack.getName()); |
|
135 |
cmdsList.add(dstFile.getName()); |
|
136 |
||
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
137 |
boolean processFailed = false; |
25149 | 138 |
try { |
139 |
Utils.runExec(cmdsList); |
|
140 |
} catch (RuntimeException re) { |
|
141 |
// unpack200 should exit with non-zero exit code |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
142 |
processFailed = true; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
143 |
} finally { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
144 |
// tidy up |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
145 |
if (testFile.exists()) |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
146 |
testFile.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
147 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
148 |
if (testPack.exists()) |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
149 |
testPack.delete(); |
25149 | 150 |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
151 |
if (!processFailed) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
152 |
throw new Exception("case " + type + |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
153 |
": file with incorrect CRC, unpacked without the error."); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
154 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
155 |
if (dstFile.exists()) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
156 |
dstFile.delete(); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
157 |
throw new Exception("case " + type + |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
158 |
": file exists: " + dstFile); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
159 |
} |
25149 | 160 |
} |
161 |
} |
|
162 |
||
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
25149
diff
changeset
|
163 |
void generateJar(File result) throws IOException { |
25149 | 164 |
if (result.exists()) { |
165 |
result.delete(); |
|
166 |
} |
|
167 |
||
168 |
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(result)); ) { |
|
169 |
for (int i = 0 ; i < 100 ; i++) { |
|
170 |
JarEntry e = new JarEntry("F-" + i + ".txt"); |
|
171 |
output.putNextEntry(e); |
|
172 |
} |
|
173 |
output.flush(); |
|
174 |
output.close(); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
} |