jdk/test/tools/pack200/PackChecksum.java
author kizune
Thu, 19 Jun 2014 18:07:02 +0400
changeset 25149 7df9d6b3ef17
child 36960 d7731fdfe7c3
permissions -rw-r--r--
8000650: unpack200.exe should check gzip crc Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25149
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     1
/*
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     4
 *
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     8
 *
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    13
 * accompanied this code).
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    14
 *
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    18
 *
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    21
 * questions.
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    22
 */
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    23
import java.io.File;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    24
import java.io.FileOutputStream;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    25
import java.io.IOException;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    26
import java.io.RandomAccessFile;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    27
import java.util.ArrayList;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    28
import java.util.List;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    29
import java.util.jar.JarEntry;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    30
import java.util.jar.JarOutputStream;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    31
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    32
/*
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    33
 * @test
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    34
 * @bug 8000650
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    35
 * @summary unpack200.exe should check gzip crc
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    36
 * @compile -XDignore.symbol.file Utils.java PackChecksum.java
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    37
 * @run main PackChecksum
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    38
 * @author kizune
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    39
 */
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    40
public class PackChecksum {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    41
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    42
    public static void main(String... args) throws Exception {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    43
        testChecksum();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    44
    }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    45
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    46
    static void testChecksum() throws Exception {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    47
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    48
        // Create a fresh .jar file
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    49
        File testFile = new File("src_tools.jar");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    50
        File testPack = new File("src_tools.pack.gz");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    51
        generateJar(testFile);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    52
        List<String> cmdsList = new ArrayList<>();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    53
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    54
        // Create .pack file
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    55
        cmdsList.add(Utils.getPack200Cmd());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    56
        cmdsList.add(testPack.getName());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    57
        cmdsList.add(testFile.getName());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    58
        Utils.runExec(cmdsList);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    59
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    60
        // Mess up with the checksum of the packed file
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    61
        RandomAccessFile raf = new RandomAccessFile(testPack, "rw");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    62
        raf.seek(raf.length() - 8);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    63
        int val = raf.readInt();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    64
        val = Integer.MAX_VALUE - val;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    65
        raf.seek(raf.length() - 8);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    66
        raf.writeInt(val);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    67
        raf.close();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    68
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    69
        File dstFile = new File("dst_tools.jar");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    70
        cmdsList.clear();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    71
        cmdsList.add(Utils.getUnpack200Cmd());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    72
        cmdsList.add(testPack.getName());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    73
        cmdsList.add(dstFile.getName());
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    74
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    75
        boolean passed = false;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    76
        try {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    77
            Utils.runExec(cmdsList);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    78
        } catch (RuntimeException re) {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    79
            // unpack200 should exit with non-zero exit code
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    80
            passed = true;
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    81
        }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    82
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    83
        // tidy up
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    84
        if (testFile.exists()) testFile.delete();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    85
        if (testPack.exists()) testPack.delete();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    86
        if (dstFile.exists()) dstFile.delete();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    87
        if (!passed) {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    88
            throw new Exception("File with incorrect CRC unpacked without the error.");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    89
        }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    90
    }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    91
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    92
    static void generateJar(File result) throws IOException {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    93
        if (result.exists()) {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    94
            result.delete();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    95
        }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    96
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    97
        try (JarOutputStream output = new JarOutputStream(new FileOutputStream(result)); ) {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    98
            for (int i = 0 ; i < 100 ; i++) {
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
    99
                JarEntry e = new JarEntry("F-" + i + ".txt");
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   100
                output.putNextEntry(e);
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   101
            }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   102
            output.flush();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   103
            output.close();
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   104
        }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   105
    }
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   106
7df9d6b3ef17 8000650: unpack200.exe should check gzip crc
kizune
parents:
diff changeset
   107
}