25149
+ − 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
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
+ − 34
* @bug 8000650
+ − 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 {
+ − 41
+ − 42
public static void main(String... args) throws Exception {
+ − 43
testChecksum();
+ − 44
}
+ − 45
+ − 46
static void testChecksum() throws Exception {
+ − 47
+ − 48
// Create a fresh .jar file
+ − 49
File testFile = new File("src_tools.jar");
+ − 50
File testPack = new File("src_tools.pack.gz");
+ − 51
generateJar(testFile);
+ − 52
List<String> cmdsList = new ArrayList<>();
+ − 53
+ − 54
// Create .pack file
+ − 55
cmdsList.add(Utils.getPack200Cmd());
+ − 56
cmdsList.add(testPack.getName());
+ − 57
cmdsList.add(testFile.getName());
+ − 58
Utils.runExec(cmdsList);
+ − 59
+ − 60
// Mess up with the checksum of the packed file
+ − 61
RandomAccessFile raf = new RandomAccessFile(testPack, "rw");
+ − 62
raf.seek(raf.length() - 8);
+ − 63
int val = raf.readInt();
+ − 64
val = Integer.MAX_VALUE - val;
+ − 65
raf.seek(raf.length() - 8);
+ − 66
raf.writeInt(val);
+ − 67
raf.close();
+ − 68
+ − 69
File dstFile = new File("dst_tools.jar");
+ − 70
cmdsList.clear();
+ − 71
cmdsList.add(Utils.getUnpack200Cmd());
+ − 72
cmdsList.add(testPack.getName());
+ − 73
cmdsList.add(dstFile.getName());
+ − 74
+ − 75
boolean passed = false;
+ − 76
try {
+ − 77
Utils.runExec(cmdsList);
+ − 78
} catch (RuntimeException re) {
+ − 79
// unpack200 should exit with non-zero exit code
+ − 80
passed = true;
+ − 81
}
+ − 82
+ − 83
// tidy up
+ − 84
if (testFile.exists()) testFile.delete();
+ − 85
if (testPack.exists()) testPack.delete();
+ − 86
if (dstFile.exists()) dstFile.delete();
+ − 87
if (!passed) {
+ − 88
throw new Exception("File with incorrect CRC unpacked without the error.");
+ − 89
}
+ − 90
}
+ − 91
+ − 92
static void generateJar(File result) throws IOException {
+ − 93
if (result.exists()) {
+ − 94
result.delete();
+ − 95
}
+ − 96
+ − 97
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(result)); ) {
+ − 98
for (int i = 0 ; i < 100 ; i++) {
+ − 99
JarEntry e = new JarEntry("F-" + i + ".txt");
+ − 100
output.putNextEntry(e);
+ − 101
}
+ − 102
output.flush();
+ − 103
output.close();
+ − 104
}
+ − 105
}
+ − 106
+ − 107
}